001/*
002 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
003 * 
004 * This software is open source. 
005 * See the bottom of this file for the licence.
006 * 
007 * $Id: DocumentHelper.java,v 1.11 2001/08/18 22:32:45 jstrachan Exp $
008 */
009
010package org.dom4j;
011
012import java.io.StringReader;
013import java.util.Comparator;
014import java.util.List;
015import java.util.Map;
016import java.util.StringTokenizer;
017
018import org.dom4j.io.SAXReader;
019import org.dom4j.rule.Pattern;
020
021import org.jaxen.VariableContext;
022
023import org.xml.sax.SAXException;
024
025/** <p><code>DocumentHelper</code> is a collection of helper methods 
026  * for using DOM4J.</p>
027  *
028  * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
029  * @version $Revision: 1.11 $
030  */
031public class DocumentHelper {
032
033    
034    // Static helper methods
035    
036    public static Document createDocument() {
037        return DocumentFactory.getInstance().createDocument();
038    }
039    
040    public static Document createDocument(Element rootElement) {
041        return DocumentFactory.getInstance().createDocument(rootElement);
042    }
043
044    
045    public static Element createElement(QName qname) {
046        return DocumentFactory.getInstance().createElement(qname);
047    }
048    
049    public static Element createElement(String name) {
050        return DocumentFactory.getInstance().createElement(name);
051    }
052    
053    
054    public static Attribute createAttribute(Element owner, QName qname, String value) {
055        return DocumentFactory.getInstance().createAttribute(owner, qname, value);
056    }
057    
058    public static Attribute createAttribute(Element owner, String name, String value) {
059        return DocumentFactory.getInstance().createAttribute(owner, name, value);
060    }
061    
062    public static CDATA createCDATA(String text) {
063        return DocumentFactory.getInstance().createCDATA(text);
064    }
065    
066    public static Comment createComment(String text) {
067        return DocumentFactory.getInstance().createComment(text);
068    }
069    
070    public static Text createText(String text) {
071        return DocumentFactory.getInstance().createText(text);
072    }
073    
074    
075    public static Entity createEntity(String name, String text) {
076        return DocumentFactory.getInstance().createEntity(name, text);
077    }
078    
079    public static Namespace createNamespace(String prefix, String uri) {
080        return DocumentFactory.getInstance().createNamespace(prefix, uri);
081    }
082    
083    public static ProcessingInstruction createProcessingInstruction(String target, String data) {
084        return DocumentFactory.getInstance().createProcessingInstruction(target, data);
085    }
086    
087    public static ProcessingInstruction createProcessingInstruction(String target, Map data) {
088        return DocumentFactory.getInstance().createProcessingInstruction(target, data);
089    }
090    
091    public static QName createQName(String localName, Namespace namespace) {
092        return DocumentFactory.getInstance().createQName(localName, namespace);
093    }
094    
095    public static QName createQName(String localName) {
096        return DocumentFactory.getInstance().createQName(localName);
097    }
098    
099    
100    /** <p><code>createXPath</code> parses an XPath expression
101      * and creates a new XPath <code>XPath</code> instance
102      * using the singleton {@link DocumentFactory}.</p>
103      *
104      * @param xpathExpression is the XPath expression to create
105      * @return a new <code>XPath</code> instance
106      * @throws InvalidXPathException if the XPath expression is invalid
107      */
108    public static XPath createXPath(String xpathExpression) throws InvalidXPathException {
109        return DocumentFactory.getInstance().createXPath(xpathExpression);
110    }
111    
112    /** <p><code>createXPath</code> parses an XPath expression
113      * and creates a new XPath <code>XPath</code> instance
114      * using the singleton {@link DocumentFactory}.</p>
115      *
116      * @param xpathExpression is the XPath expression to create
117      * @param variableContext is the variable context to use when evaluating the XPath
118      * @return a new <code>XPath</code> instance
119      * @throws InvalidXPathException if the XPath expression is invalid
120      */
121    public static XPath createXPath(String xpathExpression, VariableContext variableContext) throws InvalidXPathException {
122        return DocumentFactory.getInstance().createXPath(xpathExpression, variableContext);
123    }
124    
125    
126    /** <p><code>createXPathFilter</code> parses a NodeFilter
127      * from the given XPath filter expression using the singleton
128      * {@link DocumentFactory}.
129      * XPath filter expressions occur within XPath expressions such as
130      * <code>self::node()[ filterExpression ]</code></p>
131      *
132      * @param xpathFilterExpression is the XPath filter expression 
133      * to create
134      * @return a new <code>NodeFilter</code> instance
135      */
136    public static NodeFilter createXPathFilter(String xpathFilterExpression) {
137        return DocumentFactory.getInstance().createXPathFilter(xpathFilterExpression);
138    }
139    
140    /** <p><code>createPattern</code> parses the given 
141      * XPath expression to create an XSLT style {@link Pattern} instance
142      * which can then be used in an XSLT processing model.</p>
143      *
144      * @param xpathPattern is the XPath pattern expression 
145      * to create
146      * @return a new <code>Pattern</code> instance
147      */
148    public static Pattern createPattern(String xpathPattern) {
149        return DocumentFactory.getInstance().createPattern(xpathPattern);
150    }
151    
152    
153    /** <p><code>selectNodes</code> performs the given XPath
154      * expression on the {@link List} of {@link Node} instances appending
155      * all the results together into a single list.</p>
156      *
157      * @param xpathFilterExpression is the XPath filter expression 
158      * to evaluate
159      * @param nodes is the list of nodes on which to evalute the XPath
160      * @return the results of all the XPath evaluations as a single list
161      */
162    public static List selectNodes(String xpathFilterExpression, List nodes) {
163        XPath xpath = createXPath( xpathFilterExpression );
164        return xpath.selectNodes( nodes );
165    }
166    
167    /** <p><code>selectNodes</code> performs the given XPath
168      * expression on the {@link List} of {@link Node} instances appending
169      * all the results together into a single list.</p>
170      *
171      * @param xpathFilterExpression is the XPath filter expression 
172      * to evaluate
173      * @param node is the Node on which to evalute the XPath
174      * @return the results of all the XPath evaluations as a single list
175      */
176    public static List selectNodes(String xpathFilterExpression, Node node) {
177        XPath xpath = createXPath( xpathFilterExpression );
178        return xpath.selectNodes( node );
179    }
180    
181    /** <p><code>sort</code> sorts the given List of Nodes
182      * using an XPath expression as a {@link Comparator}.
183      *
184      * @param list is the list of Nodes to sort
185      * @param xpathExpression is the XPath expression used for comparison
186      */
187    public static void sort( List list, String xpathExpression ) {
188        XPath xpath = createXPath( xpathExpression );
189        xpath.sort( list );
190    }
191    
192    /** <p><code>sort</code> sorts the given List of Nodes
193      * using an XPath expression as a {@link Comparator}
194      * and optionally removing duplicates.</p>
195      *
196      * @param list is the list of Nodes to sort
197      * @param xpathExpression is the XPath expression used for comparison
198      * @param distinct if true then duplicate values (using the sortXPath for 
199      *     comparisions) will be removed from the List
200      */
201    public static void sort( List list, String xpathExpression, boolean distinct ) {
202        XPath xpath = createXPath( xpathExpression );
203        xpath.sort( list, distinct );
204    }
205    
206    /** <p><code>parseText</code> parses the given text as an XML document
207      * and returns the newly created Document.
208      *
209      * @param text is the XML text to be parsed
210      * @return a newly parsed Document
211      * @throws DocumentException if the document could not be parsed
212      */
213    public static Document parseText(String text) throws DocumentException {
214        SAXReader reader = new SAXReader();
215        return reader.read( new StringReader( text ) );
216    }
217
218    /** <p>makeElement</p> a helper method which navigates from the
219      * given Document or Element node to some Element using the path 
220      * expression, creating any necessary elements along the way.
221      * For example the path <code>a/b/c</code> would get the first
222      * child &lt;a&gt; element, which would be created if it did not
223      * exist, then the next child &lt;b&gt; and so on until finally a
224      * &lt;c&gt; element is returned.
225      *
226      * @param source is the Element or Document to start navigating from
227      * @param is a simple path expression, seperated by '/' which denotes
228      * the path from the source to the resulting element such as a/b/c
229      *
230      * @return the first Element on the given path which either already
231      * existed on the path or were created by this method.
232      */
233    public static Element makeElement(Branch source, String path) {
234        StringTokenizer myEnum = new StringTokenizer( path, "/" );
235        Element parent;
236        if ( source instanceof Document ) {
237            Document document = (Document) source;
238            parent = document.getRootElement();
239            
240            // lets throw a NoSuchElementException 
241            // if we are given an empty path
242            String name = myEnum.nextToken();
243            if ( parent == null ) {
244                parent = document.addElement( name );
245            }
246        }
247        else {
248            parent = (Element) source;
249        }
250        Element element = null;
251        while ( myEnum.hasMoreTokens() ) {
252            String name = myEnum.nextToken();
253            if ( name.indexOf( ':' ) > 0 ) {
254                element = parent.element( parent.getQName( name ) );
255            }
256            else {
257                element = parent.element( name );
258            }
259            if ( element == null ) {
260                element = parent.addElement( name );
261            }
262            parent = element;
263        }
264        return element;
265    }
266}
267
268
269
270
271/*
272 * Redistribution and use of this software and associated documentation
273 * ("Software"), with or without modification, are permitted provided
274 * that the following conditions are met:
275 *
276 * 1. Redistributions of source code must retain copyright
277 *    statements and notices.  Redistributions must also contain a
278 *    copy of this document.
279 *
280 * 2. Redistributions in binary form must reproduce the
281 *    above copyright notice, this list of conditions and the
282 *    following disclaimer in the documentation and/or other
283 *    materials provided with the distribution.
284 *
285 * 3. The name "DOM4J" must not be used to endorse or promote
286 *    products derived from this Software without prior written
287 *    permission of MetaStuff, Ltd.  For written permission,
288 *    please contact dom4j-info@metastuff.com.
289 *
290 * 4. Products derived from this Software may not be called "DOM4J"
291 *    nor may "DOM4J" appear in their names without prior written
292 *    permission of MetaStuff, Ltd. DOM4J is a registered
293 *    trademark of MetaStuff, Ltd.
294 *
295 * 5. Due credit should be given to the DOM4J Project
296 *    (http://dom4j.org/).
297 *
298 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
299 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
300 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
301 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
302 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
303 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
304 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
305 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
306 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
307 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
308 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
309 * OF THE POSSIBILITY OF SUCH DAMAGE.
310 *
311 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
312 *
313 * $Id: DocumentHelper.java,v 1.11 2001/08/18 22:32:45 jstrachan Exp $
314 */