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.containerpage;
029
030import org.opencms.ade.configuration.CmsADEConfigData;
031import org.opencms.ade.configuration.plugins.CmsTemplatePlugin;
032import org.opencms.file.CmsObject;
033import org.opencms.file.CmsResource;
034import org.opencms.file.types.CmsResourceTypeXmlContent;
035import org.opencms.main.CmsLog;
036import org.opencms.main.OpenCms;
037import org.opencms.util.CmsUUID;
038import org.opencms.xml.content.CmsXmlContentProperty;
039import org.opencms.xml.content.I_CmsXmlContentHandler;
040
041import java.util.ArrayList;
042import java.util.Collection;
043import java.util.Collections;
044import java.util.List;
045import java.util.Locale;
046import java.util.Map;
047import java.util.Optional;
048import java.util.Set;
049
050import org.apache.commons.logging.Log;
051
052/**
053 * Wrapper class for formatter beans which delegates all methods to the wrapped formatter bean except those
054 * for which we need to ask the content handler for additional data like element settings or head includes.<p>
055 */
056public class CmsSchemaFormatterBeanWrapper implements I_CmsFormatterBean {
057
058    /** Logger instance for this class. */
059    private static final Log LOG = CmsLog.getLog(CmsSchemaFormatterBeanWrapper.class);
060
061    /** The CMS context to use. */
062    private CmsObject m_cms;
063
064    /** The content handler to use. */
065    private I_CmsXmlContentHandler m_contentHandler;
066
067    /** The resource to use. */
068    private CmsResource m_elementResource;
069
070    /** The wrapped formatter. */
071    private I_CmsFormatterBean m_wrappedFormatter;
072
073    /**
074     * Creates a new wrapper instance.<p>
075     *
076     * @param cms the CMS context to use
077     * @param wrappedBean the wrapped formatter
078     * @param contentHandler the content handler to ask for additional information
079     * @param resource the resource which should be used to ask the content handler for additional information
080     */
081    public CmsSchemaFormatterBeanWrapper(
082        CmsObject cms,
083        I_CmsFormatterBean wrappedBean,
084        I_CmsXmlContentHandler contentHandler,
085        CmsResource resource) {
086
087        m_elementResource = resource;
088        m_contentHandler = contentHandler;
089        m_wrappedFormatter = wrappedBean;
090        m_cms = cms;
091    }
092
093    /**
094     * @see org.opencms.xml.containerpage.I_CmsFormatterBean#getAliasKeys()
095     */
096    public Set<String> getAliasKeys() {
097
098        return Collections.emptySet();
099    }
100
101    /**
102     * @see org.opencms.xml.containerpage.I_CmsFormatterBean#getAllKeys()
103     */
104    public Set<String> getAllKeys() {
105
106        return Collections.emptySet();
107    }
108
109    /**
110     * @see org.opencms.xml.containerpage.I_CmsFormatterBean#getAttributes()
111     */
112    public Map<String, String> getAttributes() {
113
114        return Collections.emptyMap();
115    }
116
117    /**
118     * @see org.opencms.xml.containerpage.I_CmsFormatterBean#getContainerTypes()
119     */
120    public Set<String> getContainerTypes() {
121
122        return m_wrappedFormatter.getContainerTypes();
123    }
124
125    /**
126     * @see org.opencms.xml.containerpage.I_CmsFormatterBean#getCssHeadIncludes()
127     */
128    public Set<String> getCssHeadIncludes() {
129
130        try {
131            return m_contentHandler.getCSSHeadIncludes(m_cms, m_elementResource);
132        } catch (Exception e) {
133            LOG.error(e.getLocalizedMessage(), e);
134            return Collections.emptySet();
135        }
136    }
137
138    /**
139     * @see org.opencms.xml.containerpage.I_CmsFormatterBean#getDescription(Locale)
140     */
141    public String getDescription(Locale locale) {
142
143        return m_wrappedFormatter.getDescription(locale);
144    }
145
146    /**
147     * @see org.opencms.xml.containerpage.I_CmsFormatterBean#getDisplayType()
148     */
149    public String getDisplayType() {
150
151        return null;
152    }
153
154    /**
155     * @see org.opencms.xml.containerpage.I_CmsFormatterBean#getId()
156     */
157    public String getId() {
158
159        return m_wrappedFormatter.getId();
160    }
161
162    /**
163     * @see org.opencms.xml.containerpage.I_CmsFormatterBean#getInlineCss()
164     */
165    public String getInlineCss() {
166
167        return "";
168    }
169
170    /**
171     * @see org.opencms.xml.containerpage.I_CmsFormatterBean#getInlineJavascript()
172     */
173    public String getInlineJavascript() {
174
175        return "";
176    }
177
178    /**
179     * @see org.opencms.xml.containerpage.I_CmsFormatterBean#getJavascriptHeadIncludes()
180     */
181    public List<String> getJavascriptHeadIncludes() {
182
183        try {
184            return new ArrayList<String>(m_contentHandler.getJSHeadIncludes(m_cms, m_elementResource));
185        } catch (Exception e) {
186            LOG.error(e.getLocalizedMessage(), e);
187            return Collections.emptyList();
188        }
189    }
190
191    /**
192     * @see org.opencms.xml.containerpage.I_CmsFormatterBean#getJspRootPath()
193     */
194    public String getJspRootPath() {
195
196        return m_wrappedFormatter.getJspRootPath();
197    }
198
199    /**
200     * @see org.opencms.xml.containerpage.I_CmsFormatterBean#getJspStructureId()
201     */
202    public CmsUUID getJspStructureId() {
203
204        return m_wrappedFormatter.getJspStructureId();
205    }
206
207    /**
208     * @see org.opencms.xml.containerpage.I_CmsFormatterBean#getKey()
209     */
210    public String getKey() {
211
212        return null;
213    }
214
215    /**
216     * @see org.opencms.xml.containerpage.I_CmsFormatterBean#getLocation()
217     */
218    public String getLocation() {
219
220        return m_wrappedFormatter.getLocation();
221    }
222
223    /**
224     * @see org.opencms.xml.containerpage.I_CmsFormatterBean#getMaxWidth()
225     */
226    public int getMaxWidth() {
227
228        return m_wrappedFormatter.getMaxWidth();
229    }
230
231    /**
232     * @see org.opencms.xml.containerpage.I_CmsFormatterBean#getMetaMappings()
233     */
234    public List<CmsMetaMapping> getMetaMappings() {
235
236        return null;
237    }
238
239    /**
240     * @see org.opencms.xml.containerpage.I_CmsFormatterBean#getMinWidth()
241     */
242    public int getMinWidth() {
243
244        return m_wrappedFormatter.getMinWidth();
245    }
246
247    /**
248     * @see org.opencms.xml.containerpage.I_CmsFormatterBean#getNiceName(Locale)
249     */
250    public String getNiceName(Locale locale) {
251
252        return m_wrappedFormatter.getNiceName(locale);
253    }
254
255    /**
256     *
257     * @see org.opencms.xml.containerpage.I_CmsFormatterBean#getRank()
258     */
259    public int getRank() {
260
261        return m_wrappedFormatter.getRank();
262    }
263
264    /**
265     *
266     * @see org.opencms.xml.containerpage.I_CmsFormatterBean#getResourceTypeNames()
267     */
268    public Collection<String> getResourceTypeNames() {
269
270        try {
271            return Collections.singleton(OpenCms.getResourceManager().getResourceType(m_elementResource).getTypeName());
272        } catch (Exception e) {
273            LOG.error(e.getLocalizedMessage(), e);
274            return Collections.singleton(CmsResourceTypeXmlContent.getStaticTypeName());
275        }
276    }
277
278    /**
279     * @see org.opencms.xml.containerpage.I_CmsFormatterBean#getSettings(org.opencms.ade.configuration.CmsADEConfigData)
280     */
281    public Map<String, CmsXmlContentProperty> getSettings(CmsADEConfigData config) {
282
283        return m_contentHandler.getSettings(m_cms, m_elementResource);
284    }
285
286    /**
287     * @see org.opencms.xml.containerpage.I_CmsFormatterBean#getTemplatePlugins()
288     */
289    public List<CmsTemplatePlugin> getTemplatePlugins() {
290
291        return Collections.emptyList();
292    }
293
294    /**
295     * @see org.opencms.xml.containerpage.I_CmsFormatterBean#hasNestedFormatterSettings()
296     */
297    public boolean hasNestedFormatterSettings() {
298
299        return false;
300    }
301
302    /**
303     * @see org.opencms.xml.containerpage.I_CmsFormatterBean#isAllowsSettingsInEditor()
304     */
305    public boolean isAllowsSettingsInEditor() {
306
307        // not supported by schema formatters
308        return false;
309    }
310
311    /**
312     *
313     * @see org.opencms.xml.containerpage.I_CmsFormatterBean#isAutoEnabled()
314     */
315    public boolean isAutoEnabled() {
316
317        return m_wrappedFormatter.isAutoEnabled();
318    }
319
320    /**
321     * @see org.opencms.xml.containerpage.I_CmsFormatterBean#isDetailFormatter()
322     */
323    public boolean isDetailFormatter() {
324
325        return true;
326    }
327
328    /**
329     * @see org.opencms.xml.containerpage.I_CmsFormatterBean#isDisplayFormatter()
330     */
331    public boolean isDisplayFormatter() {
332
333        return false;
334    }
335
336    /**
337     *
338     * @see org.opencms.xml.containerpage.I_CmsFormatterBean#isFromFormatterConfigFile()
339     */
340    public boolean isFromFormatterConfigFile() {
341
342        return m_wrappedFormatter.isFromFormatterConfigFile();
343    }
344
345    /**
346     * @see org.opencms.xml.containerpage.I_CmsFormatterBean#isMatchAll()
347     */
348    public boolean isMatchAll() {
349
350        return m_wrappedFormatter.isMatchAll();
351    }
352
353    /**
354     * @see org.opencms.xml.containerpage.I_CmsFormatterBean#isPreviewFormatter()
355     */
356    public boolean isPreviewFormatter() {
357
358        return m_wrappedFormatter.isPreviewFormatter();
359    }
360
361    /**
362     * @see org.opencms.xml.containerpage.I_CmsFormatterBean#isSearchContent()
363     */
364    public boolean isSearchContent() {
365
366        return m_wrappedFormatter.isSearchContent();
367    }
368
369    /**
370     * @see org.opencms.xml.containerpage.I_CmsFormatterBean#isTypeFormatter()
371     */
372    public boolean isTypeFormatter() {
373
374        return m_wrappedFormatter.isTypeFormatter();
375    }
376
377    /**
378     * @see org.opencms.xml.containerpage.I_CmsFormatterBean#setJspStructureId(org.opencms.util.CmsUUID)
379     */
380    public void setJspStructureId(CmsUUID jspStructureId) {
381
382        m_wrappedFormatter.setJspStructureId(jspStructureId);
383    }
384
385    /**
386     * @see org.opencms.xml.containerpage.I_CmsFormatterBean#useMetaMappingsForNormalElements()
387     */
388    @Override
389    public boolean useMetaMappingsForNormalElements() {
390
391        return false;
392    }
393
394    /**
395     * @see org.opencms.xml.containerpage.I_CmsFormatterBean#withKeys(java.util.Collection)
396     */
397    public Optional<I_CmsFormatterBean> withKeys(Collection<String> keys) {
398
399        // no keys supported
400        return Optional.empty();
401    }
402
403}