KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > engines > xmlsource > XmlSource


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12

13 package org.jahia.engines.xmlsource;
14
15 import java.io.File JavaDoc;
16 import java.io.FileNotFoundException JavaDoc;
17 import java.io.IOException JavaDoc;
18 import java.io.StringReader JavaDoc;
19 import java.io.StringWriter JavaDoc;
20 import java.io.Writer JavaDoc;
21
22 import javax.xml.transform.TransformerConfigurationException JavaDoc;
23 import javax.xml.transform.TransformerException JavaDoc;
24
25 import org.jahia.data.JahiaData;
26 import org.jahia.data.containers.JahiaContainer;
27 import org.jahia.data.containers.JahiaContainerList;
28 import org.jahia.data.fields.JahiaField;
29 import org.jahia.data.fields.LoadFlags;
30 import org.jahia.engines.JahiaEngine;
31 import org.jahia.exceptions.JahiaException;
32 import org.jahia.exceptions.JahiaForbiddenAccessException;
33 import org.jahia.params.ParamBean;
34 import org.jahia.registries.ServicesRegistry;
35 import org.jahia.services.containers.JahiaContainersService;
36 import org.jahia.services.fields.ContentField;
37 import org.jahia.services.pages.ContentPage;
38 import org.jahia.services.sites.JahiaSite;
39 import org.jahia.services.usermanager.JahiaUser;
40 import org.jahia.services.version.EntryLoadRequest;
41 import org.jahia.utils.xml.XMLSerializationOptions;
42 import org.jahia.utils.xml.XmlWriter;
43
44
45 public class XmlSource implements JahiaEngine {
46
47     private static final org.apache.log4j.Logger logger
48             = org.apache.log4j.Logger.getLogger (XmlSource.class);
49
50     private static XmlSource instance = null;
51     public static final String JavaDoc ENGINE_NAME = "xmlsource";
52
53     private XmlSource () {
54         logger.debug ("***** Starting " + XmlSource.class.getName () + " engine *****");
55     }
56
57     public static synchronized XmlSource getInstance () {
58         if (instance == null) {
59             instance = new XmlSource ();
60         }
61         return instance;
62     }
63
64
65     public boolean authoriseRender (ParamBean jParams) {
66         logger.debug (
67                 "authorizeRender=" +
68                 new Boolean JavaDoc ((jParams.getOperationMode () == ParamBean.EDIT)).toString ());
69         return (jParams.getOperationMode () == ParamBean.EDIT);
70     }
71
72     public String JavaDoc renderLink (ParamBean jParams, Object JavaDoc theObj)
73             throws JahiaException {
74         JahiaField theField = (JahiaField) theObj;
75         String JavaDoc params = "?mode=displaywindow&fid=" + theField.getID ();
76         return jParams.composeEngineUrl (ENGINE_NAME, params);
77     }
78
79
80     public boolean needsJahiaData (ParamBean jParams) {
81         return false;
82     }
83
84
85     public void handleActions (ParamBean jParams, JahiaData jData)
86             throws JahiaException {
87
88         JahiaUser curUser = jParams.getUser ();
89
90         if (curUser == null) {
91             throw new JahiaForbiddenAccessException ();
92         }
93
94         if (!curUser.isAdminMember (jParams.getSiteID ())) {
95             // for the moment only site admins have access to this engine
96
throw new JahiaForbiddenAccessException ();
97         }
98
99
100         String JavaDoc mode = jParams.getRequest ().getParameter ("mode");
101         String JavaDoc ipAddr = jParams.getRequest ().getRemoteAddr ();
102         String JavaDoc xsltFileName = jParams.getRequest ().getParameter ("xslt");
103
104         Writer JavaDoc out;
105         StringWriter JavaDoc strWriter = null;
106
107         try {
108
109             if (xsltFileName == null) {
110                 out = jParams.getResponse ().getWriter ();
111             } else {
112                 strWriter = new StringWriter JavaDoc ();
113                 out = strWriter;
114             }
115
116             if (mode != null) {
117                 if ("field".equals (mode)) {
118                     logger.debug (ipAddr + " is accessing field " + jParams.getFieldID ());
119                     handleFieldData (jParams, out);
120                 } else if ("container".equals (mode)) {
121                     logger.debug (
122                             ipAddr + " is accessing container " + jParams.getContainerID ());
123                     handleContainerData (jParams, out);
124                 } else if ("containerlist".equals (mode)) {
125                     logger.debug (
126                             ipAddr + " is accessing container list " +
127                             jParams.getContainerListID ());
128                     handleContainerListData (jParams, out);
129                 } else if ("page".equals (mode)) {
130                     logger.debug (ipAddr + " is accessing page " + jParams.getPageID ());
131                     handlePageData (jParams, out);
132                 } else if ("site".equals (mode)) {
133                     logger.debug (ipAddr + " is accessing site " + jParams.getPageID ());
134                     handleSiteData (jParams, out);
135                 }
136             }
137
138             if (xsltFileName != null) {
139                 out = jParams.getResponse ().getWriter ();
140                 try {
141                     transform (jParams, strWriter.toString (), xsltFileName, out);
142                 } catch (Throwable JavaDoc t) {
143                     logger.debug (
144                             "Error while transforming XML output using XSLT file " +
145                             xsltFileName,
146                             t);
147                 }
148             }
149         } catch (IOException JavaDoc ioe) {
150             logger.debug (
151                     "IOException while trying to output xml with params : mode=" + mode +
152                     " xsltFileName=" +
153                     xsltFileName,
154                     ioe);
155         }
156     }
157
158     /**
159      * Retrieve the engine name.
160      *
161      * @return the engine name.
162      */

163     public final String JavaDoc getName () {
164         return ENGINE_NAME;
165     }
166
167     public void handleFieldData (ParamBean jParams, Writer JavaDoc out)
168             throws JahiaException {
169         int fieldID = jParams.getFieldID ();
170         logger.debug ("Sending XML for field " + fieldID);
171         ContentField theField = ContentField.getField (fieldID);
172         try {
173             jParams.getResponse ().setContentType ("text/xml");
174             out.write ("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
175             XmlWriter xmlWriter = new XmlWriter (out);
176             xmlWriter.writeEntity ("jahia");
177             xmlWriter.enablePrettyPrint (true);
178             if (theField != null) {
179                 theField.serializeToXML (xmlWriter, new XMLSerializationOptions (), jParams);
180             } else {
181                 xmlWriter.writeEntityWithText ("error", "Field " + fieldID + " not found");
182             }
183             xmlWriter.endEntity ();
184             xmlWriter.close ();
185             out.flush ();
186
187         } catch (IOException JavaDoc ioe) {
188             logger.debug ("Error while serializing field to XML ", ioe);
189         }
190     }
191
192
193     public void handleContainerData (ParamBean jParams, Writer JavaDoc out)
194             throws JahiaException {
195         int containerID = jParams.getContainerID ();
196         logger.debug ("Sending XML for container " + containerID);
197         JahiaContainersService ctnSrv = ServicesRegistry.getInstance ()
198                 .getJahiaContainersService ();
199         JahiaContainer theContainer = ctnSrv.loadContainer (containerID, LoadFlags.ALL,
200                 jParams, EntryLoadRequest.CURRENT);
201         try {
202             jParams.getResponse ().setContentType ("text/xml");
203             out.write ("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
204             XmlWriter xmlWriter = new XmlWriter (out);
205             xmlWriter.writeEntity ("jahia");
206             xmlWriter.enablePrettyPrint (true);
207             if (theContainer != null) {
208                 ctnSrv.serializeContainerToXML (xmlWriter, new XMLSerializationOptions (),
209                         containerID, jParams);
210             } else {
211                 xmlWriter.writeEntityWithText ("error",
212                         "Container " + containerID + " not found");
213             }
214             xmlWriter.endEntity ();
215             xmlWriter.close ();
216             out.flush ();
217
218         } catch (IOException JavaDoc ioe) {
219             logger.debug ("Error while serializing container to XML ", ioe);
220         }
221     }
222
223     public void handleContainerListData (ParamBean jParams, Writer JavaDoc out)
224             throws JahiaException {
225         int containerListID = jParams.getContainerListID ();
226         logger.debug ("Sending XML for container list " + containerListID);
227         JahiaContainersService ctnSrv = ServicesRegistry.getInstance ()
228                 .getJahiaContainersService ();
229         JahiaContainerList theContainerList = ctnSrv.loadContainerList (containerListID,
230                 LoadFlags.ALL, jParams);
231         try {
232             jParams.getResponse ().setContentType ("text/xml");
233             out.write ("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
234             XmlWriter xmlWriter = new XmlWriter (out);
235             xmlWriter.writeEntity ("jahia");
236             xmlWriter.enablePrettyPrint (true);
237             if (theContainerList != null) {
238                 ctnSrv.serializeContainerListToXML (xmlWriter, new XMLSerializationOptions (),
239                         containerListID, jParams);
240             } else {
241                 xmlWriter.writeEntityWithText ("error",
242                         "Container list " + containerListID + " not found");
243             }
244             xmlWriter.endEntity ();
245             xmlWriter.close ();
246             out.flush ();
247
248         } catch (IOException JavaDoc ioe) {
249             logger.debug ("Error while serializing container list to XML ", ioe);
250         }
251     }
252
253
254     public void handlePageData (ParamBean jParams, Writer JavaDoc out)
255             throws JahiaException {
256         int pageID = jParams.getPageID ();
257         logger.debug ("Sending XML for page " + pageID);
258         ContentPage contentPage = ServicesRegistry.getInstance ().getJahiaPageService ()
259                 .lookupContentPage (pageID, true);
260         try {
261             jParams.getResponse ().setContentType ("text/xml");
262             out.write ("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
263             XmlWriter xmlWriter = new XmlWriter (out);
264             xmlWriter.writeEntity ("jahia");
265             xmlWriter.enablePrettyPrint (true);
266             if (contentPage != null) {
267                 contentPage.serializeToXML (xmlWriter, new XMLSerializationOptions (), jParams);
268             } else {
269                 xmlWriter.writeEntityWithText ("error", "Page " + pageID + " not found");
270             }
271             xmlWriter.endEntity ();
272             xmlWriter.close ();
273             out.flush ();
274
275         } catch (IOException JavaDoc ioe) {
276             logger.debug ("Error while serializing field to XML ", ioe);
277         }
278     }
279
280
281     public void handleSiteData (ParamBean jParams, Writer JavaDoc out)
282             throws JahiaException {
283
284         JahiaSite site = jParams.getSite ();
285         logger.debug ("Sending XML for site " + jParams.getSiteID ());
286         try {
287             jParams.getResponse ().setContentType ("text/xml");
288             out.write ("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
289             XmlWriter xmlWriter = new XmlWriter (out);
290             xmlWriter.enablePrettyPrint (true);
291             xmlWriter.writeEntity ("jahia");
292
293             site.serializeToXML (xmlWriter, new XMLSerializationOptions (), jParams);
294             xmlWriter.endEntity ();
295             xmlWriter.close ();
296             out.flush ();
297
298         } catch (IOException JavaDoc ioe) {
299             logger.debug ("Error while serializing field to XML ", ioe);
300         }
301     }
302
303     public void transform (ParamBean jParams, String JavaDoc xmlInputSource, String JavaDoc xsltFileName,
304                            Writer JavaDoc out)
305             throws TransformerConfigurationException JavaDoc, TransformerException JavaDoc,
306             FileNotFoundException JavaDoc, IOException JavaDoc {
307
308         logger.debug (
309                 "Using XSLT transformation file " + xsltFileName +
310                 " to transform XML output...");
311
312         String JavaDoc xslFilePath = jParams.getContext ().getRealPath (
313                 "/WEB-INF/views/xslt/" + xsltFileName);
314         if (xslFilePath == null) {
315             logger.debug (
316                     "Error while trying to resolve file name for XSLT file : " +
317                     "/WEB-INF/views/xslt/" +
318                     xsltFileName +
319                     " to disk path using ServletContext.getRealPath() method call.");
320             out.write ("Couldn't find XSLT file " + xsltFileName);
321             return;
322         }
323
324         File JavaDoc xslFile = new File JavaDoc (xslFilePath);
325         if (!xslFile.exists ()) {
326             logger.debug ("Coulnd't find XSLT file " + xslFile.toString ());
327             out.write ("Couldn't find XSLT file " + xslFile.toString ());
328             return;
329         }
330
331         // 1. Instantiate a TransformerFactory.
332
javax.xml.transform.TransformerFactory JavaDoc tFactory =
333                 javax.xml.transform.TransformerFactory.newInstance ();
334
335         // 2. Use the TransformerFactory to process the stylesheet Source and
336
// generate a Transformer.
337
javax.xml.transform.Transformer JavaDoc transformer = tFactory.newTransformer
338                 (new javax.xml.transform.stream.StreamSource JavaDoc (xslFile));
339
340         // 3. Use the Transformer to transform an XML Source and send the
341
// output to a Result object.
342
transformer.transform (
343             new javax.xml.transform.stream.StreamSource JavaDoc (new StringReader JavaDoc (xmlInputSource)),
344             new javax.xml.transform.stream.StreamResult JavaDoc (out));
345
346     }
347
348 }
349
Popular Tags