KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/file/types/CmsResourceTypeXmlContent.java,v $
3  * Date : $Date: 2005/06/27 23:22:16 $
4  * Version: $Revision: 1.22 $
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.db.CmsSecurityManager;
35 import org.opencms.file.CmsFile;
36 import org.opencms.file.CmsObject;
37 import org.opencms.file.CmsResource;
38 import org.opencms.file.CmsResourceFilter;
39 import org.opencms.loader.CmsXmlContentLoader;
40 import org.opencms.main.CmsException;
41 import org.opencms.main.OpenCms;
42 import org.opencms.security.CmsPermissionSet;
43 import org.opencms.xml.CmsXmlContentDefinition;
44 import org.opencms.xml.content.CmsXmlContent;
45 import org.opencms.xml.content.CmsXmlContentFactory;
46
47 import java.util.List JavaDoc;
48 import java.util.Locale JavaDoc;
49 import java.util.Map JavaDoc;
50 import java.util.TreeMap JavaDoc;
51
52 /**
53  * Resource type descriptor for the type "xmlcontent".<p>
54  *
55  * @author Alexander Kandzior
56  *
57  * @version $Revision: 1.22 $
58  *
59  * @since 6.0.0
60  */

61 public class CmsResourceTypeXmlContent extends A_CmsResourceType {
62
63     /** Configuration key for the (optional) schema. */
64     public static final String JavaDoc CONFIGURATION_SCHEMA = "schema";
65
66     /** The (optional) schema of this resource. */
67     private String JavaDoc m_schema;
68
69     /**
70      * @see org.opencms.file.types.A_CmsResourceType#addConfigurationParameter(java.lang.String, java.lang.String)
71      */

72     public void addConfigurationParameter(String JavaDoc paramName, String JavaDoc paramValue) {
73
74         super.addConfigurationParameter(paramName, paramValue);
75         if (CONFIGURATION_SCHEMA.equalsIgnoreCase(paramName)) {
76             m_schema = paramValue.trim();
77         }
78     }
79
80     /**
81      * @see org.opencms.file.types.I_CmsResourceType#createResource(org.opencms.file.CmsObject, org.opencms.db.CmsSecurityManager, java.lang.String, byte[], java.util.List)
82      */

83     public CmsResource createResource(
84         CmsObject cms,
85         CmsSecurityManager securityManager,
86         String JavaDoc resourcename,
87         byte[] content,
88         List JavaDoc properties) throws CmsException {
89
90         if ((m_schema != null) && ((content == null) || (content.length == 0))) {
91             // unmarshal the content definition for the new resource
92
CmsXmlContentDefinition contentDefinition = CmsXmlContentDefinition.unmarshal(cms, m_schema);
93
94             // read the default locale for the new resource
95
Locale JavaDoc locale = (Locale JavaDoc)OpenCms.getLocaleManager().getDefaultLocales(
96                 cms,
97                 CmsResource.getParentFolder(resourcename)).get(0);
98
99             // create the new content from the content defintion
100
CmsXmlContent newContent = CmsXmlContentFactory.createDocument(
101                 cms,
102                 locale,
103                 OpenCms.getSystemInfo().getDefaultEncoding(),
104                 contentDefinition);
105             // get the bytes from the created content
106
content = newContent.marshal();
107         }
108
109         // now create the resource using the super class
110
return super.createResource(cms, securityManager, resourcename, content, properties);
111     }
112
113     /**
114      * @see org.opencms.file.types.I_CmsResourceType#getCachePropertyDefault()
115      */

116     public String JavaDoc getCachePropertyDefault() {
117
118         return "element;locale;";
119     }
120
121     /**
122      * @see org.opencms.file.types.A_CmsResourceType#getConfiguration()
123      */

124     public Map JavaDoc getConfiguration() {
125
126         Map JavaDoc result = new TreeMap JavaDoc();
127         if (m_schema != null) {
128             result.put(CONFIGURATION_SCHEMA, m_schema);
129         }
130         Map JavaDoc additional = super.getConfiguration();
131         if (additional != null) {
132             result.putAll(additional);
133         }
134         return result;
135     }
136
137     /**
138      * @see org.opencms.file.types.I_CmsResourceType#getLoaderId()
139      */

140     public int getLoaderId() {
141
142         return CmsXmlContentLoader.RESOURCE_LOADER_ID;
143     }
144
145     /**
146      * @see org.opencms.file.types.I_CmsResourceType#isDirectEditable()
147      */

148     public boolean isDirectEditable() {
149
150         return true;
151     }
152
153     /**
154      * @see org.opencms.file.types.I_CmsResourceType#writeFile(org.opencms.file.CmsObject, CmsSecurityManager, CmsFile)
155      */

156     public CmsFile writeFile(CmsObject cms, CmsSecurityManager securityManager, CmsFile resource) throws CmsException {
157
158         // check if the user has write access and if resource is locked
159
// done here so that all the XML operations are not performed if permissions not granted
160
securityManager.checkPermissions(
161             cms.getRequestContext(),
162             resource,
163             CmsPermissionSet.ACCESS_WRITE,
164             true,
165             CmsResourceFilter.ALL);
166         // read the xml content, use the encoding set in the property
167
CmsXmlContent xmlContent = CmsXmlContentFactory.unmarshal(cms, resource, false);
168         // call the content handler for post-processing
169
resource = xmlContent.getContentDefinition().getContentHandler().prepareForWrite(cms, xmlContent, resource);
170         // now write the file
171
return super.writeFile(cms, securityManager, resource);
172     }
173
174 }
Popular Tags