001/**
002 *
003 * Copyright 2020-2020 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.stax;
018
019import java.io.Reader;
020
021import javax.xml.stream.XMLInputFactory;
022import javax.xml.stream.XMLStreamException;
023import javax.xml.stream.XMLStreamReader;
024
025import org.jivesoftware.smack.xml.XmlPullParserException;
026import org.jivesoftware.smack.xml.XmlPullParserFactory;
027
028public class StaxXmlPullParserFactory implements XmlPullParserFactory {
029
030    private static final XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
031
032    static {
033        // XPP3 appears to coalescing hence we need to configure our StAX parser to also return all available text on
034        // getText().
035        xmlInputFactory.setProperty(XMLInputFactory.IS_COALESCING, true);
036        // Internal and external entity references are prohibited in XMPP (RFC 6120 ยง 11.1).
037        xmlInputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
038        xmlInputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false);
039        // We don't need to support DTDs in XMPP.
040        xmlInputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
041    }
042
043    @Override
044    public StaxXmlPullParser newXmlPullParser(Reader reader) throws XmlPullParserException {
045        XMLStreamReader xmlStreamReader;
046        try {
047            xmlStreamReader = xmlInputFactory.createXMLStreamReader(reader);
048        } catch (XMLStreamException e) {
049            throw new XmlPullParserException(e);
050        }
051        return new StaxXmlPullParser(xmlStreamReader);
052    }
053}