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.Reader;
020import java.util.Iterator;
021import java.util.ServiceLoader;
022
023public class SmackXmlParser {
024
025    private static final ServiceLoader<XmlPullParserFactory> xmlPullParserFactoryServiceLoader;
026
027    static {
028        xmlPullParserFactoryServiceLoader = ServiceLoader.load(XmlPullParserFactory.class);
029    }
030
031    private static XmlPullParserFactory xmlPullParserFactory;
032
033    public static XmlPullParserFactory getXmlPullParserFactory() {
034        final XmlPullParserFactory xmlPullParserFactory = SmackXmlParser.xmlPullParserFactory;
035        if (xmlPullParserFactory != null) {
036            return xmlPullParserFactory;
037        }
038
039        Iterator<XmlPullParserFactory> iterator = xmlPullParserFactoryServiceLoader.iterator();
040        if (!iterator.hasNext()) {
041            throw new IllegalStateException(
042                    "No XmlPullParserFactory registered with Service Provider Interface (SPI). Is smack-xmlparser-xpp3 or smack-xmlparser-stax in classpath?");
043        }
044        return iterator.next();
045    }
046
047    public static void setXmlPullParserFactory(XmlPullParserFactory xmlPullParserFactory) {
048        SmackXmlParser.xmlPullParserFactory = xmlPullParserFactory;
049    }
050
051    /**
052     * Creates a new XmlPullParser suitable for parsing XMPP. This means in particular that
053     * FEATURE_PROCESS_NAMESPACES is enabled.
054     * <p>
055     * Note that not all XmlPullParser implementations will return a String on
056     * <code>getText()</code> if the parser is on START_ELEMENT or END_ELEMENT. So you must not rely on this
057     * behavior when using the parser.
058     * </p>
059     *
060     * @param reader a reader to read the XML data from.
061     * @return A suitable XmlPullParser for XMPP parsing.
062     * @throws XmlPullParserException in case of an XmlPullParserException.
063     */
064    public static XmlPullParser newXmlParser(Reader reader) throws XmlPullParserException {
065        XmlPullParserFactory xmlPullParserFactory = getXmlPullParserFactory();
066        return xmlPullParserFactory.newXmlPullParser(reader);
067    }
068
069}