Statistics
| Revision:

root / tmp / org.txm.core / src / java / org / txm / scripts / importer / PartialDomIdentityParser.groovy @ 1688

History | View | Annotate | Download (4.2 kB)

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