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.ade.configuration.plugins;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsResource;
032import org.opencms.main.CmsException;
033import org.opencms.util.CmsUUID;
034import org.opencms.xml.content.CmsXmlContent;
035import org.opencms.xml.content.CmsXmlContentFactory;
036import org.opencms.xml.content.CmsXmlContentRootLocation;
037
038import java.util.ArrayList;
039import java.util.Collections;
040import java.util.List;
041import java.util.Locale;
042
043import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
044import org.apache.commons.lang3.builder.ToStringStyle;
045
046/**
047 * Immutable collection of template plugins, normally read from a file of type site-plugin.
048 */
049public class CmsSitePlugin {
050
051    /** Content value node name. */
052    public static final String N_DESCRIPTION = "Description";
053
054    /** Content value node name. */
055    public static final String N_NICE_NAME = "NiceName";
056
057    /** Content value node name. */
058    public static final String N_PLUGIN = "Plugin";
059
060    /** The nice name. */
061    private String m_niceName;
062
063    /** The description. */
064    private String m_description;
065
066    /** The origin, for debugging. */
067    private String m_origin;
068
069    /** The list of plugins. */
070    private List<CmsTemplatePlugin> m_plugins;
071
072    /** The id (normally the structure id of the content from which this was read. */
073    private CmsUUID m_id;
074
075    /**
076     * Creates a new instance.
077     *
078     * @param id the id of the plugin group
079     * @param niceName the nice name
080     * @param description the description
081     * @param plugins the list of plugins
082     * @param origin the origin (for debugging purposes)
083     */
084    public CmsSitePlugin(
085        CmsUUID id,
086        String niceName,
087        String description,
088        List<CmsTemplatePlugin> plugins,
089        String origin) {
090
091        m_id = id;
092        m_plugins = Collections.unmodifiableList(new ArrayList<>(plugins));
093        m_niceName = niceName;
094        m_description = description;
095        m_origin = origin;
096    }
097
098    /**
099     * Reads a site plugin from a file.
100     *
101     * @param cms the CMS context to use
102     * @param res the resource
103     * @return the site plugin read from the file
104     * @throws CmsException if something goes wrong
105     */
106    public static CmsSitePlugin read(CmsObject cms, CmsResource res) throws CmsException {
107
108        CmsXmlContent content = CmsXmlContentFactory.unmarshal(cms, cms.readFile(res));
109        return readSitePlugin(cms, content);
110
111    }
112
113    /**
114     * Reads a list of plugins from the given XML content.
115     *
116     * @param cms the CMS context
117     * @param content the XML content object
118     * @return the template plugin group
119     */
120    public static CmsSitePlugin readSitePlugin(CmsObject cms, CmsXmlContent content) {
121
122        CmsXmlContentRootLocation root = new CmsXmlContentRootLocation(content, Locale.ENGLISH);
123        String niceName = root.getSubValue(N_NICE_NAME).getValue().getStringValue(cms).trim();
124        String description = root.getSubValue(N_DESCRIPTION).getValue().getStringValue(cms).trim();
125        List<CmsTemplatePlugin> plugins = CmsTemplatePlugin.parsePlugins(cms, root, N_PLUGIN);
126        CmsSitePlugin result = new CmsSitePlugin(
127            content.getFile().getStructureId(),
128            niceName,
129            description,
130            plugins,
131            content.getFile().getRootPath());
132        return result;
133
134    }
135
136    /**
137     * Gets the description.
138     *
139     * @return the description
140     */
141    public String getDescription() {
142
143        return m_description;
144    }
145
146    /**
147     * Gets the id.
148     *
149     * <p>Normally, this is the structure id of the file from which this group was read.
150     *
151     * @return the id
152     */
153    public CmsUUID getId() {
154
155        return m_id;
156    }
157
158    /**
159     * Gets the nice name.
160     *
161     * @return the nice name
162     */
163    public String getNiceName() {
164
165        return m_niceName;
166    }
167
168    /**
169     * Gets the origin, for debugging purposes.
170     *
171     * @return the origin
172     */
173    public String getOrigin() {
174
175        return m_origin;
176    }
177
178    /**
179     * Gets the immutable list of plugins.
180     *
181     * @return the list of plugins in this collection
182     */
183    public List<CmsTemplatePlugin> getPlugins() {
184
185        return m_plugins;
186    }
187
188    /**
189     * @see java.lang.Object#toString()
190     */
191    @Override
192    public String toString() {
193
194        return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
195    }
196
197}