001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software GmbH & Co. KG, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.xml.types; 029 030import org.opencms.file.CmsObject; 031import org.opencms.main.CmsIllegalArgumentException; 032import org.opencms.util.CmsStringUtil; 033import org.opencms.widgets.I_CmsWidgetParameter; 034import org.opencms.xml.I_CmsXmlDocument; 035 036import java.util.Locale; 037import java.util.regex.Pattern; 038 039import org.dom4j.Element; 040 041/** 042 * Describes the XML content type "OpenCmsBoolean".<p> 043 * 044 * @since 6.0.0 045 */ 046public class CmsXmlBooleanValue extends A_CmsXmlValueTextBase { 047 048 /** The name of this type as used in the XML schema. */ 049 public static final String TYPE_NAME = "OpenCmsBoolean"; 050 051 /** The validation rule used for this schema type. */ 052 public static final String TYPE_RULE = "true|false|1|0"; 053 054 /** Pre-compiled regular expression pattern for this rule. */ 055 private static final Pattern TYPE_PATTERN = Pattern.compile(TYPE_RULE); 056 057 /** The boolean value of the element node. */ 058 private boolean m_boolean; 059 060 /** 061 * Creates a new, empty schema type descriptor of type "OpenCmsBoolean".<p> 062 */ 063 public CmsXmlBooleanValue() { 064 065 // empty constructor is required for class registration 066 } 067 068 /** 069 * Creates a new XML content value of type "OpenCmsBoolean".<p> 070 * 071 * @param document the XML content instance this value belongs to 072 * @param element the XML element that contains this value 073 * @param locale the locale this value is created for 074 * @param type the type instance to create the value for 075 */ 076 public CmsXmlBooleanValue(I_CmsXmlDocument document, Element element, Locale locale, I_CmsXmlSchemaType type) { 077 078 super(document, element, locale, type); 079 m_boolean = getBooleanValue(m_stringValue); 080 } 081 082 /** 083 * Creates a new schema type descriptor for the type "OpenCmsBoolean".<p> 084 * 085 * @param name the name of the XML node containing the value according to the XML schema 086 * @param minOccurs minimum number of occurrences of this type according to the XML schema 087 * @param maxOccurs maximum number of occurrences of this type according to the XML schema 088 */ 089 public CmsXmlBooleanValue(String name, String minOccurs, String maxOccurs) { 090 091 super(name, minOccurs, maxOccurs); 092 } 093 094 /** 095 * Returns the boolean value of the given widget parameter.<p> 096 * 097 * @param cms an initialized instance of a CmsObject 098 * @param value the XML content value to get the boolean value of 099 * 100 * @return the boolean value of the given widget parameter 101 */ 102 public static boolean getBooleanValue(CmsObject cms, I_CmsWidgetParameter value) { 103 104 boolean result; 105 if (value instanceof CmsXmlBooleanValue) { 106 // this is a "native" boolean type 107 result = ((CmsXmlBooleanValue)value).getBooleanValue(); 108 } else { 109 // get the boolean value from the String value 110 result = getBooleanValue(value.getStringValue(cms)); 111 } 112 return result; 113 } 114 115 /** 116 * Special boolean value generation method since XML schema allows for 117 * "1" as possible value for <code>true</code>, while Java only allows <code>"true"</code>. 118 * 119 * @param value the String to get the boolean value for 120 * 121 * @return the boolean value of the String according to the XML schema rules 122 */ 123 private static boolean getBooleanValue(String value) { 124 125 if ("1".equals(value)) { 126 // XML schema allows for "1" as value for "true" 127 return true; 128 } 129 return Boolean.valueOf(value).booleanValue(); 130 } 131 132 /** 133 * @see org.opencms.xml.types.A_CmsXmlContentValue#createValue(I_CmsXmlDocument, org.dom4j.Element, Locale) 134 */ 135 public I_CmsXmlContentValue createValue(I_CmsXmlDocument document, Element element, Locale locale) { 136 137 return new CmsXmlBooleanValue(document, element, locale, this); 138 } 139 140 /** 141 * Returns the boolean value as a boolean type.<p> 142 * 143 * @return the boolean value as a boolean type 144 */ 145 public boolean getBooleanValue() { 146 147 return m_boolean; 148 } 149 150 /** 151 * @see org.opencms.xml.types.A_CmsXmlContentValue#getDefault(Locale) 152 */ 153 @Override 154 public String getDefault(Locale locale) { 155 156 if (m_defaultValue != null) { 157 return m_defaultValue; 158 } 159 return CmsStringUtil.FALSE; 160 } 161 162 /** 163 * @see org.opencms.xml.types.I_CmsXmlSchemaType#getSchemaDefinition() 164 */ 165 public String getSchemaDefinition() { 166 167 return "<xsd:simpleType name=\"" + TYPE_NAME + "\"><xsd:restriction base=\"xsd:boolean\" /></xsd:simpleType>"; 168 } 169 170 /** 171 * @see org.opencms.xml.types.A_CmsXmlContentValue#getTypeName() 172 */ 173 public String getTypeName() { 174 175 return TYPE_NAME; 176 } 177 178 /** 179 * @see org.opencms.xml.types.A_CmsXmlContentValue#isSearchable() 180 */ 181 @Override 182 public boolean isSearchable() { 183 184 // there is no point in searching boolean values 185 return false; 186 } 187 188 /** 189 * @see org.opencms.xml.types.A_CmsXmlContentValue#newInstance(java.lang.String, java.lang.String, java.lang.String) 190 */ 191 public I_CmsXmlSchemaType newInstance(String name, String minOccurs, String maxOccurs) { 192 193 return new CmsXmlBooleanValue(name, minOccurs, maxOccurs); 194 } 195 196 /** 197 * @see org.opencms.xml.types.A_CmsXmlValueTextBase#setStringValue(org.opencms.file.CmsObject, java.lang.String) 198 */ 199 @Override 200 public void setStringValue(CmsObject cms, String value) throws CmsIllegalArgumentException { 201 202 m_boolean = getBooleanValue(value); 203 super.setStringValue(cms, String.valueOf(m_boolean)); 204 } 205 206 /** 207 * @see org.opencms.xml.types.I_CmsXmlSchemaType#validateValue(java.lang.String) 208 */ 209 @Override 210 public boolean validateValue(String value) { 211 212 return TYPE_PATTERN.matcher(value).matches(); 213 } 214}