001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/file/types/CmsResourceTypeXmlPage.java,v $
003: * Date : $Date: 2008-02-27 12:05:45 $
004: * Version: $Revision: 1.30 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software GmbH, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.file.types;
033:
034: import org.opencms.configuration.CmsConfigurationException;
035: import org.opencms.db.CmsSecurityManager;
036: import org.opencms.file.CmsFile;
037: import org.opencms.file.CmsObject;
038: import org.opencms.file.CmsResource;
039: import org.opencms.file.CmsResourceFilter;
040: import org.opencms.loader.CmsXmlPageLoader;
041: import org.opencms.main.CmsException;
042: import org.opencms.main.CmsLog;
043: import org.opencms.main.OpenCms;
044: import org.opencms.relations.CmsLink;
045: import org.opencms.security.CmsPermissionSet;
046: import org.opencms.staticexport.CmsLinkTable;
047: import org.opencms.util.CmsHtmlConverter;
048: import org.opencms.xml.CmsXmlEntityResolver;
049: import org.opencms.xml.CmsXmlException;
050: import org.opencms.xml.page.CmsXmlPage;
051: import org.opencms.xml.page.CmsXmlPageFactory;
052:
053: import java.util.ArrayList;
054: import java.util.Collections;
055: import java.util.HashSet;
056: import java.util.Iterator;
057: import java.util.List;
058: import java.util.Locale;
059: import java.util.Set;
060:
061: import org.apache.commons.logging.Log;
062:
063: /**
064: * Resource type descriptor for the type "xmlpage".<p>
065: *
066: * @author Alexander Kandzior
067: *
068: * @version $Revision: 1.30 $
069: *
070: * @since 6.0.0
071: */
072: public class CmsResourceTypeXmlPage extends
073: A_CmsResourceTypeLinkParseable {
074:
075: /** The log object for this class. */
076: private static final Log LOG = CmsLog
077: .getLog(CmsResourceTypeXmlPage.class);
078:
079: /** Indicates that the static configuration of the resource type has been frozen. */
080: private static boolean m_staticFrozen;
081:
082: /** The static type id of this resource type. */
083: private static int m_staticTypeId;
084:
085: /** The type id of this resource type. */
086: private static final int RESOURCE_TYPE_ID = 6;
087:
088: /** The name of this resource type. */
089: private static final String RESOURCE_TYPE_NAME = "xmlpage";
090:
091: /**
092: * Default constructor, used to initialize member variables.<p>
093: */
094: public CmsResourceTypeXmlPage() {
095:
096: super ();
097: m_typeId = RESOURCE_TYPE_ID;
098: m_typeName = RESOURCE_TYPE_NAME;
099: }
100:
101: /**
102: * Returns the static type id of this (default) resource type.<p>
103: *
104: * @return the static type id of this (default) resource type
105: */
106: public static int getStaticTypeId() {
107:
108: return m_staticTypeId;
109: }
110:
111: /**
112: * Returns the static type name of this (default) resource type.<p>
113: *
114: * @return the static type name of this (default) resource type
115: */
116: public static String getStaticTypeName() {
117:
118: return RESOURCE_TYPE_NAME;
119: }
120:
121: /**
122: * Returns <code>true</code> in case the given resource is an XML page.<p>
123: *
124: * Internally this checks if the type id for the given resource is
125: * identical type id of the XML page.<p>
126: *
127: * @param resource the resource to check
128: *
129: * @return <code>true</code> in case the given resource is an XML page
130: *
131: * @since 7.0.2
132: */
133: public static boolean isXmlPage(CmsResource resource) {
134:
135: boolean result = false;
136: if (resource != null) {
137: result = resource.getTypeId() == m_staticTypeId;
138: }
139: return result;
140: }
141:
142: /**
143: * @see org.opencms.file.types.I_CmsResourceType#getCachePropertyDefault()
144: */
145: public String getCachePropertyDefault() {
146:
147: return "element;locale;";
148: }
149:
150: /**
151: * @see org.opencms.file.types.I_CmsResourceType#getLoaderId()
152: */
153: public int getLoaderId() {
154:
155: return CmsXmlPageLoader.RESOURCE_LOADER_ID;
156: }
157:
158: /**
159: * @see org.opencms.file.types.A_CmsResourceType#initConfiguration(java.lang.String, java.lang.String, String)
160: */
161: public void initConfiguration(String name, String id,
162: String className) throws CmsConfigurationException {
163:
164: if ((OpenCms.getRunLevel() > OpenCms.RUNLEVEL_2_INITIALIZING)
165: && m_staticFrozen) {
166: // configuration already frozen
167: throw new CmsConfigurationException(Messages.get()
168: .container(Messages.ERR_CONFIG_FROZEN_3,
169: this .getClass().getName(),
170: getStaticTypeName(),
171: new Integer(getStaticTypeId())));
172: }
173:
174: if (!RESOURCE_TYPE_NAME.equals(name)) {
175: // default resource type MUST have default name
176: throw new CmsConfigurationException(Messages.get()
177: .container(
178: Messages.ERR_INVALID_RESTYPE_CONFIG_NAME_3,
179: this .getClass().getName(),
180: RESOURCE_TYPE_NAME, name));
181: }
182:
183: // freeze the configuration
184: m_staticFrozen = true;
185:
186: super .initConfiguration(RESOURCE_TYPE_NAME, id, className);
187: // set static members with values from the configuration
188: m_staticTypeId = m_typeId;
189: }
190:
191: /**
192: * @see org.opencms.relations.I_CmsLinkParseable#parseLinks(org.opencms.file.CmsObject, org.opencms.file.CmsFile)
193: */
194: public List parseLinks(CmsObject cms, CmsFile file) {
195:
196: Set links = new HashSet();
197: try {
198: CmsXmlPage xmlPage = CmsXmlPageFactory.unmarshal(cms, file);
199: List locales = xmlPage.getLocales();
200:
201: // iterate over all languages
202: Iterator i = locales.iterator();
203: while (i.hasNext()) {
204: Locale locale = (Locale) i.next();
205: List elementNames = xmlPage.getNames(locale);
206:
207: // iterate over all body elements per language
208: Iterator j = elementNames.iterator();
209: while (j.hasNext()) {
210: String elementName = (String) j.next();
211: CmsLinkTable linkTable = xmlPage.getLinkTable(
212: elementName, locale);
213:
214: // iterate over all links inside a body element
215: Iterator k = linkTable.iterator();
216: while (k.hasNext()) {
217: CmsLink link = (CmsLink) k.next();
218: if (link.isInternal()) {
219: link.checkConsistency(cms);
220: links.add(link);
221: }
222: }
223: }
224: }
225: } catch (CmsXmlException e) {
226: if (LOG.isErrorEnabled()) {
227: LOG.error(Messages.get().getBundle().key(
228: Messages.ERR_PROCESS_HTML_CONTENT_1,
229: cms.getSitePath(file)), e);
230: }
231:
232: return Collections.EMPTY_LIST;
233: }
234: return new ArrayList(links);
235: }
236:
237: /**
238: * @see org.opencms.file.types.I_CmsResourceType#writeFile(org.opencms.file.CmsObject, CmsSecurityManager, CmsFile)
239: */
240: public CmsFile writeFile(CmsObject cms,
241: CmsSecurityManager securityManager, CmsFile resource)
242: throws CmsException {
243:
244: // check if the user has write access and if resource is locked
245: // done here so that all the XML operations are not performed if permissions not granted
246: securityManager.checkPermissions(cms.getRequestContext(),
247: resource, CmsPermissionSet.ACCESS_WRITE, true,
248: CmsResourceFilter.ALL);
249:
250: // empty file content is allowed
251: if (resource.getLength() > 0) {
252: // read the xml page, use the encoding set in the property
253: CmsXmlPage xmlPage = CmsXmlPageFactory.unmarshal(cms,
254: resource, false);
255: // validate the xml structure before writing the file
256: // an exception will be thrown if the structure is invalid
257: xmlPage.validateXmlStructure(new CmsXmlEntityResolver(cms));
258: // read the content-conversion property
259: String contentConversion = CmsHtmlConverter
260: .getConversionSettings(cms, resource);
261: xmlPage.setConversion(contentConversion);
262: // correct the HTML structure
263: resource = xmlPage.correctXmlStructure(cms);
264: }
265:
266: // xml is valid if no exception occurred
267: return super.writeFile(cms, securityManager, resource);
268: }
269: }
|