1:
37:
38: package ;
39:
40: import ;
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58: import ;
59: import ;
60: import ;
61: import ;
62: import ;
63:
64:
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:
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:
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:
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: }