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;
035import org.opencms.xml.xml2json.I_CmsJsonFormattableValue;
036
037import java.util.Locale;
038import java.util.regex.Pattern;
039
040import org.dom4j.Element;
041
042/**
043 * Describes the XML content type "OpenCmsBoolean".<p>
044 *
045 * @since 6.0.0
046 */
047public class CmsXmlBooleanValue extends A_CmsXmlValueTextBase implements I_CmsJsonFormattableValue {
048
049    /** The name of this type as used in the XML schema. */
050    public static final String TYPE_NAME = "OpenCmsBoolean";
051
052    /** The validation rule used for this schema type. */
053    public static final String TYPE_RULE = "true|false|1|0";
054
055    /** Pre-compiled regular expression pattern for this rule. */
056    private static final Pattern TYPE_PATTERN = Pattern.compile(TYPE_RULE);
057
058    /** The boolean value of the element node. */
059    private boolean m_boolean;
060
061    /**
062     * Creates a new, empty schema type descriptor of type "OpenCmsBoolean".<p>
063     */
064    public CmsXmlBooleanValue() {
065
066        // empty constructor is required for class registration
067    }
068
069    /**
070     * Creates a new XML content value of type "OpenCmsBoolean".<p>
071     *
072     * @param document the XML content instance this value belongs to
073     * @param element the XML element that contains this value
074     * @param locale the locale this value is created for
075     * @param type the type instance to create the value for
076     */
077    public CmsXmlBooleanValue(I_CmsXmlDocument document, Element element, Locale locale, I_CmsXmlSchemaType type) {
078
079        super(document, element, locale, type);
080        m_boolean = getBooleanValue(m_stringValue);
081    }
082
083    /**
084     * Creates a new schema type descriptor for the type "OpenCmsBoolean".<p>
085     *
086     * @param name the name of the XML node containing the value according to the XML schema
087     * @param minOccurs minimum number of occurrences of this type according to the XML schema
088     * @param maxOccurs maximum number of occurrences of this type according to the XML schema
089     */
090    public CmsXmlBooleanValue(String name, String minOccurs, String maxOccurs) {
091
092        super(name, minOccurs, maxOccurs);
093    }
094
095    /**
096     * Returns the boolean value of the given widget parameter.<p>
097     *
098     * @param cms an initialized instance of a CmsObject
099     * @param value the XML content value to get the boolean value of
100     *
101     * @return the boolean value of the given widget parameter
102     */
103    public static boolean getBooleanValue(CmsObject cms, I_CmsWidgetParameter value) {
104
105        boolean result;
106        if (value instanceof CmsXmlBooleanValue) {
107            // this is a "native" boolean type
108            result = ((CmsXmlBooleanValue)value).getBooleanValue();
109        } else {
110            // get the boolean value from the String value
111            result = getBooleanValue(value.getStringValue(cms));
112        }
113        return result;
114    }
115
116    /**
117     * Special boolean value generation method since XML schema allows for
118     * "1" as possible value for <code>true</code>, while Java only allows <code>"true"</code>.
119     *
120     * @param value the String to get the boolean value for
121     *
122     * @return the boolean value of the String according to the XML schema rules
123     */
124    private static boolean getBooleanValue(String value) {
125
126        if ("1".equals(value)) {
127            // XML schema allows for "1" as value for "true"
128            return true;
129        }
130        return Boolean.valueOf(value).booleanValue();
131    }
132
133    /**
134     * @see org.opencms.xml.types.A_CmsXmlContentValue#createValue(I_CmsXmlDocument, org.dom4j.Element, Locale)
135     */
136    public I_CmsXmlContentValue createValue(I_CmsXmlDocument document, Element element, Locale locale) {
137
138        return new CmsXmlBooleanValue(document, element, locale, this);
139    }
140
141    /**
142     * Returns the boolean value as a boolean type.<p>
143     *
144     * @return the boolean value as a boolean type
145     */
146    public boolean getBooleanValue() {
147
148        return m_boolean;
149    }
150
151    /**
152     * @see org.opencms.xml.types.A_CmsXmlContentValue#getDefault(Locale)
153     */
154    @Override
155    public String getDefault(Locale locale) {
156
157        if (m_defaultValue != null) {
158            return m_defaultValue;
159        }
160        return CmsStringUtil.FALSE;
161    }
162
163    /**
164     * @see org.opencms.xml.types.I_CmsXmlSchemaType#getSchemaDefinition()
165     */
166    public String getSchemaDefinition() {
167
168        return "<xsd:simpleType name=\"" + TYPE_NAME + "\"><xsd:restriction base=\"xsd:boolean\" /></xsd:simpleType>";
169    }
170
171    /**
172     * @see org.opencms.xml.types.A_CmsXmlContentValue#getTypeName()
173     */
174    public String getTypeName() {
175
176        return TYPE_NAME;
177    }
178
179    /**
180     * @see org.opencms.xml.types.A_CmsXmlContentValue#isSearchable()
181     */
182    @Override
183    public boolean isSearchable() {
184
185        // there is no point in searching boolean values
186        return false;
187    }
188
189    /**
190     * @see org.opencms.xml.types.A_CmsXmlContentValue#newInstance(java.lang.String, java.lang.String, java.lang.String)
191     */
192    public I_CmsXmlSchemaType newInstance(String name, String minOccurs, String maxOccurs) {
193
194        return new CmsXmlBooleanValue(name, minOccurs, maxOccurs);
195    }
196
197    /**
198     * @see org.opencms.xml.types.A_CmsXmlValueTextBase#setStringValue(org.opencms.file.CmsObject, java.lang.String)
199     */
200    @Override
201    public void setStringValue(CmsObject cms, String value) throws CmsIllegalArgumentException {
202
203        m_boolean = getBooleanValue(value);
204        super.setStringValue(cms, String.valueOf(m_boolean));
205    }
206
207    /**
208     * @see org.opencms.xml.xml2json.I_CmsJsonFormattableValue#toJson(org.opencms.file.CmsObject)
209     */
210    public Object toJson(CmsObject cms) {
211
212        Boolean result = Boolean.valueOf(getBooleanValue());
213        return result;
214    }
215
216    /**
217     * @see org.opencms.xml.types.I_CmsXmlSchemaType#validateValue(java.lang.String)
218     */
219    @Override
220    public boolean validateValue(String value) {
221
222        return TYPE_PATTERN.matcher(value).matches();
223    }
224}