Xpp3XmlPullParserFactory.java

  1. /**
  2.  *
  3.  * Copyright 2019 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.xpp3;

  18. import java.io.Reader;
  19. import java.util.logging.Level;
  20. import java.util.logging.Logger;

  21. import org.jivesoftware.smack.xml.XmlPullParserException;
  22. import org.jivesoftware.smack.xml.XmlPullParserFactory;

  23. public class Xpp3XmlPullParserFactory implements XmlPullParserFactory {

  24.     private static final Logger LOGGER = Logger.getLogger(Xpp3XmlPullParserFactory.class.getName());

  25.     private static final org.xmlpull.v1.XmlPullParserFactory XPP3_XML_PULL_PARSER_FACTORY;

  26.     public static final String FEATURE_XML_ROUNDTRIP = "http://xmlpull.org/v1/doc/features.html#xml-roundtrip";

  27.     /**
  28.      * True if the XmlPullParser supports the XML_ROUNDTRIP feature.
  29.      */
  30.     public static final boolean XML_PULL_PARSER_SUPPORTS_ROUNDTRIP;

  31.     static {
  32.         org.xmlpull.v1.XmlPullParser xmlPullParser;
  33.         boolean roundtrip = false;
  34.         try {
  35.             XPP3_XML_PULL_PARSER_FACTORY = org.xmlpull.v1.XmlPullParserFactory.newInstance();
  36.             xmlPullParser = XPP3_XML_PULL_PARSER_FACTORY.newPullParser();
  37.             try {
  38.                 xmlPullParser.setFeature(FEATURE_XML_ROUNDTRIP, true);
  39.                 // We could successfully set the feature
  40.                 roundtrip = true;
  41.             } catch (org.xmlpull.v1.XmlPullParserException e) {
  42.                 // Doesn't matter if FEATURE_XML_ROUNDTRIP isn't available
  43.                 LOGGER.log(Level.FINEST, "XmlPullParser does not support XML_ROUNDTRIP", e);
  44.             }
  45.         }
  46.         catch (org.xmlpull.v1.XmlPullParserException e) {
  47.             // Something really bad happened
  48.             throw new AssertionError(e);
  49.         }
  50.         XML_PULL_PARSER_SUPPORTS_ROUNDTRIP = roundtrip;
  51.     }

  52.     @Override
  53.     public Xpp3XmlPullParser newXmlPullParser(Reader reader) throws XmlPullParserException {
  54.         org.xmlpull.v1.XmlPullParser xpp3XmlPullParser;
  55.         try {
  56.             xpp3XmlPullParser = XPP3_XML_PULL_PARSER_FACTORY.newPullParser();
  57.             xpp3XmlPullParser.setFeature(org.xmlpull.v1.XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
  58.             xpp3XmlPullParser.setInput(reader);
  59.         } catch (org.xmlpull.v1.XmlPullParserException e) {
  60.             throw new XmlPullParserException(e);
  61.         }

  62.         if (XML_PULL_PARSER_SUPPORTS_ROUNDTRIP) {
  63.             try {
  64.                 xpp3XmlPullParser.setFeature(FEATURE_XML_ROUNDTRIP, true);
  65.             }
  66.             catch (org.xmlpull.v1.XmlPullParserException e) {
  67.                 LOGGER.log(Level.SEVERE,
  68.                                 "XmlPullParser does not support XML_ROUNDTRIP, although it was first determined to be supported",
  69.                                 e);
  70.             }
  71.         }

  72.         return new Xpp3XmlPullParser(xpp3XmlPullParser);
  73.     }

  74. }