KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.rmi.*;
25
26 import javax.swing.*;
27 import javax.xml.parsers.*;
28 import javax.xml.rpc.*;
29
30 import org.openharmonise.commons.xml.*;
31 import org.openharmonise.commons.xml.namespace.*;
32 import org.openharmonise.him.configuration.*;
33 import org.openharmonise.him.editors.preview.*;
34 import org.openharmonise.him.harmonise.*;
35 import org.openharmonise.him.publish.*;
36 import org.openharmonise.him.window.*;
37 import org.openharmonise.vfs.*;
38 import org.openharmonise.vfs.gui.*;
39 import org.openharmonise.vfs.status.*;
40 import org.w3c.dom.*;
41 import org.xml.sax.*;
42
43 /**
44  * Handles editing of Page and Object template XML files.
45  *
46  * @author Matthew Large
47  * @version $Revision: 1.2 $
48  *
49  */

50 public class CompositionEditor extends GenericEditor implements Editor {
51
52     private boolean m_bResourceCreated = false;
53
54     private static final String JavaDoc TAG_HARMONISE_PUBLISH_SERVICE =
55         "HarmonisePublishService";
56     protected String JavaDoc m_sBrowserPath = null;
57     /**
58      *
59      */

60     public CompositionEditor() {
61         super();
62         setup();
63     }
64
65     /**
66      *
67      */

68     private void setup() {
69         String JavaDoc sValue =
70             ConfigStore.getInstance().getPropertyValue("BROWSER_PATH");
71         if (sValue == null || sValue.length() < 1) {
72             // TODO remove windows hard coded paths
73
File fPath =
74                 new File("C:/Program Files/Internet Explorer/IEXPLORE.EXE");
75             if (fPath.exists()) {
76                 this.m_sBrowserPath = fPath.getAbsolutePath();
77                 ConfigStore.getInstance().setProperty(
78                     "BROWSER_PATH",
79                     this.m_sBrowserPath);
80             } else {
81                 JFileChooser chooser = new JFileChooser();
82                 chooser.setDialogTitle("Select Browser path");
83                 int returnVal =
84                     chooser.showOpenDialog(
85                         DisplayManager.getInstance().getMainWindow());
86
87                 this.m_sBrowserPath =
88                     chooser.getSelectedFile().getAbsolutePath();
89                 ConfigStore.getInstance().setProperty(
90                     "BROWSER_PATH",
91                     this.m_sBrowserPath);
92
93             }
94         } else {
95             this.m_sBrowserPath = sValue;
96         }
97
98     }
99
100     /* (non-Javadoc)
101      * @see com.simulacramedia.contentmanager.editors.Editor#createNew(java.lang.String, com.simulacramedia.vfs.AbstractVirtualFileSystem)
102      */

103     public PathStatusWrapper createNew(String JavaDoc sPath, AbstractVirtualFileSystem vfs) {
104         ResourceStatusWrapper statusWrapper = new ResourceStatusWrapper(null, new VFSStatus());
105         
106         VirtualFile vfFile = new VirtualFile(sPath);
107
108         vfs.getVirtualFileSystemView().setContentType(vfFile, "text/xml");
109
110         StringBuffer JavaDoc sBuff = new StringBuffer JavaDoc();
111         InputStream is = null;
112
113         if (sPath.startsWith(HarmonisePaths.PATH_OBJECT_TEMPLATES)) {
114             is =
115                 XMetaLEditor.class.getResourceAsStream(
116                     "/org/openharmonise/him/icons/xml/newObjectTemplate.xml");
117         } else {
118             is =
119                 XMetaLEditor.class.getResourceAsStream(
120                     "/org/openharmonise/him/icons/xml/newPageTemplate.xml");
121         }
122
123         BufferedReader buff = new BufferedReader(new InputStreamReader(is));
124
125         String JavaDoc sLine = null;
126         try {
127             while ((sLine = buff.readLine()) != null) {
128                 sBuff.append(sLine);
129             }
130             Document xml = null;
131             try {
132                 xml =
133                     DocumentBuilderFactory
134                         .newInstance()
135                         .newDocumentBuilder()
136                         .parse(
137                         new org.xml.sax.InputSource JavaDoc(
138                             new StringReader(sBuff.toString())));
139             } catch (SAXException e1) {
140                 e1.printStackTrace();
141             } catch (ParserConfigurationException e1) {
142                 e1.printStackTrace();
143             } catch (FactoryConfigurationError e1) {
144                 e1.printStackTrace();
145             }
146
147             if (xml != null) {
148                 Element rootEl = (Element) xml.getDocumentElement();
149
150                 XMLPrettyPrint printer = new XMLPrettyPrint();
151
152                 vfFile.setContent(printer.printNode(rootEl).getBytes());
153             }
154
155             statusWrapper = vfs.addVirtualFile(sPath, vfFile);
156
157             vfFile = vfs.getVirtualFile(sPath).getResource();
158
159             this.m_bResourceCreated = true;
160             
161             PathStatusWrapper pathStatus = new PathStatusWrapper(super.createWorkingFile(vfFile), statusWrapper.getStatus());
162             
163             return pathStatus;
164         } catch (IOException e) {
165             e.printStackTrace();
166         } catch (NamespaceClashException e) {
167             e.printStackTrace();
168         }
169         statusWrapper.getStatus().setStatusLevel(StatusData.LEVEL_ERROR);
170         return new PathStatusWrapper(null, statusWrapper.getStatus());
171     }
172
173     /* (non-Javadoc)
174      * @see com.simulacramedia.contentmanager.editors.Editor#hasResourceBeenCreated()
175      */

176     public boolean hasResourceBeenCreated() {
177         return this.m_bResourceCreated;
178     }
179
180     /* (non-Javadoc)
181      * @see com.simulacramedia.contentmanager.editors.Editor#preview(java.lang.String, com.simulacramedia.vfs.AbstractVirtualFileSystem)
182      */

183     public PathStatusWrapper preview(String JavaDoc sPath, AbstractVirtualFileSystem vfs) {
184         VirtualFile vfFile = vfs.getVirtualFile(sPath).getResource();
185         String JavaDoc sFilename = null;
186         String JavaDoc sMessage = null;
187             try {
188                 String JavaDoc sOut = invokePublishService(vfFile, vfs, null);
189                 if(sOut != null){
190                     sFilename = this.getFileName(vfFile);
191                     File fFile = super.createPreviewFile(sFilename, sOut.getBytes());
192                 
193                     Runtime.getRuntime().exec(
194                         "\""
195                             + this.m_sBrowserPath
196                             + "\" "
197                             + "file:/"
198                             + fFile.toURI().getPath());
199                 }
200             } catch (SAXException e) {
201                     e.getLocalizedMessage();
202                 e.printStackTrace(System.err);
203             } catch (Exception JavaDoc e) {
204                 e.printStackTrace(System.err);
205             }
206
207         return new PathStatusWrapper(null, new VFSStatus());
208     }
209     public static String JavaDoc invokePublishService(
210         VirtualFile vfFile,
211         AbstractVirtualFileSystem vfs, Document state)
212         throws
213             SAXException,
214             IOException,
215             ParserConfigurationException,
216             RemoteException,
217             ServiceException {
218         String JavaDoc sOutXml = null;
219         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
220         factory.setNamespaceAware(true);
221         Document pageXml =
222             factory.newDocumentBuilder().parse(
223                 new org.xml.sax.InputSource JavaDoc(
224                     new StringReader(new String JavaDoc(vfFile.getContent()))));
225         Document previewXml =
226             DocumentBuilderFactory
227                 .newInstance()
228                 .newDocumentBuilder()
229                 .newDocument();
230         Element rootEl =
231             previewXml.createElement(TAG_HARMONISE_PUBLISH_SERVICE);
232         previewXml.appendChild(rootEl);
233         Document stateDoc = null;
234         if(state == null){
235             stateDoc = getState(vfFile);
236         } else {
237             stateDoc = state;
238         }
239         Node node = null;
240         if(stateDoc != null){
241             node = XMLUtils.copyNode(stateDoc.getDocumentElement(), previewXml);
242             rootEl.appendChild(node);
243             node = XMLUtils.copyNode(pageXml.getDocumentElement(), previewXml);
244             rootEl.appendChild(node);
245     
246             String JavaDoc sInXml = previewXml.getDocumentElement().toString();
247             //send the xml to the publish service
248
URI uri = vfs.getURI();
249             String JavaDoc sURL =
250                 uri.getScheme()
251                     + "://"
252                     + uri.getHost()
253                     + ":"
254                     + uri.getPort()
255                     + "/webdav/services/PublishService";
256             URL endPointURL = new URL(sURL);
257             sOutXml =
258                 PublishServiceClient.getPublishXML(endPointURL, sInXml);
259         }
260         return sOutXml;
261     }
262     private static Document getState(VirtualFile vfFile) {
263         Frame frame = new Frame();
264         frame.setIconImage(
265             ((ImageIcon) IconManager
266                 .getInstance()
267                 .getIcon("16-page-definition.gif"))
268                 .getImage());
269         String JavaDoc sVFile = null;
270         if(vfFile.isVersionable() && ((VersionedVirtualFile)vfFile).getState().equals(VirtualFile.STATE_PENDING) && ((VersionedVirtualFile)vfFile).getLiveVersionPath()!=null ) {
271             sVFile = ((VersionedVirtualFile)vfFile).getLiveVersionPath();
272         } else {
273             sVFile = vfFile.getFullPath();
274         }
275         StateDialog sd = new StateDialog(frame, "state selector", sVFile);
276         Document doc = sd.getState();
277         if(doc == null){
278             sd.show();
279             doc = sd.getState();
280         }
281         return doc;
282     }
283 }
284
Popular Tags