1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
|
package fr.deskom.Outils;
import java.util.ArrayList;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class Handler extends DefaultHandler {
private boolean first = true;
private ArrayList listLine = new ArrayList();
private String strLineFac = "";
private String strLineTitu = "";
private String strLineGest = "";
private boolean root = true;
private BeanSFR sfrBean = null;
private boolean titulaire = false;
private boolean gestionnaire = false;
private boolean facture = false;
private boolean fadet = false;
public Handler() {
sfrBean = new BeanSFR();
}
public void characters(char[] ch, int start, int length)
throws SAXException {
// System.out.println(new String(ch).trim());
// String str = new String(new String(ch));
// str = str;
if (!fadet) {
if (first) {
String str = new String(new String(ch));
str = str.trim();
if (str.startsWith("<!DOCTYPE")) {
// System.out.println("dans le start " +
// str.substring(0,str.indexOf(">")));
sfrBean.setDtd(str.substring(0, str.indexOf(">")) + ">");
}
first = false;
}
if (titulaire) {
// System.out.println(new String(ch, start, length));
// if(new String(ch, start, length).indexOf("\n\r")!=-1)
// System.out.println("titulaire " + new String(ch).trim());
strLineTitu += new String(ch, start, length);
// sfrBean.addTitu(test(new String(ch, start, length)));
// titulaire = false;
} else if (gestionnaire) {
// System.out.println("gestionnaire " +new String(ch).trim());
// gestionnaire = false;
// if(!new String(ch, start, length).endsWith("\n"))
// sfrBean.addGest(test(new String(ch, start, length)));
strLineGest += new String(ch, start, length);
} else if (facture) {
strLineFac += new String(ch, start, length);
// listLine.add(test(new String(ch, start, length)));
// if(!new String(ch, start, length).endsWith("\n"))
// System.out.println("dans facture "+test(new String(ch, start,
// length)));
// sfrBean.addFactureSFRLine(test(new String(ch, start,
// length)));
}
}
}
public void endDocument() throws SAXException {
// super.endDocument();
}
public void endElement(String uri, String localName, String qName)
throws SAXException {
// super.endElement(uri, localName, qName);
// System.out.print("</" + qName + ">");
if (qName.equals("TITUL")) {
// sfrBean.addTitu("</" + qName + ">");
strLineTitu += "</" + qName + ">";
sfrBean.addTitu(strLineTitu);
strLineTitu = "";
titulaire = false;
} else if (qName.equals("GEST")) {
// System.out.println("gest");
strLineGest += "</" + qName + ">";
// sfrBean.addGest("</" + qName + ">");
sfrBean.addGest(strLineGest);
strLineGest = "";
gestionnaire = false;
} else if (qName.equals("FACTURESFR")) {
strLineFac += "</" + qName + ">";
// listLine.add("</" + qName + ">");
// listLine.add(strLineFac);
facture = false;
sfrBean.addFacture(strLineFac);
strLineFac = "";
// listLine = null;
// listLine = new ArrayList();
} else if (qName.endsWith("FADET")) {
facture = true;
fadet = false;
}
if (titulaire) {
strLineTitu += "</" + qName + ">";
// sfrBean.addTitu("</" + qName + ">");
} else if (gestionnaire) {
strLineGest += "</" + qName + ">";
// sfrBean.addGest("</" + qName + ">");
} else if (facture) {
strLineFac += "</" + qName + ">";
// listLine.add("</" + qName + ">");
// sfrBean.addFactureSFRLine("</" + qName + ">");
// sfrBean.addFacture(sfrBean.getFactureSFRLine());
// sfrBean.getFactureSFRLine().clear();
}
}
// public void endPrefixMapping(String prefix) throws SAXException {
// // TODO Auto-generated method stub
// super.endPrefixMapping(prefix);
// }
//
// public void error(SAXParseException e) throws SAXException {
// // TODO Auto-generated method stub
// super.error(e);
// }
//
// public void fatalError(SAXParseException e) throws SAXException {
// // TODO Auto-generated method stub
// super.fatalError(e);
// }
//
// public void ignorableWhitespace(char[] ch, int start, int length)
// throws SAXException {
// // TODO Auto-generated method stub
// super.ignorableWhitespace(ch, start, length);
// }
//
// public void notationDecl(String name, String publicId, String systemId)
// throws SAXException {
// // TODO Auto-generated method stub
// super.notationDecl(name, publicId, systemId);
// }
//
// public void processingInstruction(String target, String data)
// throws SAXException {
// // TODO Auto-generated method stub
// // System.out.println(target+ " , " + data ) ;
// super.processingInstruction(target, data);
// }
//
// public InputSource resolveEntity(String publicId, String systemId)
// throws IOException, SAXException {
// // TODO Auto-generated method stub
// //System.out.println(publicId + " " + systemId);
// // System.out.println(super.resolveEntity(publicId,
// // systemId).getEncoding());
//
// // System.out.println(super.resolveEntity(publicId,
// // systemId).getPublicId());
// // System.out.println(super.resolveEntity(publicId,
// // systemId).getSystemId());
// return super.resolveEntity(publicId, systemId);
//
// }
// public void setDocumentLocator(Locator locator) {
// // TODO Auto-generated method stub
//
// super.setDocumentLocator(locator);
// }
//
// public void skippedEntity(String name) throws SAXException {
// // TODO Auto-generated method stub
// // System.out.println("skipp " +name);
// super.skippedEntity(name);
// }
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
super.startDocument();
}
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
// super.startElement(uri, localName, qName, attributes);
if (root) {
sfrBean.setRoot(qName);
root = false;
}
String line = getBalise(qName, attributes);
if (titulaire) {
// line = getBalise(qName, attributes);
strLineTitu += line;
// sfrBean.addTitu(strLineTitu);
// sfrBean.addTitu(line);
} else if (gestionnaire) {
// line = getBalise(qName, attributes);
strLineGest += line;
// sfrBean.addGest(line);
} else if (facture) {
// line = getBalise(qName, attributes);
strLineFac += line;
// listLine.add(line);
}
if (qName.equals("TITUL")) {
// System.out.println("titulaire");
titulaire = true;
strLineTitu += line;
// sfrBean.addTitu(strLineTitu);
} else if (qName.equals("GEST")) {
// System.out.println("gest");
gestionnaire = true;
strLineGest += line;
// sfrBean.addGest(strLineTitu);
} else if (qName.equals("FACTURESFR")) {
facture = true;
// listLine = new ArrayList();
strLineFac += line;
// listLine.add(line);
} else if (qName.endsWith("FADET")) {
facture = false;
fadet = true;
}
}
// public void startPrefixMapping(String prefix, String uri)
// throws SAXException {
// // TODO Auto-generated method stub
// super.startPrefixMapping(prefix, uri);
// }
//
// public void unparsedEntityDecl(String name, String publicId,
// String systemId, String notationName) throws SAXException {
// // TODO Auto-generated method stub
// // System.out.println(name +" " +publicId+" "+ systemId+" , " +
// // notationName);
// super.unparsedEntityDecl(name, publicId, systemId, notationName);
// }
//
// public void warning(SAXParseException e) throws SAXException {
// // TODO Auto-generated method stub
// super.warning(e);
// }
public String getBalise(String qName, Attributes attributes)
throws SAXException {
String line = " ";
String attribut = " ";
line = "<" + qName + " ";
if (attributes != null) {
for (int i = 0; i < attributes.getLength(); i++) {
// try {
// System.out.println("value attribue "
// + new String(attributes.getValue(i).getBytes(
// "US-ASCII"), "US-ASCII"));
// System.out.println("value attribue "
// + new String(attributes.getValue(i).getBytes(
// "UTF-8"), "UTF-8"));
// System.out.println("value attribue "
// + new String(attributes.getValue(i).getBytes(
// "UTF-16"), "UTF-16"));
// System.out.println("value attribue "
// + new String(attributes.getValue(i).getBytes(
// "ISO-8859-1"), "ISO-8859-1"));
// } catch (UnsupportedEncodingException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
attribut += attributes.getQName(i) + "=\""
+ traitementXMLAttribut(attributes.getValue(i)) + "\" ";
}
// System.out.println(attribut);
line += attribut.trim();
}
line += ">";
return line;
}
public boolean isFacture() {
return facture;
}
public void setFacture(boolean facture) {
this.facture = facture;
}
public boolean isFadet() {
return fadet;
}
public void setFadet(boolean fadet) {
this.fadet = fadet;
}
public boolean isGestionnaire() {
return gestionnaire;
}
public void setGestionnaire(boolean gestionnaire) {
this.gestionnaire = gestionnaire;
}
public BeanSFR getSfrBean() {
return sfrBean;
}
public void setSfrBean(BeanSFR sfrBean) {
this.sfrBean = sfrBean;
}
public boolean isTitulaire() {
return titulaire;
}
public void setTitulaire(boolean titulaire) {
this.titulaire = titulaire;
}
// public String test(String line) {
// StringBuffer buffer = new StringBuffer();
//
// for (int i = 0; i < line.length(); i++) {
// char[] tmp = { line.charAt(i) };
// String tmpStr = new String(tmp);
// tmpStr.replace('\r', ' ');
// tmpStr.replace('\n', ' ');
// buffer.append(tmpStr);
// }
// // System.out.println("buffer "+buffer.toString());
// return buffer.toString().trim();
//
// }
/**
*
* @param _name
* @return
*/
public String traitementXMLAttribut(String _name) {
StringBuffer buffer = new StringBuffer();
// String tmp = "";
for (int i = 0; i < _name.length(); i++) {
char[] tmp = { _name.charAt(i) };
String tmpStr = new String(tmp);
if (tmpStr.indexOf("<") != -1) {
String prefixe = tmpStr.substring(0, tmpStr.indexOf("<"));
String suffixe = tmpStr.substring(tmpStr.indexOf("<") + 1,
tmpStr.length());
tmpStr = prefixe + "<" + suffixe;
} else if (tmpStr.indexOf(">") != -1) {
String prefixe = tmpStr.substring(0, tmpStr.indexOf(">"));
String suffixe = tmpStr.substring(tmpStr.indexOf(">") + 1,
tmpStr.length());
tmpStr = prefixe + ">" + suffixe;
} else if (tmpStr.indexOf("&") != -1) {
String prefixe = tmpStr.substring(0, tmpStr.indexOf("&"));
String suffixe = tmpStr.substring(tmpStr.indexOf("&") + 1,
tmpStr.length());
tmpStr = prefixe + "&" + suffixe;
} else if (tmpStr.indexOf("'") != -1) {
String prefixe = tmpStr.substring(0, tmpStr.indexOf("'"));
String suffixe = tmpStr.substring(tmpStr.indexOf("'") + 1,
tmpStr.length());
tmpStr = prefixe + "'" + suffixe;
}
buffer.append(tmpStr);
}
// System.out.println(buffer.toString());
return buffer.toString();
}
} |