2019-08-31 14:17:13 +02:00
|
|
|
/**
|
|
|
|
*
|
2019-08-31 17:07:17 +02:00
|
|
|
* Copyright 2017 Fernando Ramirez, 2019 Paul Schaub
|
2019-08-31 14:17:13 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
package org.jivesoftware.smackx.avatar.element;
|
|
|
|
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
import org.jivesoftware.smack.packet.ExtensionElement;
|
|
|
|
import org.jivesoftware.smack.packet.XmlEnvironment;
|
|
|
|
import org.jivesoftware.smack.util.XmlStringBuilder;
|
|
|
|
import org.jivesoftware.smackx.avatar.MetadataInfo;
|
|
|
|
import org.jivesoftware.smackx.avatar.MetadataPointer;
|
|
|
|
import org.jivesoftware.smackx.avatar.UserAvatarManager;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Metadata extension element class.
|
|
|
|
* This class contains metadata about published avatars.
|
|
|
|
*
|
|
|
|
* @author Fernando Ramirez
|
2019-08-31 17:07:17 +02:00
|
|
|
* @author Paul Schaub
|
2019-08-31 14:17:13 +02:00
|
|
|
* @see <a href="https://xmpp.org/extensions/xep-0084.html">XEP-0084: User
|
|
|
|
* Avatar</a>
|
|
|
|
*/
|
|
|
|
public class MetadataExtension implements ExtensionElement {
|
|
|
|
|
|
|
|
public static final String ELEMENT = "metadata";
|
|
|
|
public static final String NAMESPACE = UserAvatarManager.METADATA_NAMESPACE;
|
|
|
|
|
|
|
|
private final List<MetadataInfo> infos;
|
|
|
|
private final List<MetadataPointer> pointers;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Metadata Extension constructor.
|
|
|
|
*
|
2019-08-31 15:53:15 +02:00
|
|
|
* @param infos list of {@link MetadataInfo} elements.
|
2019-08-31 14:17:13 +02:00
|
|
|
*/
|
|
|
|
public MetadataExtension(List<MetadataInfo> infos) {
|
|
|
|
this(infos, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Metadata Extension constructor.
|
|
|
|
*
|
2019-08-31 15:53:15 +02:00
|
|
|
* @param infos list of {@link MetadataInfo} elements
|
|
|
|
* @param pointers optional list of {@link MetadataPointer} elements
|
2019-08-31 14:17:13 +02:00
|
|
|
*/
|
|
|
|
public MetadataExtension(List<MetadataInfo> infos, List<MetadataPointer> pointers) {
|
|
|
|
this.infos = infos;
|
|
|
|
this.pointers = pointers;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the info elements list.
|
|
|
|
*
|
|
|
|
* @return the info elements list
|
|
|
|
*/
|
|
|
|
public List<MetadataInfo> getInfoElements() {
|
|
|
|
return Collections.unmodifiableList(infos);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the pointer elements list.
|
|
|
|
*
|
|
|
|
* @return the pointer elements list
|
|
|
|
*/
|
|
|
|
public List<MetadataPointer> getPointerElements() {
|
|
|
|
return (pointers == null) ? null : Collections.unmodifiableList(pointers);
|
|
|
|
}
|
|
|
|
|
2019-08-31 15:53:15 +02:00
|
|
|
/**
|
|
|
|
* Return true, if this {@link MetadataExtension} is to be interpreted as Avatar unpublishing.
|
|
|
|
*
|
|
|
|
* @return true if unpublishing, false otherwise
|
|
|
|
*/
|
|
|
|
public boolean isDisablingPublishing() {
|
|
|
|
return getInfoElements().isEmpty();
|
|
|
|
}
|
|
|
|
|
2019-08-31 14:17:13 +02:00
|
|
|
@Override
|
|
|
|
public String getElementName() {
|
|
|
|
return ELEMENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getNamespace() {
|
|
|
|
return NAMESPACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-08-31 16:59:11 +02:00
|
|
|
public XmlStringBuilder toXML(XmlEnvironment xmlEnvironment) {
|
2019-08-31 16:48:35 +02:00
|
|
|
XmlStringBuilder xml = new XmlStringBuilder(this, xmlEnvironment);
|
2019-08-31 14:17:13 +02:00
|
|
|
appendInfoElements(xml);
|
|
|
|
appendPointerElements(xml);
|
|
|
|
closeElement(xml);
|
|
|
|
return xml;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void appendInfoElements(XmlStringBuilder xml) {
|
2019-08-31 17:07:17 +02:00
|
|
|
if (infos == null) {
|
|
|
|
return;
|
|
|
|
}
|
2019-08-31 14:17:13 +02:00
|
|
|
|
2019-08-31 17:07:17 +02:00
|
|
|
xml.rightAngleBracket();
|
2019-08-31 14:17:13 +02:00
|
|
|
|
2019-08-31 17:07:17 +02:00
|
|
|
for (MetadataInfo info : infos) {
|
|
|
|
xml.halfOpenElement("info");
|
|
|
|
xml.attribute("id", info.getId());
|
|
|
|
xml.attribute("bytes", info.getBytes().longValue());
|
|
|
|
xml.attribute("type", info.getType());
|
|
|
|
xml.optAttribute("url", info.getUrl());
|
2019-08-31 14:17:13 +02:00
|
|
|
|
2019-08-31 17:07:17 +02:00
|
|
|
if (info.getHeight().nativeRepresentation() > 0) {
|
|
|
|
xml.attribute("height", info.getHeight().nativeRepresentation());
|
|
|
|
}
|
2019-08-31 14:17:13 +02:00
|
|
|
|
2019-08-31 17:07:17 +02:00
|
|
|
if (info.getWidth().nativeRepresentation() > 0) {
|
|
|
|
xml.attribute("width", info.getWidth().nativeRepresentation());
|
2019-08-31 14:17:13 +02:00
|
|
|
}
|
2019-08-31 17:07:17 +02:00
|
|
|
|
|
|
|
xml.closeEmptyElement();
|
2019-08-31 14:17:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void appendPointerElements(XmlStringBuilder xml) {
|
2019-08-31 17:07:17 +02:00
|
|
|
if (pointers == null) {
|
|
|
|
return;
|
|
|
|
}
|
2019-08-31 14:17:13 +02:00
|
|
|
|
2019-08-31 17:07:17 +02:00
|
|
|
for (MetadataPointer pointer : pointers) {
|
|
|
|
xml.openElement("pointer");
|
|
|
|
xml.halfOpenElement("x");
|
2019-08-31 14:17:13 +02:00
|
|
|
|
2019-08-31 17:07:17 +02:00
|
|
|
String namespace = pointer.getNamespace();
|
|
|
|
if (namespace != null) {
|
|
|
|
xml.xmlnsAttribute(namespace);
|
|
|
|
}
|
2019-08-31 14:17:13 +02:00
|
|
|
|
2019-08-31 17:07:17 +02:00
|
|
|
xml.rightAngleBracket();
|
2019-08-31 14:17:13 +02:00
|
|
|
|
2019-08-31 17:07:17 +02:00
|
|
|
Map<String, Object> fields = pointer.getFields();
|
|
|
|
if (fields != null) {
|
|
|
|
for (Map.Entry<String, Object> pair : fields.entrySet()) {
|
|
|
|
xml.escapedElement(pair.getKey(), String.valueOf(pair.getValue()));
|
2019-08-31 14:17:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-31 17:07:17 +02:00
|
|
|
xml.closeElement("x");
|
|
|
|
xml.closeElement("pointer");
|
2019-08-31 14:17:13 +02:00
|
|
|
}
|
2019-08-31 17:07:17 +02:00
|
|
|
|
2019-08-31 14:17:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void closeElement(XmlStringBuilder xml) {
|
|
|
|
if (infos != null || pointers != null) {
|
|
|
|
xml.closeElement(this);
|
|
|
|
} else {
|
|
|
|
xml.closeEmptyElement();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|