Révision 2548
| tmp/org.txm.core/src/java/org/txm/importer/StaxIdentityParser.java (revision 2548) | ||
|---|---|---|
| 6 | 6 |
import java.io.IOException; |
| 7 | 7 |
import java.io.InputStream; |
| 8 | 8 |
import java.net.URL; |
| 9 |
import java.util.HashMap; |
|
| 10 |
import java.util.LinkedHashMap; |
|
| 11 | 9 |
|
| 12 | 10 |
import javax.xml.stream.XMLInputFactory; |
| 13 | 11 |
import javax.xml.stream.XMLOutputFactory; |
| ... | ... | |
| 17 | 15 |
import javax.xml.stream.XMLStreamWriter; |
| 18 | 16 |
|
| 19 | 17 |
public class StaxIdentityParser {
|
| 20 |
|
|
| 21 | 18 |
/** The input */ |
| 22 | 19 |
protected URL inputurl; |
| 23 |
|
|
| 24 | 20 |
protected InputStream inputData; |
| 25 |
|
|
| 26 | 21 |
protected XMLInputFactory factory; |
| 27 |
|
|
| 28 | 22 |
protected XMLStreamReader parser; |
| 29 |
|
|
| 23 |
|
|
| 30 | 24 |
/** The output. */ |
| 31 | 25 |
protected XMLOutputFactory outfactory = XMLOutputFactory.newInstance(); |
| 32 |
|
|
| 33 | 26 |
protected BufferedOutputStream output; |
| 34 |
|
|
| 35 | 27 |
protected XMLStreamWriter writer; |
| 36 |
|
|
| 28 |
|
|
| 37 | 29 |
public static String TXMNS = "http://textometrie.org/1.0"; |
| 38 |
|
|
| 39 | 30 |
public static String TXM = "txm"; |
| 40 |
|
|
| 41 | 31 |
public static String TEINS = "http://www.tei-c.org/ns/1.0"; |
| 42 |
|
|
| 43 | 32 |
public static String TEI = "tei"; |
| 44 |
|
|
| 45 | 33 |
protected static PersonalNamespaceContext Nscontext = new PersonalNamespaceContext(); |
| 46 |
|
|
| 47 |
protected StringBuilder currentXPath = new StringBuilder("");
|
|
| 48 |
|
|
| 34 |
|
|
| 35 |
//protected StringBuilder currentXPath = new StringBuilder("/")
|
|
| 49 | 36 |
protected String localname; |
| 50 |
|
|
| 51 | 37 |
int processingXInclude = 0; |
| 52 |
|
|
| 53 |
StaxIdentityParser parentParser = null; |
|
| 54 |
|
|
| 55 |
StaxIdentityParser activeHook = null; |
|
| 56 |
|
|
| 57 |
LinkedHashMap<String, StaxIdentityParser> hooks = new LinkedHashMap<>(); |
|
| 58 |
|
|
| 38 |
|
|
| 59 | 39 |
public StaxIdentityParser(File infile) throws IOException, XMLStreamException {
|
| 60 | 40 |
this(infile.toURI().toURL()); |
| 61 | 41 |
} |
| 62 |
|
|
| 42 |
|
|
| 63 | 43 |
public StaxIdentityParser(URL inputurl) throws IOException, XMLStreamException {
|
| 64 | 44 |
this.inputurl = inputurl; |
| 65 | 45 |
this.inputData = inputurl.openStream(); |
| ... | ... | |
| 68 | 48 |
} |
| 69 | 49 |
|
| 70 | 50 |
/** |
| 71 |
* create a hook parser. When a hook parser is activated, the processXYZ methods are used instead of the parent processXTZ methods.
|
|
| 51 |
* Helper method to get an attribute value
|
|
| 72 | 52 |
* |
| 73 |
* The hook can be activated / deactivated by the parent using the mustActivated mustDeactivated methods |
|
| 74 |
* |
|
| 75 |
* @param parentParser |
|
| 76 |
* @throws IOException |
|
| 77 |
* @throws XMLStreamException |
|
| 78 |
*/ |
|
| 79 |
public StaxIdentityParser(String name, StaxIdentityParser parentParser) throws IOException, XMLStreamException {
|
|
| 80 |
this.inputurl = null; |
|
| 81 |
this.factory = null; |
|
| 82 |
this.parentParser = parentParser; |
|
| 83 |
this.writer = parentParser.writer; |
|
| 84 |
this.parser = parentParser.parser; |
|
| 85 |
|
|
| 86 |
parentParser.hooks.put(name, this); |
|
| 87 |
} |
|
| 88 |
|
|
| 89 |
/** |
|
| 90 |
* called when the parser is activated by its parent |
|
| 91 |
* |
|
| 92 |
* @return true if correctly activated |
|
| 93 |
*/ |
|
| 94 |
public boolean activate() {
|
|
| 95 |
return false; |
|
| 96 |
} |
|
| 97 |
|
|
| 98 |
/** |
|
| 99 |
* called when the parser is deactivated by its parent |
|
| 100 |
* |
|
| 101 |
* @return true if correctly activated |
|
| 102 |
*/ |
|
| 103 |
public boolean deactivate() {
|
|
| 104 |
return false; |
|
| 105 |
} |
|
| 106 |
|
|
| 107 |
/** |
|
| 108 |
* |
|
| 109 |
* @return true if this hook parser must be activated |
|
| 110 |
*/ |
|
| 111 |
public boolean mustActivate() {
|
|
| 112 |
return false; |
|
| 113 |
} |
|
| 114 |
|
|
| 115 |
/** |
|
| 116 |
* |
|
| 117 |
* @return true if this hook parser must be desactivated |
|
| 118 |
*/ |
|
| 119 |
public boolean mustDeactivate() {
|
|
| 120 |
return true; |
|
| 121 |
} |
|
| 122 |
|
|
| 123 |
/** |
|
| 124 |
* Helper method to get an attribute value by its name |
|
| 125 |
* |
|
| 126 | 53 |
* @param name the attribute name |
| 127 | 54 |
* @return the value if any |
| 128 | 55 |
*/ |
| ... | ... | |
| 130 | 57 |
if (name == null) return null; |
| 131 | 58 |
|
| 132 | 59 |
int c = parser.getAttributeCount(); |
| 133 |
for (int i = 0; i < c; i++) {
|
|
| 60 |
for (int i = 0 ; i < c ; i++) {
|
|
| 134 | 61 |
if (name.equals(parser.getAttributeLocalName(i))) {
|
| 135 | 62 |
return parser.getAttributeValue(i); |
| 136 | 63 |
} |
| ... | ... | |
| 138 | 65 |
|
| 139 | 66 |
return null; |
| 140 | 67 |
} |
| 141 |
|
|
| 68 |
|
|
| 142 | 69 |
protected void before() {
|
| 143 |
|
|
| 70 |
|
|
| 144 | 71 |
} |
| 145 |
|
|
| 72 |
|
|
| 146 | 73 |
protected void after() throws XMLStreamException, IOException {
|
| 147 |
if (factory != null) {
|
|
| 148 |
factory = null; |
|
| 149 |
if (parser != null) {
|
|
| 150 |
parser.close(); |
|
| 151 |
} |
|
| 152 |
writer.flush(); |
|
| 153 |
if (writer != null) {
|
|
| 154 |
writer.close(); |
|
| 155 |
} |
|
| 156 |
if (inputData != null) {
|
|
| 157 |
inputData.close(); |
|
| 158 |
} |
|
| 159 |
writer = null; |
|
| 160 |
parser = null; |
|
| 161 |
} |
|
| 74 |
factory = null; |
|
| 75 |
if (parser !=null) parser.close(); |
|
| 76 |
writer.flush(); |
|
| 77 |
if (writer != null) writer.close(); |
|
| 78 |
if (inputData != null) inputData.close(); |
|
| 79 |
writer = null; |
|
| 80 |
parser = null; |
|
| 162 | 81 |
} |
| 163 |
|
|
| 82 |
|
|
| 164 | 83 |
protected void closeForError() throws XMLStreamException, IOException {
|
| 165 |
if (factory != null) {
|
|
| 166 |
if (parser != null) {
|
|
| 167 |
parser.close(); |
|
| 168 |
} |
|
| 169 |
if (inputData != null) {
|
|
| 170 |
inputData.close(); |
|
| 171 |
} |
|
| 172 |
} |
|
| 84 |
if (parser !=null) parser.close(); |
|
| 85 |
if (inputData != null) inputData.close(); |
|
| 173 | 86 |
} |
| 174 |
|
|
| 87 |
|
|
| 175 | 88 |
/** |
| 176 | 89 |
* Creates the output. |
| 177 | 90 |
* |
| ... | ... | |
| 180 | 93 |
*/ |
| 181 | 94 |
private boolean createOutput(File f) {
|
| 182 | 95 |
try {
|
| 183 |
if (writer != null) { // process from a file
|
|
| 96 |
if (writer != null) // process from a file |
|
| 184 | 97 |
writer.close(); |
| 185 |
} |
|
| 186 |
if (output != null) { // process from a file
|
|
| 98 |
if (output != null) // process from a file |
|
| 187 | 99 |
output.close(); |
| 188 |
} |
|
| 189 |
|
|
| 190 |
output = new BufferedOutputStream(new FileOutputStream(f), 16 * 1024); |
|
| 191 |
|
|
| 192 |
writer = outfactory.createXMLStreamWriter(output, "UTF-8");// create a new file |
|
| 100 |
|
|
| 101 |
output = new BufferedOutputStream(new FileOutputStream(f), 16*1024); |
|
| 102 |
|
|
| 103 |
writer = outfactory.createXMLStreamWriter(output, "UTF-8");//create a new file |
|
| 193 | 104 |
writer.setNamespaceContext(Nscontext); |
| 194 | 105 |
return true; |
| 195 |
} |
|
| 196 |
catch (Exception e) {
|
|
| 197 |
System.out.println("Error: create output of " + f + ": " + e);
|
|
| 106 |
} catch (Exception e) {
|
|
| 107 |
System.out.println("Error: create output of "+f+": "+e);
|
|
| 198 | 108 |
return false; |
| 199 | 109 |
} |
| 200 | 110 |
} |
| 201 |
|
|
| 202 |
public boolean process(File outfile) throws XMLStreamException, IOException {
|
|
| 203 |
if (factory == null) {
|
|
| 204 |
System.out.println("Error: this parser is a Hook parser to be used with another StaxIdentityParser as a Hook");
|
|
| 111 |
|
|
| 112 |
public boolean process(File outfile) throws XMLStreamException, IOException |
|
| 113 |
{
|
|
| 114 |
if (!createOutput(outfile))
|
|
| 205 | 115 |
return false; |
| 206 |
} |
|
| 207 |
if (!createOutput(outfile)) {
|
|
| 208 |
return false; |
|
| 209 |
} |
|
| 210 |
|
|
| 116 |
|
|
| 211 | 117 |
writer.writeStartDocument("UTF-8", "1.0");
|
| 212 | 118 |
writer.writeCharacters("\n");
|
| 213 | 119 |
boolean ret = process(writer); |
| ... | ... | |
| 215 | 121 |
writer.close(); |
| 216 | 122 |
} |
| 217 | 123 |
if (output != null) {
|
| 218 |
try {
|
|
| 219 |
output.close(); |
|
| 220 |
} |
|
| 221 |
catch (Exception e) {
|
|
| 222 |
System.out.println("output excep: " + e);
|
|
| 223 |
} |
|
| 124 |
try {output.close();} catch(Exception e){System.out.println("output excep: "+e);}
|
|
| 224 | 125 |
} |
| 225 |
|
|
| 126 |
|
|
| 226 | 127 |
if (parser != null) {
|
| 227 |
try {
|
|
| 228 |
parser.close(); |
|
| 229 |
} |
|
| 230 |
catch (Exception e) {
|
|
| 231 |
System.out.println("parser excep: " + e);
|
|
| 232 |
} |
|
| 128 |
try {parser.close();} catch(Exception e){System.out.println("parser excep: "+e);}
|
|
| 233 | 129 |
} |
| 234 | 130 |
|
| 235 | 131 |
if (inputData != null) {
|
| 236 |
try {
|
|
| 237 |
inputData.close(); |
|
| 238 |
} |
|
| 239 |
catch (Exception e) {
|
|
| 240 |
System.out.println("inputData excep: " + e);
|
|
| 241 |
} |
|
| 132 |
try {inputData.close();} catch(Exception e){System.out.println("inputData excep: "+e);}
|
|
| 242 | 133 |
} |
| 243 |
|
|
| 134 |
|
|
| 244 | 135 |
return ret; |
| 245 | 136 |
} |
| 246 |
|
|
| 137 |
|
|
| 247 | 138 |
public final static String SLASH = "/"; |
| 248 |
|
|
| 249 |
public boolean process(XMLStreamWriter awriter) throws XMLStreamException, IOException {
|
|
| 250 |
|
|
| 251 |
if (factory == null) {
|
|
| 252 |
System.out.println("Error: this parser is a Hook parser to be used with another StaxIdentityParser as a Hook");
|
|
| 253 |
return false; |
|
| 254 |
} |
|
| 255 |
|
|
| 139 |
public boolean process(XMLStreamWriter awriter) throws XMLStreamException, IOException |
|
| 140 |
{
|
|
| 256 | 141 |
this.writer = awriter; |
| 257 |
// if (processingXInclude == 0) {
|
|
| 142 |
//if (processingXInclude == 0) {
|
|
| 258 | 143 |
before(); // if you need to do something before reading the xml |
| 259 |
// }
|
|
| 144 |
//} |
|
| 260 | 145 |
try {
|
| 261 | 146 |
for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) {
|
| 262 |
|
|
| 263 |
if (hooks.size() == 0) {
|
|
| 264 |
this.processParserEvent(event); |
|
| 265 |
continue; |
|
| 147 |
switch (event) {
|
|
| 148 |
case XMLStreamConstants.NAMESPACE: |
|
| 149 |
processNamespace(); |
|
| 150 |
break; |
|
| 151 |
case XMLStreamConstants.START_ELEMENT: |
|
| 152 |
localname = parser.getLocalName(); |
|
| 153 |
//currentXPath.append(SLASH) |
|
| 154 |
processStartElement(); |
|
| 155 |
break; |
|
| 156 |
case XMLStreamConstants.CHARACTERS: |
|
| 157 |
processCharacters(); |
|
| 158 |
break; |
|
| 159 |
case XMLStreamConstants.END_ELEMENT: |
|
| 160 |
localname = parser.getLocalName(); |
|
| 161 |
processEndElement(); |
|
| 162 |
//currentXPath.substring(0, currentXPath.length() - localname.length() -1) |
|
| 163 |
break; |
|
| 164 |
case XMLStreamConstants.PROCESSING_INSTRUCTION: |
|
| 165 |
processProcessingInstruction(); |
|
| 166 |
break; |
|
| 167 |
case XMLStreamConstants.DTD: |
|
| 168 |
processDTD(); |
|
| 169 |
break; |
|
| 170 |
case XMLStreamConstants.CDATA: |
|
| 171 |
processCDATA(); |
|
| 172 |
break; |
|
| 173 |
case XMLStreamConstants.COMMENT: |
|
| 174 |
processComment(); |
|
| 175 |
break; |
|
| 176 |
case XMLStreamConstants.END_DOCUMENT: |
|
| 177 |
processEndDocument(); |
|
| 178 |
break; |
|
| 179 |
case XMLStreamConstants.ENTITY_REFERENCE: |
|
| 180 |
processEntityReference(); |
|
| 181 |
break; |
|
| 266 | 182 |
} |
| 267 |
else {
|
|
| 268 |
|
|
| 269 |
if (activeHook != null) {
|
|
| 270 |
|
|
| 271 |
if (activeHook.mustDeactivate()) {
|
|
| 272 |
activeHook.deactivate(); |
|
| 273 |
activeHook = null; |
|
| 274 |
} |
|
| 275 |
else {
|
|
| 276 |
activeHook.processParserEvent(event); |
|
| 277 |
continue; |
|
| 278 |
} |
|
| 279 |
} |
|
| 280 |
|
|
| 281 |
if (activeHook == null) {
|
|
| 282 |
for (StaxIdentityParser hook : hooks.values()) {
|
|
| 283 |
if (hook.mustActivate()) {
|
|
| 284 |
if (hook.activate()) {
|
|
| 285 |
activeHook = hook; |
|
| 286 |
activeHook.processParserEvent(event); |
|
| 287 |
continue; |
|
| 288 |
} |
|
| 289 |
} |
|
| 290 |
} |
|
| 291 |
} |
|
| 292 |
|
|
| 293 |
if (activeHook == null) {
|
|
| 294 |
this.processParserEvent(event); |
|
| 295 |
} |
|
| 296 |
} |
|
| 297 | 183 |
} |
| 298 |
} |
|
| 299 |
catch (Exception e) {
|
|
| 300 |
System.out.println("Unexpected error while parsing file " + inputurl + " : " + e);
|
|
| 301 |
System.out.println("Location line: " + parser.getLocation().getLineNumber() + " character: " + parser.getLocation().getColumnNumber());
|
|
| 184 |
} catch(Exception e) {
|
|
| 185 |
System.out.println("Unexpected error while parsing file "+inputurl+" : "+e);
|
|
| 186 |
System.out.println("Location line: "+parser.getLocation().getLineNumber()+" character: "+parser.getLocation().getColumnNumber());
|
|
| 302 | 187 |
org.txm.utils.logger.Log.printStackTrace(e); |
| 303 |
// e.printStackTrace();
|
|
| 188 |
//e.printStackTrace(); |
|
| 304 | 189 |
if (writer != null) writer.close(); |
| 305 | 190 |
if (output != null) output.close(); |
| 306 | 191 |
parser.close(); |
| 307 | 192 |
inputData.close(); |
| 308 | 193 |
return false; |
| 309 | 194 |
} |
| 310 |
|
|
| 311 |
// if (processingXInclude == 0) {
|
|
| 195 |
//if (processingXInclude == 0) {
|
|
| 312 | 196 |
after(); // if you need to do something before closing the parser(); |
| 313 |
// }
|
|
| 197 |
//} |
|
| 314 | 198 |
return true; |
| 315 |
|
|
| 316 | 199 |
} |
| 317 |
|
|
| 318 |
protected void processParserEvent(int event) throws XMLStreamException, IOException {
|
|
| 319 |
switch (event) {
|
|
| 320 |
case XMLStreamConstants.NAMESPACE: |
|
| 321 |
processNamespace(); |
|
| 322 |
break; |
|
| 323 |
case XMLStreamConstants.START_ELEMENT: |
|
| 324 |
localname = parser.getLocalName(); |
|
| 325 |
currentXPath.append(SLASH); |
|
| 326 |
currentXPath.append(localname); |
|
| 327 |
buildCurrentAttributes(); // for easier access later |
|
| 328 |
|
|
| 329 |
|
|
| 330 |
|
|
| 331 |
processStartElement(); |
|
| 332 |
break; |
|
| 333 |
case XMLStreamConstants.CHARACTERS: |
|
| 334 |
processCharacters(); |
|
| 335 |
break; |
|
| 336 |
case XMLStreamConstants.END_ELEMENT: |
|
| 337 |
localname = parser.getLocalName(); |
|
| 338 |
processEndElement(); |
|
| 339 |
currentXPath.setLength(currentXPath.length() - localname.length() - 1); |
|
| 340 |
break; |
|
| 341 |
case XMLStreamConstants.PROCESSING_INSTRUCTION: |
|
| 342 |
processProcessingInstruction(); |
|
| 343 |
break; |
|
| 344 |
case XMLStreamConstants.DTD: |
|
| 345 |
processDTD(); |
|
| 346 |
break; |
|
| 347 |
case XMLStreamConstants.CDATA: |
|
| 348 |
processCDATA(); |
|
| 349 |
break; |
|
| 350 |
case XMLStreamConstants.COMMENT: |
|
| 351 |
processComment(); |
|
| 352 |
break; |
|
| 353 |
case XMLStreamConstants.END_DOCUMENT: |
|
| 354 |
processEndDocument(); |
|
| 355 |
break; |
|
| 356 |
case XMLStreamConstants.ENTITY_REFERENCE: |
|
| 357 |
processEntityReference(); |
|
| 358 |
break; |
|
| 359 |
} |
|
| 360 |
} |
|
| 361 |
|
|
| 200 |
|
|
| 362 | 201 |
/** |
| 363 | 202 |
* The start element has already been written |
| 364 |
* |
|
| 365 | 203 |
* @param tagname |
| 366 |
* @throws XMLStreamException |
|
| 367 |
* @throws IOException |
|
| 204 |
* @throws XMLStreamException
|
|
| 205 |
* @throws IOException
|
|
| 368 | 206 |
*/ |
| 369 | 207 |
public void goToEnd(String tagname) throws XMLStreamException, IOException {
|
| 208 |
//System.out.println("start gotoend $tagname"
|
|
| 370 | 209 |
int elements = 1; |
| 371 | 210 |
try {
|
| 372 | 211 |
for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) {
|
| 373 |
// System.out.println("event "+event
|
|
| 212 |
//System.out.println("event "+event
|
|
| 374 | 213 |
switch (event) {
|
| 375 |
case XMLStreamConstants.NAMESPACE: |
|
| 376 |
processNamespace(); |
|
| 377 |
break; |
|
| 378 |
case XMLStreamConstants.START_ELEMENT: |
|
| 379 |
elements++; |
|
| 380 |
localname = parser.getLocalName(); |
|
| 381 |
currentXPath.append(SLASH); |
|
| 382 |
currentXPath.append(localname); |
|
| 383 |
buildCurrentAttributes(); |
|
| 384 |
|
|
| 385 |
_processStartElement(); |
|
| 386 |
break; |
|
| 387 |
case XMLStreamConstants.CHARACTERS: |
|
| 388 |
processCharacters(); |
|
| 389 |
break; |
|
| 390 |
case XMLStreamConstants.PROCESSING_INSTRUCTION: |
|
| 391 |
processProcessingInstruction(); |
|
| 392 |
break; |
|
| 393 |
case XMLStreamConstants.DTD: |
|
| 394 |
processDTD(); |
|
| 395 |
break; |
|
| 396 |
case XMLStreamConstants.CDATA: |
|
| 397 |
processCDATA(); |
|
| 398 |
break; |
|
| 399 |
case XMLStreamConstants.COMMENT: |
|
| 400 |
processComment(); |
|
| 401 |
break; |
|
| 402 |
case XMLStreamConstants.END_ELEMENT: |
|
| 403 |
elements--; |
|
| 404 |
localname = parser.getLocalName(); |
|
| 405 |
|
|
| 406 |
writer.writeEndElement(); |
|
| 407 |
|
|
| 408 |
currentXPath.setLength(currentXPath.length() - localname.length() - 1); |
|
| 409 |
|
|
| 410 |
if (elements == 0 && localname == tagname) |
|
| 411 |
return; |
|
| 412 |
break; |
|
| 413 |
case XMLStreamConstants.END_DOCUMENT: |
|
| 414 |
processEndDocument(); |
|
| 415 |
break; |
|
| 416 |
case XMLStreamConstants.ENTITY_REFERENCE: |
|
| 417 |
processEntityReference(); |
|
| 418 |
break; |
|
| 214 |
case XMLStreamConstants.NAMESPACE: |
|
| 215 |
processNamespace(); |
|
| 216 |
break; |
|
| 217 |
case XMLStreamConstants.START_ELEMENT: |
|
| 218 |
elements++; |
|
| 219 |
localname = parser.getLocalName(); |
|
| 220 |
//currentXPath.append(SLASH) |
|
| 221 |
_processStartElement(); |
|
| 222 |
break; |
|
| 223 |
case XMLStreamConstants.CHARACTERS: |
|
| 224 |
processCharacters(); |
|
| 225 |
break; |
|
| 226 |
case XMLStreamConstants.PROCESSING_INSTRUCTION: |
|
| 227 |
processProcessingInstruction(); |
|
| 228 |
break; |
|
| 229 |
case XMLStreamConstants.DTD: |
|
| 230 |
processDTD(); |
|
| 231 |
break; |
|
| 232 |
case XMLStreamConstants.CDATA: |
|
| 233 |
processCDATA(); |
|
| 234 |
break; |
|
| 235 |
case XMLStreamConstants.COMMENT: |
|
| 236 |
processComment(); |
|
| 237 |
break; |
|
| 238 |
case XMLStreamConstants.END_ELEMENT: |
|
| 239 |
elements--; |
|
| 240 |
localname = parser.getLocalName(); |
|
| 241 |
//currentXPath.substring(0, currentXPath.length() - localname.length() -1) |
|
| 242 |
writer.writeEndElement(); |
|
| 243 |
if (elements == 0 && localname == tagname) |
|
| 244 |
return; |
|
| 245 |
break; |
|
| 246 |
case XMLStreamConstants.END_DOCUMENT: |
|
| 247 |
processEndDocument(); |
|
| 248 |
break; |
|
| 249 |
case XMLStreamConstants.ENTITY_REFERENCE: |
|
| 250 |
processEntityReference(); |
|
| 251 |
break; |
|
| 419 | 252 |
} |
| 420 | 253 |
} |
| 421 |
} |
|
| 422 |
catch (Exception e) {
|
|
| 423 |
System.out.println("Error while parsing file " + inputurl);
|
|
| 424 |
System.out.println("Location " + parser.getLocation());
|
|
| 254 |
} catch(Exception e) {
|
|
| 255 |
System.out.println("Error while parsing file "+inputurl);
|
|
| 256 |
System.out.println("Location "+parser.getLocation());
|
|
| 425 | 257 |
org.txm.utils.logger.Log.printStackTrace(e); |
| 426 | 258 |
output.close(); |
| 427 | 259 |
parser.close(); |
| 428 | 260 |
return; |
| 429 | 261 |
} |
| 430 | 262 |
} |
| 431 |
|
|
| 263 |
|
|
| 432 | 264 |
public String getLocation() {
|
| 433 |
if (parser != null) {
|
|
| 434 |
return "Line: " + parser.getLocation().getLineNumber() + " Col: " + parser.getLocation().getColumnNumber(); |
|
| 435 |
} |
|
| 265 |
if (parser != null) |
|
| 266 |
return "Line: "+parser.getLocation().getLineNumber()+" Col: "+parser.getLocation(). getColumnNumber(); |
|
| 436 | 267 |
return null; |
| 437 | 268 |
} |
| 438 |
|
|
| 439 |
public static final String INCLUDE = "include"; |
|
| 440 |
|
|
| 441 |
public static final String XI = "xi"; |
|
| 442 |
|
|
| 443 |
|
|
| 269 |
|
|
| 444 | 270 |
protected void processNamespace() throws XMLStreamException {
|
| 445 | 271 |
writer.writeNamespace(parser.getPrefix(), parser.getNamespaceURI()); |
| 446 | 272 |
} |
| 447 |
|
|
| 448 |
|
|
| 449 |
protected void processStartElement() throws XMLStreamException, IOException {
|
|
| 273 |
|
|
| 274 |
public static final String INCLUDE = "include"; |
|
| 275 |
public static final String XI = "xi"; |
|
| 276 |
|
|
| 277 |
protected void processStartElement() throws XMLStreamException, IOException |
|
| 278 |
{
|
|
| 450 | 279 |
String prefix = parser.getPrefix(); |
| 451 | 280 |
if (INCLUDE == localname && XI == prefix) {
|
| 452 | 281 |
processXInclude(); |
| 453 |
} |
|
| 454 |
else {
|
|
| 455 |
if (prefix != null && prefix.length() > 0) {
|
|
| 282 |
} else {
|
|
| 283 |
if (prefix != null && prefix.length() > 0) |
|
| 456 | 284 |
writer.writeStartElement(Nscontext.getNamespaceURI(prefix), localname); |
| 457 |
} |
|
| 458 |
else {
|
|
| 285 |
else |
|
| 459 | 286 |
writer.writeStartElement(localname); |
| 460 |
} |
|
| 461 |
|
|
| 462 |
for (int i = 0; i < parser.getNamespaceCount(); i++) {
|
|
| 287 |
|
|
| 288 |
for (int i = 0 ; i < parser.getNamespaceCount() ; i++) {
|
|
| 463 | 289 |
writer.writeNamespace(parser.getNamespacePrefix(i), parser.getNamespaceURI(i)); |
| 464 | 290 |
} |
| 465 |
|
|
| 291 |
|
|
| 466 | 292 |
writeAttributes(); |
| 467 | 293 |
} |
| 468 | 294 |
} |
| 469 |
|
|
| 470 |
private void _processStartElement() throws XMLStreamException, IOException {
|
|
| 295 |
|
|
| 296 |
private void _processStartElement() throws XMLStreamException, IOException |
|
| 297 |
{
|
|
| 471 | 298 |
String prefix = parser.getPrefix(); |
| 472 | 299 |
if (INCLUDE == localname && XI == prefix) {
|
| 473 | 300 |
processXInclude(); |
| 474 |
} |
|
| 475 |
else {
|
|
| 476 |
if (prefix != null && prefix.length() > 0) {
|
|
| 301 |
} else {
|
|
| 302 |
if (prefix != null && prefix.length() > 0) |
|
| 477 | 303 |
writer.writeStartElement(Nscontext.getNamespaceURI(prefix), localname); |
| 478 |
} |
|
| 479 |
else {
|
|
| 304 |
else |
|
| 480 | 305 |
writer.writeStartElement(localname); |
| 481 |
} |
|
| 482 |
for (int i = 0; i < parser.getNamespaceCount(); i++) {
|
|
| 306 |
|
|
| 307 |
for (int i = 0 ; i < parser.getNamespaceCount() ; i++) {
|
|
| 483 | 308 |
writer.writeNamespace(parser.getNamespacePrefix(i), parser.getNamespaceURI(i)); |
| 484 | 309 |
} |
| 485 |
|
|
| 310 |
|
|
| 486 | 311 |
writeAttributes(); |
| 487 | 312 |
} |
| 488 | 313 |
} |
| 489 |
|
|
| 490 |
HashMap<String, String> currentAttributes = new HashMap<>(); |
|
| 491 |
|
|
| 492 |
public void buildCurrentAttributes() throws XMLStreamException {
|
|
| 493 |
currentAttributes.clear(); |
|
| 494 |
for (int i = 0; i < parser.getAttributeCount(); i++) {
|
|
| 495 |
currentAttributes.put(parser.getAttributeLocalName(i), parser.getAttributeValue(i)); |
|
| 496 |
} |
|
| 497 |
} |
|
| 498 |
|
|
| 499 |
public HashMap<String, String> getCurrentAttributes() throws XMLStreamException {
|
|
| 500 |
currentAttributes.clear(); |
|
| 501 |
for (int i = 0; i < parser.getAttributeCount(); i++) {
|
|
| 502 |
currentAttributes.put(parser.getAttributeLocalName(i), parser.getAttributeValue(i)); |
|
| 503 |
} |
|
| 504 |
return currentAttributes; |
|
| 505 |
} |
|
| 506 |
|
|
| 314 |
|
|
| 507 | 315 |
protected void writeAttributes() throws XMLStreamException {
|
| 508 |
for (int i = 0; i < parser.getAttributeCount(); i++) {
|
|
| 316 |
for (int i = 0 ; i < parser.getAttributeCount() ; i++) {
|
|
| 509 | 317 |
writeAttribute(parser.getAttributePrefix(i), parser.getAttributeLocalName(i), parser.getAttributeValue(i)); |
| 510 | 318 |
} |
| 511 | 319 |
} |
| 512 |
|
|
| 320 |
|
|
| 513 | 321 |
protected void writeAttribute(String prefix, String name, String value) throws XMLStreamException {
|
| 514 |
if (prefix != null && prefix.length() > 0) {
|
|
| 515 |
writer.writeAttribute(prefix + ":" + name, value); |
|
| 516 |
} |
|
| 517 |
else {
|
|
| 322 |
if (prefix != null && prefix.length() > 0) |
|
| 323 |
writer.writeAttribute(prefix+":"+name, value); |
|
| 324 |
else |
|
| 518 | 325 |
writer.writeAttribute(name, value); |
| 519 |
} |
|
| 520 | 326 |
} |
| 521 |
|
|
| 522 |
protected void processCharacters() throws XMLStreamException {
|
|
| 327 |
|
|
| 328 |
protected void processCharacters() throws XMLStreamException |
|
| 329 |
{
|
|
| 523 | 330 |
writer.writeCharacters(parser.getText()); |
| 524 | 331 |
} |
| 525 |
|
|
| 526 |
protected void processProcessingInstruction() throws XMLStreamException {
|
|
| 332 |
|
|
| 333 |
protected void processProcessingInstruction() throws XMLStreamException |
|
| 334 |
{
|
|
| 527 | 335 |
writer.writeProcessingInstruction(parser.getPITarget(), parser.getPIData()); |
| 528 | 336 |
} |
| 529 |
|
|
| 530 |
protected void processDTD() throws XMLStreamException {
|
|
| 337 |
|
|
| 338 |
protected void processDTD() throws XMLStreamException |
|
| 339 |
{
|
|
| 531 | 340 |
writer.writeDTD(parser.getText()); |
| 532 | 341 |
} |
| 533 |
|
|
| 534 |
protected void processCDATA() throws XMLStreamException {
|
|
| 535 |
writer.writeCData(parser.getText()); |
|
| 342 |
|
|
| 343 |
protected void processCDATA() throws XMLStreamException |
|
| 344 |
{
|
|
| 345 |
writer.writeCData(parser.getText()) ; |
|
| 536 | 346 |
} |
| 537 |
|
|
| 538 |
protected void processComment() throws XMLStreamException {
|
|
| 347 |
|
|
| 348 |
protected void processComment() throws XMLStreamException |
|
| 349 |
{
|
|
| 539 | 350 |
writer.writeComment(parser.getText()); |
| 540 | 351 |
} |
| 541 |
|
|
| 542 |
protected void processEndElement() throws XMLStreamException {
|
|
| 352 |
|
|
| 353 |
protected void processEndElement() throws XMLStreamException |
|
| 354 |
{
|
|
| 543 | 355 |
if (localname == INCLUDE && parser.getPrefix() == XI) {
|
| 544 | 356 |
// nothing !! |
| 545 |
} |
|
| 546 |
else {
|
|
| 357 |
} else {
|
|
| 547 | 358 |
writer.writeEndElement(); |
| 548 | 359 |
} |
| 549 | 360 |
} |
| 550 |
|
|
| 361 |
|
|
| 551 | 362 |
protected void processEndDocument() throws XMLStreamException {
|
| 552 | 363 |
writer.writeEndDocument(); |
| 553 | 364 |
} |
| 554 |
|
|
| 365 |
|
|
| 555 | 366 |
protected void processEntityReference() throws XMLStreamException {
|
| 556 | 367 |
writer.writeEntityRef(parser.getLocalName()); |
| 557 | 368 |
} |
| 558 |
|
|
| 559 | 369 |
/** |
| 560 | 370 |
* Process the XInclude elements |
| 561 |
* |
|
| 562 |
* @throws IOException |
|
| 563 |
* @throws XMLStreamException |
|
| 371 |
* @throws IOException |
|
| 372 |
* @throws XMLStreamException |
|
| 564 | 373 |
*/ |
| 565 | 374 |
protected void processXInclude() throws XMLStreamException, IOException {
|
| 566 | 375 |
String url = parser.getAttributeValue(null, "href"); // relative only |
| ... | ... | |
| 569 | 378 |
if (ref.exists()) {
|
| 570 | 379 |
URL includeurl = ref.toURI().toURL(); |
| 571 | 380 |
// save variables before killing them |
| 572 |
System.out.println("process xi include: " + ref);
|
|
| 381 |
System.out.println("process xi include: "+ref);
|
|
| 573 | 382 |
XMLStreamReader parserSave = this.parser; // back up current parser |
| 574 | 383 |
InputStream xiIncludeInputData = includeurl.openStream(); |
| 575 | 384 |
XMLStreamReader xiIncludeParser = factory.createXMLStreamReader(xiIncludeInputData); |
| ... | ... | |
| 578 | 387 |
this.process(writer); |
| 579 | 388 |
processingXInclude--; // end of XInclude processing |
| 580 | 389 |
this.parser = parserSave; // restore parser |
| 581 |
} |
|
| 582 |
else {
|
|
| 390 |
} else {
|
|
| 583 | 391 |
System.out.println("Warning referenced file: $ref does not exists");
|
| 584 | 392 |
} |
| 585 | 393 |
} |
| 586 |
|
|
| 587 |
public String getCurrentPath() {
|
|
| 588 |
return currentXPath.toString(); |
|
| 589 |
} |
|
| 590 |
|
|
| 591 |
public static void main(String[] args) {
|
|
| 394 |
|
|
| 395 |
public static void main(String[] args) |
|
| 396 |
{
|
|
| 592 | 397 |
try {
|
| 593 |
File input = new File(System.getProperty("user.home"), "xml/tdm80j/out.xml");
|
|
| 594 |
File output = new File(System.getProperty("user.home"), "TEMP/identity.xml");
|
|
| 398 |
File input = new File("/home/mdecorde/xml/xiinclude/master.xml");
|
|
| 399 |
File output = new File("/home/mdecorde/xml/xiinclude/merged.xml");
|
|
| 595 | 400 |
if (!(input.exists() && input.canRead())) {
|
| 596 | 401 |
System.out.println("cannot found $input");
|
| 597 | 402 |
return; |
| 598 | 403 |
} |
| 599 | 404 |
StaxIdentityParser builder; |
| 600 |
|
|
| 405 |
|
|
| 601 | 406 |
builder = new StaxIdentityParser(input.toURI().toURL()); |
| 602 |
long time = System.currentTimeMillis(); |
|
| 407 |
|
|
| 603 | 408 |
if (builder.process(output)) {
|
| 604 |
System.out.println("Time=" + (System.currentTimeMillis() - time));
|
|
| 605 |
System.out.println("success ? " + ValidateXml.test(output));
|
|
| 606 |
} |
|
| 607 |
else {
|
|
| 409 |
System.out.println("success ? "+ValidateXml.test(output));
|
|
| 410 |
} else {
|
|
| 608 | 411 |
System.out.println("failure !");
|
| 609 | 412 |
} |
| 610 |
} |
|
| 611 |
catch (Exception e) {
|
|
| 413 |
} catch (Exception e) {
|
|
| 612 | 414 |
// TODO Auto-generated catch block |
| 613 | 415 |
e.printStackTrace(); |
| 614 | 416 |
} |
| 615 | 417 |
} |
| 616 |
} |
|
| 418 |
} |
|
Formats disponibles : Unified diff