KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > micronova > util > codec > CodecXML


1 /*
2
3 Copyright 2003-2007 MicroNova (R)
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or
7 without modification, are permitted provided that the following
8 conditions are met:
9
10     * Redistributions of source code must retain the above copyright
11     notice, this list of conditions and the following disclaimer.
12
13     * Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions and the following disclaimer in the
15     documentation and/or other materials provided with the distribution.
16
17     * Neither the name of MicroNova nor the names of its contributors
18     may be used to endorse or promote products derived from this
19     software without specific prior written permission.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE.
32
33 */

34
35
36 package com.micronova.util.codec;
37
38 import java.util.*;
39 import com.micronova.util.*;
40 import org.w3c.dom.*;
41 import javax.xml.parsers.*;
42 import org.xml.sax.*;
43 import java.io.*;
44 import java.net.*;
45 import javax.xml.transform.*;
46 import javax.xml.transform.dom.*;
47 import javax.xml.transform.sax.*;
48 import javax.xml.transform.stream.*;
49
50 /** XML-related codecs */
51
52 public class CodecXML extends Codec
53 {
54     /** parses document */
55
56     public static final Object JavaDoc parse(Object JavaDoc object, Object JavaDoc control) throws Exception JavaDoc
57     {
58         return XMLUtil.parse(object, TypeUtil.isNestedMap(control));
59     }
60
61     /** parser with default controlMap */
62
63     public static Object JavaDoc parse(Object JavaDoc object) throws Exception JavaDoc
64     {
65         return parse(object, null);
66     }
67
68     /** encodes XML tag characters */
69
70     public static final Object JavaDoc encode(Object JavaDoc object) throws Exception JavaDoc
71     {
72         return encode(object, null);
73     }
74
75     /** encodes XML tag characters specified by the "spec" regex (e.g., "[&<>]" to encode &, <, > only); if "spec" is null, then all supported characters are encoded. If "spec" is "~", then "[&<>]" is assumed. */
76
77     public static final Object JavaDoc encode(Object JavaDoc object, Object JavaDoc spec) throws Exception JavaDoc
78     {
79         if (object != null)
80         {
81             if ("~".equals(spec))
82             {
83                 spec = "[&<>]";
84             }
85
86             object = XMLUtil.encode(object.toString(), TypeUtil.isPattern(spec));
87         }
88
89         return object;
90     }
91
92     /** decodes XML tag characters */
93
94     public static final Object JavaDoc decode(Object JavaDoc object) throws Exception JavaDoc
95     {
96         if (object != null)
97         {
98             object = XMLUtil.decode(object.toString());
99         }
100
101         return object;
102     }
103
104     /** outputs an XML object */
105
106     public static final Object JavaDoc output(Object JavaDoc object, Object JavaDoc control) throws Exception JavaDoc
107     {
108         return XMLUtil.output(XMLUtil.createSource(object), new NestedMap(control));
109     }
110
111     /** outputs using default control (omit-xml-declaration=yes) */
112
113     public static final Object JavaDoc output(Object JavaDoc object) throws Exception JavaDoc
114     {
115         return output(object, "omit-xml-declaration=yes");
116     }
117
118     /** parses HTML as in parseHtml */
119
120     public static final Object JavaDoc parseHtml(Object JavaDoc object, Object JavaDoc control) throws Exception JavaDoc
121     {
122         if (object != null)
123         {
124             object = XMLUtil.parseHtml(object, TypeUtil.isNestedMap(control));
125         }
126         
127         return object;
128     }
129
130     public static final Object JavaDoc parseHtml(Object JavaDoc object) throws Exception JavaDoc
131     {
132         return parseHtml(object, null);
133     }
134
135     /** removes a node from its parent */
136
137     public static final Object JavaDoc removeNode(Object JavaDoc object) throws Exception JavaDoc
138     {
139         Node node = TypeUtil.isNode(object);
140
141         if (node != null)
142         {
143             Node parentNode = node.getParentNode();
144
145             if (parentNode != null)
146             {
147                 parentNode.removeChild(node);
148             }
149         }
150         
151         return object;
152     }
153
154     /** insert a node before the second argument. */
155
156     public static final Object JavaDoc insertNodeBefore(Object JavaDoc object, Object JavaDoc before) throws Exception JavaDoc
157     {
158         Node beforeNode = TypeUtil.isNode(before);
159
160         if (beforeNode != null)
161         {
162             Node node = TypeUtil.isNode(object, beforeNode.getOwnerDocument());
163
164             if (node != null)
165             {
166                 Node parentNode = beforeNode.getParentNode();
167
168                 if (parentNode != null)
169                 {
170                     parentNode.insertBefore(node, beforeNode);
171                 }
172             }
173         }
174         
175         return object;
176     }
177
178     /** appends a child */
179
180     public static final Object JavaDoc appendNode(Object JavaDoc object, Object JavaDoc parent) throws Exception JavaDoc
181     {
182         Node parentNode = TypeUtil.isNode(parent);
183
184         if (parentNode != null)
185         {
186             Node node = TypeUtil.isNode(object, parentNode.getOwnerDocument());
187
188             if (node != null)
189             {
190                 parentNode.appendChild(node);
191             }
192         }
193         
194         return object;
195     }
196
197     /** shallow copies a node */
198
199     public static final Object JavaDoc cloneNode(Object JavaDoc object) throws Exception JavaDoc
200     {
201         Node node = TypeUtil.isNode(object);
202
203         if (node != null)
204         {
205             node = node.cloneNode(false);
206         }
207
208         return node;
209     }
210     
211     /** deep copies a node */
212
213     public static final Object JavaDoc copyNode(Object JavaDoc object) throws Exception JavaDoc
214     {
215         Node node = TypeUtil.isNode(object);
216
217         if (node != null)
218         {
219             node = node.cloneNode(true);
220         }
221
222         return node;
223     }
224
225     /** replace an old node */
226
227     public static final Object JavaDoc replaceNode(Object JavaDoc object, Object JavaDoc old) throws Exception JavaDoc
228     {
229         Node node = null;
230         Node oldNode = TypeUtil.isNode(old);
231
232         if (oldNode != null)
233         {
234             node = TypeUtil.isNode(object, oldNode.getOwnerDocument());
235
236             if (node != null)
237             {
238                 Node parentNode = oldNode.getParentNode();
239
240                 if (parentNode != null)
241                 {
242                     parentNode.replaceChild(node, oldNode);
243                 }
244             }
245         }
246
247         return node;
248     }
249
250     /** normalizes a node */
251
252     public static final Object JavaDoc normalizeNode(Object JavaDoc object) throws Exception JavaDoc
253     {
254         Node node = TypeUtil.isNode(object);
255
256         if (node != null)
257         {
258             node.normalize();
259         }
260
261         return node;
262     }
263
264     /** sets element attribute; if value is null, then attribute is removed. */
265
266     public static final Object JavaDoc setAttribute(Object JavaDoc object, Object JavaDoc key, Object JavaDoc value) throws Exception JavaDoc
267     {
268         Node node = TypeUtil.isNode(object);
269
270         if (node instanceof Element)
271         {
272             if (key != null)
273             {
274                 String JavaDoc keyString = key.toString();
275
276                 if (value == null)
277                 {
278                     ((Element)node).removeAttribute(keyString);
279                 }
280                 else
281                 {
282                     ((Element)node).setAttribute(keyString, value.toString());
283                 }
284             }
285         }
286
287         return object;
288     }
289
290     /** gets element attribute value */
291
292     public static final Object JavaDoc getAttribute(Object JavaDoc object, Object JavaDoc key) throws Exception JavaDoc
293     {
294         Node node = TypeUtil.isNode(object);
295
296         if (node instanceof Element)
297         {
298             if (key != null)
299             {
300                 object = ((Element)node).getAttribute(key.toString());
301             }
302         }
303
304         return object;
305     }
306 }
307
308
Popular Tags