ReferenceProvider.java

  1. /**
  2.  *
  3.  * Copyright 2018 Paul Schaub
  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.smackx.reference.provider;

  18. import java.io.IOException;
  19. import java.net.URI;
  20. import java.net.URISyntaxException;

  21. import org.jivesoftware.smack.packet.XmlElement;
  22. import org.jivesoftware.smack.packet.XmlEnvironment;
  23. import org.jivesoftware.smack.parsing.SmackParsingException;
  24. import org.jivesoftware.smack.provider.ExtensionElementProvider;
  25. import org.jivesoftware.smack.provider.ProviderManager;
  26. import org.jivesoftware.smack.util.ParserUtils;
  27. import org.jivesoftware.smack.xml.XmlPullParser;
  28. import org.jivesoftware.smack.xml.XmlPullParserException;

  29. import org.jivesoftware.smackx.reference.element.ReferenceElement;

  30. public class ReferenceProvider extends ExtensionElementProvider<ReferenceElement> {

  31.     public static final ReferenceProvider TEST_PROVIDER = new ReferenceProvider();

  32.     @Override
  33.     public ReferenceElement parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
  34.         Integer begin = ParserUtils.getIntegerAttribute(parser, ReferenceElement.ATTR_BEGIN);
  35.         Integer end =   ParserUtils.getIntegerAttribute(parser, ReferenceElement.ATTR_END);
  36.         String typeString = parser.getAttributeValue(null, ReferenceElement.ATTR_TYPE);
  37.         ReferenceElement.Type type = ReferenceElement.Type.valueOf(typeString);
  38.         String anchor = parser.getAttributeValue(null, ReferenceElement.ATTR_ANCHOR);
  39.         String uriString = parser.getAttributeValue(null, ReferenceElement.ATTR_URI);
  40.         URI uri;
  41.         try {
  42.             uri = uriString != null ? new URI(uriString) : null;
  43.         }
  44.         catch (URISyntaxException e) {
  45.             // TODO: Should be SmackParseException and probably be factored into ParserUtils.
  46.             throw new IOException(e);
  47.         }
  48.         XmlElement child = null;
  49.         outerloop: while (true) {
  50.             XmlPullParser.Event eventType = parser.next();
  51.             if (eventType == XmlPullParser.Event.START_ELEMENT) {
  52.                 String elementName = parser.getName();
  53.                 String namespace = parser.getNamespace();
  54.                 ExtensionElementProvider<?> provider = ProviderManager.getExtensionProvider(elementName, namespace);
  55.                 if (provider != null) {
  56.                     child = provider.parse(parser);
  57.                 }
  58.             }
  59.             else if (eventType == XmlPullParser.Event.END_ELEMENT) {
  60.                 break outerloop;
  61.             }
  62.         }

  63.         return new ReferenceElement(begin, end, type, anchor, uri, child);
  64.     }
  65. }