1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
package net.sf.practicalxml.builder; |
16 | |
|
17 | |
import java.io.IOException; |
18 | |
import java.io.OutputStream; |
19 | |
import java.util.ArrayList; |
20 | |
import java.util.List; |
21 | |
|
22 | |
import org.w3c.dom.Document; |
23 | |
import org.w3c.dom.Element; |
24 | |
import org.xml.sax.Attributes; |
25 | |
import org.xml.sax.ContentHandler; |
26 | |
import org.xml.sax.InputSource; |
27 | |
import org.xml.sax.SAXException; |
28 | |
import org.xml.sax.helpers.AttributesImpl; |
29 | |
import org.xml.sax.helpers.XMLFilterImpl; |
30 | |
|
31 | |
import net.sf.practicalxml.DomUtil; |
32 | |
import net.sf.practicalxml.OutputUtil; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
public final class ElementNode |
41 | |
extends Node |
42 | |
{ |
43 | |
private static final long serialVersionUID = 2L; |
44 | |
|
45 | |
private String _nsUri; |
46 | |
private String _qname; |
47 | |
private String _lclName; |
48 | 2203 | private List<AttributeNode> _attribs = new ArrayList<AttributeNode>(); |
49 | 2203 | private List<Node> _children = new ArrayList<Node>(); |
50 | |
|
51 | |
|
52 | |
ElementNode(String nsUri, String qname, Node... children) |
53 | 2203 | { |
54 | 2203 | _nsUri = nsUri; |
55 | 2203 | _qname = qname; |
56 | 2203 | _lclName = getLocalName(qname); |
57 | 6328 | for (Node child : children) |
58 | 4125 | addChild(child); |
59 | 2203 | } |
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
public ElementNode addChild(Node child) |
67 | |
{ |
68 | 4126 | if (child instanceof AttributeNode) |
69 | 892 | _attribs.add((AttributeNode)child); |
70 | 3234 | else if (child != null) |
71 | 3234 | _children.add(child); |
72 | 4126 | return this; |
73 | |
} |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
public Document toDOM() |
80 | |
{ |
81 | 595 | Element root = DomUtil.newDocument(_nsUri, _qname); |
82 | 595 | appendChildren(root); |
83 | 595 | return root.getOwnerDocument(); |
84 | |
} |
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
@Override |
94 | |
protected void toSAX(ContentHandler handler) |
95 | |
throws SAXException |
96 | |
{ |
97 | 27 | handler.startElement(_nsUri, _lclName, _qname, getAttributes()); |
98 | 27 | for (Node child : _children) |
99 | |
{ |
100 | 18 | child.toSAX(handler); |
101 | 18 | } |
102 | 27 | handler.endElement(_nsUri, _lclName, _qname); |
103 | 27 | } |
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
@Override |
113 | |
public String toString() |
114 | |
{ |
115 | 9 | return OutputUtil.compactString(new SerializationHelper()); |
116 | |
} |
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
public String toString(int indentSize) |
129 | |
{ |
130 | 1 | return OutputUtil.indentedString(new SerializationHelper(), indentSize); |
131 | |
} |
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
public void toStream(OutputStream out) |
142 | |
{ |
143 | 1 | OutputUtil.compactStream(new SerializationHelper(), out); |
144 | 1 | } |
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
public void toStream(OutputStream out, String encoding) |
153 | |
{ |
154 | 1 | OutputUtil.compactStream(toDOM(), out, encoding); |
155 | 1 | } |
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
@Override |
163 | |
protected void appendToElement(Element parent) |
164 | |
{ |
165 | 1604 | appendChildren(DomUtil.appendChild(parent, _nsUri, _qname)); |
166 | 1604 | } |
167 | |
|
168 | |
|
169 | |
private void appendChildren(Element elem) |
170 | |
{ |
171 | 2199 | for (Node child : _attribs) |
172 | |
{ |
173 | 892 | child.appendToElement(elem); |
174 | 892 | } |
175 | 2199 | for (Node child : _children) |
176 | |
{ |
177 | 3230 | child.appendToElement(elem); |
178 | 3230 | } |
179 | 2199 | } |
180 | |
|
181 | |
|
182 | |
private Attributes getAttributes() |
183 | |
{ |
184 | 27 | AttributesImpl result = new AttributesImpl(); |
185 | 27 | for (AttributeNode attr : _attribs) |
186 | |
{ |
187 | 2 | attr.appendToAttributes(result); |
188 | 2 | } |
189 | 27 | return result; |
190 | |
} |
191 | |
|
192 | |
|
193 | 22 | private class SerializationHelper |
194 | |
extends XMLFilterImpl |
195 | |
{ |
196 | |
@Override |
197 | |
public void parse(InputSource input) |
198 | |
throws SAXException, IOException |
199 | |
{ |
200 | 11 | startDocument(); |
201 | 11 | toSAX(getContentHandler()); |
202 | 11 | endDocument(); |
203 | 11 | } |
204 | |
} |
205 | |
} |