Source code: gnu/xml/dom/ls/DomLSSerializer.java
1 /* DomLSSerializer.java --
2 Copyright (C) 1999,2000,2001 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
20
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
37
38 package gnu.xml.dom.ls;
39
40 import java.io.File;
41 import java.io.FileOutputStream;
42 import java.io.IOException;
43 import java.io.OutputStream;
44 import java.io.StringWriter;
45 import java.io.Writer;
46 import java.net.HttpURLConnection;
47 import java.net.MalformedURLException;
48 import java.net.URL;
49 import java.net.URLConnection;
50 import java.util.Arrays;
51 import java.util.List;
52 import org.w3c.dom.DOMConfiguration;
53 import org.w3c.dom.DOMException;
54 import org.w3c.dom.DOMStringList;
55 import org.w3c.dom.Node;
56 import org.w3c.dom.ls.LSException;
57 import org.w3c.dom.ls.LSOutput;
58 import org.w3c.dom.ls.LSSerializer;
59 import org.w3c.dom.ls.LSSerializerFilter;
60 import org.w3c.dom.traversal.NodeFilter;
61 import gnu.xml.dom.DomDOMException;
62 import gnu.xml.transform.StreamSerializer;
63
64 /**
65 * Serialize a DOM node to a stream.
66 *
67 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
68 */
69 public class DomLSSerializer
70 extends StreamSerializer
71 implements LSSerializer, DOMConfiguration, DOMStringList
72 {
73
74 private static final List SUPPORTED_PARAMETERS =
75 Arrays.asList(new String[] {"discard-default-content",
76 "xml-declaration"});
77
78 private LSSerializerFilter filter;
79 private StreamSerializer serializer;
80
81 public DomLSSerializer()
82 {
83 super();
84 discardDefaultContent = true;
85 }
86
87 // -- LSSerializer --
88
89 public DOMConfiguration getDomConfig()
90 {
91 return this;
92 }
93
94 public String getNewLine()
95 {
96 return eol;
97 }
98
99 public void setNewLine(String newLine)
100 {
101 if (newLine == null)
102 {
103 newLine = System.getProperty("line.separator");
104 }
105 eol = newLine;
106 }
107
108 public LSSerializerFilter getFilter()
109 {
110 return filter;
111 }
112
113 public void setFilter(LSSerializerFilter filter)
114 {
115 this.filter = filter;
116 }
117
118 public boolean write(Node node, LSOutput output)
119 throws LSException
120 {
121 OutputStream out = output.getByteStream();
122 try
123 {
124 if (out == null)
125 {
126 String systemId = output.getSystemId();
127 try
128 {
129 URL url = new URL(systemId);
130 URLConnection connection = url.openConnection();
131 connection.setDoOutput(true);
132 if (connection instanceof HttpURLConnection)
133 {
134 ((HttpURLConnection) connection).setRequestMethod("PUT");
135 }
136 out = connection.getOutputStream();
137 }
138 catch (MalformedURLException e)
139 {
140 File file = new File(systemId);
141 out = new FileOutputStream(file);
142 }
143 }
144 serialize(node, out);
145 out.flush();
146 return true;
147 }
148 catch (IOException e)
149 {
150 throw new DomLSException(LSException.SERIALIZE_ERR, e);
151 }
152 }
153
154 public boolean writeToURI(Node node, String uri)
155 throws LSException
156 {
157 LSOutput output = new DomLSOutput();
158 output.setSystemId(uri);
159 return write(node, output);
160 }
161
162 public String writeToString(Node node)
163 throws DOMException, LSException
164 {
165 Writer writer = new StringWriter();
166 LSOutput output = new DomLSOutput();
167 output.setCharacterStream(writer);
168 write(node, output);
169 return writer.toString();
170 }
171
172 public void serialize(Node node, OutputStream out)
173 throws IOException
174 {
175 if (filter == null)
176 {
177 super.serialize(node, out);
178 }
179 else
180 {
181 int wts = filter.getWhatToShow();
182 if (wts != NodeFilter.SHOW_ALL)
183 {
184 switch (node.getNodeType())
185 {
186 case Node.ATTRIBUTE_NODE:
187 if ((wts & NodeFilter.SHOW_ATTRIBUTE) == 0)
188 {
189 super.serialize(node, out);
190 return;
191 }
192 break;
193 case Node.TEXT_NODE:
194 if ((wts & NodeFilter.SHOW_TEXT) == 0)
195 {
196 super.serialize(node, out);
197 return;
198 }
199 break;
200 case Node.ELEMENT_NODE:
201 if ((wts & NodeFilter.SHOW_ELEMENT) == 0)
202 {
203 super.serialize(node, out);
204 return;
205 }
206 break;
207 case Node.CDATA_SECTION_NODE:
208 if ((wts & NodeFilter.SHOW_CDATA_SECTION) == 0)
209 {
210 super.serialize(node, out);
211 return;
212 }
213 break;
214 case Node.COMMENT_NODE:
215 if ((wts & NodeFilter.SHOW_COMMENT) == 0)
216 {
217 super.serialize(node, out);
218 return;
219 }
220 break;
221 case Node.DOCUMENT_NODE:
222 if ((wts & NodeFilter.SHOW_DOCUMENT) == 0)
223 {
224 super.serialize(node, out);
225 return;
226 }
227 break;
228 case Node.DOCUMENT_TYPE_NODE:
229 if ((wts & NodeFilter.SHOW_DOCUMENT_TYPE) == 0)
230 {
231 super.serialize(node, out);
232 return;
233 }
234 break;
235 case Node.PROCESSING_INSTRUCTION_NODE:
236 if ((wts & NodeFilter.SHOW_PROCESSING_INSTRUCTION) == 0)
237 {
238 super.serialize(node, out);
239 return;
240 }
241 break;
242 case Node.DOCUMENT_FRAGMENT_NODE:
243 if ((wts & NodeFilter.SHOW_DOCUMENT_FRAGMENT) == 0)
244 {
245 super.serialize(node, out);
246 return;
247 }
248 break;
249 case Node.ENTITY_NODE:
250 if ((wts & NodeFilter.SHOW_ENTITY) == 0)
251 {
252 super.serialize(node, out);
253 return;
254 }
255 break;
256 case Node.ENTITY_REFERENCE_NODE:
257 if ((wts & NodeFilter.SHOW_ENTITY_REFERENCE) == 0)
258 {
259 super.serialize(node, out);
260 return;
261 }
262 break;
263 case Node.NOTATION_NODE:
264 if ((wts & NodeFilter.SHOW_NOTATION) == 0)
265 {
266 super.serialize(node, out);
267 return;
268 }
269 break;
270 }
271 }
272 switch (filter.acceptNode(node))
273 {
274 case NodeFilter.FILTER_ACCEPT:
275 super.serialize(node, out);
276 break;
277 case NodeFilter.FILTER_REJECT:
278 break;
279 case NodeFilter.FILTER_SKIP:
280 Node first = node.getFirstChild();
281 if (first != null)
282 {
283 serialize(first, out);
284 }
285 break;
286 }
287 }
288 }
289
290 // -- DOMConfiguration --
291
292 public void setParameter(String name, Object value)
293 throws DOMException
294 {
295 if ("discard-default-content".equals(name))
296 {
297 discardDefaultContent = "true".equals(value.toString());
298 }
299 else if ("xml-declaration".equals(name))
300 {
301 xmlDeclaration = "false".equals(value.toString());
302 }
303 else
304 {
305 throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR);
306 }
307 }
308
309 public Object getParameter(String name)
310 throws DOMException
311 {
312 if ("discard-default-content".equals(name))
313 {
314 return discardDefaultContent ? "true" : "false";
315 }
316 else if ("xml-declaration".equals(name))
317 {
318 return xmlDeclaration ? "true" : "false";
319 }
320 else
321 {
322 throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR);
323 }
324 }
325
326 public boolean canSetParameter(String name, Object value)
327 {
328 return contains(name);
329 }
330
331 public DOMStringList getParameterNames()
332 {
333 return this;
334 }
335
336 // -- DOMStringList --
337
338 public String item(int i)
339 {
340 return (String) SUPPORTED_PARAMETERS.get(i);
341 }
342
343 public int getLength()
344 {
345 return SUPPORTED_PARAMETERS.size();
346 }
347
348 public boolean contains(String str)
349 {
350 return SUPPORTED_PARAMETERS.contains(str);
351 }
352
353 }
354