001/**
002 *
003 * Copyright 2015-2024 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.packet;
018
019import java.util.Collections;
020import java.util.LinkedHashMap;
021import java.util.List;
022import java.util.Map;
023
024import javax.xml.namespace.QName;
025
026import org.jivesoftware.smack.util.MultiMap;
027import org.jivesoftware.smack.util.Objects;
028import org.jivesoftware.smack.util.StringUtils;
029import org.jivesoftware.smack.util.XmlStringBuilder;
030
031/**
032 * An {@link ExtensionElement} modeling the often required and used XML features when using XMPP. It
033 * is therefore suitable for most use cases. Use
034 * {@link StandardExtensionElement#builder(String, String)} to build these elements.
035 * <p>
036 * Note the this is only meant as catch-all if no particular extension element provider is
037 * registered. Protocol implementations should prefer to model their own extension elements tailored
038 * to their use cases.
039 * </p>
040 *
041 * @since 4.2
042 * @author Florian Schmaus
043 */
044public final class StandardExtensionElement implements XmlElement {
045
046    private final String name;
047    private final String namespace;
048    private final Map<String, String> attributes;
049    private final String text;
050    private final MultiMap<QName, StandardExtensionElement> elements;
051
052    private XmlStringBuilder xmlCache;
053
054    /**
055     * Constructs a new extension element with the given name and namespace and nothing else.
056     * This is meant to construct extension elements used as simple flags in Stanzas.
057     *
058     * @param name the name of the extension element.
059     * @param namespace the namespace of the extension element.
060     */
061    public StandardExtensionElement(String name, String namespace) {
062        this(name, namespace, null, null, null);
063    }
064
065    private StandardExtensionElement(String name, String namespace, Map<String, String> attributes, String text,
066                    MultiMap<QName, StandardExtensionElement> elements) {
067        this.name = StringUtils.requireNotNullNorEmpty(name, "Name must not be null nor empty");
068        this.namespace = StringUtils.requireNotNullNorEmpty(namespace, "Namespace must not be null nor empty");
069        if (attributes == null) {
070            this.attributes = Collections.emptyMap();
071        }
072        else {
073            this.attributes = attributes;
074        }
075        this.text = text;
076        this.elements = elements;
077    }
078
079    @Override
080    public String getElementName() {
081        return name;
082    }
083
084    @Override
085    public String getNamespace() {
086        return namespace;
087    }
088
089    public String getAttributeValue(String attribute) {
090        return attributes.get(attribute);
091    }
092
093    public Map<String, String> getAttributes() {
094        return Collections.unmodifiableMap(attributes);
095    }
096
097    public StandardExtensionElement getFirstElement(String element, String namespace) {
098        if (elements == null) {
099            return null;
100        }
101        QName key = new QName(namespace, element);
102        return elements.getFirst(key);
103    }
104
105    public StandardExtensionElement getFirstElement(String element) {
106        return getFirstElement(element, namespace);
107    }
108
109    public List<StandardExtensionElement> getElements(String element, String namespace) {
110        if (elements == null) {
111            return null;
112        }
113        QName key = new QName(namespace, element);
114        return elements.getAll(key);
115    }
116
117    public List<StandardExtensionElement> getElements(String element) {
118        return getElements(element, namespace);
119    }
120
121    public List<StandardExtensionElement> getElements() {
122        if (elements == null) {
123            return Collections.emptyList();
124        }
125        return elements.values();
126    }
127
128    public String getText() {
129        return text;
130    }
131
132    @Override
133    public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
134        if (xmlCache != null) {
135            return xmlCache;
136        }
137        XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace);
138        for (Map.Entry<String, String> entry : attributes.entrySet()) {
139            xml.attribute(entry.getKey(), entry.getValue());
140        }
141        xml.rightAngleBracket();
142
143        if (text != null) {
144            xml.text(text);
145        }
146
147        if (elements != null) {
148            for (Map.Entry<QName, StandardExtensionElement> entry : elements.entrySet()) {
149                xml.append(entry.getValue().toXML(getNamespace()));
150            }
151        }
152
153        xml.closeElement(this);
154        xmlCache = xml;
155        return xml;
156    }
157
158    public static Builder builder(String name, String namespace) {
159        return new Builder(name, namespace);
160    }
161
162    public static final class Builder {
163        private final String name;
164        private final String namespace;
165
166        private Map<String, String> attributes;
167        private String text;
168        private MultiMap<QName, StandardExtensionElement> elements;
169
170        private Builder(String name, String namespace) {
171            this.name = name;
172            this.namespace = namespace;
173        }
174
175        public Builder addAttribute(String name, String value) {
176            StringUtils.requireNotNullNorEmpty(name, "Attribute name must be set");
177            Objects.requireNonNull(value, "Attribute value must be not null");
178            if (attributes == null) {
179                attributes = new LinkedHashMap<>();
180            }
181            attributes.put(name, value);
182            return this;
183        }
184
185        public Builder addAttributes(Map<String, String> attributes) {
186            if (this.attributes == null) {
187                this.attributes = new LinkedHashMap<>(attributes.size());
188            }
189            this.attributes.putAll(attributes);
190            return this;
191        }
192
193        public Builder setText(String text) {
194            this.text = Objects.requireNonNull(text, "Text must be not null");
195            return this;
196        }
197
198        public Builder addElement(StandardExtensionElement element) {
199            Objects.requireNonNull(element, "Element must not be null");
200            if (elements == null) {
201                elements = new MultiMap<>();
202            }
203
204            QName key = element.getQName();
205            elements.put(key, element);
206            return this;
207        }
208
209        public Builder addElement(String name, String textValue) {
210            StandardExtensionElement element = StandardExtensionElement.builder(name, this.namespace).setText(
211                            textValue).build();
212            return addElement(element);
213        }
214
215        public StandardExtensionElement build() {
216            return new StandardExtensionElement(name, namespace, attributes, text, elements);
217        }
218    }
219}