KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > jdom > JDOMObjectModel


1 package net.sf.saxon.jdom;
2
3 import net.sf.saxon.Configuration;
4 import net.sf.saxon.event.PipelineConfiguration;
5 import net.sf.saxon.event.Receiver;
6 import net.sf.saxon.expr.XPathContext;
7 import net.sf.saxon.om.DocumentInfo;
8 import net.sf.saxon.om.ExternalObjectModel;
9 import net.sf.saxon.om.NodeInfo;
10 import net.sf.saxon.trans.XPathException;
11 import net.sf.saxon.value.Value;
12 import net.sf.saxon.value.SequenceExtent;
13 import org.jdom.*;
14
15 import javax.xml.transform.Result JavaDoc;
16 import javax.xml.transform.Source JavaDoc;
17 import java.io.Serializable JavaDoc;
18
19
20 /**
21  * This interface must be implemented by any third-party object model that can
22  * be wrapped with a wrapper that implements the Saxon Object Model (the NodeInfo interface).
23  * This implementation of the interface supports wrapping of JDOM Documents.
24  */

25
26 public class JDOMObjectModel implements ExternalObjectModel, Serializable JavaDoc {
27
28     public JDOMObjectModel() {}
29
30      /**
31      * Test whether this object model recognizes a given node as one of its own
32      */

33
34     public boolean isRecognizedNode(Object JavaDoc object) {
35          return object instanceof Document ||
36                  object instanceof Element ||
37                  object instanceof Attribute ||
38                  object instanceof Text ||
39                  object instanceof CDATA ||
40                  object instanceof Comment ||
41                  object instanceof ProcessingInstruction ||
42                  object instanceof Namespace;
43     }
44
45     /**
46      * Test whether this object model recognizes a given class as representing a
47      * node in that object model. This method will generally be called at compile time.
48      *
49      * @param nodeClass A class that possibly represents nodes
50      * @return true if the class is used to represent nodes in this object model
51      */

52
53     public boolean isRecognizedNodeClass(Class JavaDoc nodeClass) {
54         return Document.class.isAssignableFrom(nodeClass) ||
55                 Element.class.isAssignableFrom(nodeClass) ||
56                 Attribute.class.isAssignableFrom(nodeClass) ||
57                 Text.class.isAssignableFrom(nodeClass) ||
58                 CDATA.class.isAssignableFrom(nodeClass) ||
59                 Comment.class.isAssignableFrom(nodeClass) ||
60                 ProcessingInstruction.class.isAssignableFrom(nodeClass) ||
61                 Namespace.class.isAssignableFrom(nodeClass);
62     }
63
64     /**
65      * Test whether this object model recognizes a given class as representing a
66      * list of nodes in that object model. This method will generally be called at compile time.
67      *
68      * @param nodeClass A class that possibly represents nodes
69      * @return true if the class is used to represent nodes in this object model
70      */

71
72     public boolean isRecognizedNodeListClass(Class JavaDoc nodeClass) {
73         return false;
74     }
75
76     /**
77      * Test whether this object model recognizes a particular kind of JAXP Result object,
78      * and if it does, return a Receiver that builds an instance of this data model from
79      * a sequence of events. If the Result is not recognised, return null.
80      */

81
82     public Receiver getDocumentBuilder(Result JavaDoc result) {
83         return null; //To change body of implemented methods use File | Settings | File Templates.
84
}
85
86     /**
87      * Test whether this object model recognizes a particular kind of JAXP Source object,
88      * and if it does, send the contents of the document to a supplied Receiver, and return true.
89      * Otherwise, return false.
90      */

91
92     public boolean sendSource(Source JavaDoc source, Receiver receiver, PipelineConfiguration pipe) throws XPathException {
93         return false; //To change body of implemented methods use File | Settings | File Templates.
94
}
95
96     /**
97      * Wrap or unwrap a node using this object model to return the corresponding Saxon node. If the supplied
98      * source does not belong to this object model, return null
99      */

100
101     public NodeInfo unravel(Source JavaDoc source, Configuration config) {
102         return null; //To change body of implemented methods use File | Settings | File Templates.
103
}
104
105     /**
106      * Convert a Java object to an XPath value. If the supplied object is recognized as a representation
107      * of a value using this object model, the object model should convert the value to an XPath value
108      * and return this as the result. If not, it should return null. If the object is recognized but cannot
109      * be converted, an exception should be thrown
110      */

111
112     public Value convertObjectToXPathValue(Object JavaDoc object, Configuration config) throws XPathException {
113         return null; //To change body of implemented methods use File | Settings | File Templates.
114
}
115
116     /**
117      * Convert an XPath value to an object in this object model. If the supplied value can be converted
118      * to an object in this model, of the specified class, then the conversion should be done and the
119      * resulting object returned. If the value cannot be converted, the method should return null. Note
120      * that the supplied class might be a List, in which case the method should inspect the contents of the
121      * Value to see whether they belong to this object model.
122      */

123
124     public Object JavaDoc convertXPathValueToObject(Value value, Class JavaDoc targetClass, XPathContext context) {
125         return null; //To change body of implemented methods use File | Settings | File Templates.
126
}
127
128     /**
129      * Wrap a document node in the external object model in a document wrapper that implements
130      * the Saxon DocumentInfo interface
131      * @param node a node (any node) in the third party document
132      * @param baseURI the base URI of the node (supply "" if unknown)
133      * @param config the Saxon configuration (which among other things provides access to the NamePool)
134      * @return the wrapper, which must implement DocumentInfo
135      */

136
137     public DocumentInfo wrapDocument(Object JavaDoc node, String JavaDoc baseURI, Configuration config) {
138         Document documentNode = getDocumentRoot(node);
139         return new DocumentWrapper(documentNode, baseURI, config);
140     }
141
142     /**
143      * Wrap a node within the external object model in a node wrapper that implements the Saxon
144      * VirtualNode interface (which is an extension of NodeInfo)
145      * @param document the document wrapper, as a DocumentInfo object
146      * @param node the node to be wrapped. This must be a node within the document wrapped by the
147      * DocumentInfo provided in the first argument
148      * @return the wrapper for the node, as an instance of VirtualNode
149      */

150
151     public NodeInfo wrapNode(DocumentInfo document, Object JavaDoc node) {
152         return ((DocumentWrapper)document).wrap(node);
153     }
154
155     /**
156      * Get the document root
157      */

158
159     private Document getDocumentRoot(Object JavaDoc node) {
160         while (!(node instanceof Document)) {
161             if (node instanceof Element) {
162                 if (((Element)node).isRootElement()) {
163                     return ((Element)node).getDocument();
164                 } else {
165                     node = ((Element)node).getParent();
166                 }
167             } else if (node instanceof Text) {
168                 node = ((Text)node).getParent();
169             } else if (node instanceof Comment) {
170                 node = ((Comment)node).getParent();
171             } else if (node instanceof ProcessingInstruction) {
172                 node = ((ProcessingInstruction)node).getParent();
173             } else if (node instanceof Attribute) {
174                 node = ((Attribute)node).getParent();
175             } else if (node instanceof Document) {
176                 return (Document)node;
177             } else if (node instanceof Namespace) {
178                 throw new UnsupportedOperationException JavaDoc("Cannot find parent of JDOM namespace node");
179             } else {
180                 throw new IllegalStateException JavaDoc("Unknown JDOM node type " + node.getClass());
181             }
182         }
183         return (Document)node;
184     }
185
186     /**
187      * Convert a sequence of values to a NODELIST, as defined in the JAXP XPath API spec. This method
188      * is used when the evaluate() request specifies the return type as NODELIST, regardless of the
189      * actual results of the expression. If the sequence contains things other than nodes, the fallback
190      * is to return the sequence as a Java List object. The method can return null to invoke fallback
191      * behaviour.
192      */

193
194     public Object JavaDoc convertToNodeList(SequenceExtent extent) {
195         return null;
196     }
197 }
198
199
200 //
201
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
202
// you may not use this file except in compliance with the License. You may obtain a copy of the
203
// License at http://www.mozilla.org/MPL/
204
//
205
// Software distributed under the License is distributed on an "AS IS" basis,
206
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
207
// See the License for the specific language governing rights and limitations under the License.
208
//
209
// The Original Code is: all this file.
210
//
211
// The Initial Developer of the Original Code is Michael H. Kay.
212
//
213
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
214
//
215
// Contributor(s): Gunther Schadow (changes to allow access to public fields; also wrapping
216
// of extensions and mapping of null to empty sequence).
217
//
218
Popular Tags