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.CmsRuntimeException;
032import org.opencms.xml.CmsXmlContentDefinition;
033import org.opencms.xml.I_CmsXmlDocument;
034
035import java.util.Locale;
036
037import org.dom4j.Element;
038
039/**
040 * A nested content XML definition that is included by another XML content definition.<p>
041 *
042 * @since 6.0.0
043 */
044public class CmsXmlNestedContentDefinition extends A_CmsXmlContentValue {
045
046    /** The nested content definition. */
047    private CmsXmlContentDefinition m_nestedContentDefinition;
048
049    /**
050     * Creates a new XML content value for the nested content definition.<p>
051     *
052     * @param document the XML content instance this value belongs to
053     * @param contentDefinition the nested XML content definition
054     * @param element the XML element that contains this value
055     * @param locale the locale this value is created for
056     * @param type the type instance to create the value for
057     */
058    public CmsXmlNestedContentDefinition(
059        CmsXmlContentDefinition contentDefinition,
060        I_CmsXmlDocument document,
061        Element element,
062        Locale locale,
063        I_CmsXmlSchemaType type) {
064
065        super(document, element, locale, type);
066        m_nestedContentDefinition = contentDefinition;
067    }
068
069    /**
070     * Creates a new nested content definition.<p>
071     *
072     * @param contentDefinition the content definition to nest
073     * @param name the name of the XML node containing the value according to the XML schema
074     * @param minOccurs minimum number of occurrences of this type according to the XML schema
075     * @param maxOccurs maximum number of occurrences of this type according to the XML schema
076     */
077    public CmsXmlNestedContentDefinition(
078        CmsXmlContentDefinition contentDefinition,
079        String name,
080        String minOccurs,
081        String maxOccurs) {
082
083        super(name, minOccurs, maxOccurs);
084        m_nestedContentDefinition = contentDefinition;
085    }
086
087    /**
088     * @see org.opencms.xml.types.I_CmsXmlSchemaType#createValue(I_CmsXmlDocument, org.dom4j.Element, Locale)
089     */
090    public I_CmsXmlContentValue createValue(I_CmsXmlDocument document, Element element, Locale locale) {
091
092        return new CmsXmlNestedContentDefinition(m_nestedContentDefinition, document, element, locale, this);
093    }
094
095    /**
096     * @see org.opencms.xml.types.I_CmsXmlSchemaType#generateXml(org.opencms.file.CmsObject, org.opencms.xml.I_CmsXmlDocument, org.dom4j.Element, java.util.Locale)
097     */
098    @Override
099    public Element generateXml(CmsObject cms, I_CmsXmlDocument document, Element root, Locale locale) {
100
101        // create the XML base node for the nested content definition
102        Element element = root.addElement(getName());
103        // create a default XML element for the nested content definition
104        element = m_nestedContentDefinition.createDefaultXml(cms, document, element, locale);
105        // return the generated element
106        return element;
107    }
108
109    /**
110     * Returns the nested content definition.<p>
111     *
112     * @return the nested content definition
113     */
114    public CmsXmlContentDefinition getNestedContentDefinition() {
115
116        return m_nestedContentDefinition;
117    }
118
119    /**
120     * @see org.opencms.xml.types.I_CmsXmlSchemaType#getSchemaDefinition()
121     */
122    public String getSchemaDefinition() {
123
124        throw new CmsRuntimeException(Messages.get().container(Messages.ERR_NESTED_SCHEMA_0));
125    }
126
127    /**
128     * @see org.opencms.xml.types.I_CmsXmlContentValue#getStringValue(org.opencms.file.CmsObject)
129     */
130    public String getStringValue(CmsObject cms) throws CmsRuntimeException {
131
132        throw new CmsRuntimeException(Messages.get().container(Messages.ERR_NESTED_GETVALUE_0));
133    }
134
135    /**
136     * @see org.opencms.xml.types.I_CmsXmlSchemaType#getTypeName()
137     */
138    public String getTypeName() {
139
140        return m_nestedContentDefinition.getTypeName();
141    }
142
143    /**
144     * @see org.opencms.xml.types.I_CmsXmlSchemaType#isChoiceType()
145     */
146    @Override
147    public boolean isChoiceType() {
148
149        return m_nestedContentDefinition.getChoiceMaxOccurs() > 0;
150    }
151
152    /**
153     * Returns <code>false</code>, since nested content definitions are never simple.<p>
154     *
155     * @see org.opencms.xml.types.I_CmsXmlSchemaType#isSimpleType()
156     */
157    @Override
158    public boolean isSimpleType() {
159
160        return false;
161    }
162
163    /**
164     * @see org.opencms.xml.types.I_CmsXmlSchemaType#newInstance(java.lang.String, java.lang.String, java.lang.String)
165     */
166    public I_CmsXmlSchemaType newInstance(String name, String minOccurs, String maxOccurs) {
167
168        throw new CmsRuntimeException(Messages.get().container(Messages.ERR_NESTED_NEWINSTANCE_0));
169    }
170
171    /**
172     * @see org.opencms.xml.types.I_CmsXmlContentValue#setStringValue(org.opencms.file.CmsObject, java.lang.String)
173     */
174    public void setStringValue(CmsObject cms, String value) {
175
176        throw new CmsRuntimeException(Messages.get().container(Messages.ERR_NESTED_SETVALUE_0));
177    }
178}