KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freemarker > ext > xml > JdomNavigator


1 /*
2  * Copyright (c) 2003 The Visigoth Software Society. All rights
3  * reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  *
17  * 3. The end-user documentation included with the redistribution, if
18  * any, must include the following acknowledgement:
19  * "This product includes software developed by the
20  * Visigoth Software Society (http://www.visigoths.org/)."
21  * Alternately, this acknowledgement may appear in the software itself,
22  * if and wherever such third-party acknowledgements normally appear.
23  *
24  * 4. Neither the name "FreeMarker", "Visigoth", nor any of the names of the
25  * project contributors may be used to endorse or promote products derived
26  * from this software without prior written permission. For written
27  * permission, please contact visigoths@visigoths.org.
28  *
29  * 5. Products derived from this software may not be called "FreeMarker" or "Visigoth"
30  * nor may "FreeMarker" or "Visigoth" appear in their names
31  * without prior written permission of the Visigoth Software Society.
32  *
33  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
34  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36  * DISCLAIMED. IN NO EVENT SHALL THE VISIGOTH SOFTWARE SOCIETY OR
37  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
40  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
41  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
42  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
43  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44  * SUCH DAMAGE.
45  * ====================================================================
46  *
47  * This software consists of voluntary contributions made by many
48  * individuals on behalf of the Visigoth Software Society. For more
49  * information on the Visigoth Software Society, please see
50  * http://www.visigoths.org/
51  */

52
53 package freemarker.ext.xml;
54
55 import java.io.IOException JavaDoc;
56 import java.io.StringWriter JavaDoc;
57 import java.util.Iterator JavaDoc;
58 import java.util.List JavaDoc;
59
60 import freemarker.template.TemplateModelException;
61
62 import org.jaxen.Context;
63 import org.jaxen.NamespaceContext;
64 import org.jaxen.jdom.JDOMXPath;
65 import org.jdom.Element;
66 import org.jdom.Attribute;
67 import org.jdom.CDATA;
68 import org.jdom.Comment;
69 import org.jdom.DocType;
70 import org.jdom.Document;
71 import org.jdom.EntityRef;
72 import org.jdom.Namespace;
73 import org.jdom.ProcessingInstruction;
74 import org.jdom.Text;
75 import org.jdom.output.XMLOutputter;
76
77 /**
78  * @version $Id: JdomNavigator.java,v 1.3 2003/01/31 11:39:17 szegedia Exp $
79  * @author Attila Szegedi
80  */

81 class JdomNavigator extends Navigator {
82     private static final XMLOutputter OUTPUT = new XMLOutputter();
83     
84     JdomNavigator() {
85     }
86
87     void getAsString(Object JavaDoc node, StringWriter JavaDoc sw)
88     throws
89         TemplateModelException
90     {
91         try {
92             if (node instanceof Element) {
93                 OUTPUT.output((Element)node, sw);
94             }
95             else if (node instanceof Attribute) {
96                 Attribute attribute = (Attribute)node;
97                 sw.write(" ");
98                 sw.write(attribute.getQualifiedName());
99                 sw.write("=\"");
100                 sw.write(OUTPUT.escapeAttributeEntities(attribute.getValue()));
101                 sw.write("\"");
102             }
103             else if (node instanceof Text) {
104                 OUTPUT.output((Text)node, sw);
105             }
106             else if (node instanceof Document) {
107                 OUTPUT.output((Document)node, sw);
108             }
109             else if (node instanceof ProcessingInstruction) {
110                 OUTPUT.output((ProcessingInstruction)node, sw);
111             }
112             else if (node instanceof Comment) {
113                 OUTPUT.output((Comment)node, sw);
114             }
115             else if (node instanceof CDATA) {
116                 OUTPUT.output((CDATA)node, sw);
117             }
118             else if (node instanceof DocType) {
119                 OUTPUT.output((DocType)node, sw);
120             }
121             else if (node instanceof EntityRef) {
122                 OUTPUT.output((EntityRef)node, sw);
123             }
124             else {
125                 throw new TemplateModelException(node.getClass().getName() + " is not a core JDOM class");
126             }
127         }
128         catch(IOException JavaDoc e) {
129             throw new TemplateModelException(e);
130         }
131     }
132
133     void getChildren(Object JavaDoc node, String JavaDoc localName, String JavaDoc namespaceUri, List JavaDoc result) {
134         if(node instanceof Element) {
135             Element e = (Element)node;
136             if(localName == null) {
137                 result.addAll(e.getChildren());
138             }
139             else {
140                 result.addAll(e.getChildren(localName, Namespace.getNamespace("", namespaceUri)));
141             }
142         }
143         else if(node instanceof Document) {
144             Element root = ((Document)node).getRootElement();
145             if(localName == null || (equal(root.getName(), localName) && equal(root.getNamespaceURI(), namespaceUri))) {
146                 result.add(root);
147             }
148         }
149     }
150     
151     void getAttributes(Object JavaDoc node, String JavaDoc localName, String JavaDoc namespaceUri, List JavaDoc result) {
152         if(node instanceof Element) {
153             Element e = (Element)node;
154             if(localName == null) {
155                 result.addAll(e.getAttributes());
156             }
157             else {
158                 Attribute attr = e.getAttribute(localName, Namespace.getNamespace("", namespaceUri));
159                 if(attr != null) {
160                     result.add(attr);
161                 }
162             }
163         } else if (node instanceof ProcessingInstruction) {
164             ProcessingInstruction pi = (ProcessingInstruction)node;
165             if ("target".equals(localName)) {
166                 result.add(new Attribute("target", pi.getTarget()));
167             }
168             else if ("data".equals(localName)) {
169                 result.add(new Attribute("data", pi.getData()));
170             }
171             else {
172                 result.add(new Attribute(localName, pi.getValue(localName)));
173             }
174         } else if (node instanceof DocType) {
175             DocType doctype = (DocType)node;
176             if ("publicId".equals(localName)) {
177                 result.add(new Attribute("publicId", doctype.getPublicID()));
178             }
179             else if ("systemId".equals(localName)) {
180                 result.add(new Attribute("systemId", doctype.getSystemID()));
181             }
182             else if ("elementName".equals(localName)) {
183                 result.add(new Attribute("elementName", doctype.getElementName()));
184             }
185         }
186     }
187
188     void getDescendants(Object JavaDoc node, List JavaDoc result) {
189         if(node instanceof Document) {
190             Element root = ((Document)node).getRootElement();
191             result.add(root);
192             getDescendants(root, result);
193         }
194         else if(node instanceof Element) {
195             getDescendants((Element)node, result);
196         }
197     }
198     
199     private void getDescendants(Element node, List JavaDoc result) {
200         for (Iterator JavaDoc iter = node.getChildren().iterator(); iter.hasNext();) {
201             Element subnode = (Element)iter.next();
202             result.add(subnode);
203             getDescendants(subnode, result);
204         }
205     }
206
207     Object JavaDoc getParent(Object JavaDoc node) {
208         if (node instanceof Element) {
209             return((Element)node).getParent();
210         }
211         if (node instanceof Attribute) {
212             return((Attribute)node).getParent();
213         }
214         if (node instanceof Text) {
215             return((Text)node).getParent();
216         }
217         if (node instanceof ProcessingInstruction) {
218             return((ProcessingInstruction)node).getParent();
219         }
220         if (node instanceof Comment) {
221             return((Comment)node).getParent();
222         }
223         if (node instanceof EntityRef) {
224             return((EntityRef)node).getParent();
225         }
226         return null;
227     }
228
229     Object JavaDoc getDocument(Object JavaDoc node) {
230         if (node instanceof Element) {
231             return ((Element)node).getDocument();
232         }
233         else if (node instanceof Attribute) {
234             Element parent = ((Attribute)node).getParent();
235             return parent == null ? null : parent.getDocument();
236         }
237         else if (node instanceof Text) {
238             Element parent = ((Text)node).getParent();
239             return parent == null ? null : parent.getDocument();
240         }
241         else if (node instanceof Document)
242             return (Document)node;
243         else if (node instanceof ProcessingInstruction) {
244             return ((ProcessingInstruction)node).getDocument();
245         }
246         else if (node instanceof EntityRef) {
247             return ((EntityRef)node).getDocument();
248         }
249         else if (node instanceof Comment) {
250             return ((Comment)node).getDocument();
251         }
252         return null;
253     }
254
255     Object JavaDoc getDocumentType(Object JavaDoc node) {
256         return
257             node instanceof Document
258             ? ((Document)node).getDocType()
259             : null;
260     }
261     
262     void getContent(Object JavaDoc node, List JavaDoc result) {
263         if (node instanceof Element)
264             result.addAll(((Element)node).getContent());
265         else if (node instanceof Document)
266             result.addAll(((Document)node).getContent());
267     }
268
269     String JavaDoc getText(Object JavaDoc node) {
270         if (node instanceof Element) {
271             return ((Element)node).getTextTrim();
272         }
273         if (node instanceof Attribute) {
274             return ((Attribute)node).getValue();
275         }
276         if (node instanceof CDATA) {
277             return ((CDATA)node).getText();
278         }
279         if (node instanceof Comment) {
280             return ((Comment)node).getText();
281         }
282         if (node instanceof ProcessingInstruction) {
283             return ((ProcessingInstruction)node).getData();
284         }
285         return null;
286     }
287
288     String JavaDoc getLocalName(Object JavaDoc node) {
289         if (node instanceof Element) {
290             return ((Element)node).getName();
291         }
292         if (node instanceof Attribute) {
293             return ((Attribute)node).getName();
294         }
295         if (node instanceof EntityRef) {
296             return ((EntityRef)node).getName();
297         }
298         if (node instanceof ProcessingInstruction) {
299             return ((ProcessingInstruction)node).getTarget();
300         }
301         if (node instanceof DocType) {
302             return ((DocType)node).getElementName();
303         }
304         return null;
305     }
306
307     String JavaDoc getNamespacePrefix(Object JavaDoc node) {
308         if(node instanceof Element) {
309             return ((Element)node).getNamespacePrefix();
310         }
311         if(node instanceof Attribute) {
312             return ((Attribute)node).getNamespacePrefix();
313         }
314         return null;
315     }
316
317     String JavaDoc getNamespaceUri(Object JavaDoc node) {
318         if(node instanceof Element) {
319             return ((Element)node).getNamespaceURI();
320         }
321         if(node instanceof Attribute) {
322             return ((Attribute)node).getNamespaceURI();
323         }
324         return null;
325     }
326
327     String JavaDoc getType(Object JavaDoc node) {
328         if(node instanceof Attribute) {
329             return "attribute";
330         }
331         if(node instanceof CDATA) {
332             return "cdata";
333         }
334         if(node instanceof Comment) {
335             return "comment";
336         }
337         if(node instanceof Document) {
338             return "document";
339         }
340         if(node instanceof DocType) {
341             return "documentType";
342         }
343         if(node instanceof Element) {
344             return "element";
345         }
346         if(node instanceof EntityRef) {
347             return "entityReference";
348         }
349         if(node instanceof Namespace) {
350             return "namespace";
351         }
352         if(node instanceof ProcessingInstruction) {
353             return "processingInstruction";
354         }
355         if(node instanceof Text) {
356             return "text";
357         }
358         return "unknown";
359     }
360
361     XPathEx createXPathEx(String JavaDoc xpathString) throws TemplateModelException
362     {
363         try {
364             return new JDOMXPathEx(xpathString);
365         }
366         catch(Exception JavaDoc e) {
367             throw new TemplateModelException(e);
368         }
369     }
370
371     private static final class JDOMXPathEx
372     extends
373         JDOMXPath
374     implements
375         XPathEx
376     {
377         JDOMXPathEx(String JavaDoc path)
378         throws
379             Exception JavaDoc
380         {
381             super(path);
382         }
383
384         public List JavaDoc selectNodes(Object JavaDoc object, NamespaceContext namespaces)
385         throws
386             TemplateModelException
387         {
388             Context context = getContext(object);
389             context.getContextSupport().setNamespaceContext(namespaces);
390             try {
391                 return selectNodesForContext(context);
392             }
393             catch(Exception JavaDoc e) {
394                 throw new TemplateModelException(e);
395             }
396         }
397     }
398 }
399
Popular Tags