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.main.CmsIllegalArgumentException;
032import org.opencms.main.CmsRuntimeException;
033import org.opencms.xml.I_CmsXmlDocument;
034
035import java.util.Locale;
036
037import org.dom4j.Element;
038
039/**
040 * Describes the XML content type "OpenCmsVfsFile".<p>
041 *
042 * This type allows links to internal VFS resources only.<p>
043 *
044 * @since 7.0.0
045 */
046public class CmsXmlAccessRestrictionValue extends A_CmsXmlContentValue {
047
048    /** The name of this type as used in the XML schema. */
049    public static final String TYPE_NAME = "OpenCmsAccessRestriction";
050
051    public static final String N_ACCESS_RESTRICTION_VALUE = "access-restriction-value";
052
053    /** The schema definition String is located in a text for easier editing. */
054    private static String m_schemaDefinition;
055
056    /**
057     * Creates a new, empty schema type descriptor of type "OpenCmsCategoryValue".<p>
058     */
059    public CmsXmlAccessRestrictionValue() {
060
061        // empty constructor is required for class registration
062    }
063
064    /**
065     * Creates a new XML content value of type "OpenCmsCategoryValue".<p>
066     *
067     * @param document the XML content instance this value belongs to
068     * @param element the XML element that contains this value
069     * @param locale the locale this value is created for
070     * @param type the type instance to create the value for
071     */
072    public CmsXmlAccessRestrictionValue(
073        I_CmsXmlDocument document,
074        Element element,
075        Locale locale,
076        I_CmsXmlSchemaType type) {
077
078        super(document, element, locale, type);
079    }
080
081    /**
082     * Creates a new schema type descriptor for the type "OpenCmsCategoryValue".<p>
083     *
084     * @param name the name of the XML node containing the value according to the XML schema
085     * @param minOccurs minimum number of occurrences of this type according to the XML schema
086     * @param maxOccurs maximum number of occurrences of this type according to the XML schema
087     */
088    public CmsXmlAccessRestrictionValue(String name, String minOccurs, String maxOccurs) {
089
090        super(name, minOccurs, maxOccurs);
091    }
092
093    /**
094     * @see org.opencms.xml.types.A_CmsXmlContentValue#createValue(I_CmsXmlDocument, org.dom4j.Element, Locale)
095     */
096    public I_CmsXmlContentValue createValue(I_CmsXmlDocument document, Element element, Locale locale) {
097
098        return new CmsXmlAccessRestrictionValue(document, element, locale, this);
099    }
100
101    /**
102     * @see org.opencms.xml.types.I_CmsXmlSchemaType#generateXml(org.opencms.file.CmsObject, org.opencms.xml.I_CmsXmlDocument, org.dom4j.Element, java.util.Locale)
103     */
104    @Override
105    public Element generateXml(CmsObject cms, I_CmsXmlDocument document, Element root, Locale locale) {
106
107        Element element = root.addElement(getName());
108        element.addComment("Access restriction is dynamically read from permissions.");
109        return element;
110    }
111
112    /**
113     * @see org.opencms.xml.types.I_CmsXmlContentValue#getPlainText(org.opencms.file.CmsObject)
114     */
115    @Override
116    public String getPlainText(CmsObject cms) {
117
118        return getStringValue(cms);
119    }
120
121    /**
122     * @see org.opencms.xml.types.I_CmsXmlSchemaType#getSchemaDefinition()
123     */
124    public String getSchemaDefinition() {
125
126        if (m_schemaDefinition == null) {
127            m_schemaDefinition = readSchemaDefinition("org/opencms/xml/types/XmlAccessRestrictionValue.xsd");
128        }
129        return m_schemaDefinition;
130    }
131
132    /**
133     * @see org.opencms.xml.types.I_CmsXmlContentValue#getStringValue(CmsObject)
134     */
135    public String getStringValue(CmsObject cms) throws CmsRuntimeException {
136
137        Element categoryElement = valueElem(false);
138        if (categoryElement == null) {
139            return "";
140        }
141        return categoryElement.getText();
142
143    }
144
145    /**
146     * @see org.opencms.xml.types.A_CmsXmlContentValue#getTypeName()
147     */
148    public String getTypeName() {
149
150        return TYPE_NAME;
151    }
152
153    /**
154     * @see org.opencms.xml.types.A_CmsXmlContentValue#isSearchable()
155     */
156    @Override
157    public boolean isSearchable() {
158
159        return false;
160    }
161
162    /**
163     * @see org.opencms.xml.types.A_CmsXmlContentValue#newInstance(java.lang.String, java.lang.String, java.lang.String)
164     */
165    public I_CmsXmlSchemaType newInstance(String name, String minOccurs, String maxOccurs) {
166
167        return new CmsXmlAccessRestrictionValue(name, minOccurs, maxOccurs);
168    }
169
170    /**
171     * @see org.opencms.xml.types.A_CmsXmlContentValue#setStringValue(org.opencms.file.CmsObject, java.lang.String)
172     */
173    public void setStringValue(CmsObject cms, String value) throws CmsIllegalArgumentException {
174
175        valueElem(true).setText(value);
176    }
177
178    /**
179     * Gets the category-string subelement, creating it if necessary.
180     *
181     * @param create if true, the category string element is created if it doesn't exist; if false, null is returned in that case.
182     * @return the category-string subelement
183     */
184    Element valueElem(boolean create) {
185
186        Element result = m_element.element(N_ACCESS_RESTRICTION_VALUE);
187        if ((result == null) && create) {
188            result = m_element.addElement(N_ACCESS_RESTRICTION_VALUE);
189            result.detach();
190            m_element.elements().add(0, result);
191        }
192        return result;
193    }
194
195}