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