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.xml.I_CmsXmlDocument;
031
032import java.util.Locale;
033import java.util.regex.Pattern;
034
035import org.dom4j.Element;
036
037/**
038 * Describes the XML content type "OpenCmsColor".<p>
039 *
040 * @since 6.0.0
041 */
042public class CmsXmlColorValue extends A_CmsXmlValueTextBase {
043
044    /** The name of this type as used in the XML schema. */
045    public static final String TYPE_NAME = "OpenCmsColor";
046
047    /** The validation rule used for this schema type. */
048    public static final String TYPE_RULE = "(#([a-f]|[A-F]|[0-9]){3}(([a-f]|[A-F]|[0-9]){3})?)|ActiveBorder|ActiveCaption|ActiveCaptionText|AppWorkspace|Background|ButtonFace|ButtonHighlight|ButtonShadow|ButtonText|CaptionText|GrayText|Highlight|HighlightText|InactiveBorder|InactiveCaption|InactiveCaptionText|InfoBackground|InfoBackground|InfoText|MenuText|Menu|ScrollBar|ThreeDDarkShadow|ThreeDFace|ThreeDHighlight|ThreeDLightShadow|ThreeDShadow|Window|WindowFrame|WindowText";
049
050    /** Pre-compiled regular expression pattern for this rule. */
051    private static final Pattern TYPE_PATTERN = Pattern.compile(TYPE_RULE);
052
053    /**
054     * Creates a new, empty schema type descriptor of type "OpenCmsColor".<p>
055     */
056    public CmsXmlColorValue() {
057
058        // empty constructor is required for class registration
059    }
060
061    /**
062     * Creates a new XML content value of type "OpenCmsColor".<p>
063     *
064     * @param document the XML content instance this value belongs to
065     * @param element the XML element that contains this value
066     * @param locale the locale this value is created for
067     * @param type the type instance to create the value for
068     */
069    public CmsXmlColorValue(I_CmsXmlDocument document, Element element, Locale locale, I_CmsXmlSchemaType type) {
070
071        super(document, element, locale, type);
072    }
073
074    /**
075     * Creates a new schema type descriptor for the type "OpenCmsColor".<p>
076     *
077     * @param name the name of the XML node containing the value according to the XML schema
078     * @param minOccurs minimum number of occurrences of this type according to the XML schema
079     * @param maxOccurs maximum number of occurrences of this type according to the XML schema
080     */
081    public CmsXmlColorValue(String name, String minOccurs, String maxOccurs) {
082
083        super(name, minOccurs, maxOccurs);
084    }
085
086    /**
087     * @see org.opencms.xml.types.A_CmsXmlContentValue#createValue(I_CmsXmlDocument, org.dom4j.Element, Locale)
088     */
089    public I_CmsXmlContentValue createValue(I_CmsXmlDocument document, Element element, Locale locale) {
090
091        return new CmsXmlColorValue(document, element, locale, this);
092    }
093
094    /**
095     * @see org.opencms.xml.types.A_CmsXmlContentValue#getDefault(Locale)
096     */
097    @Override
098    public String getDefault(Locale locale) {
099
100        if (m_defaultValue != null) {
101            return m_defaultValue;
102        }
103        return "#000000";
104    }
105
106    /**
107     * @see org.opencms.xml.types.I_CmsXmlSchemaType#getSchemaDefinition()
108     */
109    public String getSchemaDefinition() {
110
111        return "<xsd:simpleType name=\""
112            + TYPE_NAME
113            + "\"><xsd:restriction base=\"xsd:string\">"
114            + "<xsd:pattern value=\""
115            + TYPE_RULE
116            + "\" /></xsd:restriction></xsd:simpleType>";
117    }
118
119    /**
120     * @see org.opencms.xml.types.A_CmsXmlContentValue#getTypeName()
121     */
122    public String getTypeName() {
123
124        return TYPE_NAME;
125    }
126
127    /**
128     * @see org.opencms.xml.types.A_CmsXmlContentValue#isSearchable()
129     */
130    @Override
131    public boolean isSearchable() {
132
133        // there is no point in searching color values
134        return false;
135    }
136
137    /**
138     * @see org.opencms.xml.types.A_CmsXmlContentValue#newInstance(java.lang.String, java.lang.String, java.lang.String)
139     */
140    public I_CmsXmlSchemaType newInstance(String name, String minOccurs, String maxOccurs) {
141
142        return new CmsXmlColorValue(name, minOccurs, maxOccurs);
143    }
144
145    /**
146     * @see org.opencms.xml.types.I_CmsXmlSchemaType#validateValue(java.lang.String)
147     */
148    @Override
149    public boolean validateValue(String value) {
150
151        return TYPE_PATTERN.matcher(value).matches();
152    }
153}