ExternalElement.java

  1. /**
  2.  *
  3.  * Copyright 2019 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.message_fastening.element;

  18. import org.jivesoftware.smack.packet.NamedElement;
  19. import org.jivesoftware.smack.packet.XmlEnvironment;
  20. import org.jivesoftware.smack.util.XmlStringBuilder;

  21. /**
  22.  * Child element of {@link FasteningElement}.
  23.  * Reference to a top level element in the stanza that contains the {@link FasteningElement}.
  24.  */
  25. public class ExternalElement implements NamedElement {

  26.     public static final String ELEMENT = "external";
  27.     public static final String ATTR_NAME = "name";
  28.     public static final String ATTR_ELEMENT_NAMESPACE = "element-namespace";

  29.     private final String name;
  30.     private final String elementNamespace;

  31.     /**
  32.      * Create a new {@link ExternalElement} that references a top level element with the given name.
  33.      *
  34.      * @param name name of the top level element
  35.      */
  36.     public ExternalElement(String name) {
  37.         this(name, null);
  38.     }

  39.     /**
  40.      * Create a new {@link ExternalElement} that references a top level element with the given name and namespace.
  41.      *
  42.      * @param name name of the top level element
  43.      * @param elementNamespace namespace of the top level element
  44.      */
  45.     public ExternalElement(String name, String elementNamespace) {
  46.         this.name = name;
  47.         this.elementNamespace = elementNamespace;
  48.     }

  49.     @Override
  50.     public String getElementName() {
  51.         return ELEMENT;
  52.     }

  53.     @Override
  54.     public XmlStringBuilder toXML(XmlEnvironment xmlEnvironment) {
  55.         XmlStringBuilder xml = new XmlStringBuilder(this);
  56.         xml.attribute(ATTR_NAME, getName());
  57.         xml.optAttribute(ATTR_ELEMENT_NAMESPACE, getElementNamespace());
  58.         return xml.closeEmptyElement();
  59.     }

  60.     /**
  61.      * Name of the referenced top level element, eg. 'body'.
  62.      * @return element name
  63.      */
  64.     public String getName() {
  65.         return name;
  66.     }

  67.     /**
  68.      * Namespace of the referenced top level element, eg. 'urn:example:lik'.
  69.      * @return element namespace
  70.      */
  71.     public String getElementNamespace() {
  72.         return elementNamespace;
  73.     }
  74. }