001/** 002 * 003 * Copyright © 2016 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.smackx.iot.discovery.element; 018 019import org.jivesoftware.smack.packet.NamedElement; 020import org.jivesoftware.smack.util.Objects; 021import org.jivesoftware.smack.util.StringUtils; 022import org.jivesoftware.smack.util.XmlStringBuilder; 023 024public class Tag implements NamedElement { 025 026 public enum Type { 027 str, 028 num 029 } 030 031 private final String name; 032 private final Type type; 033 private final String value; 034 035 public Tag(String name, Type type, String value) { 036 // TODO According to XEP-0347 § 5.2 names are case insensitive. Uppercase them all? 037 this.name = StringUtils.requireNotNullNorEmpty(name, "name must not be null nor empty"); 038 this.type = Objects.requireNonNull(type); 039 this.value = StringUtils.requireNotNullNorEmpty(value, "value must not be null nor empty"); 040 if (this.name.length() > 32) { 041 throw new IllegalArgumentException("Meta Tag names must not be longer then 32 characters (XEP-0347 § 5.2"); 042 } 043 if (this.type == Type.str && this.value.length() > 128) { 044 throw new IllegalArgumentException("Meta Tag string values must not be longer then 128 characters (XEP-0347 § 5.2"); 045 } 046 } 047 048 public String getName() { 049 return name; 050 } 051 052 public Type getType() { 053 return type; 054 } 055 056 public String getValue() { 057 return value; 058 } 059 060 @Override 061 public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { 062 XmlStringBuilder xml = new XmlStringBuilder(this); 063 xml.attribute("name", name); 064 xml.attribute("value", value); 065 xml.closeEmptyElement(); 066 return xml; 067 } 068 069 @Override 070 public String getElementName() { 071 return getType().toString(); 072 } 073 074 @Override 075 public String toString() { 076 return name + '(' + type + "):" + value; 077 } 078}