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.awt.*;
022: import java.io.*;
023: import java.net.*;
024:
025: import javax.swing.*;
026: import javax.xml.parsers.*;
027: import javax.xml.transform.*;
028: import javax.xml.transform.dom.*;
029: import javax.xml.transform.stream.*;
030:
031: import org.openharmonise.commons.net.*;
032: import org.openharmonise.him.editors.pagegui.*;
033: import org.openharmonise.him.window.messages.*;
034: import org.openharmonise.vfs.*;
035: import org.openharmonise.vfs.gui.*;
036: import org.openharmonise.vfs.status.*;
037: import org.w3c.dom.*;
038:
039: /**
040: * The page definition editor handles the editing of page objects.
041: *
042: * @author Matthew Large
043: * @version $Revision: 1.2 $
044: *
045: */
046: public class PageDefinitionEditor extends CompositionEditor implements
047: Editor {
048:
049: private boolean m_bResourceCreated = false;
050:
051: /**
052: *
053: */
054: public PageDefinitionEditor() {
055: super ();
056: }
057:
058: /* (non-Javadoc)
059: * @see com.simulacramedia.contentmanager.editors.Editor#open(java.lang.String, com.simulacramedia.vfs.AbstractVirtualFileSystem)
060: */
061: public PathStatusWrapper open(String sPath,
062: AbstractVirtualFileSystem vfs) {
063: VirtualFile vfFile = vfs.getVirtualFile(sPath).getResource();
064:
065: String sTitle = "Page Definition Editor";
066:
067: Frame frame = new Frame();
068: frame.setIconImage(((ImageIcon) IconManager.getInstance()
069: .getIcon("16-page-definition.gif")).getImage());
070: PageDefinitionDialog dialog = new PageDefinitionDialog(frame,
071: sTitle);
072:
073: dialog.setData(vfFile.getContent());
074:
075: dialog.show();
076:
077: if (dialog.getData() != null) {
078: vfFile.setContent(dialog.getData());
079: }
080:
081: return new PathStatusWrapper(null, new VFSStatus());
082: }
083:
084: /* (non-Javadoc)
085: * @see com.simulacramedia.contentmanager.editors.Editor#createNew(java.lang.String, com.simulacramedia.vfs.AbstractVirtualFileSystem)
086: */
087: public PathStatusWrapper createNew(String sPath,
088: AbstractVirtualFileSystem vfs) {
089: ResourceStatusWrapper statusWrapper = new ResourceStatusWrapper(
090: null, new VFSStatus());
091:
092: String sTitle = "Page Definition Editor";
093:
094: Frame frame = new Frame();
095: frame.setIconImage(((ImageIcon) IconManager.getInstance()
096: .getIcon("16-page-definition.gif")).getImage());
097: PageDefinitionDialog dialog = new PageDefinitionDialog(frame,
098: sTitle);
099:
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) MimeTypeMapping.getMimeTypes("xml")
109: .get(0));
110: }
111:
112: vfFile.setContent(dialog.getData());
113: statusWrapper = vfs.addVirtualFile(sPath, vfFile);
114: if (statusWrapper.getStatus().isOK()) {
115: this .m_bResourceCreated = true;
116: }
117: }
118:
119: return new PathStatusWrapper(null, statusWrapper.getStatus());
120: }
121:
122: /* (non-Javadoc)
123: * @see com.simulacramedia.contentmanager.editors.Editor#discardChanges(java.lang.String, com.simulacramedia.vfs.AbstractVirtualFileSystem)
124: */
125: public StatusData discardChanges(String sPath,
126: AbstractVirtualFileSystem vfs) {
127: return new VFSStatus();
128: }
129:
130: /* (non-Javadoc)
131: * @see com.simulacramedia.contentmanager.editors.Editor#export(java.lang.String, com.simulacramedia.vfs.AbstractVirtualFileSystem)
132: */
133: public StatusData export(String sPath, AbstractVirtualFileSystem vfs) {
134: return new VFSStatus();
135: }
136:
137: /* (non-Javadoc)
138: * @see com.simulacramedia.contentmanager.editors.Editor#createNew(java.lang.String, byte[], com.simulacramedia.vfs.AbstractVirtualFileSystem)
139: */
140: public PathStatusWrapper createNew(String sPath, byte[] content,
141: AbstractVirtualFileSystem vfs) {
142:
143: ResourceStatusWrapper statusWrapper = new ResourceStatusWrapper(
144: null, new VFSStatus());
145:
146: VirtualFile vfFile = new VirtualFile(sPath);
147:
148: if (sPath.startsWith(".webdav/Content/Assets/links")) {
149: vfs.getVirtualFileSystemView()
150: .setContentType(
151: vfFile,
152: (String) MimeTypeMapping
153: .getMimeTypes("xml").get(0));
154: }
155:
156: vfFile.setContent(content);
157: statusWrapper = vfs.addVirtualFile(sPath, vfFile);
158: if (statusWrapper.getStatus().isOK()) {
159: this .m_bResourceCreated = true;
160: }
161:
162: return new PathStatusWrapper(null, statusWrapper.getStatus());
163: }
164:
165: /* (non-Javadoc)
166: * @see com.simulacramedia.contentmanager.editors.Editor#hasResourceBeenCreated()
167: */
168: public boolean hasResourceBeenCreated() {
169: return this .m_bResourceCreated;
170: }
171:
172: /* (non-Javadoc)
173: * @see com.simulacramedia.contentmanager.editors.Editor#preview(java.lang.String, com.simulacramedia.vfs.AbstractVirtualFileSystem)
174: */
175: public PathStatusWrapper preview(String sPath,
176: AbstractVirtualFileSystem vfs) {
177: return preview(sPath, vfs, null);
178: }
179:
180: public PathStatusWrapper preview(String sPath,
181: AbstractVirtualFileSystem vfs, Document state) {
182: VirtualFile vfFile = vfs.getVirtualFile(sPath).getResource();
183: String sPageFileName = vfFile.getFileName();
184: VirtualFile vfXmlFile = null;
185: VirtualFile vfXslFile = null;
186: String sMessage = null;
187: try {
188: Document xml = DocumentBuilderFactory.newInstance()
189: .newDocumentBuilder().parse(
190: new org.xml.sax.InputSource(
191: new StringReader(new String(vfFile
192: .getContent()))));
193:
194: Element rootEl = xml.getDocumentElement();
195: NodeList nl = rootEl.getChildNodes();
196: String sMimeType = null;
197: for (int i = 0; i < nl.getLength(); i++) {
198: Node node = nl.item(i);
199: if (node.getNodeType() == Node.ELEMENT_NODE) {
200: Element element = (Element) node;
201: if (element.getTagName().equals("XML")) {
202: NodeList nl2 = element.getChildNodes();
203: for (int j = 0; j < nl2.getLength(); j++) {
204: Node child = nl2.item(j);
205: if (child.getNodeType() == Node.ELEMENT_NODE
206: && ((Element) child).getTagName()
207: .equals("href")
208: && child.getChildNodes()
209: .getLength() == 1
210: && child.getFirstChild()
211: .getNodeType() == Node.TEXT_NODE) {
212: Text txt = (Text) child.getFirstChild();
213: vfXmlFile = vfs.getVirtualFile(
214: txt.getNodeValue())
215: .getResource();
216: }
217: }
218: } else if (element.getTagName().equals("XSL")) {
219: NodeList nl2 = element.getChildNodes();
220: for (int j = 0; j < nl2.getLength(); j++) {
221: Node child = nl2.item(j);
222: if (child.getNodeType() == Node.ELEMENT_NODE
223: && child.getChildNodes()
224: .getLength() == 1
225: && child.getFirstChild()
226: .getNodeType() == Node.TEXT_NODE) {
227: if (((Element) child).getTagName()
228: .equals("href")) {
229: Text txt = (Text) child
230: .getFirstChild();
231: vfXslFile = vfs.getVirtualFile(
232: txt.getNodeValue())
233: .getResource();
234: } else if (((Element) child)
235: .getTagName()
236: .equals("mimetype")) {
237: sMimeType = ((Text) child
238: .getFirstChild()).getData();
239: }
240: }
241: }
242: }
243: }
244: }
245: if (vfXmlFile != null && vfXslFile != null) {
246: //get the harmonise page xml
247: String sOut = invokePublishService(vfXmlFile, vfs,
248: state);
249: if (sOut != null) {
250: Document pageXml = DocumentBuilderFactory
251: .newInstance().newDocumentBuilder().parse(
252: new org.xml.sax.InputSource(
253: new StringReader(sOut)));
254: //get the xslt
255: XSLTEditor xsltEditor = new XSLTEditor();
256: TransformerFactory factory = TransformerFactory
257: .newInstance();
258:
259: String sWorkingFilePath = xsltEditor
260: .createPreviewFile(vfXslFile);
261: File file = new File(sWorkingFilePath);
262: xsltEditor.downloadPreviewImports(vfXslFile);
263: StreamSource ssource = new StreamSource(
264: new FileReader(file));
265: ssource.setSystemId(file);
266: Templates templates = factory.newTemplates(ssource);
267: String sFilename = createPreviewFileName(
268: getFileName(vfFile), sMimeType);
269: sWorkingFilePath = createPreviewFile(sFilename);
270: File fFile = new File(sWorkingFilePath);
271:
272: ByteArrayOutputStream bos = new ByteArrayOutputStream();
273: render(pageXml, templates, bos);
274: String result = bos.toString();
275: String newResult = resolveRelativePaths(result, vfs);
276: BufferedWriter out = new BufferedWriter(
277: new FileWriter(sWorkingFilePath));
278: out.write(newResult);
279: bos.close();
280: out.close();
281: Runtime.getRuntime().exec(
282: "\"" + m_sBrowserPath + "\" " + "file:/"
283: + fFile.toURI().getPath());
284: }
285: }
286: } catch (Exception e) {
287: sMessage = "There was a problem previewing "
288: + sPageFileName + ":\n" + e.getLocalizedMessage();
289: e.printStackTrace(System.err);
290: }
291: if (sMessage != null) {
292: MessageHandler.getInstance().fireMessageEvent(sMessage,
293: MessageHandler.TYPE_ERROR);
294: }
295: return new PathStatusWrapper(null, new VFSStatus());
296: }
297:
298: public void render(Document doc, Templates templates,
299: OutputStream out) throws Exception {
300: try {
301: OutputStreamWriter outwriter = new OutputStreamWriter(out,
302: "UTF-8");
303:
304: Transformer trans = templates.newTransformer();
305:
306: DOMSource ds = new DOMSource(doc.getDocumentElement());
307:
308: StreamResult res = new StreamResult(outwriter);
309:
310: trans.transform(ds, res);
311: } catch (TransformerConfigurationException e) {
312: throw new Exception("Error occured with configuration", e);
313: } catch (TransformerException e) {
314: throw new Exception("Error occured with tramsformation", e);
315: } catch (UnsupportedEncodingException e) {
316: throw new Exception("Unsupported encoding error", e);
317: }
318: }
319:
320: protected String createPreviewFileName(String sFilename,
321: String sMimeType) {
322: String sNewFilename = sFilename;
323: if (sMimeType != null) {
324: int index = sFilename.lastIndexOf(".");
325: sNewFilename = sFilename.substring(0, index + 1);
326: sNewFilename += MimeTypeMapping
327: .getExtensionFromMimeType(sMimeType);
328: }
329: return sNewFilename;
330: }
331:
332: protected String resolveRelativePaths(String sContents,
333: AbstractVirtualFileSystem vfs) {
334: URI uri = vfs.getURI();
335: String sURL = uri.getScheme() + "://" + uri.getHost() + ":"
336: + uri.getPort();
337: String sNewContents = replaceAllIgnoreCase(sContents,
338: "href=\"/", "href=\"" + sURL + "/");
339: sNewContents = replaceAllIgnoreCase(sNewContents, "src=\"/",
340: "src=\"" + sURL + "/");
341: sURL += "/webdav/servlet/";
342: sNewContents = sNewContents.replaceAll("\"HarmoniseServlet",
343: "\"" + sURL + "HarmoniseServlet");
344: sNewContents = sNewContents.replaceAll("'HarmoniseServlet", "'"
345: + sURL + "HarmoniseServlet");
346: return sNewContents;
347: }
348:
349: public String replaceAllIgnoreCase(String in, String arg0,
350: String arg1) {
351: String out = in;
352: String lower = in.toLowerCase();
353: int index = lower.indexOf(arg0.toLowerCase());
354: if (index > 0) {
355: out = out.substring(0, index) + arg1
356: + out.substring(index + arg0.length());
357: out = replaceAllIgnoreCase(out, arg0, arg1);
358: }
359: return out;
360: }
361: }
|