Révision 2649
tmp/org.txm.core/src/java/org/txm/xml/LocalNamesHookActivator.java (revision 2649) | ||
---|---|---|
1 |
package org.txm.xml; |
|
2 |
|
|
3 |
import java.util.Arrays; |
|
4 |
import java.util.HashSet; |
|
5 |
import java.util.List; |
|
6 |
|
|
7 |
/** |
|
8 |
* Simple Activator activated when the localname matches the localname constructor parameter |
|
9 |
* |
|
10 |
* @author mdecorde |
|
11 |
* |
|
12 |
*/ |
|
13 |
public class LocalNamesHookActivator extends HookActivator { |
|
14 |
|
|
15 |
HashSet<String> localnames; |
|
16 |
|
|
17 |
/** |
|
18 |
* @param localnames the element localnames to match |
|
19 |
*/ |
|
20 |
public LocalNamesHookActivator(Hook hook, List<String> localnames) { |
|
21 |
super(); |
|
22 |
this.localnames = new HashSet<>(); |
|
23 |
this.localnames.addAll(localnames); |
|
24 |
} |
|
25 |
|
|
26 |
/** |
|
27 |
* @param localnames the element localnames to match |
|
28 |
*/ |
|
29 |
public LocalNamesHookActivator(Hook hook, String... localnames) { |
|
30 |
this(hook, Arrays.asList(localnames)); |
|
31 |
} |
|
32 |
|
|
33 |
@Override |
|
34 |
public boolean mustActivate() { |
|
35 |
if (localnames == null) return false; |
|
36 |
|
|
37 |
return localnames.contains(hook.getLocalName()); |
|
38 |
} |
|
39 |
|
|
40 |
@Override |
|
41 |
public void hookWasActivated() { |
|
42 |
// nothing to do |
|
43 |
} |
|
44 |
} |
|
0 | 45 |
tmp/org.txm.core/src/java/org/txm/xml/XMLParser.java (revision 2649) | ||
---|---|---|
1 |
package org.txm.xml; |
|
2 |
|
|
3 |
import java.io.BufferedOutputStream; |
|
4 |
import java.io.File; |
|
5 |
import java.io.FileOutputStream; |
|
6 |
import java.io.IOException; |
|
7 |
import java.io.InputStream; |
|
8 |
import java.net.URL; |
|
9 |
import java.util.Arrays; |
|
10 |
import java.util.HashMap; |
|
11 |
import java.util.LinkedHashMap; |
|
12 |
import java.util.Stack; |
|
13 |
|
|
14 |
import javax.xml.stream.XMLInputFactory; |
|
15 |
import javax.xml.stream.XMLOutputFactory; |
|
16 |
import javax.xml.stream.XMLStreamConstants; |
|
17 |
import javax.xml.stream.XMLStreamException; |
|
18 |
import javax.xml.stream.XMLStreamReader; |
|
19 |
import javax.xml.stream.XMLStreamWriter; |
|
20 |
|
|
21 |
import org.txm.importer.PersonalNamespaceContext; |
|
22 |
|
|
23 |
/** |
|
24 |
* parse a XML files using Stax and allow to set somes hooks activated with their HookActivator |
|
25 |
* |
|
26 |
* @author mdecorde |
|
27 |
* |
|
28 |
*/ |
|
29 |
public class XMLParser { |
|
30 |
|
|
31 |
/** The input */ |
|
32 |
protected URL inputurl; |
|
33 |
|
|
34 |
protected InputStream inputData; |
|
35 |
|
|
36 |
protected XMLInputFactory factory; |
|
37 |
|
|
38 |
protected XMLStreamReader parser; |
|
39 |
|
|
40 |
public static String TXMNS = "http://textometrie.org/1.0"; |
|
41 |
|
|
42 |
public static String TXM = "txm"; |
|
43 |
|
|
44 |
public static String TEINS = "http://www.tei-c.org/ns/1.0"; |
|
45 |
|
|
46 |
public static String TEI = "tei"; |
|
47 |
|
|
48 |
protected static PersonalNamespaceContext Nscontext = new PersonalNamespaceContext(); |
|
49 |
|
|
50 |
protected StringBuilder currentXPath = new StringBuilder(""); |
|
51 |
|
|
52 |
protected String localname; |
|
53 |
|
|
54 |
int processingXInclude = 0; |
|
55 |
|
|
56 |
LinkedHashMap<String, Hook> hooks = new LinkedHashMap<>(); |
|
57 |
|
|
58 |
private Hook activeHook = null; |
|
59 |
|
|
60 |
public XMLParser(File infile) throws IOException, XMLStreamException { |
|
61 |
this(infile.toURI().toURL()); |
|
62 |
} |
|
63 |
|
|
64 |
public XMLParser(URL inputurl) throws IOException, XMLStreamException { |
|
65 |
this.inputurl = inputurl; |
|
66 |
this.inputData = inputurl.openStream(); |
|
67 |
this.factory = XMLInputFactory.newInstance(); |
|
68 |
this.parser = factory.createXMLStreamReader(inputData); |
|
69 |
} |
|
70 |
|
|
71 |
/** |
|
72 |
* Helper method to get an attribute value by its name |
|
73 |
* |
|
74 |
* @param name the attribute name |
|
75 |
* @return the value if any |
|
76 |
*/ |
|
77 |
public String getParserAttributeValue(String name) { |
|
78 |
if (name == null) return null; |
|
79 |
|
|
80 |
int c = parser.getAttributeCount(); |
|
81 |
for (int i = 0; i < c; i++) { |
|
82 |
if (name.equals(parser.getAttributeLocalName(i))) { |
|
83 |
return parser.getAttributeValue(i); |
|
84 |
} |
|
85 |
} |
|
86 |
|
|
87 |
return null; |
|
88 |
} |
|
89 |
|
|
90 |
protected void before() { |
|
91 |
|
|
92 |
} |
|
93 |
|
|
94 |
protected void after() throws XMLStreamException, IOException { |
|
95 |
if (factory != null) { |
|
96 |
factory = null; |
|
97 |
|
|
98 |
if (parser != null) { |
|
99 |
parser.close(); |
|
100 |
} |
|
101 |
if (inputData != null) { |
|
102 |
inputData.close(); |
|
103 |
} |
|
104 |
parser = null; |
|
105 |
|
|
106 |
if (parser != null) { |
|
107 |
try { |
|
108 |
parser.close(); |
|
109 |
} |
|
110 |
catch (Exception e) { |
|
111 |
System.out.println("parser excep: " + e); |
|
112 |
} |
|
113 |
} |
|
114 |
|
|
115 |
if (inputData != null) { |
|
116 |
try { |
|
117 |
inputData.close(); |
|
118 |
} |
|
119 |
catch (Exception e) { |
|
120 |
System.out.println("inputData excep: " + e); |
|
121 |
} |
|
122 |
} |
|
123 |
|
|
124 |
|
|
125 |
} |
|
126 |
} |
|
127 |
|
|
128 |
public final static String SLASH = "/"; |
|
129 |
|
|
130 |
public final boolean process() throws XMLStreamException, IOException { |
|
131 |
|
|
132 |
// if (processingXInclude == 0) { |
|
133 |
before(); // if you need to do something before reading the xml |
|
134 |
// } |
|
135 |
try { |
|
136 |
for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) { |
|
137 |
|
|
138 |
// preparing process |
|
139 |
if (event == XMLStreamConstants.START_ELEMENT) { |
|
140 |
localname = parser.getLocalName(); |
|
141 |
currentXPath.append(SLASH); |
|
142 |
currentXPath.append(localname); |
|
143 |
buildCurrentAttributes(); // for easier access later |
|
144 |
} |
|
145 |
else if (event == XMLStreamConstants.END_ELEMENT) { |
|
146 |
localname = parser.getLocalName(); |
|
147 |
} |
|
148 |
|
|
149 |
if (activeHook != null) { |
|
150 |
if (activeHook.mustDeactivate()) { |
|
151 |
activeHook.deactivate(); |
|
152 |
activeHook = null; |
|
153 |
} |
|
154 |
else { |
|
155 |
activeHook.processParserEvent(event); |
|
156 |
} |
|
157 |
} |
|
158 |
|
|
159 |
if (activeHook == null && event == XMLStreamConstants.START_ELEMENT) { // only activate when starting an element (for now, thus we cannot process only text nodes) |
|
160 |
for (Hook hook : hooks.values()) { |
|
161 |
if (hook.mustActivate()) { |
|
162 |
if (hook.activate()) { |
|
163 |
activeHook = hook; |
|
164 |
|
|
165 |
hook.processParserEvent(event); |
|
166 |
break; |
|
167 |
} |
|
168 |
} |
|
169 |
} |
|
170 |
} |
|
171 |
|
|
172 |
if (activeHook == null) { |
|
173 |
this.processParserEvent(event); |
|
174 |
} |
|
175 |
|
|
176 |
// post-preparing process |
|
177 |
if (event == XMLStreamConstants.END_ELEMENT) { |
|
178 |
currentXPath.setLength(currentXPath.length() - localname.length() - 1); |
|
179 |
popCurrentAttributes(); |
|
180 |
} |
|
181 |
} |
|
182 |
} |
|
183 |
catch (Exception e) { |
|
184 |
System.out.println("Unexpected error while parsing file " + inputurl + " : " + e); |
|
185 |
System.out.println("Location line: " + parser.getLocation().getLineNumber() + " character: " + parser.getLocation().getColumnNumber()); |
|
186 |
org.txm.utils.logger.Log.printStackTrace(e); |
|
187 |
// e.printStackTrace(); |
|
188 |
return false; |
|
189 |
} |
|
190 |
finally { |
|
191 |
after(); // if you need to do something before closing the parser(); |
|
192 |
} |
|
193 |
|
|
194 |
return true; |
|
195 |
} |
|
196 |
|
|
197 |
protected final void processParserEvent(int event) throws XMLStreamException, IOException { |
|
198 |
switch (event) { |
|
199 |
case XMLStreamConstants.NAMESPACE: |
|
200 |
processNamespace(); |
|
201 |
break; |
|
202 |
case XMLStreamConstants.START_ELEMENT: |
|
203 |
processStartElement(); |
|
204 |
break; |
|
205 |
case XMLStreamConstants.CHARACTERS: |
|
206 |
processCharacters(); |
|
207 |
break; |
|
208 |
case XMLStreamConstants.END_ELEMENT: |
|
209 |
processEndElement(); |
|
210 |
break; |
|
211 |
case XMLStreamConstants.PROCESSING_INSTRUCTION: |
|
212 |
processProcessingInstruction(); |
|
213 |
break; |
|
214 |
case XMLStreamConstants.DTD: |
|
215 |
processDTD(); |
|
216 |
break; |
|
217 |
case XMLStreamConstants.CDATA: |
|
218 |
processCDATA(); |
|
219 |
break; |
|
220 |
case XMLStreamConstants.COMMENT: |
|
221 |
processComment(); |
|
222 |
break; |
|
223 |
case XMLStreamConstants.END_DOCUMENT: |
|
224 |
processEndDocument(); |
|
225 |
break; |
|
226 |
case XMLStreamConstants.ENTITY_REFERENCE: |
|
227 |
processEntityReference(); |
|
228 |
break; |
|
229 |
} |
|
230 |
} |
|
231 |
|
|
232 |
/** |
|
233 |
* The start element has already been written |
|
234 |
* |
|
235 |
* @param tagname |
|
236 |
* @throws XMLStreamException |
|
237 |
* @throws IOException |
|
238 |
*/ |
|
239 |
public void goToEnd(String tagname) throws XMLStreamException, IOException { |
|
240 |
int elements = 1; |
|
241 |
for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) { |
|
242 |
// System.out.println("event "+event |
|
243 |
switch (event) { |
|
244 |
case XMLStreamConstants.NAMESPACE: |
|
245 |
processNamespace(); |
|
246 |
break; |
|
247 |
case XMLStreamConstants.START_ELEMENT: |
|
248 |
elements++; |
|
249 |
localname = parser.getLocalName(); |
|
250 |
currentXPath.append(SLASH); |
|
251 |
currentXPath.append(localname); |
|
252 |
buildCurrentAttributes(); |
|
253 |
|
|
254 |
processStartElement(); |
|
255 |
break; |
|
256 |
case XMLStreamConstants.CHARACTERS: |
|
257 |
processCharacters(); |
|
258 |
break; |
|
259 |
case XMLStreamConstants.PROCESSING_INSTRUCTION: |
|
260 |
processProcessingInstruction(); |
|
261 |
break; |
|
262 |
case XMLStreamConstants.DTD: |
|
263 |
processDTD(); |
|
264 |
break; |
|
265 |
case XMLStreamConstants.CDATA: |
|
266 |
processCDATA(); |
|
267 |
break; |
|
268 |
case XMLStreamConstants.COMMENT: |
|
269 |
processComment(); |
|
270 |
break; |
|
271 |
case XMLStreamConstants.END_ELEMENT: |
|
272 |
elements--; |
|
273 |
localname = parser.getLocalName(); |
|
274 |
|
|
275 |
currentXPath.setLength(currentXPath.length() - localname.length() - 1); |
|
276 |
|
|
277 |
if (elements == 0 && localname == tagname) |
|
278 |
return; |
|
279 |
break; |
|
280 |
case XMLStreamConstants.END_DOCUMENT: |
|
281 |
processEndDocument(); |
|
282 |
break; |
|
283 |
case XMLStreamConstants.ENTITY_REFERENCE: |
|
284 |
processEntityReference(); |
|
285 |
break; |
|
286 |
} |
|
287 |
} |
|
288 |
|
|
289 |
} |
|
290 |
|
|
291 |
public final String getLocation() { |
|
292 |
if (parser != null) { |
|
293 |
return "Line: " + parser.getLocation().getLineNumber() + " Col: " + parser.getLocation().getColumnNumber(); |
|
294 |
} |
|
295 |
return null; |
|
296 |
} |
|
297 |
|
|
298 |
private Stack<LinkedHashMap<String, String>> attributesStack = new Stack<>(); |
|
299 |
|
|
300 |
private final HashMap<String, String> popCurrentAttributes() { |
|
301 |
return attributesStack.pop(); |
|
302 |
} |
|
303 |
|
|
304 |
protected final void buildCurrentAttributes() throws XMLStreamException { |
|
305 |
LinkedHashMap<String, String> currentAttributes = new LinkedHashMap<>(); // keep order |
|
306 |
for (int i = 0; i < parser.getAttributeCount(); i++) { |
|
307 |
currentAttributes.put(parser.getAttributeLocalName(i), parser.getAttributeValue(i)); |
|
308 |
} |
|
309 |
attributesStack.push(currentAttributes); |
|
310 |
} |
|
311 |
|
|
312 |
public final LinkedHashMap<String, String> getCurrentAttributes() { |
|
313 |
return attributesStack.lastElement(); |
|
314 |
} |
|
315 |
|
|
316 |
protected void processNamespace() throws XMLStreamException {} |
|
317 |
|
|
318 |
protected void processStartElement() throws XMLStreamException, IOException {} |
|
319 |
|
|
320 |
protected void processCharacters() throws XMLStreamException {} |
|
321 |
|
|
322 |
protected void processProcessingInstruction() throws XMLStreamException {} |
|
323 |
|
|
324 |
protected void processDTD() throws XMLStreamException {} |
|
325 |
|
|
326 |
protected void processCDATA() throws XMLStreamException {} |
|
327 |
|
|
328 |
protected void processComment() throws XMLStreamException {} |
|
329 |
|
|
330 |
protected void processEndElement() throws XMLStreamException {} |
|
331 |
|
|
332 |
protected void processEndDocument() throws XMLStreamException {} |
|
333 |
|
|
334 |
protected void processEntityReference() throws XMLStreamException {} |
|
335 |
|
|
336 |
public String getCurrentPath() { |
|
337 |
return currentXPath.toString(); |
|
338 |
} |
|
339 |
|
|
340 |
public static void main(String[] args) { |
|
341 |
try { |
|
342 |
File input = new File(System.getProperty("user.home"), "xml/tdm80j/tdm80j.xml"); |
|
343 |
if (!(input.exists() && input.canRead())) { |
|
344 |
System.out.println("cannot found $input"); |
|
345 |
return; |
|
346 |
} |
|
347 |
XMLParser builder; |
|
348 |
|
|
349 |
builder = new XMLParser(input.toURI().toURL()); |
|
350 |
|
|
351 |
// IdentityHook hook1 = new IdentityHook("hook1", new LocalNameHookActivator(null, "p"), builder) { |
|
352 |
// |
|
353 |
// @Override |
|
354 |
// public boolean _activate() { |
|
355 |
// System.out.println("ACTIVATING " + name + " AT " + getLocation()); |
|
356 |
// return true; |
|
357 |
// } |
|
358 |
// |
|
359 |
// @Override |
|
360 |
// public boolean deactivate() { |
|
361 |
// System.out.println("DEACTIVATING " + name + " AT " + getLocation()); |
|
362 |
// return true; |
|
363 |
// } |
|
364 |
// |
|
365 |
// @Override |
|
366 |
// public void processStartElement() throws XMLStreamException, IOException { |
|
367 |
// super.processStartElement(); |
|
368 |
// parentParser.writer.writeAttribute("hook", name); |
|
369 |
// } |
|
370 |
// }; |
|
371 |
XPathHookActivator xpathActivator = new XPathHookActivator(null, "//p"); |
|
372 |
XPathsHookActivator xpathsActivator = new XPathsHookActivator(null, Arrays.asList("//p", "//p")); |
|
373 |
// IdentityHook hook2 = new IdentityHook("hook2", xpathsActivator, builder) { |
|
374 |
// |
|
375 |
// @Override |
|
376 |
// public boolean _activate() { |
|
377 |
// System.out.println("ACTIVATING " + name + " AT " + getLocation()); |
|
378 |
// return true; |
|
379 |
// } |
|
380 |
// |
|
381 |
// @Override |
|
382 |
// public boolean deactivate() { |
|
383 |
// System.out.println("DEACTIVATING " + name + " AT " + getLocation()); |
|
384 |
// return true; |
|
385 |
// } |
|
386 |
// |
|
387 |
// @Override |
|
388 |
// public void processStartElement() throws XMLStreamException, IOException { |
|
389 |
// super.processStartElement(); |
|
390 |
// parentParser.writer.writeAttribute("hook", name); |
|
391 |
// } |
|
392 |
// }; |
|
393 |
|
|
394 |
long time = System.currentTimeMillis(); |
|
395 |
if (builder.process()) { |
|
396 |
System.out.println("Time=" + (System.currentTimeMillis() - time)); |
|
397 |
System.out.println("XPaths activator has done working ? " + xpathsActivator.hasAllXpathsBeenProcessed()); |
|
398 |
} |
|
399 |
else { |
|
400 |
System.out.println("failure !"); |
|
401 |
} |
|
402 |
} |
|
403 |
catch (Exception e) { |
|
404 |
e.printStackTrace(); |
|
405 |
} |
|
406 |
} |
|
407 |
|
|
408 |
/** |
|
409 |
* |
|
410 |
* @return the actual hooks hash |
|
411 |
*/ |
|
412 |
public LinkedHashMap<String, Hook> getHooks() { |
|
413 |
return hooks; |
|
414 |
} |
|
415 |
|
|
416 |
public void addHook(String name, Hook hook) { |
|
417 |
hooks.put(name, hook); |
|
418 |
} |
|
419 |
} |
|
0 | 420 |
tmp/org.txm.core/src/java/org/txm/xml/Hook.java (revision 2649) | ||
---|---|---|
4 | 4 |
|
5 | 5 |
import javax.xml.stream.XMLStreamConstants; |
6 | 6 |
import javax.xml.stream.XMLStreamException; |
7 |
import javax.xml.stream.XMLStreamReader; |
|
7 | 8 |
|
8 | 9 |
import org.txm.utils.logger.Log; |
9 | 10 |
|
10 |
public abstract class Hook { |
|
11 |
public abstract class Hook<T extends XMLParser> {
|
|
11 | 12 |
|
12 |
protected XMLProcessor parentParser = null;
|
|
13 |
protected T parentParser = null;
|
|
13 | 14 |
|
14 | 15 |
protected HookActivator activator = null; |
15 | 16 |
|
... | ... | |
17 | 18 |
|
18 | 19 |
private int elements = 0; |
19 | 20 |
|
21 |
protected XMLStreamReader parser; |
|
22 |
|
|
20 | 23 |
/** |
21 | 24 |
* create a hook parser. When a hook parser is activated, the processXYZ methods are used instead of the parent processXTZ methods. |
22 | 25 |
* |
... | ... | |
28 | 31 |
* @throws IOException |
29 | 32 |
* @throws XMLStreamException |
30 | 33 |
*/ |
31 |
public Hook(String name, HookActivator activator, XMLProcessor parentParser) throws IOException, XMLStreamException {
|
|
34 |
public Hook(String name, HookActivator activator, T parentParser) throws IOException, XMLStreamException {
|
|
32 | 35 |
this.parentParser = parentParser; |
33 | 36 |
this.name = name; |
34 | 37 |
setActivator(activator); // set the activator |
35 | 38 |
parentParser.addHook(name, this); // register the hook to the XMLProcessor |
39 |
this.parser = parentParser.parser; |
|
36 | 40 |
} |
37 | 41 |
|
38 | 42 |
public void setActivator(HookActivator activator) { |
... | ... | |
43 | 47 |
} |
44 | 48 |
|
45 | 49 |
/** |
50 |
* |
|
51 |
* @return the hook's name |
|
52 |
*/ |
|
53 |
public String getName() { |
|
54 |
return name; |
|
55 |
} |
|
56 |
|
|
57 |
/** |
|
46 | 58 |
* called when the parser is activated by its parent |
47 | 59 |
* |
48 | 60 |
* @return true if correctly activated |
tmp/org.txm.core/src/java/org/txm/xml/UDPipeAnnotate.java (revision 2649) | ||
---|---|---|
1 |
package org.txm.xml; |
|
2 |
|
|
3 |
|
|
4 |
public class UDPipeAnnotate { |
|
5 |
|
|
6 |
} |
|
0 | 7 |
tmp/org.txm.core/src/java/org/txm/xml/XMLProcessor.java (revision 2649) | ||
---|---|---|
4 | 4 |
import java.io.File; |
5 | 5 |
import java.io.FileOutputStream; |
6 | 6 |
import java.io.IOException; |
7 |
import java.io.InputStream; |
|
8 | 7 |
import java.net.URL; |
9 | 8 |
import java.util.Arrays; |
10 |
import java.util.HashMap; |
|
11 | 9 |
import java.util.LinkedHashMap; |
12 |
import java.util.Stack; |
|
13 | 10 |
|
14 |
import javax.xml.stream.XMLInputFactory; |
|
15 | 11 |
import javax.xml.stream.XMLOutputFactory; |
16 | 12 |
import javax.xml.stream.XMLStreamConstants; |
17 | 13 |
import javax.xml.stream.XMLStreamException; |
18 |
import javax.xml.stream.XMLStreamReader; |
|
19 | 14 |
import javax.xml.stream.XMLStreamWriter; |
20 | 15 |
|
21 |
import org.txm.importer.PersonalNamespaceContext; |
|
22 |
|
|
23 |
public class XMLProcessor { |
|
16 |
/** |
|
17 |
* Parse and write a XML file. If no hook specified the file is not transformed |
|
18 |
* |
|
19 |
* @author mdecorde |
|
20 |
* |
|
21 |
*/ |
|
22 |
public class XMLProcessor extends XMLParser { |
|
24 | 23 |
|
25 |
/** The input */ |
|
26 |
protected URL inputurl; |
|
27 |
|
|
28 |
protected InputStream inputData; |
|
29 |
|
|
30 |
protected XMLInputFactory factory; |
|
31 |
|
|
32 |
protected XMLStreamReader parser; |
|
33 |
|
|
34 | 24 |
/** The output. */ |
35 | 25 |
protected XMLOutputFactory outfactory = XMLOutputFactory.newInstance(); |
36 | 26 |
|
... | ... | |
38 | 28 |
|
39 | 29 |
protected XMLStreamWriter writer; |
40 | 30 |
|
41 |
public static String TXMNS = "http://textometrie.org/1.0"; |
|
42 |
|
|
43 |
public static String TXM = "txm"; |
|
44 |
|
|
45 |
public static String TEINS = "http://www.tei-c.org/ns/1.0"; |
|
46 |
|
|
47 |
public static String TEI = "tei"; |
|
48 |
|
|
49 |
protected static PersonalNamespaceContext Nscontext = new PersonalNamespaceContext(); |
|
50 |
|
|
51 |
protected StringBuilder currentXPath = new StringBuilder(""); |
|
52 |
|
|
53 |
protected String localname; |
|
54 |
|
|
55 |
int processingXInclude = 0; |
|
56 |
|
|
57 |
LinkedHashMap<String, Hook> hooks = new LinkedHashMap<>(); |
|
58 |
|
|
59 |
private Hook activeHook = null; |
|
60 |
|
|
61 | 31 |
public XMLProcessor(File infile) throws IOException, XMLStreamException { |
62 | 32 |
this(infile.toURI().toURL()); |
63 | 33 |
} |
64 | 34 |
|
65 | 35 |
public XMLProcessor(URL inputurl) throws IOException, XMLStreamException { |
66 |
this.inputurl = inputurl; |
|
67 |
this.inputData = inputurl.openStream(); |
|
68 |
this.factory = XMLInputFactory.newInstance(); |
|
69 |
this.parser = factory.createXMLStreamReader(inputData); |
|
36 |
super(inputurl); |
|
70 | 37 |
} |
71 | 38 |
|
72 | 39 |
/** |
73 |
* called when the parser is activated by its parent |
|
74 |
* |
|
75 |
* @return true if correctly activated |
|
76 |
*/ |
|
77 |
public boolean activate() { |
|
78 |
return false; |
|
79 |
} |
|
80 |
|
|
81 |
/** |
|
82 |
* called when the parser is deactivated by its parent |
|
83 |
* |
|
84 |
* @return true if correctly activated |
|
85 |
*/ |
|
86 |
public boolean deactivate() { |
|
87 |
return false; |
|
88 |
} |
|
89 |
|
|
90 |
/** |
|
91 |
* |
|
92 |
* @return true if this hook parser must be activated |
|
93 |
*/ |
|
94 |
public boolean mustActivate() { |
|
95 |
return false; |
|
96 |
} |
|
97 |
|
|
98 |
/** |
|
99 |
* |
|
100 |
* @return true if this hook parser must be desactivated |
|
101 |
*/ |
|
102 |
public boolean mustDeactivate() { |
|
103 |
return true; |
|
104 |
} |
|
105 |
|
|
106 |
/** |
|
107 | 40 |
* Helper method to get an attribute value by its name |
108 | 41 |
* |
109 | 42 |
* @param name the attribute name |
110 | 43 |
* @return the value if any |
111 | 44 |
*/ |
45 |
@Override |
|
112 | 46 |
public String getParserAttributeValue(String name) { |
113 | 47 |
if (name == null) return null; |
114 | 48 |
|
... | ... | |
122 | 56 |
return null; |
123 | 57 |
} |
124 | 58 |
|
59 |
@Override |
|
125 | 60 |
protected void before() { |
126 | 61 |
|
62 |
super.before(); |
|
127 | 63 |
} |
128 | 64 |
|
65 |
@Override |
|
129 | 66 |
protected void after() throws XMLStreamException, IOException { |
67 |
|
|
68 |
super.after(); |
|
69 |
|
|
130 | 70 |
if (factory != null) { |
131 | 71 |
factory = null; |
132 | 72 |
if (parser != null) { |
... | ... | |
142 | 82 |
writer = null; |
143 | 83 |
parser = null; |
144 | 84 |
} |
85 |
|
|
86 |
if (writer != null) writer.close(); |
|
87 |
|
|
88 |
if (output != null) output.close(); |
|
145 | 89 |
} |
146 | 90 |
|
147 |
protected void closeForError() throws XMLStreamException, IOException { |
|
148 |
if (factory != null) { |
|
149 |
if (parser != null) { |
|
150 |
parser.close(); |
|
151 |
} |
|
152 |
if (inputData != null) { |
|
153 |
inputData.close(); |
|
154 |
} |
|
155 |
} |
|
156 |
} |
|
157 |
|
|
158 | 91 |
/** |
159 | 92 |
* Creates the output. |
160 | 93 |
* |
... | ... | |
182 | 115 |
} |
183 | 116 |
} |
184 | 117 |
|
185 |
public boolean process(File outfile) throws XMLStreamException, IOException {
|
|
186 |
if (factory == null) { |
|
187 |
System.out.println("Error: this parser is a Hook parser to be used with another StaxIdentityParser as a Hook");
|
|
118 |
public boolean process(File outFile) throws XMLStreamException, IOException {
|
|
119 |
|
|
120 |
if (!createOutput(outFile)) {
|
|
188 | 121 |
return false; |
189 | 122 |
} |
190 |
if (!createOutput(outfile)) { |
|
191 |
return false; |
|
192 |
} |
|
193 | 123 |
|
194 | 124 |
writer.writeStartDocument("UTF-8", "1.0"); |
195 | 125 |
writer.writeCharacters("\n"); |
126 |
|
|
196 | 127 |
boolean ret = process(writer); |
197 |
if (writer != null) { |
|
198 |
writer.close(); |
|
199 |
} |
|
200 |
if (output != null) { |
|
201 |
try { |
|
202 |
output.close(); |
|
203 |
} |
|
204 |
catch (Exception e) { |
|
205 |
System.out.println("output excep: " + e); |
|
206 |
} |
|
207 |
} |
|
208 | 128 |
|
209 |
if (parser != null) { |
|
210 |
try { |
|
211 |
parser.close(); |
|
212 |
} |
|
213 |
catch (Exception e) { |
|
214 |
System.out.println("parser excep: " + e); |
|
215 |
} |
|
216 |
} |
|
217 |
|
|
218 |
if (inputData != null) { |
|
219 |
try { |
|
220 |
inputData.close(); |
|
221 |
} |
|
222 |
catch (Exception e) { |
|
223 |
System.out.println("inputData excep: " + e); |
|
224 |
} |
|
225 |
} |
|
226 |
|
|
227 | 129 |
return ret; |
228 | 130 |
} |
229 | 131 |
|
230 |
public final static String SLASH = "/"; |
|
231 |
|
|
232 | 132 |
public boolean process(XMLStreamWriter awriter) throws XMLStreamException, IOException { |
233 | 133 |
|
234 | 134 |
if (factory == null) { |
... | ... | |
237 | 137 |
} |
238 | 138 |
|
239 | 139 |
this.writer = awriter; |
240 |
// if (processingXInclude == 0) { |
|
241 |
before(); // if you need to do something before reading the xml |
|
242 |
// } |
|
243 |
try { |
|
244 |
for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) { |
|
245 |
|
|
246 |
// preparing process |
|
247 |
if (event == XMLStreamConstants.START_ELEMENT) { |
|
248 |
localname = parser.getLocalName(); |
|
249 |
currentXPath.append(SLASH); |
|
250 |
currentXPath.append(localname); |
|
251 |
buildCurrentAttributes(); // for easier access later |
|
252 |
} |
|
253 |
else if (event == XMLStreamConstants.END_ELEMENT) { |
|
254 |
localname = parser.getLocalName(); |
|
255 |
} |
|
256 |
|
|
257 |
if (activeHook != null) { |
|
258 |
if (activeHook.mustDeactivate()) { |
|
259 |
activeHook.deactivate(); |
|
260 |
activeHook = null; |
|
261 |
} |
|
262 |
else { |
|
263 |
activeHook.processParserEvent(event); |
|
264 |
} |
|
265 |
} |
|
266 |
|
|
267 |
if (activeHook == null && event == XMLStreamConstants.START_ELEMENT) { // only activate when starting an element (for now, thus we cannot process only text nodes) |
|
268 |
for (Hook hook : hooks.values()) { |
|
269 |
if (hook.mustActivate()) { |
|
270 |
if (hook.activate()) { |
|
271 |
activeHook = hook; |
|
272 |
|
|
273 |
hook.processParserEvent(event); |
|
274 |
break; |
|
275 |
} |
|
276 |
} |
|
277 |
} |
|
278 |
} |
|
279 |
|
|
280 |
if (activeHook == null) { |
|
281 |
this.processParserEvent(event); |
|
282 |
} |
|
283 |
|
|
284 |
// post-preparing process |
|
285 |
if (event == XMLStreamConstants.END_ELEMENT) { |
|
286 |
currentXPath.setLength(currentXPath.length() - localname.length() - 1); |
|
287 |
popCurrentAttributes(); |
|
288 |
} |
|
289 |
} |
|
290 |
} |
|
291 |
catch (Exception e) { |
|
292 |
System.out.println("Unexpected error while parsing file " + inputurl + " : " + e); |
|
293 |
System.out.println("Location line: " + parser.getLocation().getLineNumber() + " character: " + parser.getLocation().getColumnNumber()); |
|
294 |
org.txm.utils.logger.Log.printStackTrace(e); |
|
295 |
// e.printStackTrace(); |
|
296 |
if (writer != null) writer.close(); |
|
297 |
if (output != null) output.close(); |
|
298 |
parser.close(); |
|
299 |
inputData.close(); |
|
300 |
return false; |
|
301 |
} |
|
302 | 140 |
|
303 |
// if (processingXInclude == 0) { |
|
304 |
after(); // if you need to do something before closing the parser(); |
|
305 |
// } |
|
306 |
return true; |
|
141 |
return process(); |
|
307 | 142 |
|
308 | 143 |
} |
309 | 144 |
|
310 |
protected void processParserEvent(int event) throws XMLStreamException, IOException { |
|
311 |
switch (event) { |
|
312 |
case XMLStreamConstants.NAMESPACE: |
|
313 |
processNamespace(); |
|
314 |
break; |
|
315 |
case XMLStreamConstants.START_ELEMENT: |
|
316 |
processStartElement(); |
|
317 |
break; |
|
318 |
case XMLStreamConstants.CHARACTERS: |
|
319 |
processCharacters(); |
|
320 |
break; |
|
321 |
case XMLStreamConstants.END_ELEMENT: |
|
322 |
processEndElement(); |
|
323 |
break; |
|
324 |
case XMLStreamConstants.PROCESSING_INSTRUCTION: |
|
325 |
processProcessingInstruction(); |
|
326 |
break; |
|
327 |
case XMLStreamConstants.DTD: |
|
328 |
processDTD(); |
|
329 |
break; |
|
330 |
case XMLStreamConstants.CDATA: |
|
331 |
processCDATA(); |
|
332 |
break; |
|
333 |
case XMLStreamConstants.COMMENT: |
|
334 |
processComment(); |
|
335 |
break; |
|
336 |
case XMLStreamConstants.END_DOCUMENT: |
|
337 |
processEndDocument(); |
|
338 |
break; |
|
339 |
case XMLStreamConstants.ENTITY_REFERENCE: |
|
340 |
processEntityReference(); |
|
341 |
break; |
|
342 |
} |
|
343 |
} |
|
344 |
|
|
345 | 145 |
/** |
346 |
* The start element has already been written |
|
146 |
* Go to the end of an element : |
|
147 |
* - The start element has already been written |
|
148 |
* - the content is skipped and not written |
|
347 | 149 |
* |
348 | 150 |
* @param tagname |
349 | 151 |
* @throws XMLStreamException |
350 | 152 |
* @throws IOException |
351 | 153 |
*/ |
154 |
@Override |
|
352 | 155 |
public void goToEnd(String tagname) throws XMLStreamException, IOException { |
353 | 156 |
int elements = 1; |
354 | 157 |
try { |
... | ... | |
365 | 168 |
currentXPath.append(localname); |
366 | 169 |
buildCurrentAttributes(); |
367 | 170 |
|
368 |
_processStartElement();
|
|
171 |
processStartElement(); |
|
369 | 172 |
break; |
370 | 173 |
case XMLStreamConstants.CHARACTERS: |
371 | 174 |
processCharacters(); |
... | ... | |
406 | 209 |
System.out.println("Error while parsing file " + inputurl); |
407 | 210 |
System.out.println("Location " + parser.getLocation()); |
408 | 211 |
org.txm.utils.logger.Log.printStackTrace(e); |
409 |
output.close(); |
|
410 |
parser.close(); |
|
212 |
after(); |
|
411 | 213 |
return; |
412 | 214 |
} |
413 | 215 |
} |
414 | 216 |
|
415 |
public String getLocation() { |
|
416 |
if (parser != null) { |
|
417 |
return "Line: " + parser.getLocation().getLineNumber() + " Col: " + parser.getLocation().getColumnNumber(); |
|
418 |
} |
|
419 |
return null; |
|
420 |
} |
|
421 |
|
|
422 |
public static final String INCLUDE = "include"; |
|
423 |
|
|
424 |
public static final String XI = "xi"; |
|
425 |
|
|
426 |
|
|
217 |
@Override |
|
427 | 218 |
protected void processNamespace() throws XMLStreamException { |
428 | 219 |
writer.writeNamespace(parser.getPrefix(), parser.getNamespaceURI()); |
429 | 220 |
} |
430 | 221 |
|
431 | 222 |
|
223 |
@Override |
|
432 | 224 |
protected void processStartElement() throws XMLStreamException, IOException { |
433 | 225 |
String prefix = parser.getPrefix(); |
434 |
if (prefix != null && prefix.length() > 0) { |
|
435 |
writer.writeStartElement(Nscontext.getNamespaceURI(prefix), localname); |
|
436 |
} |
|
437 |
else { |
|
438 |
writer.writeStartElement(localname); |
|
439 |
} |
|
440 |
|
|
441 |
for (int i = 0; i < parser.getNamespaceCount(); i++) { |
|
442 |
writer.writeNamespace(parser.getNamespacePrefix(i), parser.getNamespaceURI(i)); |
|
443 |
} |
|
444 |
|
|
445 |
writeAttributes(); |
|
446 |
} |
|
447 |
|
|
448 |
private void _processStartElement() throws XMLStreamException, IOException { |
|
449 |
String prefix = parser.getPrefix(); |
|
450 |
if (prefix != null && prefix.length() > 0) { |
|
451 |
writer.writeStartElement(Nscontext.getNamespaceURI(prefix), localname); |
|
452 |
} |
|
453 |
else { |
|
454 |
writer.writeStartElement(localname); |
|
455 |
} |
|
456 |
for (int i = 0; i < parser.getNamespaceCount(); i++) { |
|
457 |
writer.writeNamespace(parser.getNamespacePrefix(i), parser.getNamespaceURI(i)); |
|
458 |
} |
|
459 |
|
|
460 |
writeAttributes(); |
|
461 |
} |
|
462 |
|
|
463 |
private Stack<LinkedHashMap<String, String>> attributesStack = new Stack<>(); |
|
464 |
|
|
465 |
private HashMap<String, String> popCurrentAttributes() { |
|
466 |
return attributesStack.pop(); |
|
467 |
} |
|
468 |
|
|
469 |
private void buildCurrentAttributes() throws XMLStreamException { |
|
470 |
LinkedHashMap<String, String> currentAttributes = new LinkedHashMap<>(); // keep order |
|
471 |
for (int i = 0; i < parser.getAttributeCount(); i++) { |
|
472 |
currentAttributes.put(parser.getAttributeLocalName(i), parser.getAttributeValue(i)); |
|
226 |
if (prefix != null && prefix.length() > 0) { |
|
227 |
writer.writeStartElement(Nscontext.getNamespaceURI(prefix), localname); |
|
473 | 228 |
} |
474 |
attributesStack.push(currentAttributes); |
|
229 |
else { |
|
230 |
writer.writeStartElement(localname); |
|
231 |
} |
|
232 |
|
|
233 |
for (int i = 0; i < parser.getNamespaceCount(); i++) { |
|
234 |
writer.writeNamespace(parser.getNamespacePrefix(i), parser.getNamespaceURI(i)); |
|
235 |
} |
|
236 |
|
|
237 |
writeAttributes(); |
|
475 | 238 |
} |
476 | 239 |
|
477 |
public LinkedHashMap<String, String> getCurrentAttributes() { |
|
478 |
return attributesStack.lastElement(); |
|
479 |
} |
|
480 |
|
|
481 | 240 |
protected void writeAttributes() throws XMLStreamException { |
482 | 241 |
for (int i = 0; i < parser.getAttributeCount(); i++) { |
483 | 242 |
writeAttribute(parser.getAttributePrefix(i), parser.getAttributeLocalName(i), parser.getAttributeValue(i)); |
... | ... | |
493 | 252 |
} |
494 | 253 |
} |
495 | 254 |
|
255 |
@Override |
|
496 | 256 |
protected void processCharacters() throws XMLStreamException { |
497 | 257 |
writer.writeCharacters(parser.getText()); |
498 | 258 |
} |
499 | 259 |
|
260 |
@Override |
|
500 | 261 |
protected void processProcessingInstruction() throws XMLStreamException { |
501 | 262 |
writer.writeProcessingInstruction(parser.getPITarget(), parser.getPIData()); |
502 | 263 |
} |
503 | 264 |
|
265 |
@Override |
|
504 | 266 |
protected void processDTD() throws XMLStreamException { |
505 | 267 |
writer.writeDTD(parser.getText()); |
506 | 268 |
} |
507 | 269 |
|
270 |
@Override |
|
508 | 271 |
protected void processCDATA() throws XMLStreamException { |
509 | 272 |
writer.writeCData(parser.getText()); |
510 | 273 |
} |
511 | 274 |
|
275 |
@Override |
|
512 | 276 |
protected void processComment() throws XMLStreamException { |
513 | 277 |
writer.writeComment(parser.getText()); |
514 | 278 |
} |
515 | 279 |
|
280 |
@Override |
|
516 | 281 |
protected void processEndElement() throws XMLStreamException { |
517 |
if (localname == INCLUDE && parser.getPrefix() == XI) { |
|
518 |
// nothing !! |
|
519 |
} |
|
520 |
else { |
|
521 |
writer.writeEndElement(); |
|
522 |
} |
|
282 |
writer.writeEndElement(); |
|
523 | 283 |
} |
524 | 284 |
|
285 |
@Override |
|
525 | 286 |
protected void processEndDocument() throws XMLStreamException { |
526 | 287 |
writer.writeEndDocument(); |
527 | 288 |
} |
528 | 289 |
|
290 |
@Override |
|
529 | 291 |
protected void processEntityReference() throws XMLStreamException { |
530 | 292 |
writer.writeEntityRef(parser.getLocalName()); |
531 | 293 |
} |
532 | 294 |
|
295 |
@Override |
|
533 | 296 |
public String getCurrentPath() { |
534 | 297 |
return currentXPath.toString(); |
535 | 298 |
} |
... | ... | |
568 | 331 |
// }; |
569 | 332 |
XPathHookActivator xpathActivator = new XPathHookActivator(null, "//p"); |
570 | 333 |
XPathsHookActivator xpathsActivator = new XPathsHookActivator(null, Arrays.asList("//p", "//p")); |
571 |
// IdentityHook hook2 = new IdentityHook("hook2", xpathsActivator, builder) { |
|
572 |
// |
|
573 |
// @Override |
|
574 |
// public boolean _activate() { |
|
575 |
// System.out.println("ACTIVATING " + name + " AT " + getLocation()); |
|
576 |
// return true; |
|
577 |
// } |
|
578 |
// |
|
579 |
// @Override |
|
580 |
// public boolean deactivate() { |
|
581 |
// System.out.println("DEACTIVATING " + name + " AT " + getLocation()); |
|
582 |
// return true; |
|
583 |
// } |
|
584 |
// |
|
585 |
// @Override |
|
586 |
// public void processStartElement() throws XMLStreamException, IOException { |
|
587 |
// super.processStartElement(); |
|
588 |
// parentParser.writer.writeAttribute("hook", name); |
|
589 |
// } |
|
590 |
// }; |
|
591 |
|
|
592 |
DOMIdentityHook hook3 = new DOMIdentityHook("hook3", xpathsActivator, builder) { |
|
593 |
|
|
334 |
IdentityHook hook2 = new IdentityHook("hook2", xpathsActivator, builder) { |
|
335 |
|
|
336 |
@Override |
|
337 |
public boolean _activate() { |
|
338 |
System.out.println("ACTIVATING " + name + " AT " + getLocation()); |
|
339 |
return true; |
|
340 |
} |
|
341 |
|
|
342 |
@Override |
|
343 |
public boolean deactivate() { |
|
344 |
System.out.println("DEACTIVATING " + name + " AT " + getLocation()); |
|
345 |
return true; |
|
346 |
} |
|
347 |
|
|
348 |
@Override |
|
349 |
public void processStartElement() throws XMLStreamException, IOException { |
|
350 |
super.processStartElement(); |
|
351 |
parentParser.writer.writeAttribute("hook", name); |
|
352 |
} |
|
594 | 353 |
}; |
595 | 354 |
|
355 |
// DOMIdentityHook hook3 = new DOMIdentityHook("hook3", xpathsActivator, builder) { |
|
356 |
// |
|
357 |
// }; |
|
358 |
|
|
596 | 359 |
long time = System.currentTimeMillis(); |
597 | 360 |
if (builder.process(output)) { |
598 | 361 |
System.out.println("Time=" + (System.currentTimeMillis() - time)); |
599 |
System.out.println("XPaths activator has done working ? "+xpathsActivator.hasAllXpathsBeenProcessed());
|
|
362 |
System.out.println("XPaths activator has done working ? " + xpathsActivator.hasAllXpathsBeenProcessed());
|
|
600 | 363 |
} |
601 | 364 |
else { |
602 | 365 |
System.out.println("failure !"); |
... | ... | |
612 | 375 |
* |
613 | 376 |
* @return the actual hooks hash |
614 | 377 |
*/ |
378 |
@Override |
|
615 | 379 |
public LinkedHashMap<String, Hook> getHooks() { |
616 | 380 |
return hooks; |
617 | 381 |
} |
618 | 382 |
|
383 |
@Override |
|
619 | 384 |
public void addHook(String name, Hook hook) { |
620 | 385 |
hooks.put(name, hook); |
621 | 386 |
} |
tmp/org.txm.core/src/java/org/txm/xml/DOMIdentityHook.java (revision 2649) | ||
---|---|---|
4 | 4 |
|
5 | 5 |
import javax.xml.stream.XMLStreamException; |
6 | 6 |
|
7 |
import org.txm.utils.xml.DomUtils; |
|
8 | 7 |
import org.w3c.dom.Element; |
9 | 8 |
import org.w3c.dom.Node; |
10 | 9 |
import org.w3c.dom.NodeList; |
11 | 10 |
|
12 |
public class DOMIdentityHook extends Hook { |
|
13 |
|
|
11 |
public class DOMIdentityHook extends Hook<XMLProcessor> {
|
|
12 |
|
|
14 | 13 |
Element dom; |
15 | 14 |
|
16 | 15 |
public DOMIdentityHook(String name, HookActivator activator, XMLProcessor processor) throws IOException, XMLStreamException { |
17 | 16 |
super(name, activator, processor); |
18 | 17 |
} |
19 |
|
|
18 |
|
|
20 | 19 |
@Override |
21 | 20 |
public boolean _activate() { |
22 | 21 |
StaxDomConstructor domConstructor = new StaxDomConstructor(parentParser.parser); |
23 | 22 |
try { |
24 | 23 |
dom = domConstructor.getDom(); |
25 | 24 |
return dom != null; |
26 |
} catch (Exception e) { |
|
25 |
} |
|
26 |
catch (Exception e) { |
|
27 | 27 |
// TODO Auto-generated catch block |
28 | 28 |
e.printStackTrace(); |
29 | 29 |
return false; |
... | ... | |
35 | 35 |
boolean b = dom != null; // the DOM has been constructed and the XMLProcess can continue parsing |
36 | 36 |
if (b) { |
37 | 37 |
try { |
38 |
processDom(); |
|
38 | 39 |
writeDOM(); |
39 | 40 |
dom = null; |
40 |
} catch (XMLStreamException e) { |
|
41 |
} |
|
42 |
catch (XMLStreamException e) { |
|
41 | 43 |
e.printStackTrace(); |
42 | 44 |
} |
43 | 45 |
} |
44 | 46 |
return b; |
45 | 47 |
} |
46 |
|
|
48 |
|
|
49 |
/** |
|
50 |
* extends this method to process the DOM before it is written |
|
51 |
*/ |
|
52 |
public void processDom() { |
|
53 |
// nothing since its the DomIdentityHook |
|
54 |
} |
|
55 |
|
|
47 | 56 |
@Override |
48 | 57 |
public boolean deactivate() { |
49 | 58 |
return dom == null; // dom should be null |
50 | 59 |
} |
51 |
|
|
60 |
|
|
52 | 61 |
private void writeDOM() throws XMLStreamException { |
53 | 62 |
writeDOM(dom); |
54 | 63 |
} |
55 | 64 |
|
56 | 65 |
private void writeDOM(Node node) throws XMLStreamException { |
57 | 66 |
if (node.getNodeType() == Node.ELEMENT_NODE) { |
58 |
Element e = (Element)node; |
|
67 |
Element e = (Element) node;
|
|
59 | 68 |
NodeList children = e.getChildNodes(); |
60 | 69 |
if (children.getLength() > 0) { |
61 | 70 |
parentParser.writer.writeStartElement(e.getNamespaceURI(), e.getLocalName()); |
62 |
} else { |
|
63 |
parentParser.writer.writeEmptyElement(e.getNamespaceURI(),e.getLocalName()); |
|
64 | 71 |
} |
72 |
else { |
|
73 |
parentParser.writer.writeEmptyElement(e.getNamespaceURI(), e.getLocalName()); |
|
74 |
} |
|
65 | 75 |
|
66 |
for (int i = 0 ; i < e.getAttributes().getLength() ; i++) {
|
|
76 |
for (int i = 0; i < e.getAttributes().getLength(); i++) {
|
|
67 | 77 |
Node att = e.getAttributes().item(i); |
68 | 78 |
parentParser.writer.writeAttribute(att.getNamespaceURI(), att.getNodeName(), att.getNodeValue()); |
69 | 79 |
} |
70 | 80 |
|
71 |
for (int c = 0 ; c < children.getLength() ; c++) {
|
|
81 |
for (int c = 0; c < children.getLength(); c++) {
|
|
72 | 82 |
writeDOM(children.item(c)); |
73 | 83 |
} |
74 | 84 |
if (children.getLength() > 0) { |
75 | 85 |
parentParser.writer.writeEndElement(); |
76 | 86 |
} |
77 |
} else if (node.getNodeType() == Node.TEXT_NODE) { |
|
87 |
} |
|
88 |
else if (node.getNodeType() == Node.TEXT_NODE) { |
|
78 | 89 |
parentParser.writer.writeCharacters(node.getTextContent()); |
79 | 90 |
} |
80 | 91 |
} |
81 |
|
|
92 |
|
|
82 | 93 |
@Override |
83 |
protected void processNamespace() throws XMLStreamException { }
|
|
84 |
|
|
94 |
protected void processNamespace() throws XMLStreamException {} |
|
95 |
|
|
85 | 96 |
@Override |
86 |
protected void processStartElement() throws XMLStreamException, IOException { }
|
|
87 |
|
|
97 |
protected void processStartElement() throws XMLStreamException, IOException {} |
|
98 |
|
|
88 | 99 |
@Override |
89 |
protected void processCharacters() throws XMLStreamException { }
|
|
90 |
|
|
100 |
protected void processCharacters() throws XMLStreamException {} |
|
101 |
|
|
91 | 102 |
@Override |
92 |
protected void processProcessingInstruction() throws XMLStreamException { }
|
|
93 |
|
|
103 |
protected void processProcessingInstruction() throws XMLStreamException {} |
|
104 |
|
|
94 | 105 |
@Override |
95 |
protected void processDTD() throws XMLStreamException { }
|
|
96 |
|
|
106 |
protected void processDTD() throws XMLStreamException {} |
|
107 |
|
|
97 | 108 |
@Override |
98 |
protected void processCDATA() throws XMLStreamException { }
|
|
99 |
|
|
109 |
protected void processCDATA() throws XMLStreamException {} |
|
110 |
|
|
100 | 111 |
@Override |
101 |
protected void processComment() throws XMLStreamException { }
|
|
102 |
|
|
112 |
protected void processComment() throws XMLStreamException {} |
|
113 |
|
|
103 | 114 |
@Override |
104 |
protected void processEndElement() throws XMLStreamException { }
|
|
105 |
|
|
115 |
protected void processEndElement() throws XMLStreamException {} |
|
116 |
|
|
106 | 117 |
@Override |
107 |
protected void processEndDocument() throws XMLStreamException { }
|
|
108 |
|
|
118 |
protected void processEndDocument() throws XMLStreamException {} |
|
119 |
|
|
109 | 120 |
@Override |
110 |
protected void processEntityReference() throws XMLStreamException { }
|
|
121 |
protected void processEntityReference() throws XMLStreamException {} |
|
111 | 122 |
} |
tmp/org.txm.core/src/java/org/txm/xml/IdentityHook.java (revision 2649) | ||
---|---|---|
4 | 4 |
|
5 | 5 |
import javax.xml.stream.XMLStreamException; |
6 | 6 |
|
7 |
public abstract class IdentityHook extends Hook { |
|
7 |
public abstract class IdentityHook extends Hook<XMLProcessor> {
|
|
8 | 8 |
|
9 | 9 |
public IdentityHook(String name, HookActivator activator, XMLProcessor parentParser) throws IOException, XMLStreamException { |
10 | 10 |
super(name, activator, parentParser); |
tmp/org.txm.core/src/java/org/txm/xml/SimpleHook.java (revision 2649) | ||
---|---|---|
1 |
package org.txm.xml; |
|
2 |
|
|
3 |
import java.io.IOException; |
|
4 |
|
|
5 |
import javax.xml.stream.XMLStreamException; |
|
6 |
|
|
7 |
/** |
|
8 |
* Simple wrapper to process only the start element, end element and characters Stax events |
|
9 |
* |
|
10 |
* @author mdecorde |
|
11 |
* |
|
12 |
*/ |
|
13 |
public abstract class SimpleHook<T extends XMLParser> extends Hook<T> { |
|
14 |
|
|
15 |
public SimpleHook(String name, HookActivator activator, T parentParser) throws IOException, XMLStreamException { |
|
16 |
super(name, activator, parentParser); |
|
17 |
} |
|
18 |
|
|
19 |
@Override |
|
20 |
protected void processNamespace() throws XMLStreamException {} |
|
21 |
|
|
22 |
@Override |
|
23 |
protected void processProcessingInstruction() throws XMLStreamException {} |
|
24 |
|
|
25 |
@Override |
|
26 |
protected void processDTD() throws XMLStreamException {} |
|
27 |
|
|
28 |
@Override |
|
29 |
protected void processCDATA() throws XMLStreamException {} |
|
30 |
|
|
31 |
@Override |
|
32 |
protected void processComment() throws XMLStreamException {} |
|
33 |
|
|
34 |
@Override |
|
35 |
protected void processEndDocument() throws XMLStreamException {} |
|
36 |
|
|
37 |
@Override |
|
38 |
protected void processEntityReference() throws XMLStreamException {} |
|
39 |
} |
|
0 | 40 |
Formats disponibles : Unified diff