KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > loader > A_CmsXmlDocumentLoader


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/loader/A_CmsXmlDocumentLoader.java,v $
3  * Date : $Date: 2006/07/20 12:02:21 $
4  * Version: $Revision: 1.3 $
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.loader;
33
34 import org.opencms.file.CmsObject;
35 import org.opencms.file.CmsResource;
36 import org.opencms.i18n.CmsLocaleManager;
37 import org.opencms.main.CmsException;
38 import org.opencms.main.CmsLog;
39 import org.opencms.main.OpenCms;
40 import org.opencms.xml.I_CmsXmlDocument;
41
42 import java.io.IOException JavaDoc;
43 import java.util.List JavaDoc;
44 import java.util.Locale JavaDoc;
45 import java.util.Map JavaDoc;
46
47 import javax.servlet.ServletException JavaDoc;
48 import javax.servlet.ServletRequest JavaDoc;
49 import javax.servlet.ServletResponse JavaDoc;
50 import javax.servlet.http.HttpServletRequest JavaDoc;
51 import javax.servlet.http.HttpServletResponse JavaDoc;
52
53 /**
54  * OpenCms base loader implementation for resources of type <code>{@link org.opencms.xml.I_CmsXmlDocument}</code>.<p>
55  *
56  * @author Alexander Kandzior
57  *
58  * @version $Revision: 1.3 $
59  *
60  * @since 6.2.0
61  */

62 abstract class A_CmsXmlDocumentLoader implements I_CmsResourceLoader, I_CmsResourceStringDumpLoader {
63
64     /**
65      * @see org.opencms.configuration.I_CmsConfigurationParameterHandler#addConfigurationParameter(java.lang.String, java.lang.String)
66      */

67     public void addConfigurationParameter(String JavaDoc paramName, String JavaDoc paramValue) {
68
69         // xml document loaders require no parameters
70
}
71
72     /**
73      * @see org.opencms.loader.I_CmsResourceLoader#destroy()
74      */

75     public void destroy() {
76
77         // NOOP
78
}
79
80     /**
81      * @see org.opencms.loader.I_CmsResourceLoader#dump(org.opencms.file.CmsObject, org.opencms.file.CmsResource, java.lang.String, java.util.Locale, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
82      */

83     public byte[] dump(
84         CmsObject cms,
85         CmsResource resource,
86         String JavaDoc element,
87         Locale JavaDoc selectedLocale,
88         HttpServletRequest JavaDoc req,
89         HttpServletResponse JavaDoc res) throws CmsException, IOException JavaDoc {
90
91         if ((element == null) || (selectedLocale == null)) {
92             // element and locale to display must be specified
93
throw new CmsLoaderException(Messages.get().container(
94                 Messages.ERR_LOADER_XML_NEED_ELEMENT_LOCALE_1,
95                 resource.getRootPath()));
96         }
97
98         // get the value as a String
99
String JavaDoc value = dumpAsString(cms, resource, element, selectedLocale, req, res);
100
101         if (value != null) {
102             // extract the XML document from the current request (should have been cached already)
103
I_CmsXmlDocument doc = unmarshalXmlDocument(cms, resource, req);
104             // convert the value to bytes
105
return value.getBytes(doc.getEncoding());
106         }
107         return new byte[0];
108     }
109
110     /**
111      * @see org.opencms.loader.I_CmsResourceStringDumpLoader#dumpAsString(org.opencms.file.CmsObject, org.opencms.file.CmsResource, java.lang.String, java.util.Locale, javax.servlet.ServletRequest, javax.servlet.ServletResponse)
112      */

113     public String JavaDoc dumpAsString(
114         CmsObject cms,
115         CmsResource resource,
116         String JavaDoc element,
117         Locale JavaDoc selectedLocale,
118         ServletRequest JavaDoc req,
119         ServletResponse JavaDoc res) throws CmsException {
120
121         // extract the XML document from the current request
122
I_CmsXmlDocument doc = unmarshalXmlDocument(cms, resource, req);
123
124         // check the page locales
125
List JavaDoc locales = doc.getLocales(element);
126         if (locales.isEmpty()) {
127             // selected element is not available in any locale
128
return null;
129         }
130         // try to find the best matching locale
131
Locale JavaDoc locale = OpenCms.getLocaleManager().getBestMatchingLocale(
132             selectedLocale,
133             OpenCms.getLocaleManager().getDefaultLocales(cms, cms.getSitePath(resource)),
134             locales);
135         if (locale == null) {
136             // no locale can be determined to display, output a meaningfull error message
137
throw new CmsLoaderException(Messages.get().container(
138                 Messages.ERR_LOADER_UNKNOWN_LOCALE_5,
139                 new Object JavaDoc[] {
140                     resource.getRootPath(),
141                     element,
142                     selectedLocale,
143                     CmsLocaleManager.getLocaleNames(locales),
144                     CmsLocaleManager.getLocaleNames(OpenCms.getLocaleManager().getDefaultLocales(
145                         cms,
146                         cms.getSitePath(resource)))}));
147         }
148         // resturn the appropriate content
149
return doc.getStringValue(cms, element, locale);
150     }
151
152     /**
153      * @see org.opencms.loader.I_CmsResourceLoader#export(org.opencms.file.CmsObject, org.opencms.file.CmsResource, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
154      */

155     public byte[] export(CmsObject cms, CmsResource resource, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
156     throws ServletException JavaDoc, IOException JavaDoc, CmsException {
157
158         CmsTemplateLoaderFacade loaderFacade = OpenCms.getResourceManager().getTemplateLoaderFacade(
159             cms,
160             resource,
161             getTemplatePropertyDefinition());
162         return loaderFacade.getLoader().export(cms, loaderFacade.getLoaderStartResource(), req, res);
163     }
164
165     /**
166      * Returns <code>null</code> since XML document loaders does usually not need to be configured.<p>
167      *
168      * @see org.opencms.configuration.I_CmsConfigurationParameterHandler#getConfiguration()
169      */

170     public Map JavaDoc getConfiguration() {
171
172         return null;
173     }
174
175     /**
176      * @see org.opencms.configuration.I_CmsConfigurationParameterHandler#initConfiguration()
177      */

178     public void initConfiguration() {
179
180         if (CmsLog.INIT.isInfoEnabled()) {
181             CmsLog.INIT.info(Messages.get().getBundle().key(
182                 Messages.INIT_LOADER_INITIALIZED_1,
183                 this.getClass().getName()));
184         }
185     }
186
187     /**
188      * @see org.opencms.loader.I_CmsResourceLoader#isStaticExportEnabled()
189      */

190     public boolean isStaticExportEnabled() {
191
192         return true;
193     }
194
195     /**
196      * @see org.opencms.loader.I_CmsResourceLoader#isStaticExportProcessable()
197      */

198     public boolean isStaticExportProcessable() {
199
200         return true;
201     }
202
203     /**
204      * @see org.opencms.loader.I_CmsResourceLoader#isUsableForTemplates()
205      */

206     public boolean isUsableForTemplates() {
207
208         return false;
209     }
210
211     /**
212      * @see org.opencms.loader.I_CmsResourceLoader#isUsingUriWhenLoadingTemplate()
213      */

214     public boolean isUsingUriWhenLoadingTemplate() {
215
216         return false;
217     }
218
219     /**
220      * @see org.opencms.loader.I_CmsResourceLoader#load(org.opencms.file.CmsObject, org.opencms.file.CmsResource, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
221      */

222     public void load(CmsObject cms, CmsResource resource, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
223     throws ServletException JavaDoc, IOException JavaDoc, CmsException {
224
225         // ensure the requested XML document gets cached in the request attributes
226
unmarshalXmlDocument(cms, resource, req);
227
228         CmsTemplateLoaderFacade loaderFacade = OpenCms.getResourceManager().getTemplateLoaderFacade(
229             cms,
230             resource,
231             getTemplatePropertyDefinition());
232         loaderFacade.getLoader().load(cms, loaderFacade.getLoaderStartResource(), req, res);
233     }
234
235     /**
236      * @see org.opencms.loader.I_CmsResourceLoader#service(org.opencms.file.CmsObject, org.opencms.file.CmsResource, javax.servlet.ServletRequest, javax.servlet.ServletResponse)
237      */

238     public void service(CmsObject cms, CmsResource resource, ServletRequest JavaDoc req, ServletResponse JavaDoc res)
239     throws IOException JavaDoc, CmsException {
240
241         // get the selected element from the parameters
242
String JavaDoc element = req.getParameter(I_CmsResourceLoader.PARAMETER_ELEMENT);
243
244         // get the value as a String
245
String JavaDoc value = dumpAsString(cms, resource, element, cms.getRequestContext().getLocale(), req, res);
246
247         if (value != null) {
248             // extract the XML document from the current request (should have been cached already)
249
I_CmsXmlDocument doc = unmarshalXmlDocument(cms, resource, req);
250             // append the result to the output stream
251
byte[] result = value.getBytes(doc.getEncoding());
252             res.getOutputStream().write(result);
253         }
254     }
255
256     /**
257      * Returns the property definition name used to selecte the template for this XML document resource loader.<p>
258      *
259      * @return the property definition name used to selecte the template for this XML document resource loader
260      */

261     protected abstract String JavaDoc getTemplatePropertyDefinition();
262
263     /**
264      * Returns the unmarshalled XML document.<p>
265      *
266      * @param cms the current users OpenCms context
267      * @param resource the requested resource
268      * @param req the current Servlet request
269      *
270      * @return the unmarshalled XML document
271      *
272      * @throws CmsException in case the unmarshalling fails
273      */

274     protected abstract I_CmsXmlDocument unmarshalXmlDocument(CmsObject cms, CmsResource resource, ServletRequest JavaDoc req)
275     throws CmsException;
276 }
Free Books   Free Magazines  
Popular Tags