root / tmp / org.txm.core / src / java / org / txm / scripts / importer / PartialDomIdentityParser.groovy @ 2473
History | View | Annotate | Download (4.2 kB)
1 |
package org.txm.scripts.importer
|
---|---|
2 |
|
3 |
import java.util.ArrayList; |
4 |
|
5 |
import javax.xml.stream.XMLStreamConstants; |
6 |
import javax.xml.stream.XMLOutputFactory; |
7 |
import javax.xml.stream.XMLStreamWriter; |
8 |
|
9 |
import org.txm.importer.StaxIdentityParser |
10 |
|
11 |
import java.io.File; |
12 |
import java.io.Writer; |
13 |
import java.net.URL; |
14 |
|
15 |
import javax.xml.stream.*; |
16 |
|
17 |
class PartialDomIdentityParser extends StaxIdentityParser { |
18 |
|
19 |
boolean domMode = false; |
20 |
def partialDom = []; |
21 |
def currentNode;
|
22 |
|
23 |
public PartialDomIdentityParser(File infile) { |
24 |
super(infile)
|
25 |
} |
26 |
|
27 |
protected void processNamespace() { |
28 |
super.processNamespace()
|
29 |
} |
30 |
|
31 |
protected void processStartElement() |
32 |
{ |
33 |
if (parser.getLocalName() == "test") { |
34 |
println "Build dom..."
|
35 |
Tag testTag = new Tag(parser);
|
36 |
println testTag.toString() |
37 |
testTag.writeDom(writer) |
38 |
} else {
|
39 |
super.processStartElement()
|
40 |
} |
41 |
} |
42 |
|
43 |
protected void writeAttributes() { |
44 |
super.writeAttributes();
|
45 |
} |
46 |
|
47 |
protected void processCharacters() |
48 |
{ |
49 |
super.processCharacters()
|
50 |
} |
51 |
|
52 |
protected void processProcessingInstruction() |
53 |
{ |
54 |
super.processProcessingInstruction()
|
55 |
} |
56 |
|
57 |
protected void processDTD() |
58 |
{ |
59 |
super.processDTD()
|
60 |
} |
61 |
|
62 |
protected void processCDATA() |
63 |
{ |
64 |
super.processCDATA();
|
65 |
} |
66 |
|
67 |
protected void processComment() |
68 |
{ |
69 |
super.processComment()
|
70 |
} |
71 |
|
72 |
protected void processEndElement() |
73 |
{ |
74 |
super.processEndElement();
|
75 |
} |
76 |
|
77 |
protected void processEndDocument() { |
78 |
super.processEndDocument()
|
79 |
} |
80 |
|
81 |
protected void processEntityReference() { |
82 |
super.processEntityReference()
|
83 |
} |
84 |
|
85 |
public class Tag { |
86 |
public String[] attnames, attvalues, attprefix; |
87 |
public String localname, prefix |
88 |
int count;
|
89 |
public def children = []; |
90 |
|
91 |
public Tag(def parser) { |
92 |
prefix = parser.getPrefix() |
93 |
localname = parser.getLocalName(); |
94 |
count = parser.getAttributeCount() |
95 |
attnames = new String[count] |
96 |
attvalues = new String[count] |
97 |
attprefix = new String[count] |
98 |
for (int i = 0 ; i < count ; i++) { |
99 |
attnames[i] = parser.getAttributeLocalName(i) |
100 |
attprefix[i] = parser.getAttributePrefix(i).toString() |
101 |
attvalues[i] = parser.getAttributeValue(i).toString() |
102 |
} |
103 |
buildNodeContent(parser) |
104 |
} |
105 |
|
106 |
private void buildNodeContent(def parser) { |
107 |
try {
|
108 |
for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) { |
109 |
//println "event "+event
|
110 |
switch (event) {
|
111 |
case XMLStreamConstants.START_ELEMENT:
|
112 |
children << new Tag(parser);
|
113 |
break;
|
114 |
case XMLStreamConstants.CHARACTERS:
|
115 |
children << parser.getText() |
116 |
break;
|
117 |
case XMLStreamConstants.END_ELEMENT:
|
118 |
if (localname == parser.getLocalName()) {
|
119 |
return; // end of stax parsing, the Tag is built |
120 |
} |
121 |
break;
|
122 |
case XMLStreamConstants.END_DOCUMENT:
|
123 |
println "ERROR: END OF DOCUMENT REACHED"
|
124 |
break;
|
125 |
} |
126 |
} |
127 |
} catch(Exception e) { |
128 |
println("Error while parsing partial content of file "+inputurl);
|
129 |
println("Location "+parser.getLocation());
|
130 |
org.txm.utils.logger.Log.printStackTrace(e); |
131 |
return;
|
132 |
} |
133 |
} |
134 |
|
135 |
public String toString() { |
136 |
def out = new StringWriter() |
137 |
XMLStreamWriter strwriter = outfactory.createXMLStreamWriter(out);//create a new file
|
138 |
println ""+(strwriter != null) |
139 |
writeDom(strwriter); |
140 |
return out.toString()
|
141 |
} |
142 |
|
143 |
public writeDom(def writer) { |
144 |
// Open the Tag
|
145 |
if (prefix != null && prefix.length() > 0) |
146 |
writer.writeStartElement(prefix+":"+localname)
|
147 |
else
|
148 |
writer.writeStartElement(localname) |
149 |
|
150 |
// write Attributes
|
151 |
for (int i = 0 ; i < count ; i++) { |
152 |
if (attprefix[i] != null && attprefix[i].length() > 0) { |
153 |
writer.writeAttribute(attprefix[i]+":"+attnames[i], attvalues[i])
|
154 |
} else {
|
155 |
writer.writeAttribute(attnames[i], attvalues[i]) |
156 |
} |
157 |
} |
158 |
|
159 |
// write children recursivelly
|
160 |
for (def child : children) { |
161 |
if (child instanceof String) { |
162 |
writer.writeCharacters(child) |
163 |
} else if (child instanceof Tag) { |
164 |
child.writeDom(writer) |
165 |
} else {
|
166 |
println "Error: can't write $child"
|
167 |
} |
168 |
} |
169 |
|
170 |
// close the Tag
|
171 |
writer.writeEndElement() |
172 |
} |
173 |
} |
174 |
|
175 |
public static void main(String[] args) { |
176 |
File infile = new File("/home/mdecorde/xml/partialdom/test.xml") |
177 |
File outfile = new File("/home/mdecorde/xml/partialdom/test-o.xml") |
178 |
PartialDomIdentityParser p = new PartialDomIdentityParser(infile);
|
179 |
println p.process(outfile) |
180 |
} |
181 |
} |