Statistics
| Revision:

root / tmp / org.txm.core / src / groovy / org / txm / importer / PartialDomIdentityParser.groovy @ 187

History | View | Annotate | Download (4.2 kB)

1
package org.txm.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 java.io.File;
10
import java.io.Writer;
11
import java.net.URL;
12

    
13
import javax.xml.stream.*;
14

    
15
class PartialDomIdentityParser extends StaxIdentityParser {
16
        
17
        boolean domMode = false;
18
        def partialDom = [];
19
        def currentNode;
20
        
21
        public PartialDomIdentityParser(File infile) {
22
                super(infile)
23
        }
24
        
25
        protected void processNamespace() {
26
                super.processNamespace()
27
        }
28
        
29
        protected void processStartElement()
30
        {
31
                if (parser.getLocalName() == "test") {
32
                        println "Build dom..."
33
                        Tag testTag = new Tag(parser);
34
                        println testTag.toString()
35
                        testTag.writeDom(writer)
36
                } else {
37
                        super.processStartElement()
38
                }
39
        }
40
        
41
        protected void writeAttributes() {
42
                super.writeAttributes();
43
        }
44
        
45
        protected void processCharacters()
46
        {
47
                super.processCharacters()
48
        }
49
        
50
        protected void processProcessingInstruction()
51
        {
52
                super.processProcessingInstruction()
53
        }
54
        
55
        protected void processDTD()
56
        {
57
                super.processDTD()
58
        }
59
        
60
        protected void processCDATA()
61
        {
62
                super.processCDATA();
63
        }
64
        
65
        protected void processComment()
66
        {
67
                super.processComment()
68
        }
69
        
70
        protected void processEndElement()
71
        {
72
                super.processEndElement();
73
        }
74
        
75
        protected void processEndDocument() {
76
                super.processEndDocument()
77
        }
78
        
79
        protected void processEntityReference() {
80
                super.processEntityReference()
81
        }
82
        
83
        public class Tag {
84
                public String[] attnames, attvalues, attprefix;
85
                public String localname, prefix
86
                int count;
87
                public def children = [];
88

    
89
                public Tag(def parser) {
90
                        prefix = parser.getPrefix()
91
                        localname = parser.getLocalName();
92
                        count = parser.getAttributeCount()
93
                        attnames =  new String[count]
94
                        attvalues = new String[count]
95
                        attprefix = new String[count]
96
                        for (int i = 0 ; i < count ; i++) {
97
                                attnames[i] = parser.getAttributeLocalName(i)
98
                                attprefix[i] = parser.getAttributePrefix(i).toString()
99
                                attvalues[i] = parser.getAttributeValue(i).toString()
100
                        }
101
                        buildNodeContent(parser)
102
                }
103
                
104
                private void buildNodeContent(def parser) {
105
                        try {
106
                                for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) {
107
                                        //println "event "+event
108
                                        switch (event) {
109
                                                case XMLStreamConstants.START_ELEMENT:
110
                                                        children << new Tag(parser);
111
                                                        break;
112
                                                case XMLStreamConstants.CHARACTERS:
113
                                                        children << parser.getText()
114
                                                        break;
115
                                                case XMLStreamConstants.END_ELEMENT:
116
                                                        if (localname == parser.getLocalName()) {
117
                                                                return; // end of stax parsing, the Tag is built
118
                                                        }
119
                                                        break;
120
                                                case XMLStreamConstants.END_DOCUMENT:
121
                                                        println "ERROR: END OF DOCUMENT REACHED"
122
                                                        break;
123
                                        }
124
                                }
125
                        } catch(Exception e) {
126
                                println("Error while parsing partial content of file "+inputurl);
127
                                println("Location "+parser.getLocation());
128
                                org.txm.utils.logger.Log.printStackTrace(e);
129
                                return;
130
                        }
131
                }
132

    
133
                public String toString() {
134
                        def out = new StringWriter()
135
                        XMLStreamWriter strwriter = outfactory.createXMLStreamWriter(out);//create a new file
136
                        println ""+(strwriter != null)
137
                        writeDom(strwriter);
138
                        return out.toString()
139
                }
140
                
141
                public writeDom(def writer) {
142
                        // Open the Tag
143
                        if (prefix != null && prefix.length() > 0)
144
                                writer.writeStartElement(prefix+":"+localname)
145
                        else
146
                                writer.writeStartElement(localname)
147

    
148
                        // write Attributes
149
                        for (int i = 0 ; i < count ; i++) {
150
                                if (attprefix[i] != null && attprefix[i].length() > 0) {
151
                                        writer.writeAttribute(attprefix[i]+":"+attnames[i], attvalues[i])
152
                                } else {
153
                                        writer.writeAttribute(attnames[i], attvalues[i])
154
                                }
155
                        }
156
                        
157
                        // write children recursivelly
158
                        for (def child : children) {
159
                                if (child instanceof String) {
160
                                        writer.writeCharacters(child)
161
                                } else if (child instanceof Tag) {
162
                                        child.writeDom(writer)
163
                                } else {
164
                                        println "Error: can't write $child"
165
                                }
166
                        }
167
                        
168
                        // close the Tag
169
                        writer.writeEndElement() 
170
                }
171
        }
172
        
173
        public static void main(String[] args) {
174
                File infile = new File("/home/mdecorde/xml/partialdom/test.xml")
175
                File outfile = new File("/home/mdecorde/xml/partialdom/test-o.xml")
176
                PartialDomIdentityParser p = new PartialDomIdentityParser(infile);
177
                println p.process(outfile)
178
        }
179
}