KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > xml > content > CmsXmlContentFactory


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/xml/content/CmsXmlContentFactory.java,v $
3  * Date : $Date: 2005/06/23 11:11:54 $
4  * Version: $Revision: 1.11 $
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.xml.content;
33
34 import org.opencms.file.CmsFile;
35 import org.opencms.file.CmsObject;
36 import org.opencms.file.CmsPropertyDefinition;
37 import org.opencms.file.CmsResource;
38 import org.opencms.i18n.CmsEncoder;
39 import org.opencms.loader.CmsLoaderException;
40 import org.opencms.loader.CmsXmlContentLoader;
41 import org.opencms.main.CmsException;
42 import org.opencms.main.OpenCms;
43 import org.opencms.xml.CmsXmlContentDefinition;
44 import org.opencms.xml.CmsXmlEntityResolver;
45 import org.opencms.xml.CmsXmlException;
46 import org.opencms.xml.CmsXmlUtils;
47
48 import java.io.UnsupportedEncodingException JavaDoc;
49 import java.util.Locale JavaDoc;
50
51 import javax.servlet.ServletRequest JavaDoc;
52
53 import org.dom4j.Document;
54 import org.dom4j.DocumentHelper;
55 import org.xml.sax.EntityResolver JavaDoc;
56
57 /**
58  * Provides factory methods to unmarshal (read) an XML content object.<p>
59  *
60  * @author Alexander Kandzior
61  *
62  * @version $Revision: 1.11 $
63  *
64  * @since 6.0.0
65  */

66 public final class CmsXmlContentFactory {
67
68     /**
69      * No instances of this class should be created.<p>
70      */

71     private CmsXmlContentFactory() {
72
73         // noop
74
}
75
76     /**
77      * Create a new instance of an XML content based on the given content definiton,
78      * that will have one language node for the given locale all initialized with default values.<p>
79      *
80      * The given encoding is used when marshalling the XML again later.<p>
81      *
82      * @param cms the current users OpenCms content
83      * @param locale the locale to generate the default content for
84      * @param encoding the encoding to use when marshalling the XML content later
85      * @param contentDefinition the content definiton to create the content for
86      *
87      * @return the created XML content
88      */

89     public static CmsXmlContent createDocument(
90         CmsObject cms,
91         Locale JavaDoc locale,
92         String JavaDoc encoding,
93         CmsXmlContentDefinition contentDefinition) {
94
95         // create the XML content
96
return new CmsXmlContent(cms, locale, encoding, contentDefinition);
97     }
98
99     /**
100      * Factory method to unmarshal (generate) a XML content instance from a byte array
101      * that contains XML data.<p>
102      *
103      * When unmarshalling, the encoding is read directly from the XML header of the byte array.
104      * The given encoding is used only when marshalling the XML again later.<p>
105      *
106      * @param xmlData the XML data in a byte array
107      * @param encoding the encoding to use when marshalling the XML content later
108      * @param resolver the XML entitiy resolver to use
109      * @return a XML content instance unmarshalled from the byte array
110      * @throws CmsXmlException if something goes wrong
111      */

112     public static CmsXmlContent unmarshal(byte[] xmlData, String JavaDoc encoding, EntityResolver resolver)
113     throws CmsXmlException {
114
115         return unmarshal(CmsXmlUtils.unmarshalHelper(xmlData, resolver), encoding, resolver);
116     }
117
118     /**
119      * Factory method to unmarshal (read) a XML content instance from a OpenCms VFS file
120      * that contains XML data.<p>
121      *
122      * @param cms the current cms object
123      * @param file the file with the XML data to unmarshal
124      * @return a XML page instance unmarshalled from the provided file
125      * @throws CmsXmlException if something goes wrong
126      */

127     public static CmsXmlContent unmarshal(CmsObject cms, CmsFile file) throws CmsXmlException {
128
129         return unmarshal(cms, file, true);
130     }
131
132     /**
133      * Factory method to unmarshal (read) a XML content instance from a OpenCms VFS file
134      * that contains XML data, using wither the encoding set
135      * in the XML file header, or the encoding set in the VFS file property.<p>
136      *
137      * If you are not sure about the implications of the encoding issues,
138      * use {@link #unmarshal(CmsObject, CmsFile)} instead.<p>
139      *
140      * @param cms the current cms object
141      * @param file the file with the XML data to unmarshal
142      * @param keepEncoding if true, the encoding spefified in the XML header is used,
143      * otherwise the encoding from the VFS file property is used
144      * @return a XML content instance unmarshalled from the provided file
145      * @throws CmsXmlException if something goes wrong
146      */

147     public static CmsXmlContent unmarshal(CmsObject cms, CmsFile file, boolean keepEncoding) throws CmsXmlException {
148
149         byte[] contentBytes = file.getContents();
150         String JavaDoc filename = cms.getSitePath(file);
151
152         String JavaDoc encoding = null;
153         try {
154             encoding = cms.readPropertyObject(filename, CmsPropertyDefinition.PROPERTY_CONTENT_ENCODING, true).getValue();
155         } catch (CmsException e) {
156             // encoding will be null
157
}
158         if (encoding == null) {
159             encoding = OpenCms.getSystemInfo().getDefaultEncoding();
160         } else {
161             encoding = CmsEncoder.lookupEncoding(encoding, null);
162             if (encoding == null) {
163                 throw new CmsXmlException(Messages.get().container(Messages.ERR_XMLCONTENT_INVALID_ENC_1, filename));
164             }
165         }
166
167         CmsXmlContent content;
168         if (contentBytes.length > 0) {
169             // content is initialized
170
if (keepEncoding) {
171                 // use the encoding from the content
172
content = unmarshal(contentBytes, encoding, new CmsXmlEntityResolver(cms));
173             } else {
174                 // use the encoding from the file property
175
// this usually only triggered by a save operation
176
try {
177                     String JavaDoc contentStr = new String JavaDoc(contentBytes, encoding);
178                     content = unmarshal(contentStr, encoding, new CmsXmlEntityResolver(cms));
179                 } catch (UnsupportedEncodingException JavaDoc e) {
180                     // this will not happen since the encodig has already been validated
181
throw new CmsXmlException(Messages.get().container(Messages.ERR_XMLCONTENT_INVALID_ENC_1, filename));
182                 }
183             }
184         } else {
185             // content is empty
186
content = new CmsXmlContent(DocumentHelper.createDocument(), encoding, new CmsXmlEntityResolver(cms));
187         }
188
189         // set the file
190
content.setFile(file);
191         // return the result
192
return content;
193     }
194
195     /**
196      * Factory method to unmarshal (read) a XML content instance from
197      * a resource, using the request attributes as cache.<p>
198      *
199      * @param cms the current OpenCms context object
200      * @param resource the resource to unmarshal
201      * @param req the current request
202      *
203      * @return the unmarshaled xml content, or null if the given resource was not of type {@link org.opencms.file.types.CmsResourceTypeXmlContent}
204      *
205      * @throws CmsException in something goes wrong
206      * @throws CmsLoaderException if no loader for the given <code>resource</code> type ({@link CmsResource#getTypeId()}) is available
207      * @throws CmsXmlException if the given <code>resource</code> is not of type xml content
208      */

209     public static CmsXmlContent unmarshal(CmsObject cms, CmsResource resource, ServletRequest JavaDoc req)
210     throws CmsXmlException, CmsLoaderException, CmsException {
211
212         String JavaDoc rootPath = resource.getRootPath();
213
214         if (!(OpenCms.getResourceManager().getLoader(resource) instanceof CmsXmlContentLoader)) {
215             // sanity check: resource must be of type XML content
216
throw new CmsXmlException(Messages.get().container(
217                 Messages.ERR_XMLCONTENT_INVALID_TYPE_1,
218                 cms.getSitePath(resource)));
219         }
220
221         // try to get the requested content form the current request attributes
222
CmsXmlContent content = (CmsXmlContent)req.getAttribute(rootPath);
223
224         if (content == null) {
225             // unmarshal XML structure from the file content
226
content = unmarshal(cms, CmsFile.upgrade(resource, cms));
227             // store the content as request attribute for future read requests
228
req.setAttribute(rootPath, content);
229         }
230
231         // return the result
232
return content;
233     }
234
235     /**
236      * Factory method to unmarshal (generate) a XML content instance from a XML document.<p>
237      *
238      * The given encoding is used when marshalling the XML again later.<p>
239      *
240      * @param document the XML document to generate the XML content from
241      * @param encoding the encoding to use when marshalling the XML content later
242      * @param resolver the XML entitiy resolver to use
243      * @return a XML content instance unmarshalled from the String
244      */

245     public static CmsXmlContent unmarshal(Document document, String JavaDoc encoding, EntityResolver resolver) {
246
247         return new CmsXmlContent(document, encoding, resolver);
248     }
249
250     /**
251      * Factory method to unmarshal (generate) a XML content instance from a String
252      * that contains XML data.<p>
253      *
254      * The given encoding is used when marshalling the XML again later.<p>
255      *
256      * @param xmlData the XML data in a String
257      * @param encoding the encoding to use when marshalling the XML content later
258      * @param resolver the XML entitiy resolver to use
259      * @return a XML content instance unmarshalled from the String
260      * @throws CmsXmlException if something goes wrong
261      */

262     public static CmsXmlContent unmarshal(String JavaDoc xmlData, String JavaDoc encoding, EntityResolver resolver)
263     throws CmsXmlException {
264
265         // create the XML content object from the provided String
266
CmsXmlContent content = unmarshal(CmsXmlUtils.unmarshalHelper(xmlData, resolver), encoding, resolver);
267         // return the result with the user context set
268
return content;
269
270     }
271 }
Popular Tags