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 "OpenCmsLocale".<p>
039 *
040 * @since 6.0.0
041 */
042public class CmsXmlLocaleValue extends A_CmsXmlValueTextBase {
043
044    /** The name of this type as used in the XML schema. */
045    public static final String TYPE_NAME = "OpenCmsLocale";
046
047    /** The validation rule used for this schema type. */
048    public static final String TYPE_RULE = "[a-z]{2,3}(_[A-Z]{2}(_[a-zA-Z0-9]+){0,1}){0,1}";
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 "OpenCmsLocale".<p>
055     */
056    public CmsXmlLocaleValue() {
057
058        // empty constructor is required for class registration
059    }
060
061    /**
062     * Creates a new XML content value of type "OpenCmsLocale".<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 CmsXmlLocaleValue(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 "OpenCmsLocale".<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 CmsXmlLocaleValue(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 CmsXmlLocaleValue(document, element, locale, this);
092    }
093
094    /**
095     * @see org.opencms.xml.types.I_CmsXmlSchemaType#getSchemaDefinition()
096     */
097    public String getSchemaDefinition() {
098
099        return "<xsd:simpleType name=\""
100            + TYPE_NAME
101            + "\"><xsd:restriction base=\"xsd:string\">"
102            + "<xsd:pattern value=\""
103            + TYPE_RULE
104            + "\" /></xsd:restriction></xsd:simpleType>";
105    }
106
107    /**
108     * @see org.opencms.xml.types.A_CmsXmlContentValue#getTypeName()
109     */
110    public String getTypeName() {
111
112        return TYPE_NAME;
113    }
114
115    /**
116     * @see org.opencms.xml.types.A_CmsXmlContentValue#isSearchable()
117     */
118    @Override
119    public boolean isSearchable() {
120
121        // there is no point in searching locale values
122        return false;
123    }
124
125    /**
126     * @see org.opencms.xml.types.A_CmsXmlContentValue#newInstance(java.lang.String, java.lang.String, java.lang.String)
127     */
128    public I_CmsXmlSchemaType newInstance(String name, String minOccurs, String maxOccurs) {
129
130        return new CmsXmlLocaleValue(name, minOccurs, maxOccurs);
131    }
132
133    /**
134     * @see org.opencms.xml.types.I_CmsXmlSchemaType#validateValue(java.lang.String)
135     */
136    @Override
137    public boolean validateValue(String value) {
138
139        return TYPE_PATTERN.matcher(value).matches();
140    }
141}