KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > jaxp > W3CDOMStreamWriter


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.jbi.jaxp;
18
19 /*
20  * This implementation comes from the XFire project
21  * https://svn.codehaus.org/xfire/trunk/xfire/xfire-core/src/main/org/codehaus/xfire/util/stax/
22  */

23
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Stack JavaDoc;
27
28 import javax.xml.namespace.NamespaceContext JavaDoc;
29 import javax.xml.parsers.DocumentBuilder JavaDoc;
30 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
31 import javax.xml.parsers.ParserConfigurationException JavaDoc;
32 import javax.xml.stream.XMLStreamException;
33
34 import org.w3c.dom.Attr JavaDoc;
35 import org.w3c.dom.Document JavaDoc;
36 import org.w3c.dom.Element JavaDoc;
37
38 public class W3CDOMStreamWriter
39     extends DOMStreamWriter
40 {
41     static final String JavaDoc XML_NS = "http://www.w3.org/2000/xmlns/";
42     private Stack JavaDoc stack = new Stack JavaDoc();
43     private Document JavaDoc document;
44     private Element JavaDoc currentNode;
45     private NamespaceContext JavaDoc context;
46     private Map JavaDoc properties = new HashMap JavaDoc();
47
48     public W3CDOMStreamWriter() throws ParserConfigurationException JavaDoc
49     {
50         this(DocumentBuilderFactory.newInstance().newDocumentBuilder());
51     }
52     
53     public W3CDOMStreamWriter(DocumentBuilder JavaDoc builder)
54     {
55         document = builder.newDocument();
56     }
57     
58     public W3CDOMStreamWriter(Document JavaDoc document)
59     {
60         this.document = document;
61     }
62     
63     public Document JavaDoc getDocument()
64     {
65         return document;
66     }
67     
68     public void writeStartElement(String JavaDoc local)
69         throws XMLStreamException
70     {
71         newChild(document.createElement(local));
72     }
73
74     private void newChild(Element JavaDoc element)
75     {
76         if (currentNode != null)
77         {
78             stack.push(currentNode);
79             currentNode.appendChild(element);
80         }
81         else
82         {
83             document.appendChild(element);
84         }
85         
86         W3CNamespaceContext context = new W3CNamespaceContext();
87         context.setElement(element);
88         this.context = context;
89         
90         currentNode = element;
91     }
92
93     public void writeStartElement(String JavaDoc namespace, String JavaDoc local)
94         throws XMLStreamException
95     {
96         newChild(document.createElementNS(namespace, local));
97     }
98
99     public void writeStartElement(String JavaDoc prefix, String JavaDoc local, String JavaDoc namespace)
100         throws XMLStreamException
101     {
102         if (prefix == null || prefix.equals(""))
103         {
104             writeStartElement(namespace, local);
105         }
106         else
107         {
108             newChild(document.createElementNS(namespace, prefix + ":" + local));
109         }
110     }
111
112     public void writeEmptyElement(String JavaDoc namespace, String JavaDoc local)
113         throws XMLStreamException
114     {
115         writeStartElement(namespace, local);
116     }
117
118     public void writeEmptyElement(String JavaDoc prefix, String JavaDoc namespace, String JavaDoc local)
119         throws XMLStreamException
120     {
121         writeStartElement(prefix, namespace, local);
122     }
123
124     public void writeEmptyElement(String JavaDoc local)
125         throws XMLStreamException
126     {
127         writeStartElement(local);
128     }
129
130     public void writeEndElement()
131         throws XMLStreamException
132     {
133         if (stack.size() > 0)
134             currentNode = (Element JavaDoc) stack.pop();
135         else
136             currentNode = null;
137     }
138
139     public void writeEndDocument()
140         throws XMLStreamException
141     {
142     }
143
144     public void writeAttribute(String JavaDoc local, String JavaDoc value)
145         throws XMLStreamException
146     {
147         Attr JavaDoc a = document.createAttribute(local);
148         a.setValue(value);
149         currentNode.setAttributeNode(a);
150     }
151
152     public void writeAttribute(String JavaDoc prefix, String JavaDoc namespace, String JavaDoc local, String JavaDoc value)
153         throws XMLStreamException
154     {
155         if (prefix.length() > 0)
156             local = prefix + ":" + local;
157         
158         Attr JavaDoc a = document.createAttributeNS(namespace, local);
159         a.setValue(value);
160         currentNode.setAttributeNodeNS(a);
161     }
162
163     public void writeAttribute(String JavaDoc namespace, String JavaDoc local, String JavaDoc value)
164         throws XMLStreamException
165     {
166         Attr JavaDoc a = document.createAttributeNS(namespace, local);
167         a.setValue(value);
168         currentNode.setAttributeNodeNS(a);
169     }
170
171     public void writeNamespace(String JavaDoc prefix, String JavaDoc namespace)
172         throws XMLStreamException
173     {
174         if (prefix.length() == 0)
175         {
176             writeDefaultNamespace(namespace);
177         }
178         else
179         {
180             currentNode.setAttributeNS(XML_NS, "xmlns:" + prefix, namespace);
181         }
182     }
183
184     public void writeDefaultNamespace(String JavaDoc namespace)
185         throws XMLStreamException
186     {
187         currentNode.setAttributeNS(XML_NS, "xmlns", namespace);
188     }
189
190     public void writeComment(String JavaDoc value)
191         throws XMLStreamException
192     {
193         currentNode.appendChild(document.createComment(value));
194     }
195
196     public void writeProcessingInstruction(String JavaDoc target)
197         throws XMLStreamException
198     {
199         currentNode.appendChild(document.createProcessingInstruction(target, null));
200     }
201
202     public void writeProcessingInstruction(String JavaDoc target, String JavaDoc data)
203         throws XMLStreamException
204     {
205         currentNode.appendChild(document.createProcessingInstruction(target, data));
206     }
207
208     public void writeCData(String JavaDoc data)
209         throws XMLStreamException
210     {
211         currentNode.appendChild(document.createCDATASection(data));
212     }
213
214     public void writeDTD(String JavaDoc arg0)
215         throws XMLStreamException
216     {
217         throw new UnsupportedOperationException JavaDoc();
218     }
219
220     public void writeEntityRef(String JavaDoc ref)
221         throws XMLStreamException
222     {
223         currentNode.appendChild(document.createEntityReference(ref));
224     }
225
226     public void writeStartDocument()
227         throws XMLStreamException
228     {
229     }
230
231     public void writeStartDocument(String JavaDoc version)
232         throws XMLStreamException
233     {
234         writeStartDocument();
235     }
236
237     public void writeStartDocument(String JavaDoc encoding, String JavaDoc version)
238         throws XMLStreamException
239     {
240         writeStartDocument();
241     }
242
243     public void writeCharacters(String JavaDoc text)
244         throws XMLStreamException
245     {
246         currentNode.appendChild(document.createTextNode(text));
247     }
248
249     public void writeCharacters(char[] text, int start, int len)
250         throws XMLStreamException
251     {
252         writeCharacters(new String JavaDoc(text, start, len));
253     }
254
255     public String JavaDoc getPrefix(String JavaDoc uri)
256         throws XMLStreamException
257     {
258         return context.getPrefix(uri);
259     }
260
261     public void setPrefix(String JavaDoc arg0, String JavaDoc arg1)
262         throws XMLStreamException
263     {
264     }
265
266     public void setDefaultNamespace(String JavaDoc arg0)
267         throws XMLStreamException
268     {
269     }
270
271     public void setNamespaceContext(NamespaceContext JavaDoc context)
272         throws XMLStreamException
273     {
274         this.context = context;
275     }
276
277     public NamespaceContext JavaDoc getNamespaceContext()
278     {
279         return context;
280     }
281
282     public Object JavaDoc getProperty(String JavaDoc prop)
283         throws IllegalArgumentException JavaDoc
284     {
285         return properties.get(prop);
286     }
287 }
288
Popular Tags