KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > file > types > CmsResourceTypeXmlPage


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/file/types/CmsResourceTypeXmlPage.java,v $
3  * Date : $Date: 2006/03/27 14:52:48 $
4  * Version: $Revision: 1.25 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.file.types;
33
34 import org.opencms.configuration.CmsConfigurationException;
35 import org.opencms.db.CmsSecurityManager;
36 import org.opencms.file.CmsFile;
37 import org.opencms.file.CmsObject;
38 import org.opencms.file.CmsResource;
39 import org.opencms.file.CmsResourceFilter;
40 import org.opencms.loader.CmsXmlPageLoader;
41 import org.opencms.main.CmsException;
42 import org.opencms.main.CmsLog;
43 import org.opencms.main.OpenCms;
44 import org.opencms.security.CmsPermissionSet;
45 import org.opencms.staticexport.CmsLink;
46 import org.opencms.staticexport.CmsLinkTable;
47 import org.opencms.util.CmsHtmlConverter;
48 import org.opencms.validation.I_CmsXmlDocumentLinkValidatable;
49 import org.opencms.xml.CmsXmlEntityResolver;
50 import org.opencms.xml.CmsXmlException;
51 import org.opencms.xml.page.CmsXmlPage;
52 import org.opencms.xml.page.CmsXmlPageFactory;
53
54 import java.util.ArrayList JavaDoc;
55 import java.util.Collections JavaDoc;
56 import java.util.Iterator JavaDoc;
57 import java.util.List JavaDoc;
58 import java.util.Locale JavaDoc;
59
60 import org.apache.commons.logging.Log;
61
62 /**
63  * Resource type descriptor for the type "xmlpage".<p>
64  *
65  * @author Alexander Kandzior
66  *
67  * @version $Revision: 1.25 $
68  *
69  * @since 6.0.0
70  */

71 public class CmsResourceTypeXmlPage extends A_CmsResourceType implements I_CmsXmlDocumentLinkValidatable {
72
73     /** The log object for this class. */
74     private static final Log LOG = CmsLog.getLog(CmsResourceTypeXmlPage.class);
75
76     /** Indicates that the static configuration of the resource type has been frozen. */
77     private static boolean m_staticFrozen;
78
79     /** The static type id of this resource type. */
80     private static int m_staticTypeId;
81
82     /** The type id of this resource type. */
83     private static final int RESOURCE_TYPE_ID = 6;
84
85     /** The name of this resource type. */
86     private static final String JavaDoc RESOURCE_TYPE_NAME = "xmlpage";
87
88     /**
89      * Default constructor, used to initialize member variables.<p>
90      */

91     public CmsResourceTypeXmlPage() {
92
93         super();
94         m_typeId = RESOURCE_TYPE_ID;
95         m_typeName = RESOURCE_TYPE_NAME;
96     }
97
98     /**
99      * Returns the static type id of this (default) resource type.<p>
100      *
101      * @return the static type id of this (default) resource type
102      */

103     public static int getStaticTypeId() {
104
105         return m_staticTypeId;
106     }
107
108     /**
109      * Returns the static type name of this (default) resource type.<p>
110      *
111      * @return the static type name of this (default) resource type
112      */

113     public static String JavaDoc getStaticTypeName() {
114
115         return RESOURCE_TYPE_NAME;
116     }
117
118     /**
119      * @see org.opencms.validation.I_CmsXmlDocumentLinkValidatable#findLinks(org.opencms.file.CmsObject, org.opencms.file.CmsResource)
120      */

121     public List JavaDoc findLinks(CmsObject cms, CmsResource resource) {
122
123         List JavaDoc links = new ArrayList JavaDoc();
124         CmsFile file = null;
125         CmsXmlPage xmlPage = null;
126         List JavaDoc locales = null;
127         List JavaDoc elementNames = null;
128         String JavaDoc elementName = null;
129         CmsLinkTable linkTable = null;
130         CmsLink link = null;
131
132         try {
133             file = cms.readFile(
134                 cms.getRequestContext().removeSiteRoot(resource.getRootPath()),
135                 CmsResourceFilter.IGNORE_EXPIRATION);
136         } catch (CmsException e) {
137             if (LOG.isErrorEnabled()) {
138                 LOG.error(org.opencms.db.Messages.get().getBundle().key(
139                     org.opencms.db.Messages.ERR_READ_RESOURCE_1,
140                     cms.getSitePath(resource)), e);
141             }
142
143             return Collections.EMPTY_LIST;
144         }
145
146         try {
147             xmlPage = CmsXmlPageFactory.unmarshal(cms, file);
148             locales = xmlPage.getLocales();
149
150             // iterate over all languages
151
Iterator JavaDoc i = locales.iterator();
152             while (i.hasNext()) {
153                 Locale JavaDoc locale = (Locale JavaDoc)i.next();
154                 elementNames = xmlPage.getNames(locale);
155
156                 // iterate over all body elements per language
157
Iterator JavaDoc j = elementNames.iterator();
158                 while (j.hasNext()) {
159                     elementName = (String JavaDoc)j.next();
160                     linkTable = xmlPage.getLinkTable(elementName, locale);
161
162                     // iterate over all links inside a body element
163
Iterator JavaDoc k = linkTable.iterator();
164                     while (k.hasNext()) {
165                         link = (CmsLink)k.next();
166
167                         // external links are ommitted
168
if (link.isInternal()) {
169                             links.add(link.getTarget());
170                         }
171                     }
172                 }
173             }
174         } catch (CmsXmlException e) {
175             if (LOG.isErrorEnabled()) {
176                 LOG.error(
177                     Messages.get().getBundle().key(Messages.ERR_PROCESS_HTML_CONTENT_1, cms.getSitePath(resource)),
178                     e);
179             }
180
181             return Collections.EMPTY_LIST;
182         }
183
184         return links;
185     }
186
187     /**
188      * @see org.opencms.file.types.I_CmsResourceType#getCachePropertyDefault()
189      */

190     public String JavaDoc getCachePropertyDefault() {
191
192         return "element;locale;";
193     }
194
195     /**
196      * @see org.opencms.file.types.I_CmsResourceType#getLoaderId()
197      */

198     public int getLoaderId() {
199
200         return CmsXmlPageLoader.RESOURCE_LOADER_ID;
201     }
202
203     /**
204      * @see org.opencms.file.types.A_CmsResourceType#initConfiguration(java.lang.String, java.lang.String, String)
205      */

206     public void initConfiguration(String JavaDoc name, String JavaDoc id, String JavaDoc className) throws CmsConfigurationException {
207
208         if ((OpenCms.getRunLevel() > OpenCms.RUNLEVEL_2_INITIALIZING) && m_staticFrozen) {
209             // configuration already frozen
210
throw new CmsConfigurationException(Messages.get().container(
211                 Messages.ERR_CONFIG_FROZEN_3,
212                 this.getClass().getName(),
213                 getStaticTypeName(),
214                 new Integer JavaDoc(getStaticTypeId())));
215         }
216
217         if (!RESOURCE_TYPE_NAME.equals(name)) {
218             // default resource type MUST have default name
219
throw new CmsConfigurationException(Messages.get().container(
220                 Messages.ERR_INVALID_RESTYPE_CONFIG_NAME_3,
221                 this.getClass().getName(),
222                 RESOURCE_TYPE_NAME,
223                 name));
224         }
225
226         // freeze the configuration
227
m_staticFrozen = true;
228
229         super.initConfiguration(RESOURCE_TYPE_NAME, id, className);
230         // set static members with values from the configuration
231
m_staticTypeId = m_typeId;
232     }
233
234     /**
235      * @see org.opencms.file.types.I_CmsResourceType#isDirectEditable()
236      */

237     public boolean isDirectEditable() {
238
239         return true;
240     }
241
242     /**
243      * @see org.opencms.file.types.I_CmsResourceType#writeFile(org.opencms.file.CmsObject, CmsSecurityManager, CmsFile)
244      */

245     public CmsFile writeFile(CmsObject cms, CmsSecurityManager securityManager, CmsFile resource) throws CmsException {
246
247         // check if the user has write access and if resource is locked
248
// done here so that all the XML operations are not performed if permissions not granted
249
securityManager.checkPermissions(
250             cms.getRequestContext(),
251             resource,
252             CmsPermissionSet.ACCESS_WRITE,
253             true,
254             CmsResourceFilter.ALL);
255
256         // empty file content is allowed
257
if (resource.getLength() > 0) {
258             // read the xml page, use the encoding set in the property
259
CmsXmlPage xmlPage = CmsXmlPageFactory.unmarshal(cms, resource, false);
260             // validate the xml structure before writing the file
261
// an exception will be thrown if the structure is invalid
262
xmlPage.validateXmlStructure(new CmsXmlEntityResolver(cms));
263             // read the content-conversion property
264
String JavaDoc contentConversion = CmsHtmlConverter.getConversionSettings(cms, resource);
265             xmlPage.setConversion(contentConversion);
266             // correct the HTML structure
267
resource = xmlPage.correctXmlStructure(cms);
268         }
269         // xml is valid if no exception occured
270
return super.writeFile(cms, securityManager, resource);
271     }
272
273 }
Popular Tags