KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.*;
22 import java.util.*;
23
24 import javax.swing.*;
25 import javax.xml.parsers.*;
26
27 import org.openharmonise.commons.xml.*;
28 import org.openharmonise.commons.xml.namespace.*;
29 import org.openharmonise.him.configuration.*;
30 import org.openharmonise.him.editors.filefilters.XMLFilter;
31 import org.openharmonise.him.window.*;
32 import org.openharmonise.him.window.messages.*;
33 import org.openharmonise.vfs.*;
34 import org.openharmonise.vfs.metadata.*;
35 import org.openharmonise.vfs.metadata.value.*;
36 import org.openharmonise.vfs.status.*;
37 import org.w3c.dom.*;
38 import org.xml.sax.*;
39
40
41 /**
42  * Handles editing of documents in XMetaL.
43  *
44  * @author Matthew Large
45  * @version $Revision: 1.2 $
46  *
47  */

48 public class XMetaLEditor extends CompositionEditor implements Editor {
49     
50     private boolean m_bResourceCreated = false;
51     
52     /**
53      * Path to XMetaL application directory.
54      */

55     private static String JavaDoc m_sXMetalPath = null;
56     
57     /**
58      * true if XMetaL is available on this system.
59      */

60     private static boolean m_bXMetalAvailable = true;
61     private static boolean m_bTested = false;
62
63     public XMetaLEditor() {
64         super();
65     }
66
67     /* (non-Javadoc)
68      * @see com.simulacramedia.contentmanager.editors.Editor#open(java.lang.String, com.simulacramedia.vfs.AbstractVirtualFileSystem)
69      */

70     public PathStatusWrapper open(String JavaDoc sPath, AbstractVirtualFileSystem vfs) {
71         PathStatusWrapper pathStatus = new PathStatusWrapper(null, new VFSStatus());
72         
73         String JavaDoc sWorkingFilePath = null;
74         
75         VirtualFile vfFile = vfs.getVirtualFile(sPath).getResource();
76         
77         if(isXMetalAvailable()) {
78             VFSStatus status = new VFSStatus();
79             try {
80                 sWorkingFilePath = createWorkingFile(vfFile);
81                 XMetaLController.getInstance().openFile(sWorkingFilePath);
82             } catch (Exception JavaDoc e1) {
83                 e1.printStackTrace();
84                 status.setStatusCode(StatusData.STATUS_INVALID_RESOURCE_STATE);
85             } finally {
86                 pathStatus = new PathStatusWrapper(sWorkingFilePath, status);
87             }
88         } else {
89             FileAssetEditor edit = new FileAssetEditor();
90             pathStatus = edit.open(sPath, vfs);
91         }
92             
93         return pathStatus;
94     }
95
96     /* (non-Javadoc)
97      * @see com.simulacramedia.contentmanager.editors.Editor#createNew(java.lang.String, com.simulacramedia.vfs.AbstractVirtualFileSystem)
98      */

99     public PathStatusWrapper createNew(String JavaDoc sPath, AbstractVirtualFileSystem vfs) {
100         ResourceStatusWrapper statusWrapper = new ResourceStatusWrapper(null, new VFSStatus());
101         
102         VirtualFile vfFile = new VirtualFile(sPath);
103         
104         vfs.getVirtualFileSystemView().setContentType(vfFile, "text/xml");
105
106         StringBuffer JavaDoc sBuff = new StringBuffer JavaDoc();
107         InputStream is = null;
108         
109         is = XMetaLEditor.class.getResourceAsStream("/org/openharmonise/him/icons/xml/newdoc.xml");
110
111         BufferedReader buff = new BufferedReader( new InputStreamReader(is));
112         
113         String JavaDoc sLine = null;
114         try {
115             while((sLine=buff.readLine())!=null) {
116                 sBuff.append(sLine);
117             }
118             Document xml = null;
119             try {
120                 xml =
121                     DocumentBuilderFactory
122                         .newInstance()
123                         .newDocumentBuilder()
124                         .parse(
125                         new org.xml.sax.InputSource JavaDoc(
126                             new StringReader(sBuff.toString())));
127             } catch (SAXException e1) {
128                 e1.printStackTrace();
129             } catch (ParserConfigurationException e1) {
130                 e1.printStackTrace();
131             } catch (FactoryConfigurationError e1) {
132                 e1.printStackTrace();
133             }
134
135             if(xml!=null) {
136                 Element rootEl = (Element) xml.getDocumentElement();
137                 
138                 XMLPrettyPrint printer = new XMLPrettyPrint();
139             
140                 vfFile.setContent(printer.printNode(rootEl).getBytes());
141             }
142         
143             statusWrapper = vfs.addVirtualFile(sPath, vfFile);
144         
145             vfFile = vfs.getVirtualFile(sPath).getResource();
146         
147             if(statusWrapper.getStatus().isOK()) {
148                 this.m_bResourceCreated = true;
149                 return new PathStatusWrapper(super.createWorkingFile(vfFile), statusWrapper.getStatus());
150             }
151         } catch (IOException e) {
152             e.printStackTrace();
153         } catch (NamespaceClashException e) {
154             e.printStackTrace();
155         }
156         statusWrapper.getStatus().setStatusLevel(StatusData.LEVEL_ERROR);
157         return new PathStatusWrapper(null, statusWrapper.getStatus());
158     }
159
160     /**
161      * Configures this editor.
162      *
163      */

164     private boolean isXMetalAvailable() {
165         if(m_bTested == false){
166             String JavaDoc sValue = ConfigStore.getInstance().getPropertyValue("XMETAL_PATH");
167             if(sValue==null || sValue.length()<1) {
168                 File fPath = new File("C:\\Program Files\\Corel\\XMetaL 4\\Author");
169                 if(fPath.exists()) {
170                     XMetaLEditor.m_sXMetalPath = fPath.getAbsolutePath();
171                     ConfigStore.getInstance().setProperty("XMETAL_PATH", XMetaLEditor.m_sXMetalPath);
172                 } else {
173                     JFileChooser chooser = new JFileChooser();
174                     chooser.setDialogTitle("Select XMetaL 4: Author path");
175                     int returnVal =
176                         chooser.showOpenDialog(
177                             DisplayManager.getInstance().getMainWindow());
178                     if (returnVal == JFileChooser.APPROVE_OPTION) {
179                         XMetaLEditor.m_sXMetalPath = chooser.getSelectedFile().getAbsolutePath();
180                         ConfigStore.getInstance().setProperty("XMETAL_PATH", XMetaLEditor.m_sXMetalPath);
181                     } else {
182                         m_bXMetalAvailable = false;
183                     }
184                 }
185             } else {
186                 XMetaLEditor.m_sXMetalPath = sValue;
187             }
188         
189             if(m_bXMetalAvailable && XMetaLEditor.m_sXMetalPath!=null && XMetaLEditor.m_sXMetalPath.length()>0) {
190                 File file = new File(XMetaLEditor.m_sXMetalPath + "\\rules\\harmonise-document.xsd");
191         
192                 InputStream is = XMetaLEditor.class.getResourceAsStream("/org/openharmonise/him/icons/files/harmonise-document.xsd");
193                 FileOutputStream fo = null;
194         
195                 try {
196                     fo = new FileOutputStream(file);
197                     int nValue;
198                     while((nValue=is.read())!=-1) {
199                         fo.write(nValue);
200                     }
201                 } catch (FileNotFoundException e) {
202                     e.printStackTrace();
203                 } catch (IOException e) {
204                     e.printStackTrace();
205                 }
206                 
207                 file = new File(XMetaLEditor.m_sXMetalPath + "\\rules\\harmonise-document.xac");
208         
209                 is = XMetaLEditor.class.getResourceAsStream("/org/openharmonise/him/icons/files/harmonise-document.xac");
210                 fo = null;
211         
212                 try {
213                     fo = new FileOutputStream(file);
214                     int nValue;
215                     while((nValue=is.read())!=-1) {
216                         fo.write(nValue);
217                     }
218                 } catch (FileNotFoundException e) {
219                     e.printStackTrace();
220                 } catch (IOException e) {
221                     e.printStackTrace();
222                 }
223                 
224                 file = new File(this.m_sXMetalPath + "\\rules\\harmonise-document.rld");
225         
226                 is = XMetaLEditor.class.getResourceAsStream("/org/openharmonise/him/icons/files/harmonise-document.rld");
227                 fo = null;
228         
229                 try {
230                     fo = new FileOutputStream(file);
231                     int nValue;
232                     while((nValue=is.read())!=-1) {
233                         fo.write(nValue);
234                     }
235                 } catch (FileNotFoundException e) {
236                     e.printStackTrace();
237                 } catch (IOException e) {
238                     e.printStackTrace();
239                 }
240                 
241                 file = new File(this.m_sXMetalPath + "\\rules\\harmonise-document.tbr");
242         
243                 is = XMetaLEditor.class.getResourceAsStream("/org/openharmonise/him/icons/files/harmonise-document.tbr");
244                 fo = null;
245         
246                 try {
247                     fo = new FileOutputStream(file);
248                     int nValue;
249                     while((nValue=is.read())!=-1) {
250                         fo.write(nValue);
251                     }
252                 } catch (FileNotFoundException e) {
253                     e.printStackTrace();
254                 } catch (IOException e) {
255                     e.printStackTrace();
256                 }
257             
258                 
259             }
260             m_bTested = true;
261         }
262         return m_bXMetalAvailable;
263     }
264
265     /* (non-Javadoc)
266      * @see com.simulacramedia.contentmanager.editors.AbstractEditor#createWorkingFile(com.simulacramedia.vfs.VirtualFile)
267      */

268     protected String JavaDoc createWorkingFile(VirtualFile vfFile) {
269         String JavaDoc sFilepath = super.createWorkingFile(vfFile);
270         
271         if(sFilepath==null) {
272             return null;
273         }
274         
275         File fFile = new File(sFilepath);
276
277         FileOutputStream fos = null;
278         
279         Document xml = null;
280         try {
281             ByteArrayInputStream bais = new ByteArrayInputStream(vfFile.getContent());
282             InputStreamReader isr = new InputStreamReader(bais, "UTF-8");
283             
284             xml =
285                 DocumentBuilderFactory
286                     .newInstance()
287                     .newDocumentBuilder()
288                     .parse(
289                     new org.xml.sax.InputSource JavaDoc(isr));
290         } catch (SAXException e1) {
291             e1.printStackTrace();
292         } catch (ParserConfigurationException e1) {
293             e1.printStackTrace();
294         } catch (FactoryConfigurationError e1) {
295             e1.printStackTrace();
296         } catch (IOException e) {
297             e.printStackTrace();
298         }
299
300         if(xml!=null) {
301             Element rootEl = (Element) xml.getDocumentElement();
302     
303             String JavaDoc sXMPath = XMetaLEditor.m_sXMetalPath;
304             sXMPath = replace(sXMPath, "\\", "/");
305             sXMPath = replace(sXMPath, " ", "%20");
306     
307             String JavaDoc sValue = "http://www.simulacramedia.com/harmonise/document file:///" + sXMPath + "/Rules/harmonise-document.xsd";
308
309             rootEl.setAttribute("xsi:schemaLocation", sValue);
310             rootEl.setAttribute("xmlns", "http://www.simulacramedia.com/harmonise/document");
311             rootEl.setAttribute("xmlns:" + NamespaceType.XML_SCHEMA_INSTANCE.getPrefix(), NamespaceType.XML_SCHEMA_INSTANCE.getURI());
312
313             XMLPrettyPrint printer = new XMLPrettyPrint();
314             OutputStreamWriter osw = null;
315             try {
316     
317                 fos = new FileOutputStream(fFile);
318
319                 osw = new OutputStreamWriter(fos, "UTF-8");
320                 osw.write(printer.printNode(rootEl));
321             } catch (FileNotFoundException e) {
322                 e.printStackTrace();
323             } catch (IOException e) {
324                 e.printStackTrace();
325             } catch (NamespaceClashException e) {
326                 e.printStackTrace();
327             } finally {
328                 try {
329                     osw.close();
330                     fos.close();
331                 } catch (IOException e1) {
332                     e1.printStackTrace();
333                 }
334             }
335         }
336         
337         return fFile.getAbsolutePath();
338     }
339     
340     String JavaDoc replace(String JavaDoc s, String JavaDoc one, String JavaDoc another) {
341 // In a string replace one substring with another
342
if (s==null || one==null || another==null || s.equals("")) return "";
343       String JavaDoc res = "";
344       int i = s.indexOf(one,0);
345       int lastpos = 0;
346       while (i != -1) {
347         res += s.substring(lastpos,i) + another;
348         lastpos = i + one.length();
349         i = s.indexOf(one,lastpos);
350       }
351       res += s.substring(lastpos); // the rest
352
return res;
353     }
354
355     /* (non-Javadoc)
356      * @see com.simulacramedia.contentmanager.editors.Editor#export(java.lang.String, com.simulacramedia.vfs.AbstractVirtualFileSystem)
357      */

358     public StatusData export(String JavaDoc sPath, AbstractVirtualFileSystem vfs) {
359         VirtualFile vfFile = vfs.getVirtualFile(sPath).getResource();
360         String JavaDoc sFilename = super.getFileName(vfFile);
361         
362         String JavaDoc sTempPath = m_sEditFilePath + sFilename;
363         File fTempFile = new File(sTempPath);
364         
365         JFileChooser chooser = new JFileChooser();
366         chooser.setSelectedFile(fTempFile);
367         
368         chooser.setFileFilter(new XMLFilter());
369         
370         int returnVal =
371             chooser.showSaveDialog(
372                 DisplayManager.getInstance().getMainWindow());
373         if (returnVal == JFileChooser.APPROVE_OPTION) {
374             File fFile = chooser.getSelectedFile();
375             super.createWorkingFile(vfFile, fFile);
376         }
377         
378         return new VFSStatus();
379     }
380
381     /* (non-Javadoc)
382      * @see com.simulacramedia.contentmanager.editors.Editor#createNew(java.lang.String, byte[], com.simulacramedia.vfs.AbstractVirtualFileSystem)
383      */

384     public PathStatusWrapper createNew(String JavaDoc sPath, byte[] content, AbstractVirtualFileSystem vfs) {
385         ResourceStatusWrapper statusWrapper = new ResourceStatusWrapper(null, new VFSStatus());
386         
387         VirtualFile vfFile = new VirtualFile(sPath);
388         
389         vfs.getVirtualFileSystemView().setContentType(vfFile, "text/xml");
390
391         vfFile.setContent(content);
392
393         
394         statusWrapper = vfs.addVirtualFile(sPath, vfFile);
395         
396         vfFile = vfs.getVirtualFile(sPath).getResource();
397         this.m_bResourceCreated = true;
398
399         
400         if(statusWrapper.getStatus().isOK()) {
401             this.m_bResourceCreated = true;
402             return new PathStatusWrapper(super.createWorkingFile(vfFile), statusWrapper.getStatus());
403         } else {
404             statusWrapper.getStatus().setStatusLevel(StatusData.LEVEL_ERROR);
405             return new PathStatusWrapper(null, statusWrapper.getStatus());
406         }
407     }
408
409     /* (non-Javadoc)
410      * @see com.simulacramedia.contentmanager.editors.Editor#hasResourceBeenCreated()
411      */

412     public boolean hasResourceBeenCreated() {
413         return this.m_bResourceCreated;
414     }
415
416     /* (non-Javadoc)
417      * @see com.simulacramedia.contentmanager.editors.Editor#preview(java.lang.String, com.simulacramedia.vfs.AbstractVirtualFileSystem)
418      */

419     public PathStatusWrapper preview(String JavaDoc sPath, AbstractVirtualFileSystem vfs) {
420         VirtualFile vfFile = vfs.getVirtualFile(sPath).getResource();
421         PropertyInstance propInst = vfFile.getProperty(NamespaceType.OHRM.getURI(), "harmonise-id");
422         String JavaDoc sId = getPropValue(propInst);
423
424         System.out.println("Doc id: " + sId);
425
426         String JavaDoc pagePath = getPreviewPagePath(vfFile,vfs);
427         if(pagePath != null) {
428             PageDefinitionEditor pde = new PageDefinitionEditor();
429             Document stateDoc = null;
430             try {
431                 stateDoc = DocumentBuilderFactory
432                 .newInstance()
433                 .newDocumentBuilder().newDocument();
434                 Element stateRoot = stateDoc.createElement("State");
435                 Element docEl = stateDoc.createElement("Document");
436                 docEl.setAttribute("id", sId);
437                 stateRoot.appendChild(docEl);
438                 stateDoc.appendChild(stateRoot);
439             } catch (ParserConfigurationException e) {
440                 e.printStackTrace();
441             } catch (FactoryConfigurationError e) {
442                 e.printStackTrace();
443             }
444             
445             pde.preview(pagePath, vfs, stateDoc);
446         }
447         return new PathStatusWrapper(null, new VFSStatus());
448     }
449     private String JavaDoc getPreviewPagePath(VirtualFile vfFile, AbstractVirtualFileSystem vfs){
450         String JavaDoc path = null;
451         PropertyInstance propInst = vfFile.getProperty(NamespaceType.OHRM.getURI(),"PREVIEWPAGE");
452         if(propInst != null){
453             List values = propInst.getValues();
454             Iterator iter = values.iterator();
455             if(iter.hasNext()){
456                 ResourceValue val = (ResourceValue) iter.next();
457                 path = val.getValue();
458             }
459         }
460         if(path == null) {
461             String JavaDoc fullPath = ((VersionedVirtualFile)vfFile).getLiveVersionPath();
462             if(fullPath == null){
463                 fullPath = vfFile.getFilePath();
464             }
465             ((VersionedVirtualFile)vfFile).getLiveVersionPath();
466             //stop here if down to webdav root
467
System.out.println("Trying " + fullPath);
468             if(fullPath.equalsIgnoreCase("/webdav")){
469                 //try and get the default preview page
470
path = "/webdav/Website/Page Definition/defaultpreview";
471                 VirtualFile vFile = vfs.getVirtualFile(path).getResource();
472                 if(vFile == null){
473                     String JavaDoc sMsg = "You have no preview page for this collection and no default preview page set up.";
474                     MessageHandler.getInstance().fireMessageEvent(sMsg, MessageHandler.TYPE_ERROR);
475                     return null;
476                 }
477             } else {
478                 path = getPreviewPagePath(vfs.getVirtualFile(fullPath).getResource(), vfs);
479             }
480         }
481         return path;
482     }
483
484
485     private String JavaDoc getPropValue(PropertyInstance propInst){
486         String JavaDoc sValue = null;
487         if(propInst!=null && propInst.getValues().size()>0) {
488             if(propInst.getValues().get(0) instanceof StringValue) {
489                 StringValue value = (StringValue) propInst.getValues().get(0);
490                 if(value.getValue()!=null && value.getValue().length()>0) {
491                     sValue = value.getValue();
492                 }
493             }
494         }
495         return sValue;
496     }
497 }
498
Popular Tags