1 31 32 package org.opencms.xml.types; 33 34 import org.opencms.file.CmsObject; 35 import org.opencms.main.CmsIllegalArgumentException; 36 import org.opencms.util.CmsStringUtil; 37 import org.opencms.widgets.I_CmsWidgetParameter; 38 import org.opencms.xml.I_CmsXmlDocument; 39 40 import java.util.Locale ; 41 import java.util.regex.Pattern ; 42 43 import org.dom4j.Element; 44 45 54 public class CmsXmlBooleanValue extends A_CmsXmlValueTextBase { 55 56 57 public static final String TYPE_NAME = "OpenCmsBoolean"; 58 59 60 public static final String TYPE_RULE = "true|false|1|0"; 61 62 63 private static final Pattern TYPE_PATTERN = Pattern.compile(TYPE_RULE); 64 65 66 private boolean m_boolean; 67 68 71 public CmsXmlBooleanValue() { 72 73 } 75 76 84 public CmsXmlBooleanValue(I_CmsXmlDocument document, Element element, Locale locale, I_CmsXmlSchemaType type) { 85 86 super(document, element, locale, type); 87 m_boolean = getBooleanValue(m_stringValue); 88 } 89 90 97 public CmsXmlBooleanValue(String name, String minOccurs, String maxOccurs) { 98 99 super(name, minOccurs, maxOccurs); 100 } 101 102 110 public static boolean getBooleanValue(CmsObject cms, I_CmsWidgetParameter value) { 111 112 boolean result; 113 if (value instanceof CmsXmlBooleanValue) { 114 result = ((CmsXmlBooleanValue)value).getBooleanValue(); 116 } else { 117 result = getBooleanValue(value.getStringValue(cms)); 119 } 120 return result; 121 } 122 123 131 private static boolean getBooleanValue(String value) { 132 133 if ("1".equals(value)) { 134 return true; 136 } 137 return Boolean.valueOf(value).booleanValue(); 138 } 139 140 143 public I_CmsXmlContentValue createValue(I_CmsXmlDocument document, Element element, Locale locale) { 144 145 return new CmsXmlBooleanValue(document, element, locale, this); 146 } 147 148 153 public boolean getBooleanValue() { 154 155 return m_boolean; 156 } 157 158 161 public String getDefault(Locale locale) { 162 163 if (m_defaultValue != null) { 164 return m_defaultValue; 165 } 166 return CmsStringUtil.FALSE; 167 } 168 169 172 public String getSchemaDefinition() { 173 174 return "<xsd:simpleType name=\"" + TYPE_NAME + "\"><xsd:restriction base=\"xsd:boolean\" /></xsd:simpleType>"; 175 } 176 177 180 public String getTypeName() { 181 182 return TYPE_NAME; 183 } 184 185 188 public I_CmsXmlSchemaType newInstance(String name, String minOccurs, String maxOccurs) { 189 190 return new CmsXmlBooleanValue(name, minOccurs, maxOccurs); 191 } 192 193 196 public void setStringValue(CmsObject cms, String value) throws CmsIllegalArgumentException { 197 198 super.setStringValue(cms, value); 199 m_boolean = getBooleanValue(m_stringValue); 200 } 201 202 205 public boolean validateValue(String value) { 206 207 return TYPE_PATTERN.matcher(value).matches(); 208 } 209 } | Popular Tags |