KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/xml/types/CmsXmlDateTimeValue.java,v $
3  * Date : $Date: 2005/06/23 11:11:23 $
4  * Version: $Revision: 1.17 $
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.xml.I_CmsXmlDocument;
35
36 import java.util.Locale JavaDoc;
37 import java.util.regex.Pattern JavaDoc;
38
39 import org.dom4j.Element;
40
41 /**
42  * Describes the XML content type "OpenCmsDateTime".<p>
43  *
44  * @author Alexander Kandzior
45  *
46  * @version $Revision: 1.17 $
47  *
48  * @since 6.0.0
49  */

50 public class CmsXmlDateTimeValue extends A_CmsXmlValueTextBase {
51
52     /** The name of this type as used in the XML schema. */
53     public static final String JavaDoc TYPE_NAME = "OpenCmsDateTime";
54
55     /** The validation rule used for this schema type. */
56     public static final String JavaDoc TYPE_RULE = "\\p{Digit}+";
57
58     /** Pre-compiled regular expression pattern for this rule. */
59     private static final Pattern JavaDoc TYPE_PATTERN = Pattern.compile(TYPE_RULE);
60
61     /** The long value (timestamp). */
62     private long m_dateTime;
63
64     /**
65      * Creates a new, empty schema type descriptor of type "OpenCmsDateTime".<p>
66      */

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

80     public CmsXmlDateTimeValue(I_CmsXmlDocument document, Element element, Locale JavaDoc locale, I_CmsXmlSchemaType type) {
81
82         super(document, element, locale, type);
83         try {
84             m_dateTime = Long.valueOf(m_stringValue).longValue();
85         } catch (NumberFormatException JavaDoc e) {
86             m_dateTime = 0;
87         }
88     }
89
90     /**
91      * Creates a new schema type descriptor for the type "OpenCmsDateTime".<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 CmsXmlDateTimeValue(String JavaDoc name, String JavaDoc minOccurs, String JavaDoc maxOccurs) {
98
99         super(name, minOccurs, maxOccurs);
100     }
101
102     /**
103      * @see org.opencms.xml.types.A_CmsXmlContentValue#createValue(I_CmsXmlDocument, org.dom4j.Element, Locale)
104      */

105     public I_CmsXmlContentValue createValue(I_CmsXmlDocument document, Element element, Locale JavaDoc locale) {
106
107         return new CmsXmlDateTimeValue(document, element, locale, this);
108     }
109
110     /**
111      * Returns the date time value as a long.<p>
112      *
113      * @return the date time value as a long
114      */

115     public long getDateTimeValue() {
116
117         return m_dateTime;
118     }
119
120     /**
121      * @see org.opencms.xml.types.A_CmsXmlContentValue#getDefault(Locale)
122      */

123     public String JavaDoc getDefault(Locale JavaDoc locale) {
124
125         if (m_defaultValue != null) {
126             return m_defaultValue;
127         }
128         return "0";
129     }
130
131     /**
132      * @see org.opencms.xml.types.I_CmsXmlSchemaType#getSchemaDefinition()
133      */

134     public String JavaDoc getSchemaDefinition() {
135
136         return "<xsd:simpleType name=\"" + TYPE_NAME + "\"><xsd:restriction base=\"xsd:decimal\" /></xsd:simpleType>";
137     }
138
139     /**
140      * @see org.opencms.xml.types.A_CmsXmlContentValue#getTypeName()
141      */

142     public String JavaDoc getTypeName() {
143
144         return TYPE_NAME;
145     }
146
147     /**
148      * @see org.opencms.xml.types.A_CmsXmlContentValue#newInstance(java.lang.String, java.lang.String, java.lang.String)
149      */

150     public I_CmsXmlSchemaType newInstance(String JavaDoc name, String JavaDoc minOccurs, String JavaDoc maxOccurs) {
151
152         return new CmsXmlDateTimeValue(name, minOccurs, maxOccurs);
153     }
154
155     /**
156      * @see org.opencms.xml.types.I_CmsXmlSchemaType#validateValue(java.lang.String)
157      */

158     public boolean validateValue(String JavaDoc value) {
159
160         return TYPE_PATTERN.matcher(value).matches();
161     }
162 }
Popular Tags