001/**
002 *
003 * Copyright 2019-2021 Florian Schmaus.
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smack.xml;
018
019import java.io.BufferedReader;
020import java.io.InputStream;
021import java.io.InputStreamReader;
022import java.io.Reader;
023import java.nio.charset.CharsetDecoder;
024import java.nio.charset.StandardCharsets;
025import java.util.Iterator;
026import java.util.ServiceLoader;
027
028public class SmackXmlParser {
029
030    private static final ServiceLoader<XmlPullParserFactory> xmlPullParserFactoryServiceLoader;
031
032    static {
033        xmlPullParserFactoryServiceLoader = ServiceLoader.load(XmlPullParserFactory.class);
034    }
035
036    private static XmlPullParserFactory xmlPullParserFactory;
037
038    public static XmlPullParserFactory getXmlPullParserFactory() {
039        final XmlPullParserFactory xmlPullParserFactory = SmackXmlParser.xmlPullParserFactory;
040        if (xmlPullParserFactory != null) {
041            return xmlPullParserFactory;
042        }
043
044        Iterator<XmlPullParserFactory> iterator = xmlPullParserFactoryServiceLoader.iterator();
045        if (!iterator.hasNext()) {
046            throw new IllegalStateException(
047                    "No XmlPullParserFactory registered with Service Provider Interface (SPI). Is smack-xmlparser-xpp3 or smack-xmlparser-stax in classpath?");
048        }
049        return iterator.next();
050    }
051
052    public static void setXmlPullParserFactory(XmlPullParserFactory xmlPullParserFactory) {
053        SmackXmlParser.xmlPullParserFactory = xmlPullParserFactory;
054    }
055
056    /**
057     * Creates a new XmlPullParser suitable for parsing XMPP. This means in particular that
058     * FEATURE_PROCESS_NAMESPACES is enabled.
059     * <p>
060     * Note that not all XmlPullParser implementations will return a String on
061     * <code>getText()</code> if the parser is on START_ELEMENT or END_ELEMENT. So you must not rely on this
062     * behavior when using the parser.
063     * </p>
064     *
065     * @param reader a reader to read the XML data from.
066     * @return A suitable XmlPullParser for XMPP parsing.
067     * @throws XmlPullParserException in case of an XmlPullParserException.
068     */
069    public static XmlPullParser newXmlParser(Reader reader) throws XmlPullParserException {
070        XmlPullParserFactory xmlPullParserFactory = getXmlPullParserFactory();
071        return xmlPullParserFactory.newXmlPullParser(reader);
072    }
073
074    public static XmlPullParser newXmlParser(InputStream inputStream) throws XmlPullParserException {
075        CharsetDecoder utf8Decoder = StandardCharsets.UTF_8.newDecoder();
076        InputStreamReader inputStreamReader = new InputStreamReader(inputStream, utf8Decoder);
077        Reader reader = new BufferedReader(inputStreamReader);
078        return newXmlParser(reader);
079    }
080}