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.util.CmsMacroResolver;
031import org.opencms.util.CmsStringUtil;
032import org.opencms.xml.I_CmsXmlDocument;
033
034import java.util.Locale;
035import java.util.regex.Pattern;
036
037import org.dom4j.Element;
038
039/**
040 * Describes the XML content type "OpenCmsDateTime".<p>
041 *
042 * @since 6.0.0
043 */
044public class CmsXmlDateTimeValue extends A_CmsXmlValueTextBase {
045
046    /** The name of this type as used in the XML schema. */
047    public static final String TYPE_NAME = "OpenCmsDateTime";
048
049    /** The validation rule used for this schema type. */
050    public static final String TYPE_RULE = "-?\\p{Digit}+|"
051        + CmsStringUtil.escapePattern(CmsMacroResolver.formatMacro(CmsMacroResolver.KEY_CURRENT_TIME));
052
053    /** Pre-compiled regular expression pattern for this rule. */
054    private static final Pattern TYPE_PATTERN = Pattern.compile(TYPE_RULE);
055
056    /** The long value (timestamp). */
057    private long m_dateTime;
058
059    /**
060     * Creates a new, empty schema type descriptor of type "OpenCmsDateTime".<p>
061     */
062    public CmsXmlDateTimeValue() {
063
064        // empty constructor is required for class registration
065    }
066
067    /**
068     * Creates a new XML content value of type "OpenCmsDateTime".<p>
069     *
070     * @param document the XML content instance this value belongs to
071     * @param element the XML element that contains this value
072     * @param locale the locale this value is created for
073     * @param type the type instance to create the value for
074     */
075    public CmsXmlDateTimeValue(I_CmsXmlDocument document, Element element, Locale locale, I_CmsXmlSchemaType type) {
076
077        super(document, element, locale, type);
078        try {
079            m_dateTime = Long.valueOf(m_stringValue).longValue();
080        } catch (NumberFormatException e) {
081            m_dateTime = 0;
082        }
083    }
084
085    /**
086     * Creates a new schema type descriptor for the type "OpenCmsDateTime".<p>
087     *
088     * @param name the name of the XML node containing the value according to the XML schema
089     * @param minOccurs minimum number of occurrences of this type according to the XML schema
090     * @param maxOccurs maximum number of occurrences of this type according to the XML schema
091     */
092    public CmsXmlDateTimeValue(String name, String minOccurs, String maxOccurs) {
093
094        super(name, minOccurs, maxOccurs);
095    }
096
097    /**
098     * @see org.opencms.xml.types.A_CmsXmlContentValue#createValue(I_CmsXmlDocument, org.dom4j.Element, Locale)
099     */
100    public I_CmsXmlContentValue createValue(I_CmsXmlDocument document, Element element, Locale locale) {
101
102        return new CmsXmlDateTimeValue(document, element, locale, this);
103    }
104
105    /**
106     * Returns the date time value as a long.<p>
107     *
108     * @return the date time value as a long
109     */
110    public long getDateTimeValue() {
111
112        return m_dateTime;
113    }
114
115    /**
116     * @see org.opencms.xml.types.A_CmsXmlContentValue#getDefault(Locale)
117     */
118    @Override
119    public String getDefault(Locale locale) {
120
121        if (m_defaultValue != null) {
122            return m_defaultValue;
123        }
124        return "0";
125    }
126
127    /**
128     * @see org.opencms.xml.types.I_CmsXmlSchemaType#getSchemaDefinition()
129     */
130    public String getSchemaDefinition() {
131
132        StringBuffer result = new StringBuffer(256);
133        // create a named decimal simpletype (for long values)
134        result.append("<xsd:simpleType name=\"ocmsdatedec\"><xsd:restriction base=\"xsd:decimal\">");
135        result.append("</xsd:restriction></xsd:simpleType>");
136        // create a simpletype containing the "currenttime" macro
137        result.append("<xsd:simpleType name=\"ocmsdatemacro\">");
138        result.append("<xsd:restriction base=\"xsd:string\">");
139        result.append("<xsd:enumeration value=\"");
140        result.append(CmsMacroResolver.formatMacro(CmsMacroResolver.KEY_CURRENT_TIME));
141        result.append("\"/>");
142        result.append("</xsd:restriction></xsd:simpleType>");
143        // unify the simpletypes for the datetime value
144        result.append("<xsd:simpleType name=\"");
145        result.append(TYPE_NAME);
146        result.append("\"><xsd:union memberTypes=\"ocmsdatedec ocmsdatemacro\"/></xsd:simpleType>");
147
148        return result.toString();
149    }
150
151    /**
152     * @see org.opencms.xml.types.A_CmsXmlContentValue#getTypeName()
153     */
154    public String getTypeName() {
155
156        return TYPE_NAME;
157    }
158
159    /**
160     * @see org.opencms.xml.types.A_CmsXmlContentValue#isSearchable()
161     */
162    @Override
163    public boolean isSearchable() {
164
165        // there is no point in searching date/time values
166        // they are stored as long int anyway
167        return false;
168    }
169
170    /**
171     * @see org.opencms.xml.types.A_CmsXmlContentValue#newInstance(java.lang.String, java.lang.String, java.lang.String)
172     */
173    public I_CmsXmlSchemaType newInstance(String name, String minOccurs, String maxOccurs) {
174
175        return new CmsXmlDateTimeValue(name, minOccurs, maxOccurs);
176    }
177
178    /**
179     * @see org.opencms.xml.types.I_CmsXmlSchemaType#validateValue(java.lang.String)
180     */
181    @Override
182    public boolean validateValue(String value) {
183
184        return TYPE_PATTERN.matcher(value).matches();
185    }
186}