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, 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.ade.configuration.CmsADEConfigData;
031import org.opencms.file.CmsObject;
032import org.opencms.main.CmsRuntimeException;
033import org.opencms.main.OpenCms;
034import org.opencms.util.CmsStringUtil;
035import org.opencms.xml.I_CmsXmlDocument;
036import org.opencms.xml.containerpage.I_CmsFormatterBean;
037import org.opencms.xml.content.CmsXmlContent;
038
039import java.util.Locale;
040
041import org.dom4j.Element;
042
043/**
044 * XML value type for display formatters.<p>
045 */
046public class CmsXmlDisplayFormatterValue extends A_CmsXmlValueTextBase {
047
048    /** The value separator string. */
049    public static final String SEPARATOR = ":";
050
051    /** The XSD type name. */
052    private static final String TYPE_NAME = "OpenCmsDisplayFormatter";
053
054    /**
055     * Creates a new, empty schema type descriptor of type "OpenCmsDisplayFormatter".<p>
056     */
057    public CmsXmlDisplayFormatterValue() {
058
059        // empty constructor is required for class registration
060    }
061
062    /**
063     * Creates a new XML content value of type "OpenCmsDisplayFormatter".<p>
064     *
065     * @param document the XML content instance this value belongs to
066     * @param element the XML element that contains this value
067     * @param locale the locale this value is created for
068     * @param type the type instance to create the value for
069     */
070    public CmsXmlDisplayFormatterValue(
071        I_CmsXmlDocument document,
072        Element element,
073        Locale locale,
074        I_CmsXmlSchemaType type) {
075
076        super(document, element, locale, type);
077    }
078
079    /**
080     * Creates a new schema type descriptor for the type "OpenCmsDisplayFormatter".<p>
081     *
082     * @param name the name of the XML node containing the value according to the XML schema
083     * @param minOccurs minimum number of occurrences of this type according to the XML schema
084     * @param maxOccurs maximum number of occurrences of this type according to the XML schema
085     */
086    public CmsXmlDisplayFormatterValue(String name, String minOccurs, String maxOccurs) {
087
088        super(name, minOccurs, maxOccurs);
089    }
090
091    /**
092     * @see org.opencms.xml.types.I_CmsXmlSchemaType#createValue(org.opencms.xml.I_CmsXmlDocument, org.dom4j.Element, java.util.Locale)
093     */
094    public I_CmsXmlContentValue createValue(I_CmsXmlDocument document, Element element, Locale locale) {
095
096        return new CmsXmlDisplayFormatterValue(document, element, locale, this);
097    }
098
099    /**
100     * Returns the display resource type name.<p>
101     *
102     * @return the display resource type
103     */
104    public String getDisplayType() {
105
106        String value = getStringValue(null);
107        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(value)) {
108            return value.split(SEPARATOR)[0];
109        }
110        return null;
111    }
112
113    /**
114     * Returns the formatter config id.<p>
115     *
116     * @return the formatter config id
117     */
118    public String getFormatterId() {
119
120        String value = getStringValue(null);
121        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(value)) {
122            String[] parts = value.split(SEPARATOR);
123            if (parts.length == 2) {
124                return parts[1];
125            }
126        }
127        return null;
128    }
129
130    /**
131     * @see org.opencms.xml.types.I_CmsXmlSchemaType#getSchemaDefinition()
132     */
133    public String getSchemaDefinition() {
134
135        return "<xsd:simpleType name=\"" + TYPE_NAME + "\"><xsd:restriction base=\"xsd:string\" /></xsd:simpleType>";
136    }
137
138    /**
139     * @see org.opencms.xml.types.A_CmsXmlValueTextBase#getStringValue(org.opencms.file.CmsObject)
140     */
141    @Override
142    public String getStringValue(CmsObject cms) throws CmsRuntimeException {
143
144        // always try to return the value with the formatter key (rather than id) if possible
145        // (this matches the handling of options in the display formatter widget)
146
147        I_CmsXmlDocument doc = getDocument();
148        if ((doc != null) && (doc instanceof CmsXmlContent)) {
149            CmsXmlContent content = (CmsXmlContent)doc;
150            if (content.getFile() != null) {
151                CmsADEConfigData config = OpenCms.getADEManager().lookupConfigurationWithCache(
152                    cms,
153                    content.getFile().getRootPath());
154                String internalValue = super.getStringValue(cms);
155                if (internalValue == null) {
156                    return null;
157                }
158                int colonPos = internalValue.indexOf(':');
159                if (colonPos == -1) {
160                    return internalValue;
161                }
162                String keyOrId = internalValue.substring(colonPos + 1);
163                I_CmsFormatterBean formatter = config.findFormatter(keyOrId);
164                if (formatter != null) {
165                    return internalValue.substring(0, colonPos + 1) + formatter.getKeyOrId();
166                }
167            }
168        }
169        return super.getStringValue(cms);
170    }
171
172    /**
173     * @see org.opencms.xml.types.A_CmsXmlContentValue#getTypeName()
174     */
175    public String getTypeName() {
176
177        return TYPE_NAME;
178    }
179
180    /**
181     * @see org.opencms.xml.types.I_CmsXmlSchemaType#newInstance(java.lang.String, java.lang.String, java.lang.String)
182     */
183    public I_CmsXmlSchemaType newInstance(String name, String minOccurs, String maxOccurs) {
184
185        return new CmsXmlDisplayFormatterValue(name, minOccurs, maxOccurs);
186    }
187
188    /**
189     * @see org.opencms.xml.types.A_CmsXmlValueTextBase#setStringValue(org.opencms.file.CmsObject, java.lang.String)
190     */
191    @Override
192    public void setStringValue(CmsObject cms, String value) {
193
194        I_CmsXmlDocument doc = getDocument();
195        if (!CmsStringUtil.isEmpty(value)) {
196            if ((doc != null) && (doc instanceof CmsXmlContent)) {
197                CmsXmlContent content = (CmsXmlContent)doc;
198                if (content.getFile() != null) {
199                    CmsADEConfigData config = OpenCms.getADEManager().lookupConfigurationWithCache(
200                        cms,
201                        content.getFile().getRootPath());
202                    int colonPos = value.indexOf(":");
203                    if (colonPos > -1) {
204                        String keyOrId = value.substring(colonPos + 1);
205                        I_CmsFormatterBean formatter = config.findFormatter(keyOrId);
206                        if (formatter != null) {
207                            String newId = config.isUseFormatterKeys() ? formatter.getKeyOrId() : formatter.getId();
208                            value = value.substring(0, colonPos) + ":" + newId;
209                        }
210                    }
211                }
212            }
213        }
214        super.setStringValue(cms, value);
215    }
216
217}