KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > xml > types > CmsXmlBooleanValue


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/xml/types/CmsXmlBooleanValue.java,v $
3  * Date : $Date: 2006/03/27 14:53:03 $
4  * Version: $Revision: 1.20 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

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 JavaDoc;
41 import java.util.regex.Pattern JavaDoc;
42
43 import org.dom4j.Element;
44
45 /**
46  * Describes the XML content type "OpenCmsBoolean".<p>
47  *
48  * @author Andreas Zahner
49  *
50  * @version $Revision: 1.20 $
51  *
52  * @since 6.0.0
53  */

54 public class CmsXmlBooleanValue extends A_CmsXmlValueTextBase {
55
56     /** The name of this type as used in the XML schema. */
57     public static final String JavaDoc TYPE_NAME = "OpenCmsBoolean";
58
59     /** The validation rule used for this schema type. */
60     public static final String JavaDoc TYPE_RULE = "true|false|1|0";
61
62     /** Pre-compiled regular expression pattern for this rule. */
63     private static final Pattern JavaDoc TYPE_PATTERN = Pattern.compile(TYPE_RULE);
64
65     /** The boolean value of the element node. */
66     private boolean m_boolean;
67
68     /**
69      * Creates a new, empty schema type descriptor of type "OpenCmsBoolean".<p>
70      */

71     public CmsXmlBooleanValue() {
72
73         // empty constructor is required for class registration
74
}
75
76     /**
77      * Creates a new XML content value of type "OpenCmsBoolean".<p>
78      *
79      * @param document the XML content instance this value belongs to
80      * @param element the XML element that contains this value
81      * @param locale the locale this value is created for
82      * @param type the type instance to create the value for
83      */

84     public CmsXmlBooleanValue(I_CmsXmlDocument document, Element element, Locale JavaDoc locale, I_CmsXmlSchemaType type) {
85
86         super(document, element, locale, type);
87         m_boolean = getBooleanValue(m_stringValue);
88     }
89
90     /**
91      * Creates a new schema type descriptor for the type "OpenCmsBoolean".<p>
92      *
93      * @param name the name of the XML node containing the value according to the XML schema
94      * @param minOccurs minimum number of occurences of this type according to the XML schema
95      * @param maxOccurs maximum number of occurences of this type according to the XML schema
96      */

97     public CmsXmlBooleanValue(String JavaDoc name, String JavaDoc minOccurs, String JavaDoc maxOccurs) {
98
99         super(name, minOccurs, maxOccurs);
100     }
101
102     /**
103      * Returns the boolean value of the given widget parameter.<p>
104      *
105      * @param cms an initialized instance of a CmsObject
106      * @param value the XML content value to get the boolean value of
107      *
108      * @return the boolean value of the given widget parameter
109      */

110     public static boolean getBooleanValue(CmsObject cms, I_CmsWidgetParameter value) {
111
112         boolean result;
113         if (value instanceof CmsXmlBooleanValue) {
114             // this is a "native" boolean type
115
result = ((CmsXmlBooleanValue)value).getBooleanValue();
116         } else {
117             // get the boolean value from the String value
118
result = getBooleanValue(value.getStringValue(cms));
119         }
120         return result;
121     }
122
123     /**
124      * Special boolean value generation method since XML schema allows for
125      * "1" as possible value for <code>true</code>, while Java only allows <code>"true"</code>.
126      *
127      * @param value the String to get the boolean value for
128      *
129      * @return the boolean value of the String according to the XML schema rules
130      */

131     private static boolean getBooleanValue(String JavaDoc value) {
132
133         if ("1".equals(value)) {
134             // XML schema allows for "1" as value for "true"
135
return true;
136         }
137         return Boolean.valueOf(value).booleanValue();
138     }
139
140     /**
141      * @see org.opencms.xml.types.A_CmsXmlContentValue#createValue(I_CmsXmlDocument, org.dom4j.Element, Locale)
142      */

143     public I_CmsXmlContentValue createValue(I_CmsXmlDocument document, Element element, Locale JavaDoc locale) {
144
145         return new CmsXmlBooleanValue(document, element, locale, this);
146     }
147
148     /**
149      * Returns the boolean value as a boolean type.<p>
150      *
151      * @return the boolean value as a boolean type
152      */

153     public boolean getBooleanValue() {
154
155         return m_boolean;
156     }
157
158     /**
159      * @see org.opencms.xml.types.A_CmsXmlContentValue#getDefault(Locale)
160      */

161     public String JavaDoc getDefault(Locale JavaDoc locale) {
162
163         if (m_defaultValue != null) {
164             return m_defaultValue;
165         }
166         return CmsStringUtil.FALSE;
167     }
168
169     /**
170      * @see org.opencms.xml.types.I_CmsXmlSchemaType#getSchemaDefinition()
171      */

172     public String JavaDoc getSchemaDefinition() {
173
174         return "<xsd:simpleType name=\"" + TYPE_NAME + "\"><xsd:restriction base=\"xsd:boolean\" /></xsd:simpleType>";
175     }
176
177     /**
178      * @see org.opencms.xml.types.A_CmsXmlContentValue#getTypeName()
179      */

180     public String JavaDoc getTypeName() {
181
182         return TYPE_NAME;
183     }
184
185     /**
186      * @see org.opencms.xml.types.A_CmsXmlContentValue#newInstance(java.lang.String, java.lang.String, java.lang.String)
187      */

188     public I_CmsXmlSchemaType newInstance(String JavaDoc name, String JavaDoc minOccurs, String JavaDoc maxOccurs) {
189
190         return new CmsXmlBooleanValue(name, minOccurs, maxOccurs);
191     }
192
193     /**
194      * @see org.opencms.xml.types.A_CmsXmlValueTextBase#setStringValue(org.opencms.file.CmsObject, java.lang.String)
195      */

196     public void setStringValue(CmsObject cms, String JavaDoc value) throws CmsIllegalArgumentException {
197
198         super.setStringValue(cms, value);
199         m_boolean = getBooleanValue(m_stringValue);
200     }
201
202     /**
203      * @see org.opencms.xml.types.I_CmsXmlSchemaType#validateValue(java.lang.String)
204      */

205     public boolean validateValue(String JavaDoc value) {
206
207         return TYPE_PATTERN.matcher(value).matches();
208     }
209 }
Popular Tags