StaxXmlPullParserFactory.java

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

  18. import java.io.Reader;

  19. import javax.xml.stream.XMLInputFactory;
  20. import javax.xml.stream.XMLStreamException;
  21. import javax.xml.stream.XMLStreamReader;

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

  24. public class StaxXmlPullParserFactory implements XmlPullParserFactory {

  25.     private static final XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();

  26.     static {
  27.         // XPP3 appears to coalescing hence we need to configure our StAX parser to also return all available text on
  28.         // getText().
  29.         xmlInputFactory.setProperty(XMLInputFactory.IS_COALESCING, true);
  30.         // Internal and external entity references are prohibited in XMPP (RFC 6120 ยง 11.1).
  31.         xmlInputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
  32.         xmlInputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false);
  33.         // We don't need to support DTDs in XMPP.
  34.         xmlInputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
  35.     }

  36.     @Override
  37.     public StaxXmlPullParser newXmlPullParser(Reader reader) throws XmlPullParserException {
  38.         XMLStreamReader xmlStreamReader;
  39.         try {
  40.             xmlStreamReader = xmlInputFactory.createXMLStreamReader(reader);
  41.         } catch (XMLStreamException e) {
  42.             throw new XmlPullParserException(e);
  43.         }
  44.         return new StaxXmlPullParser(xmlStreamReader);
  45.     }
  46. }