SmackXmlParser.java

  1. /**
  2.  *
  3.  * Copyright 2019-2021 Florian Schmaus.
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smack.xml;

  18. import java.io.BufferedReader;
  19. import java.io.InputStream;
  20. import java.io.InputStreamReader;
  21. import java.io.Reader;
  22. import java.nio.charset.CharsetDecoder;
  23. import java.nio.charset.StandardCharsets;
  24. import java.util.Iterator;
  25. import java.util.ServiceLoader;

  26. public class SmackXmlParser {

  27.     private static final ServiceLoader<XmlPullParserFactory> xmlPullParserFactoryServiceLoader;

  28.     static {
  29.         xmlPullParserFactoryServiceLoader = ServiceLoader.load(XmlPullParserFactory.class);
  30.     }

  31.     private static XmlPullParserFactory xmlPullParserFactory;

  32.     public static XmlPullParserFactory getXmlPullParserFactory() {
  33.         final XmlPullParserFactory xmlPullParserFactory = SmackXmlParser.xmlPullParserFactory;
  34.         if (xmlPullParserFactory != null) {
  35.             return xmlPullParserFactory;
  36.         }

  37.         Iterator<XmlPullParserFactory> iterator = xmlPullParserFactoryServiceLoader.iterator();
  38.         if (!iterator.hasNext()) {
  39.             throw new IllegalStateException(
  40.                     "No XmlPullParserFactory registered with Service Provider Interface (SPI). Is smack-xmlparser-xpp3 or smack-xmlparser-stax in classpath?");
  41.         }
  42.         return iterator.next();
  43.     }

  44.     public static void setXmlPullParserFactory(XmlPullParserFactory xmlPullParserFactory) {
  45.         SmackXmlParser.xmlPullParserFactory = xmlPullParserFactory;
  46.     }

  47.     /**
  48.      * Creates a new XmlPullParser suitable for parsing XMPP. This means in particular that
  49.      * FEATURE_PROCESS_NAMESPACES is enabled.
  50.      * <p>
  51.      * Note that not all XmlPullParser implementations will return a String on
  52.      * <code>getText()</code> if the parser is on START_ELEMENT or END_ELEMENT. So you must not rely on this
  53.      * behavior when using the parser.
  54.      * </p>
  55.      *
  56.      * @param reader a reader to read the XML data from.
  57.      * @return A suitable XmlPullParser for XMPP parsing.
  58.      * @throws XmlPullParserException in case of an XmlPullParserException.
  59.      */
  60.     public static XmlPullParser newXmlParser(Reader reader) throws XmlPullParserException {
  61.         XmlPullParserFactory xmlPullParserFactory = getXmlPullParserFactory();
  62.         return xmlPullParserFactory.newXmlPullParser(reader);
  63.     }

  64.     public static XmlPullParser newXmlParser(InputStream inputStream) throws XmlPullParserException {
  65.         CharsetDecoder utf8Decoder = StandardCharsets.UTF_8.newDecoder();
  66.         InputStreamReader inputStreamReader = new InputStreamReader(inputStream, utf8Decoder);
  67.         Reader reader = new BufferedReader(inputStreamReader);
  68.         return newXmlParser(reader);
  69.     }
  70. }