001/** 002 * 003 * Copyright 2018 Paul Schaub 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.smackx.reference.provider; 018 019import java.io.IOException; 020import java.net.URI; 021import java.net.URISyntaxException; 022 023import org.jivesoftware.smack.packet.XmlElement; 024import org.jivesoftware.smack.packet.XmlEnvironment; 025import org.jivesoftware.smack.parsing.SmackParsingException; 026import org.jivesoftware.smack.provider.ExtensionElementProvider; 027import org.jivesoftware.smack.provider.ProviderManager; 028import org.jivesoftware.smack.util.ParserUtils; 029import org.jivesoftware.smack.xml.XmlPullParser; 030import org.jivesoftware.smack.xml.XmlPullParserException; 031 032import org.jivesoftware.smackx.reference.element.ReferenceElement; 033 034public class ReferenceProvider extends ExtensionElementProvider<ReferenceElement> { 035 036 public static final ReferenceProvider TEST_PROVIDER = new ReferenceProvider(); 037 038 @Override 039 public ReferenceElement parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException { 040 Integer begin = ParserUtils.getIntegerAttribute(parser, ReferenceElement.ATTR_BEGIN); 041 Integer end = ParserUtils.getIntegerAttribute(parser, ReferenceElement.ATTR_END); 042 String typeString = parser.getAttributeValue(null, ReferenceElement.ATTR_TYPE); 043 ReferenceElement.Type type = ReferenceElement.Type.valueOf(typeString); 044 String anchor = parser.getAttributeValue(null, ReferenceElement.ATTR_ANCHOR); 045 String uriString = parser.getAttributeValue(null, ReferenceElement.ATTR_URI); 046 URI uri; 047 try { 048 uri = uriString != null ? new URI(uriString) : null; 049 } 050 catch (URISyntaxException e) { 051 // TODO: Should be SmackParseException and probably be factored into ParserUtils. 052 throw new IOException(e); 053 } 054 XmlElement child = null; 055 outerloop: while (true) { 056 XmlPullParser.Event eventType = parser.next(); 057 if (eventType == XmlPullParser.Event.START_ELEMENT) { 058 String elementName = parser.getName(); 059 String namespace = parser.getNamespace(); 060 ExtensionElementProvider<?> provider = ProviderManager.getExtensionProvider(elementName, namespace); 061 if (provider != null) { 062 child = provider.parse(parser); 063 } 064 } 065 else if (eventType == XmlPullParser.Event.END_ELEMENT) { 066 break outerloop; 067 } 068 } 069 070 return new ReferenceElement(begin, end, type, anchor, uri, child); 071 } 072}