001/** 002 * 003 * Copyright 2019 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 public static XmlPullParserFactory getXmlPullParserFactory() { 032 Iterator<XmlPullParserFactory> iterator = xmlPullParserFactoryServiceLoader.iterator(); 033 if (!iterator.hasNext()) { 034 throw new IllegalStateException( 035 "No XmlPullParserFactory registered with Service Provider Interface (SPI). Is smack-xmlparser-xpp3 or smack-xmlparser-stax in classpath?"); 036 } 037 return iterator.next(); 038 } 039 040 /** 041 * Creates a new XmlPullParser suitable for parsing XMPP. This means in particular that 042 * FEATURE_PROCESS_NAMESPACES is enabled. 043 * <p> 044 * Note that not all XmlPullParser implementations will return a String on 045 * <code>getText()</code> if the parser is on START_ELEMENT or END_ELEMENT. So you must not rely on this 046 * behavior when using the parser. 047 * </p> 048 * 049 * @param reader a reader to read the XML data from. 050 * @return A suitable XmlPullParser for XMPP parsing. 051 * @throws XmlPullParserException in case of an XmlPullParserException. 052 */ 053 public static XmlPullParser newXmlParser(Reader reader) throws XmlPullParserException { 054 XmlPullParserFactory xmlPullParserFactory = getXmlPullParserFactory(); 055 return xmlPullParserFactory.newXmlPullParser(reader); 056 } 057 058}