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.workplace.tools.content.check;
029
030import org.opencms.file.CmsFile;
031import org.opencms.file.CmsObject;
032import org.opencms.file.CmsResource;
033import org.opencms.main.CmsException;
034import org.opencms.xml.content.CmsXmlContent;
035import org.opencms.xml.content.CmsXmlContentFactory;
036
037import java.util.ArrayList;
038import java.util.List;
039
040/**
041 * This object encapuslates a CmsResource, its content and unmarshalled xml content
042 * for processing in the content check plugins.<p>
043 *
044 *
045 * @since 6.1.2
046 */
047public class CmsContentCheckResource {
048
049    /** Encapsulated content array. */
050    private byte[] m_content;
051
052    /** List of errors found during content check. */
053    private List m_errors;
054
055    /** Encapsulated CmsResource. */
056    private CmsResource m_resource;
057
058    /** List of warnings found during content check. */
059    private List m_warnings;
060
061    /** Encapsulated unmashalled xml content. */
062    private CmsXmlContent m_xmlcontent;
063
064    /**
065     * Constructor, creates an CmsContentCheckResource object.<p>
066     *
067     * @param res the CmsResource to encapsulate in the CmsContentCheckResource.
068     */
069    public CmsContentCheckResource(CmsResource res) {
070
071        m_resource = res;
072        m_content = null;
073        m_xmlcontent = null;
074        m_errors = new ArrayList();
075        m_warnings = new ArrayList();
076    }
077
078    /** Adds a new error to the list of errors for this resource.<p>
079     *
080     * @param error the error message to be added
081     */
082    public void addError(String error) {
083
084        m_errors.add(error);
085    }
086
087    /** Adds a list of errors to the list of errors for this resource.<p>
088     *
089     * @param errors the error messages to be added
090     */
091    public void addErrors(List errors) {
092
093        m_errors.addAll(errors);
094    }
095
096    /** Adds a new warning to the list of warnings for this resource.<p>
097     *
098     * @param warning the warning message to be added
099     */
100    public void addWarning(String warning) {
101
102        m_warnings.add(warning);
103    }
104
105    /** Adds a list of warnings to the list of warnings for this resource.<p>
106     *
107     * @param warnings the error messages to be added
108     */
109    public void addWarnings(List warnings) {
110
111        m_warnings.addAll(warnings);
112    }
113
114    /**
115     * Gets the encapuslated file content.<p>
116     *
117     * @return the byte array holding the file content
118     */
119    public byte[] getContent() {
120
121        return m_content;
122    }
123
124    /**
125     * Gets the list of all errors found during the content checks for this resource.<p>
126     * @return List of erros, delivered as strings
127     */
128    public List getErrors() {
129
130        return m_errors;
131    }
132
133    /**
134     * Gets the encapsulated CmsResource.<p>
135     *
136     * @return the CmsResource
137     */
138    public CmsResource getResource() {
139
140        return m_resource;
141    }
142
143    /**
144     * Gets the root path of the encapsulated CmsResource.<p>
145     *
146     * @return root path of the encapsulated CmsResource
147     */
148    public String getResourceName() {
149
150        return m_resource.getRootPath();
151    }
152
153    /**
154     * Gets the list of all warnings found during the content checks for this resource.<p>
155     * @return List of warnings, delivered as strings
156     */
157    public List getWarnings() {
158
159        return m_warnings;
160    }
161
162    /**
163     * Gets the encapuslated and unmarshalled xml content.<p>
164     *
165     * @return the unmarshalled xml content
166     */
167    public CmsXmlContent getXmlContent() {
168
169        return m_xmlcontent;
170    }
171
172    /**
173     * Loads the content of the encapsulated CmsResource and stores it within the
174     * CmsContentCheckResource. If the content is already existing, it is not loaded
175     * again.<p>
176     *
177     * @param cms the CmsObject
178     * @throws CmsException if loading of the content fails
179     */
180    public void upgradeContent(CmsObject cms) throws CmsException {
181
182        if (m_content == null) {
183            m_content = cms.readFile(m_resource).getContents();
184        }
185    }
186
187    /**
188     * Unmarshalls the content of the encapsulated CmsResource and stores it within the
189     * CmsContentCheckResource. If the xmlcontent is already existing, it is not unmarshalled
190     * again.<p>
191     *
192     * @param cms the CmsObject
193     * @throws CmsException if loading of the content fails
194     */
195    public void upgradeXmlContent(CmsObject cms) throws CmsException {
196
197        if (m_xmlcontent == null) {
198            CmsFile file = cms.readFile(m_resource);
199            m_xmlcontent = CmsXmlContentFactory.unmarshal(cms, file);
200        }
201    }
202}