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