001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.xml;
016    
017    import com.liferay.portal.kernel.security.pacl.permission.PortalRuntimePermission;
018    import com.liferay.portal.kernel.util.GetterUtil;
019    import com.liferay.portal.kernel.util.PropsKeys;
020    import com.liferay.portal.kernel.util.PropsUtil;
021    
022    import java.io.File;
023    import java.io.InputStream;
024    import java.io.Reader;
025    
026    import java.net.MalformedURLException;
027    import java.net.URL;
028    
029    import java.util.List;
030    import java.util.Map;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     */
035    public class SAXReaderUtil {
036    
037            public static Attribute createAttribute(
038                    Element element, QName qName, String value) {
039    
040                    return getSAXReader().createAttribute(element, qName, value);
041            }
042    
043            public static Attribute createAttribute(
044                    Element element, String name, String value) {
045    
046                    return getSAXReader().createAttribute(element, name, value);
047            }
048    
049            public static Document createDocument() {
050                    return getSAXReader().createDocument();
051            }
052    
053            public static Document createDocument(Element rootElement) {
054                    return getSAXReader().createDocument(rootElement);
055            }
056    
057            public static Document createDocument(String encoding) {
058                    return getSAXReader().createDocument(encoding);
059            }
060    
061            public static Element createElement(QName qName) {
062                    return getSAXReader().createElement(qName);
063            }
064    
065            public static Element createElement(String name) {
066                    return getSAXReader().createElement(name);
067            }
068    
069            public static Entity createEntity(String name, String text) {
070                    return getSAXReader().createEntity(name, text);
071            }
072    
073            public static Namespace createNamespace(String uri) {
074                    return getSAXReader().createNamespace(uri);
075            }
076    
077            public static Namespace createNamespace(String prefix, String uri) {
078                    return getSAXReader().createNamespace(prefix, uri);
079            }
080    
081            public static ProcessingInstruction createProcessingInstruction(
082                    String target, Map<String, String> data) {
083    
084                    return getSAXReader().createProcessingInstruction(target, data);
085            }
086    
087            public static ProcessingInstruction createProcessingInstruction(
088                    String target, String data) {
089    
090                    return getSAXReader().createProcessingInstruction(target, data);
091            }
092    
093            public static QName createQName(String localName) {
094                    return getSAXReader().createQName(localName);
095            }
096    
097            public static QName createQName(String localName, Namespace namespace) {
098                    return getSAXReader().createQName(localName, namespace);
099            }
100    
101            public static Text createText(String text) {
102                    return getSAXReader().createText(text);
103            }
104    
105            public static XPath createXPath(String xPathExpression) {
106                    return getSAXReader().createXPath(xPathExpression);
107            }
108    
109            public static XPath createXPath(
110                    String xPathExpression, Map<String, String> namespaceContextMap) {
111    
112                    return getSAXReader().createXPath(xPathExpression, namespaceContextMap);
113            }
114    
115            public static XPath createXPath(
116                    String xPathExpression, String prefix, String namespace) {
117    
118                    return getSAXReader().createXPath(xPathExpression, prefix, namespace);
119            }
120    
121            public static SAXReader getSAXReader() {
122                    PortalRuntimePermission.checkGetBeanProperty(SAXReaderUtil.class);
123    
124                    if (!_XML_SECURITY_ENABLED) {
125                            return UnsecureSAXReaderUtil.getSAXReader();
126                    }
127    
128                    return _saxReader;
129            }
130    
131            /**
132             * @deprecated As of 6.2.0, renamed to {@link #getSAXReader}
133             */
134            public static SAXReader getSecureSAXReader() {
135                    return getSAXReader();
136            }
137    
138            /**
139             * @deprecated As of 6.2.0, renamed to {@link
140             * UnsecureSAXReaderUtil#getSAXReader()}
141             */
142            public static SAXReader getUnsecureSAXReader() {
143                    return UnsecureSAXReaderUtil.getSAXReader();
144            }
145    
146            public static Document read(File file) throws DocumentException {
147                    return getSAXReader().read(file);
148            }
149    
150            public static Document read(File file, boolean validate)
151                    throws DocumentException {
152    
153                    return getSAXReader().read(file, validate);
154            }
155    
156            public static Document read(InputStream is) throws DocumentException {
157                    return getSAXReader().read(is);
158            }
159    
160            public static Document read(InputStream is, boolean validate)
161                    throws DocumentException {
162    
163                    return getSAXReader().read(is, validate);
164            }
165    
166            public static Document read(Reader reader) throws DocumentException {
167                    return getSAXReader().read(reader);
168            }
169    
170            public static Document read(Reader reader, boolean validate)
171                    throws DocumentException {
172    
173                    return getSAXReader().read(reader, validate);
174            }
175    
176            public static Document read(String xml) throws DocumentException {
177                    return getSAXReader().read(xml);
178            }
179    
180            public static Document read(String xml, boolean validate)
181                    throws DocumentException {
182    
183                    return getSAXReader().read(xml, validate);
184            }
185    
186            public static Document read(String xml, XMLSchema xmlSchema)
187                    throws DocumentException {
188    
189                    return getSAXReader().read(xml, xmlSchema);
190            }
191    
192            public static Document read(URL url) throws DocumentException {
193                    return getSAXReader().read(url);
194            }
195    
196            public static Document read(URL url, boolean validate)
197                    throws DocumentException {
198    
199                    return getSAXReader().read(url, validate);
200            }
201    
202            public static Document readURL(String url)
203                    throws DocumentException, MalformedURLException {
204    
205                    return getSAXReader().readURL(url);
206            }
207    
208            public static Document readURL(String url, boolean validate)
209                    throws DocumentException, MalformedURLException {
210    
211                    return getSAXReader().readURL(url, validate);
212            }
213    
214            public static List<Node> selectNodes(
215                    String xPathFilterExpression, List<Node> nodes) {
216    
217                    return getSAXReader().selectNodes(xPathFilterExpression, nodes);
218            }
219    
220            public static List<Node> selectNodes(
221                    String xPathFilterExpression, Node node) {
222    
223                    return getSAXReader().selectNodes(xPathFilterExpression, node);
224            }
225    
226            public static void sort(List<Node> nodes, String xPathExpression) {
227                    getSAXReader().sort(nodes, xPathExpression);
228            }
229    
230            public static void sort(
231                    List<Node> nodes, String xPathExpression, boolean distinct) {
232    
233                    getSAXReader().sort(nodes, xPathExpression, distinct);
234            }
235    
236            public void setSAXReader(SAXReader saxReader) {
237                    PortalRuntimePermission.checkSetBeanProperty(getClass());
238    
239                    _saxReader = saxReader;
240            }
241    
242            /**
243             * @deprecated As of 6.2.0, renamed to {@link #setSAXReader}
244             */
245            public void setSecureSAXReader(SAXReader saxReader) {
246                    setSAXReader(saxReader);
247            }
248    
249            /**
250             * @deprecated As of 6.2.0, renamed to {@link
251             * UnsecureSAXReaderUtil#setSAXReader()}
252             */
253            public void setUnsecureSAXReader(SAXReader unsecureSAXReader) {
254                    UnsecureSAXReaderUtil unsecureSAXReaderUtil =
255                            new UnsecureSAXReaderUtil();
256    
257                    unsecureSAXReaderUtil.setSAXReader(unsecureSAXReader);
258            }
259    
260            private static final boolean _XML_SECURITY_ENABLED = GetterUtil.getBoolean(
261                    PropsUtil.get(PropsKeys.XML_SECURITY_ENABLED));
262    
263            private static SAXReader _saxReader;
264    
265    }