001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.him.editors;
020:
021: import java.io.BufferedReader;
022: import java.io.ByteArrayInputStream;
023: import java.io.File;
024: import java.io.IOException;
025: import java.io.InputStream;
026: import java.io.InputStreamReader;
027: import java.io.StringReader;
028: import java.net.MalformedURLException;
029: import java.util.ArrayList;
030: import java.util.Iterator;
031: import java.util.StringTokenizer;
032:
033: import javax.xml.parsers.DocumentBuilderFactory;
034: import javax.xml.parsers.FactoryConfigurationError;
035: import javax.xml.parsers.ParserConfigurationException;
036:
037: import org.openharmonise.commons.net.*;
038: import org.openharmonise.commons.xml.*;
039: import org.openharmonise.commons.xml.namespace.*;
040: import org.openharmonise.him.harmonise.*;
041: import org.openharmonise.vfs.*;
042: import org.openharmonise.vfs.status.*;
043: import org.w3c.dom.Document;
044: import org.w3c.dom.Element;
045: import org.w3c.dom.Node;
046: import org.w3c.dom.NodeList;
047: import org.xml.sax.InputSource;
048: import org.xml.sax.SAXException;
049:
050: /**
051: * Handles editing of XSLT resources. Parses XSLTs and recursively
052: * downloads all imported and included XSLTs so that XSLTs can be run
053: * locally.
054: *
055: * @author Matthew Large
056: * @version $Revision: 1.2 $
057: *
058: */
059: public class XSLTEditor extends GenericEditor implements Editor {
060:
061: private boolean m_bResourceCreated = false;
062:
063: /**
064: *
065: */
066: public XSLTEditor() {
067: super ();
068: }
069:
070: /* (non-Javadoc)
071: * @see com.simulacramedia.contentmanager.editors.Editor#createNew(java.lang.String, com.simulacramedia.vfs.AbstractVirtualFileSystem)
072: */
073: public PathStatusWrapper createNew(String sPath,
074: AbstractVirtualFileSystem vfs) {
075: ResourceStatusWrapper statusWrapper = new ResourceStatusWrapper(
076: null, new VFSStatus());
077:
078: VirtualFile vfFile = new VirtualFile(sPath);
079:
080: vfs.getVirtualFileSystemView().setContentType(vfFile,
081: MimeTypeMapping.XSLT.getMimeType());
082:
083: StringBuffer sBuff = new StringBuffer();
084: InputStream is = null;
085:
086: is = XMetaLEditor.class
087: .getResourceAsStream("/org/openharmonise/him/icons/xml/newXSLT.xsl");
088:
089: BufferedReader buff = new BufferedReader(new InputStreamReader(
090: is));
091:
092: String sLine = null;
093: try {
094: while ((sLine = buff.readLine()) != null) {
095: sBuff.append(sLine);
096: }
097: Document xml = null;
098: try {
099: xml = DocumentBuilderFactory.newInstance()
100: .newDocumentBuilder().parse(
101: new org.xml.sax.InputSource(
102: new StringReader(sBuff
103: .toString())));
104: } catch (SAXException e1) {
105: e1.printStackTrace();
106: } catch (ParserConfigurationException e1) {
107: e1.printStackTrace();
108: } catch (FactoryConfigurationError e1) {
109: e1.printStackTrace();
110: }
111:
112: if (xml != null) {
113: Element rootEl = (Element) xml.getDocumentElement();
114:
115: XMLPrettyPrint printer = new XMLPrettyPrint();
116:
117: vfFile.setContent(printer.printNode(rootEl).getBytes());
118: }
119:
120: statusWrapper = vfs.addVirtualFile(sPath, vfFile);
121:
122: vfFile = vfs.getVirtualFile(sPath).getResource();
123:
124: if (statusWrapper.getStatus().isOK()) {
125: this .m_bResourceCreated = true;
126: return new PathStatusWrapper(createWorkingFile(vfFile),
127: statusWrapper.getStatus());
128: }
129: } catch (IOException e) {
130: e.printStackTrace();
131: } catch (NamespaceClashException e) {
132: e.printStackTrace();
133: }
134: statusWrapper.getStatus()
135: .setStatusLevel(StatusData.LEVEL_ERROR);
136: return new PathStatusWrapper(null, statusWrapper.getStatus());
137: }
138:
139: protected String createPreviewFile(VirtualFile vfFile) {
140: return createWorkingFile(vfFile, m_sPreviewFilePath, true);
141: }
142:
143: protected String createWorkingFile(VirtualFile vfFile) {
144: return createWorkingFile(vfFile, m_sEditFilePath, true);
145: }
146:
147: /* (non-Javadoc)
148: * @see com.simulacramedia.contentmanager.editors.AbstractEditor#createWorkingFile(com.simulacramedia.vfs.VirtualFile)
149: */
150: private String createWorkingFile(VirtualFile vfFile, String path,
151: boolean bOverWrite) {
152: String sVirtualPath = vfFile.getFullPath();
153: String sRelativeVirtualPath = sVirtualPath
154: .substring(HarmonisePaths.PATH_XSLT.length() + 1);
155:
156: String sBuildPath = path;
157: StringTokenizer sTok = new StringTokenizer(
158: sRelativeVirtualPath, "/");
159: while (sTok.hasMoreTokens()) {
160: String sToken = sTok.nextToken();
161: if (sTok.hasMoreTokens()) {
162: sBuildPath = sBuildPath + "\\" + sToken;
163: File dir = new File(sBuildPath);
164: if (!dir.exists()) {
165: dir.mkdir();
166: }
167: }
168: }
169:
170: String sRealFilePath = sBuildPath + "\\"
171: + super .getFileName(vfFile);
172:
173: File file = new File(sRealFilePath);
174: if (bOverWrite == false && file.exists()) {
175: return file.getAbsolutePath();
176: } else {
177: return super .createWorkingFile(vfFile, file);
178: }
179: }
180:
181: /* (non-Javadoc)
182: * @see com.simulacramedia.contentmanager.editors.Editor#open(java.lang.String, com.simulacramedia.vfs.AbstractVirtualFileSystem)
183: */
184: public PathStatusWrapper open(String sPath,
185: AbstractVirtualFileSystem vfs) {
186: VirtualFile vfFile = vfs.getVirtualFile(sPath).getResource();
187:
188: String sWorkingFilePath = createWorkingFile(vfFile);
189:
190: File fFile = new File(sWorkingFilePath);
191:
192: try {
193: Process proc5 = Runtime.getRuntime().exec(
194: "rundll32 url.dll,FileProtocolHandler file:/"
195: + fFile.toURI().getPath());
196: } catch (MalformedURLException e1) {
197: e1.printStackTrace();
198: } catch (IOException e1) {
199: e1.printStackTrace();
200: }
201:
202: return new PathStatusWrapper(sWorkingFilePath, new VFSStatus());
203: }
204:
205: public void downloadImports(VirtualFile vfFile) {
206: downloadImports(vfFile, this .m_sEditFilePath);
207: }
208:
209: public void downloadPreviewImports(VirtualFile vfFile) {
210: downloadImports(vfFile, this .m_sPreviewFilePath);
211: }
212:
213: /**
214: * Recursively parses XSLTs and downloads imported and included
215: * XSLT resources.
216: *
217: * @param vfFile XSLT virtual file
218: */
219: private void downloadImports(VirtualFile vfFile, String path) {
220:
221: Document document = null;
222: try {
223: DocumentBuilderFactory factory = DocumentBuilderFactory
224: .newInstance();
225: factory.setNamespaceAware(true);
226: document = factory.newDocumentBuilder().parse(
227: new InputSource(new InputStreamReader(
228: new ByteArrayInputStream(vfFile
229: .getContent()))));
230: } catch (SAXException e) {
231: e.printStackTrace(System.out);
232: } catch (ParserConfigurationException e) {
233: e.printStackTrace(System.out);
234: } catch (FactoryConfigurationError e) {
235: e.printStackTrace(System.out);
236: } catch (IOException e) {
237: e.printStackTrace();
238: }
239:
240: if (document != null) {
241: Element elRoot = document.getDocumentElement();
242: NodeList nl = elRoot.getElementsByTagNameNS(
243: NamespaceType.XSLT.getURI(), "import");
244: for (int i = 0; i < nl.getLength(); i++) {
245: Node node = nl.item(i);
246: if (node.getNodeType() == Node.ELEMENT_NODE) {
247: Element element = (Element) node;
248: if (element.hasAttribute("href")) {
249: String sHREF = element.getAttribute("href");
250: sHREF = sHREF.substring(0, sHREF
251: .lastIndexOf("."));
252: //try getting the live path first
253: String sServerPath = ((VersionedVirtualFile) vfFile)
254: .getLiveVersionPath();
255: if (sServerPath == null) {
256: sServerPath = vfFile.getFullPath();
257: }
258: String sVirtualPath = this .buildNewPath(
259: sServerPath, sHREF);
260: VirtualFile vfImport = vfFile.getVFS()
261: .getVirtualFile(sVirtualPath)
262: .getResource();
263: if (vfImport != null) {
264: this .createWorkingFile(vfImport, path,
265: false);
266: this .downloadImports(vfImport, path);
267: } else {
268: //System.out.println("IMPORT VFFILE from [" + sVirtualPath + "] is NULL!!!!");
269: }
270: }
271: }
272: }
273: nl = elRoot.getElementsByTagNameNS(NamespaceType.XSLT
274: .getURI(), "include");
275: for (int i = 0; i < nl.getLength(); i++) {
276: Node node = nl.item(i);
277: if (node.getNodeType() == Node.ELEMENT_NODE) {
278: Element element = (Element) node;
279: if (element.hasAttribute("href")) {
280: String sHREF = element.getAttribute("href");
281: sHREF = sHREF.substring(0, sHREF
282: .lastIndexOf("."));
283:
284: String sVirtualPath = this .buildNewPath(vfFile
285: .getFullPath(), sHREF);
286: VirtualFile vfImport = vfFile.getVFS()
287: .getVirtualFile(sVirtualPath)
288: .getResource();
289: if (vfImport != null) {
290: this .createWorkingFile(vfImport, path,
291: false);
292: this .downloadImports(vfImport, path);
293: } else {
294:
295: }
296: }
297: }
298: }
299: }
300: }
301:
302: private String buildNewPath(String sBasePath, String sRelativePath) {
303: ArrayList aBasePathSegmenets = new ArrayList();
304: ArrayList aRelativePathSegmenets = new ArrayList();
305:
306: StringTokenizer sTok = new StringTokenizer(sBasePath, "/\\");
307: while (sTok.hasMoreTokens()) {
308: String sTemp = sTok.nextToken();
309: if (sTok.hasMoreTokens()) {
310: aBasePathSegmenets.add(sTemp);
311: }
312: }
313:
314: sTok = new StringTokenizer(sRelativePath, "/\\");
315: while (sTok.hasMoreTokens()) {
316: String sTemp = sTok.nextToken();
317: aRelativePathSegmenets.add(sTemp);
318: }
319:
320: Iterator itor = aRelativePathSegmenets.iterator();
321: while (itor.hasNext()) {
322: String sRelativeSegment = (String) itor.next();
323: if (sRelativeSegment.equals(".")) {
324: //NO-OP ignore
325: } else if (sRelativeSegment.equals("..")) {
326: aBasePathSegmenets
327: .remove(aBasePathSegmenets.size() - 1);
328: }
329: }
330:
331: StringBuffer sBuff = new StringBuffer();
332: itor = aBasePathSegmenets.iterator();
333: while (itor.hasNext()) {
334: String sBaseSegment = (String) itor.next();
335: sBuff.append("/").append(sBaseSegment);
336: }
337:
338: itor = aRelativePathSegmenets.iterator();
339: while (itor.hasNext()) {
340: String sRelativeSegment = (String) itor.next();
341: if (!sRelativeSegment.equals(".")
342: && !sRelativeSegment.equals("..")) {
343: sBuff.append("/").append(sRelativeSegment);
344: }
345: }
346:
347: return sBuff.toString();
348: }
349:
350: /* (non-Javadoc)
351: * @see com.simulacramedia.contentmanager.editors.Editor#hasResourceBeenCreated()
352: */
353: public boolean hasResourceBeenCreated() {
354: return this.m_bResourceCreated;
355: }
356: }
|