KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > editors > PageDefinitionEditor


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.him.editors;
20
21 import java.awt.*;
22 import java.io.*;
23 import java.net.*;
24
25 import javax.swing.*;
26 import javax.xml.parsers.*;
27 import javax.xml.transform.*;
28 import javax.xml.transform.dom.*;
29 import javax.xml.transform.stream.*;
30
31 import org.openharmonise.commons.net.*;
32 import org.openharmonise.him.editors.pagegui.*;
33 import org.openharmonise.him.window.messages.*;
34 import org.openharmonise.vfs.*;
35 import org.openharmonise.vfs.gui.*;
36 import org.openharmonise.vfs.status.*;
37 import org.w3c.dom.*;
38
39 /**
40  * The page definition editor handles the editing of page objects.
41  *
42  * @author Matthew Large
43  * @version $Revision: 1.2 $
44  *
45  */

46 public class PageDefinitionEditor extends CompositionEditor implements Editor {
47
48     private boolean m_bResourceCreated = false;
49
50     /**
51      *
52      */

53     public PageDefinitionEditor() {
54         super();
55     }
56
57     /* (non-Javadoc)
58      * @see com.simulacramedia.contentmanager.editors.Editor#open(java.lang.String, com.simulacramedia.vfs.AbstractVirtualFileSystem)
59      */

60     public PathStatusWrapper open(String JavaDoc sPath, AbstractVirtualFileSystem vfs) {
61         VirtualFile vfFile = vfs.getVirtualFile(sPath).getResource();
62
63         String JavaDoc sTitle = "Page Definition Editor";
64
65         Frame frame = new Frame();
66         frame.setIconImage(
67             ((ImageIcon) IconManager
68                 .getInstance()
69                 .getIcon("16-page-definition.gif"))
70                 .getImage());
71         PageDefinitionDialog dialog = new PageDefinitionDialog(frame, sTitle);
72
73         dialog.setData(vfFile.getContent());
74
75         dialog.show();
76
77         if (dialog.getData() != null) {
78             vfFile.setContent(dialog.getData());
79         }
80
81         return new PathStatusWrapper(null, new VFSStatus());
82     }
83
84     /* (non-Javadoc)
85      * @see com.simulacramedia.contentmanager.editors.Editor#createNew(java.lang.String, com.simulacramedia.vfs.AbstractVirtualFileSystem)
86      */

87     public PathStatusWrapper createNew(String JavaDoc sPath, AbstractVirtualFileSystem vfs) {
88         ResourceStatusWrapper statusWrapper = new ResourceStatusWrapper(null, new VFSStatus());
89         
90         String JavaDoc sTitle = "Page Definition Editor";
91
92         Frame frame = new Frame();
93         frame.setIconImage(
94             ((ImageIcon) IconManager
95                 .getInstance()
96                 .getIcon("16-page-definition.gif"))
97                 .getImage());
98         PageDefinitionDialog dialog = new PageDefinitionDialog(frame, sTitle);
99
100         dialog.show();
101
102         if (dialog.getData() != null) {
103             VirtualFile vfFile = new VirtualFile(sPath);
104
105             if (sPath.startsWith(".webdav/Content/Assets/links")) {
106                 vfs.getVirtualFileSystemView().setContentType(
107                     vfFile,
108                     (String JavaDoc) MimeTypeMapping.getMimeTypes("xml").get(0));
109             }
110
111             vfFile.setContent(dialog.getData());
112             statusWrapper = vfs.addVirtualFile(sPath, vfFile);
113             if(statusWrapper.getStatus().isOK()) {
114                 this.m_bResourceCreated = true;
115             }
116         }
117
118         return new PathStatusWrapper(null, statusWrapper.getStatus());
119     }
120
121     /* (non-Javadoc)
122      * @see com.simulacramedia.contentmanager.editors.Editor#discardChanges(java.lang.String, com.simulacramedia.vfs.AbstractVirtualFileSystem)
123      */

124     public StatusData discardChanges(String JavaDoc sPath, AbstractVirtualFileSystem vfs) {
125         return new VFSStatus();
126     }
127
128     /* (non-Javadoc)
129      * @see com.simulacramedia.contentmanager.editors.Editor#export(java.lang.String, com.simulacramedia.vfs.AbstractVirtualFileSystem)
130      */

131     public StatusData export(String JavaDoc sPath, AbstractVirtualFileSystem vfs) {
132         return new VFSStatus();
133     }
134
135     /* (non-Javadoc)
136      * @see com.simulacramedia.contentmanager.editors.Editor#createNew(java.lang.String, byte[], com.simulacramedia.vfs.AbstractVirtualFileSystem)
137      */

138     public PathStatusWrapper createNew(
139         String JavaDoc sPath,
140         byte[] content,
141         AbstractVirtualFileSystem vfs) {
142         
143         ResourceStatusWrapper statusWrapper = new ResourceStatusWrapper(null, new VFSStatus());
144         
145         VirtualFile vfFile = new VirtualFile(sPath);
146
147         if (sPath.startsWith(".webdav/Content/Assets/links")) {
148             vfs.getVirtualFileSystemView().setContentType(
149                 vfFile,
150                 (String JavaDoc) MimeTypeMapping.getMimeTypes("xml").get(0));
151         }
152
153         vfFile.setContent(content);
154         statusWrapper = vfs.addVirtualFile(sPath, vfFile);
155         if(statusWrapper.getStatus().isOK()) {
156             this.m_bResourceCreated = true;
157         }
158
159         return new PathStatusWrapper(null, statusWrapper.getStatus());
160     }
161
162     /* (non-Javadoc)
163      * @see com.simulacramedia.contentmanager.editors.Editor#hasResourceBeenCreated()
164      */

165     public boolean hasResourceBeenCreated() {
166         return this.m_bResourceCreated;
167     }
168
169     /* (non-Javadoc)
170      * @see com.simulacramedia.contentmanager.editors.Editor#preview(java.lang.String, com.simulacramedia.vfs.AbstractVirtualFileSystem)
171      */

172     public PathStatusWrapper preview(String JavaDoc sPath, AbstractVirtualFileSystem vfs){
173         return preview(sPath, vfs, null);
174     }
175     public PathStatusWrapper preview(String JavaDoc sPath, AbstractVirtualFileSystem vfs, Document state) {
176         VirtualFile vfFile = vfs.getVirtualFile(sPath).getResource();
177         String JavaDoc sPageFileName = vfFile.getFileName();
178         VirtualFile vfXmlFile = null;
179         VirtualFile vfXslFile = null;
180         String JavaDoc sMessage = null;
181         try {
182             Document xml =
183                 DocumentBuilderFactory
184                     .newInstance()
185                     .newDocumentBuilder()
186                     .parse(
187                     new org.xml.sax.InputSource JavaDoc(
188                         new StringReader(new String JavaDoc(vfFile.getContent()))));
189
190             Element rootEl = xml.getDocumentElement();
191             NodeList nl = rootEl.getChildNodes();
192             String JavaDoc sMimeType = null;
193             for (int i = 0; i < nl.getLength(); i++) {
194                 Node node = nl.item(i);
195                 if (node.getNodeType() == Node.ELEMENT_NODE) {
196                     Element element = (Element) node;
197                     if (element.getTagName().equals("XML")) {
198                         NodeList nl2 = element.getChildNodes();
199                         for (int j = 0; j < nl2.getLength(); j++) {
200                             Node child = nl2.item(j);
201                             if (child.getNodeType() == Node.ELEMENT_NODE
202                                 && ((Element) child).getTagName().equals("href")
203                                 && child.getChildNodes().getLength() == 1
204                                 && child.getFirstChild().getNodeType()
205                                     == Node.TEXT_NODE) {
206                                 Text txt = (Text) child.getFirstChild();
207                                 vfXmlFile =
208                                     vfs.getVirtualFile(txt.getNodeValue()).getResource();
209                             }
210                         }
211                     } else if (element.getTagName().equals("XSL")) {
212                         NodeList nl2 = element.getChildNodes();
213                         for (int j = 0; j < nl2.getLength(); j++) {
214                             Node child = nl2.item(j);
215                             if (child.getNodeType() == Node.ELEMENT_NODE
216                                 && child.getChildNodes().getLength() == 1
217                                 && child.getFirstChild().getNodeType()
218                                     == Node.TEXT_NODE) {
219                                 if (((Element) child)
220                                     .getTagName()
221                                     .equals("href")) {
222                                     Text txt = (Text) child.getFirstChild();
223                                     vfXslFile =
224                                         vfs.getVirtualFile(txt.getNodeValue()).getResource();
225                                 } else if (
226                                     ((Element) child).getTagName().equals(
227                                         "mimetype")) {
228                                     sMimeType =
229                                         ((Text) child.getFirstChild())
230                                             .getData();
231                                 }
232                             }
233                         }
234                     }
235                 }
236             }
237             if (vfXmlFile != null && vfXslFile != null) {
238                 //get the harmonise page xml
239
String JavaDoc sOut = invokePublishService(vfXmlFile, vfs, state);
240                 if(sOut != null){
241                     Document pageXml =
242                         DocumentBuilderFactory
243                             .newInstance()
244                             .newDocumentBuilder()
245                             .parse(
246                             new org.xml.sax.InputSource JavaDoc(
247                                 new StringReader(sOut)));
248                     //get the xslt
249
XSLTEditor xsltEditor = new XSLTEditor();
250                     TransformerFactory factory = TransformerFactory.newInstance();
251
252                     String JavaDoc sWorkingFilePath =
253                         xsltEditor.createPreviewFile(vfXslFile);
254                     File file = new File(sWorkingFilePath);
255                     xsltEditor.downloadPreviewImports(vfXslFile);
256                     StreamSource ssource = new StreamSource(new FileReader(file));
257                     ssource.setSystemId(file);
258                     Templates templates = factory.newTemplates(ssource);
259                     String JavaDoc sFilename =
260                         createPreviewFileName(getFileName(vfFile), sMimeType);
261                     sWorkingFilePath = createPreviewFile(sFilename);
262                     File fFile = new File(sWorkingFilePath);
263
264                     ByteArrayOutputStream bos = new ByteArrayOutputStream();
265                     render(pageXml, templates, bos);
266                     String JavaDoc result = bos.toString();
267                     String JavaDoc newResult = resolveRelativePaths(result, vfs);
268                     BufferedWriter out =
269                         new BufferedWriter(new FileWriter(sWorkingFilePath));
270                     out.write(newResult);
271                     bos.close();
272                     out.close();
273                     Runtime.getRuntime().exec(
274                         "\""
275                             + m_sBrowserPath
276                             + "\" "
277                             + "file:/"
278                             + fFile.toURI().getPath());
279                 }
280             }
281         } catch (Exception JavaDoc e) {
282             sMessage = "There was a problem previewing " + sPageFileName + ":\n" +
283             e.getLocalizedMessage();
284             e.printStackTrace(System.err);
285         }
286         if(sMessage != null){
287             MessageHandler.getInstance().fireMessageEvent(
288                     sMessage,
289                     MessageHandler.TYPE_ERROR);
290         }
291         return new PathStatusWrapper(null, new VFSStatus());
292     }
293     public void render(Document doc, Templates templates, OutputStream out)
294         throws Exception JavaDoc {
295         try {
296             OutputStreamWriter outwriter = new OutputStreamWriter(out, "UTF-8");
297
298             Transformer trans = templates.newTransformer();
299
300             DOMSource ds = new DOMSource(doc.getDocumentElement());
301
302             StreamResult res = new StreamResult(outwriter);
303
304             trans.transform(ds, res);
305         } catch (TransformerConfigurationException e) {
306             throw new Exception JavaDoc("Error occured with configuration", e);
307         } catch (TransformerException e) {
308             throw new Exception JavaDoc("Error occured with tramsformation", e);
309         } catch (UnsupportedEncodingException e) {
310             throw new Exception JavaDoc("Unsupported encoding error", e);
311         }
312     }
313     protected String JavaDoc createPreviewFileName(
314         String JavaDoc sFilename,
315         String JavaDoc sMimeType) {
316         String JavaDoc sNewFilename = sFilename;
317         if (sMimeType != null) {
318             int index = sFilename.lastIndexOf(".");
319             sNewFilename = sFilename.substring(0, index + 1);
320             sNewFilename += MimeTypeMapping.getExtensionFromMimeType(sMimeType);
321         }
322         return sNewFilename;
323     }
324     protected String JavaDoc resolveRelativePaths(
325         String JavaDoc sContents,
326         AbstractVirtualFileSystem vfs) {
327         URI uri = vfs.getURI();
328         String JavaDoc sURL =
329             uri.getScheme() + "://" + uri.getHost() + ":" + uri.getPort();
330         String JavaDoc sNewContents =
331             replaceAllIgnoreCase(sContents, "href=\"/", "href=\"" + sURL + "/");
332         sNewContents =
333             replaceAllIgnoreCase(sNewContents, "src=\"/", "src=\"" + sURL + "/");
334         sURL += "/webdav/servlet/";
335         sNewContents = sNewContents.replaceAll("\"HarmoniseServlet", "\"" + sURL + "HarmoniseServlet");
336         sNewContents = sNewContents.replaceAll("'HarmoniseServlet", "'" + sURL + "HarmoniseServlet");
337         return sNewContents;
338     }
339     public String JavaDoc replaceAllIgnoreCase(String JavaDoc in, String JavaDoc arg0, String JavaDoc arg1) {
340         String JavaDoc out = in;
341         String JavaDoc lower = in.toLowerCase();
342         int index = lower.indexOf(arg0.toLowerCase());
343         if (index > 0) {
344             out = out.substring(0, index) + arg1
345                     + out.substring(index + arg0.length());
346             out = replaceAllIgnoreCase(out, arg0, arg1);
347         }
348         return out;
349     }
350 }
351
Popular Tags