KickJava   Java API By Example, From Geeks To Geeks.

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


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.BufferedReader JavaDoc;
22 import java.io.ByteArrayInputStream JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.InputStreamReader JavaDoc;
27 import java.io.StringReader JavaDoc;
28 import java.net.MalformedURLException JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.StringTokenizer JavaDoc;
32
33 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
34 import javax.xml.parsers.FactoryConfigurationError JavaDoc;
35 import javax.xml.parsers.ParserConfigurationException JavaDoc;
36
37 import org.openharmonise.commons.net.*;
38 import org.openharmonise.commons.xml.*;
39 import org.openharmonise.commons.xml.namespace.*;
40 import org.openharmonise.him.harmonise.*;
41 import org.openharmonise.vfs.*;
42 import org.openharmonise.vfs.status.*;
43 import org.w3c.dom.Document JavaDoc;
44 import org.w3c.dom.Element JavaDoc;
45 import org.w3c.dom.Node JavaDoc;
46 import org.w3c.dom.NodeList JavaDoc;
47 import org.xml.sax.InputSource JavaDoc;
48 import org.xml.sax.SAXException JavaDoc;
49
50
51 /**
52  * Handles editing of XSLT resources. Parses XSLTs and recursively
53  * downloads all imported and included XSLTs so that XSLTs can be run
54  * locally.
55  *
56  * @author Matthew Large
57  * @version $Revision: 1.2 $
58  *
59  */

60 public class XSLTEditor extends GenericEditor implements Editor {
61
62     private boolean m_bResourceCreated = false;
63
64     /**
65      *
66      */

67     public XSLTEditor() {
68         super();
69     }
70
71     /* (non-Javadoc)
72      * @see com.simulacramedia.contentmanager.editors.Editor#createNew(java.lang.String, com.simulacramedia.vfs.AbstractVirtualFileSystem)
73      */

74     public PathStatusWrapper createNew(String JavaDoc sPath, AbstractVirtualFileSystem vfs) {
75         ResourceStatusWrapper statusWrapper = new ResourceStatusWrapper(null, new VFSStatus());
76         
77         VirtualFile vfFile = new VirtualFile(sPath);
78         
79         vfs.getVirtualFileSystemView().setContentType(vfFile, MimeTypeMapping.XSLT.getMimeType());
80
81         StringBuffer JavaDoc sBuff = new StringBuffer JavaDoc();
82         InputStream JavaDoc is = null;
83         
84         is = XMetaLEditor.class.getResourceAsStream("/org/openharmonise/him/icons/xml/newXSLT.xsl");
85         
86         BufferedReader JavaDoc buff = new BufferedReader JavaDoc( new InputStreamReader JavaDoc(is));
87         
88         String JavaDoc sLine = null;
89         try {
90             while((sLine=buff.readLine())!=null) {
91                 sBuff.append(sLine);
92             }
93             Document JavaDoc xml = null;
94             try {
95                 xml =
96                     DocumentBuilderFactory
97                         .newInstance()
98                         .newDocumentBuilder()
99                         .parse(
100                         new org.xml.sax.InputSource JavaDoc(
101                             new StringReader JavaDoc(sBuff.toString())));
102             } catch (SAXException JavaDoc e1) {
103                 e1.printStackTrace();
104             } catch (ParserConfigurationException JavaDoc e1) {
105                 e1.printStackTrace();
106             } catch (FactoryConfigurationError JavaDoc e1) {
107                 e1.printStackTrace();
108             }
109
110             if(xml!=null) {
111                 Element JavaDoc rootEl = (Element JavaDoc) xml.getDocumentElement();
112                 
113                 XMLPrettyPrint printer = new XMLPrettyPrint();
114                 
115                 vfFile.setContent(printer.printNode(rootEl).getBytes());
116             }
117         
118             statusWrapper = vfs.addVirtualFile(sPath, vfFile);
119         
120             vfFile = vfs.getVirtualFile(sPath).getResource();
121         
122             if(statusWrapper.getStatus().isOK()) {
123                 this.m_bResourceCreated = true;
124                 return new PathStatusWrapper(createWorkingFile(vfFile), statusWrapper.getStatus());
125             }
126         } catch (IOException JavaDoc e) {
127             e.printStackTrace();
128         } catch (NamespaceClashException e) {
129             e.printStackTrace();
130         }
131         statusWrapper.getStatus().setStatusLevel(StatusData.LEVEL_ERROR);
132         return new PathStatusWrapper(null, statusWrapper.getStatus());
133     }
134     protected String JavaDoc createPreviewFile(VirtualFile vfFile){
135         return createWorkingFile(vfFile,m_sPreviewFilePath, true);
136     }
137     protected String JavaDoc createWorkingFile(VirtualFile vfFile) {
138         return createWorkingFile(vfFile,m_sEditFilePath, true);
139     }
140
141     /* (non-Javadoc)
142      * @see com.simulacramedia.contentmanager.editors.AbstractEditor#createWorkingFile(com.simulacramedia.vfs.VirtualFile)
143      */

144     private String JavaDoc createWorkingFile(VirtualFile vfFile, String JavaDoc path, boolean bOverWrite) {
145         String JavaDoc sVirtualPath = vfFile.getFullPath();
146         String JavaDoc sRelativeVirtualPath = sVirtualPath.substring(HarmonisePaths.PATH_XSLT.length() + 1);
147         
148         String JavaDoc sBuildPath = path;
149         StringTokenizer JavaDoc sTok = new StringTokenizer JavaDoc(sRelativeVirtualPath, "/");
150         while(sTok.hasMoreTokens()) {
151             String JavaDoc sToken = sTok.nextToken();
152             if(sTok.hasMoreTokens()) {
153                 sBuildPath = sBuildPath + "\\" + sToken;
154                 File JavaDoc dir = new File JavaDoc(sBuildPath);
155                 if(!dir.exists()) {
156                     dir.mkdir();
157                 }
158             }
159         }
160         
161         String JavaDoc sRealFilePath = sBuildPath + "\\" + super.getFileName(vfFile);
162         
163         File JavaDoc file = new File JavaDoc(sRealFilePath);
164         if(bOverWrite == false && file.exists()){
165             return file.getAbsolutePath();
166         } else {
167             return super.createWorkingFile(vfFile, file);
168         }
169     }
170
171     /* (non-Javadoc)
172      * @see com.simulacramedia.contentmanager.editors.Editor#open(java.lang.String, com.simulacramedia.vfs.AbstractVirtualFileSystem)
173      */

174     public PathStatusWrapper open(String JavaDoc sPath, AbstractVirtualFileSystem vfs) {
175         VirtualFile vfFile = vfs.getVirtualFile(sPath).getResource();
176         
177         String JavaDoc sWorkingFilePath = createWorkingFile(vfFile);
178
179         File JavaDoc fFile = new File JavaDoc(sWorkingFilePath);
180         
181         try {
182             Process JavaDoc proc5 = Runtime.getRuntime().exec( "rundll32 url.dll,FileProtocolHandler file:/" + fFile.toURI().getPath() );
183         } catch (MalformedURLException JavaDoc e1) {
184             e1.printStackTrace();
185         } catch (IOException JavaDoc e1) {
186             e1.printStackTrace();
187         }
188
189         return new PathStatusWrapper(sWorkingFilePath, new VFSStatus());
190     }
191     public void downloadImports(VirtualFile vfFile) {
192         downloadImports(vfFile, this.m_sEditFilePath);
193     }
194     public void downloadPreviewImports(VirtualFile vfFile) {
195         downloadImports(vfFile, this.m_sPreviewFilePath);
196     }
197     /**
198      * Recursively parses XSLTs and downloads imported and included
199      * XSLT resources.
200      *
201      * @param vfFile XSLT virtual file
202      */

203     private void downloadImports(VirtualFile vfFile, String JavaDoc path) {
204
205         Document JavaDoc document = null;
206         try {
207             DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
208             factory.setNamespaceAware(true);
209             document = factory.newDocumentBuilder().parse( new InputSource JavaDoc( new InputStreamReader JavaDoc( new ByteArrayInputStream JavaDoc(vfFile.getContent()) ) ) );
210         } catch (SAXException JavaDoc e) {
211             e.printStackTrace(System.out);
212         } catch (ParserConfigurationException JavaDoc e) {
213             e.printStackTrace(System.out);
214         } catch (FactoryConfigurationError JavaDoc e) {
215             e.printStackTrace(System.out);
216         } catch (IOException JavaDoc e) {
217             e.printStackTrace();
218         }
219         
220         if(document!=null) {
221             Element JavaDoc elRoot = document.getDocumentElement();
222             NodeList JavaDoc nl = elRoot.getElementsByTagNameNS(NamespaceType.XSLT.getURI(), "import");
223             for(int i=0; i<nl.getLength(); i++) {
224                 Node JavaDoc node = nl.item(i);
225                 if(node.getNodeType()==Node.ELEMENT_NODE) {
226                     Element JavaDoc element = (Element JavaDoc)node;
227                     if(element.hasAttribute("href")) {
228                         String JavaDoc sHREF = element.getAttribute("href");
229                         sHREF = sHREF.substring(0,sHREF.lastIndexOf("."));
230                         //try getting the live path first
231
String JavaDoc sServerPath = ((VersionedVirtualFile)vfFile).getLiveVersionPath();
232                         if(sServerPath == null){
233                             sServerPath = vfFile.getFullPath();
234                         }
235                         String JavaDoc sVirtualPath = this.buildNewPath(sServerPath, sHREF);
236                         VirtualFile vfImport = vfFile.getVFS().getVirtualFile(sVirtualPath).getResource();
237                         if(vfImport!=null) {
238                             this.createWorkingFile(vfImport,path, false);
239                             this.downloadImports(vfImport,path);
240                         } else {
241                             //System.out.println("IMPORT VFFILE from [" + sVirtualPath + "] is NULL!!!!");
242
}
243                     }
244                 }
245             }
246             nl = elRoot.getElementsByTagNameNS(NamespaceType.XSLT.getURI(), "include");
247             for(int i=0; i<nl.getLength(); i++) {
248                 Node JavaDoc node = nl.item(i);
249                 if(node.getNodeType()==Node.ELEMENT_NODE) {
250                     Element JavaDoc element = (Element JavaDoc)node;
251                     if(element.hasAttribute("href")) {
252                         String JavaDoc sHREF = element.getAttribute("href");
253                         sHREF = sHREF.substring(0,sHREF.lastIndexOf("."));
254                         
255                         String JavaDoc sVirtualPath = this.buildNewPath(vfFile.getFullPath(), sHREF);
256                         VirtualFile vfImport = vfFile.getVFS().getVirtualFile(sVirtualPath).getResource();
257                         if(vfImport!=null) {
258                             this.createWorkingFile(vfImport,path, false);
259                             this.downloadImports(vfImport,path);
260                         } else {
261                             
262                         }
263                     }
264                 }
265             }
266         }
267     }
268         
269     private String JavaDoc buildNewPath(String JavaDoc sBasePath, String JavaDoc sRelativePath) {
270         ArrayList JavaDoc aBasePathSegmenets = new ArrayList JavaDoc();
271         ArrayList JavaDoc aRelativePathSegmenets = new ArrayList JavaDoc();
272         
273         StringTokenizer JavaDoc sTok = new StringTokenizer JavaDoc(sBasePath, "/\\");
274         while(sTok.hasMoreTokens()) {
275             String JavaDoc sTemp = sTok.nextToken();
276             if(sTok.hasMoreTokens()) {
277                 aBasePathSegmenets.add(sTemp);
278             }
279         }
280         
281         sTok = new StringTokenizer JavaDoc(sRelativePath, "/\\");
282         while(sTok.hasMoreTokens()) {
283             String JavaDoc sTemp = sTok.nextToken();
284             aRelativePathSegmenets.add(sTemp);
285         }
286         
287         Iterator JavaDoc itor = aRelativePathSegmenets.iterator();
288         while (itor.hasNext()) {
289             String JavaDoc sRelativeSegment = (String JavaDoc) itor.next();
290             if(sRelativeSegment.equals(".")) {
291                 //NO-OP ignore
292
} else if(sRelativeSegment.equals("..")) {
293                 aBasePathSegmenets.remove(aBasePathSegmenets.size()-1);
294             }
295         }
296         
297         StringBuffer JavaDoc sBuff = new StringBuffer JavaDoc();
298         itor = aBasePathSegmenets.iterator();
299         while (itor.hasNext()) {
300             String JavaDoc sBaseSegment = (String JavaDoc) itor.next();
301             sBuff.append("/").append(sBaseSegment);
302         }
303
304         itor = aRelativePathSegmenets.iterator();
305         while (itor.hasNext()) {
306             String JavaDoc sRelativeSegment = (String JavaDoc) itor.next();
307             if(!sRelativeSegment.equals(".") && !sRelativeSegment.equals("..")) {
308                 sBuff.append("/").append(sRelativeSegment);
309             }
310         }
311         
312         return sBuff.toString();
313     }
314
315     /* (non-Javadoc)
316      * @see com.simulacramedia.contentmanager.editors.Editor#hasResourceBeenCreated()
317      */

318     public boolean hasResourceBeenCreated() {
319         return this.m_bResourceCreated;
320     }
321 }
Popular Tags