001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.xml;
029
030import java.util.Iterator;
031import java.util.List;
032
033import org.dom4j.Document;
034import org.dom4j.Element;
035import org.dom4j.Node;
036import org.dom4j.QName;
037
038/**
039 * Provides generic wrappers for XML library methods that do not support Java 5 generic types.<p>
040 *
041 * @since 7.5.1
042 */
043public final class CmsXmlGenericWrapper {
044
045    /**
046     * Prevents instances of this class from being generated.<p>
047     */
048    private CmsXmlGenericWrapper() {
049
050        // NOOP
051    }
052
053    /**
054     * Provides a type safe / generic wrapper for {@link Element#content()}.<p>
055     *
056     * @param element the element to get the content for
057     *
058     * @return type safe access to {@link Element#content()}.<p>
059     */
060    @SuppressWarnings("unchecked")
061    public static List<Node> content(Element element) {
062
063        return element.content();
064    }
065
066    /**
067     * Returns an element iterator.<p>
068     *
069     * @param element the element
070     * @param name the name
071     *
072     * @return the iterator
073     */
074    public static Iterable<Element> elementIterable(final Element element, final String name) {
075
076        return new Iterable<Element>() {
077
078            public Iterator<Element> iterator() {
079
080                return elementIterator(element, name);
081            }
082        };
083    }
084
085    /**
086     * Provides a type safe / generic wrapper for {@link Element#elementIterator(org.dom4j.QName)}.<p>
087     *
088     * @param element the element to iterate
089     *
090     * @return type safe access to {@link Element#elementIterator(org.dom4j.QName)}.<p>
091     */
092    @SuppressWarnings("unchecked")
093    public static Iterator<Element> elementIterator(Element element) {
094
095        return element.elementIterator();
096    }
097
098    /**
099     * Provides a type safe / generic wrapper for {@link Element#elementIterator(String)}.<p>
100     *
101     * @param element the element to iterate
102     * @param name the element name to match
103     *
104     * @return type safe access to {@link Element#elementIterator(String)}.<p>
105     */
106    @SuppressWarnings("unchecked")
107    public static Iterator<Element> elementIterator(Element element, String name) {
108
109        return element.elementIterator(name);
110    }
111
112    /**
113     * Provides a type safe / generic wrapper for {@link Element#elements()}.<p>
114     *
115     * @param element the element to iterate
116     *
117     * @return type safe access to {@link Element#elements()}.<p>
118     */
119    @SuppressWarnings("unchecked")
120    public static List<Element> elements(Element element) {
121
122        return element.elements();
123    }
124
125    /**
126     * Provides a type safe / generic wrapper for {@link Element#elements(org.dom4j.QName)}.<p>
127     *
128     * @param element the element to iterate
129     * @param name the element name to match
130     *
131     * @return type safe access to {@link Element#elements(org.dom4j.QName)}.<p>
132     */
133    @SuppressWarnings("unchecked")
134    public static List<Element> elements(Element element, QName name) {
135
136        return element.elements(name);
137    }
138
139    /**
140     * Provides a type safe / generic wrapper for {@link Element#elements(String)}.<p>
141     *
142     * @param element the element to iterate
143     * @param name the element name to match
144     *
145     * @return type safe access to {@link Element#elements(String)}.<p>
146     */
147    @SuppressWarnings("unchecked")
148    public static List<Element> elements(Element element, String name) {
149
150        return element.elements(name);
151    }
152
153    /**
154     * Provides a type safe / generic wrapper for {@link Document#selectNodes(String)}.<p>
155     *
156     * @param doc the document to select the nodes from
157     * @param xpathExpression the XPATH expression to select
158     *
159     * @return type safe access to {@link Document#selectNodes(String)}
160     */
161    @SuppressWarnings("unchecked")
162    public static List<Node> selectNodes(Document doc, String xpathExpression) {
163
164        return doc.selectNodes(xpathExpression);
165    }
166}