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.*;
022: import java.util.*;
023:
024: import javax.swing.*;
025: import javax.xml.parsers.*;
026:
027: import org.openharmonise.commons.xml.*;
028: import org.openharmonise.commons.xml.namespace.*;
029: import org.openharmonise.him.configuration.*;
030: import org.openharmonise.him.editors.filefilters.XMLFilter;
031: import org.openharmonise.him.window.*;
032: import org.openharmonise.him.window.messages.*;
033: import org.openharmonise.vfs.*;
034: import org.openharmonise.vfs.metadata.*;
035: import org.openharmonise.vfs.metadata.value.*;
036: import org.openharmonise.vfs.status.*;
037: import org.w3c.dom.*;
038: import org.xml.sax.*;
039:
040: /**
041: * Handles editing of documents in XMetaL.
042: *
043: * @author Matthew Large
044: * @version $Revision: 1.2 $
045: *
046: */
047: public class XMetaLEditor extends CompositionEditor implements Editor {
048:
049: private boolean m_bResourceCreated = false;
050:
051: /**
052: * Path to XMetaL application directory.
053: */
054: private static String m_sXMetalPath = null;
055:
056: /**
057: * true if XMetaL is available on this system.
058: */
059: private static boolean m_bXMetalAvailable = true;
060: private static boolean m_bTested = false;
061:
062: public XMetaLEditor() {
063: super ();
064: }
065:
066: /* (non-Javadoc)
067: * @see com.simulacramedia.contentmanager.editors.Editor#open(java.lang.String, com.simulacramedia.vfs.AbstractVirtualFileSystem)
068: */
069: public PathStatusWrapper open(String sPath,
070: AbstractVirtualFileSystem vfs) {
071: PathStatusWrapper pathStatus = new PathStatusWrapper(null,
072: new VFSStatus());
073:
074: String sWorkingFilePath = null;
075:
076: VirtualFile vfFile = vfs.getVirtualFile(sPath).getResource();
077:
078: if (isXMetalAvailable()) {
079: VFSStatus status = new VFSStatus();
080: try {
081: sWorkingFilePath = createWorkingFile(vfFile);
082: XMetaLController.getInstance().openFile(
083: sWorkingFilePath);
084: } catch (Exception e1) {
085: e1.printStackTrace();
086: status
087: .setStatusCode(StatusData.STATUS_INVALID_RESOURCE_STATE);
088: } finally {
089: pathStatus = new PathStatusWrapper(sWorkingFilePath,
090: status);
091: }
092: } else {
093: FileAssetEditor edit = new FileAssetEditor();
094: pathStatus = edit.open(sPath, vfs);
095: }
096:
097: return pathStatus;
098: }
099:
100: /* (non-Javadoc)
101: * @see com.simulacramedia.contentmanager.editors.Editor#createNew(java.lang.String, com.simulacramedia.vfs.AbstractVirtualFileSystem)
102: */
103: public PathStatusWrapper createNew(String sPath,
104: AbstractVirtualFileSystem vfs) {
105: ResourceStatusWrapper statusWrapper = new ResourceStatusWrapper(
106: null, new VFSStatus());
107:
108: VirtualFile vfFile = new VirtualFile(sPath);
109:
110: vfs.getVirtualFileSystemView().setContentType(vfFile,
111: "text/xml");
112:
113: StringBuffer sBuff = new StringBuffer();
114: InputStream is = null;
115:
116: is = XMetaLEditor.class
117: .getResourceAsStream("/org/openharmonise/him/icons/xml/newdoc.xml");
118:
119: BufferedReader buff = new BufferedReader(new InputStreamReader(
120: is));
121:
122: String sLine = null;
123: try {
124: while ((sLine = buff.readLine()) != null) {
125: sBuff.append(sLine);
126: }
127: Document xml = null;
128: try {
129: xml = DocumentBuilderFactory.newInstance()
130: .newDocumentBuilder().parse(
131: new org.xml.sax.InputSource(
132: new StringReader(sBuff
133: .toString())));
134: } catch (SAXException e1) {
135: e1.printStackTrace();
136: } catch (ParserConfigurationException e1) {
137: e1.printStackTrace();
138: } catch (FactoryConfigurationError e1) {
139: e1.printStackTrace();
140: }
141:
142: if (xml != null) {
143: Element rootEl = (Element) xml.getDocumentElement();
144:
145: XMLPrettyPrint printer = new XMLPrettyPrint();
146:
147: vfFile.setContent(printer.printNode(rootEl).getBytes());
148: }
149:
150: statusWrapper = vfs.addVirtualFile(sPath, vfFile);
151:
152: vfFile = vfs.getVirtualFile(sPath).getResource();
153:
154: if (statusWrapper.getStatus().isOK()) {
155: this .m_bResourceCreated = true;
156: return new PathStatusWrapper(super
157: .createWorkingFile(vfFile), statusWrapper
158: .getStatus());
159: }
160: } catch (IOException e) {
161: e.printStackTrace();
162: } catch (NamespaceClashException e) {
163: e.printStackTrace();
164: }
165: statusWrapper.getStatus()
166: .setStatusLevel(StatusData.LEVEL_ERROR);
167: return new PathStatusWrapper(null, statusWrapper.getStatus());
168: }
169:
170: /**
171: * Configures this editor.
172: *
173: */
174: private boolean isXMetalAvailable() {
175: if (m_bTested == false) {
176: String sValue = ConfigStore.getInstance().getPropertyValue(
177: "XMETAL_PATH");
178: if (sValue == null || sValue.length() < 1) {
179: File fPath = new File(
180: "C:\\Program Files\\Corel\\XMetaL 4\\Author");
181: if (fPath.exists()) {
182: XMetaLEditor.m_sXMetalPath = fPath
183: .getAbsolutePath();
184: ConfigStore.getInstance().setProperty(
185: "XMETAL_PATH", XMetaLEditor.m_sXMetalPath);
186: } else {
187: JFileChooser chooser = new JFileChooser();
188: chooser
189: .setDialogTitle("Select XMetaL 4: Author path");
190: int returnVal = chooser
191: .showOpenDialog(DisplayManager
192: .getInstance().getMainWindow());
193: if (returnVal == JFileChooser.APPROVE_OPTION) {
194: XMetaLEditor.m_sXMetalPath = chooser
195: .getSelectedFile().getAbsolutePath();
196: ConfigStore.getInstance().setProperty(
197: "XMETAL_PATH",
198: XMetaLEditor.m_sXMetalPath);
199: } else {
200: m_bXMetalAvailable = false;
201: }
202: }
203: } else {
204: XMetaLEditor.m_sXMetalPath = sValue;
205: }
206:
207: if (m_bXMetalAvailable
208: && XMetaLEditor.m_sXMetalPath != null
209: && XMetaLEditor.m_sXMetalPath.length() > 0) {
210: File file = new File(XMetaLEditor.m_sXMetalPath
211: + "\\rules\\harmonise-document.xsd");
212:
213: InputStream is = XMetaLEditor.class
214: .getResourceAsStream("/org/openharmonise/him/icons/files/harmonise-document.xsd");
215: FileOutputStream fo = null;
216:
217: try {
218: fo = new FileOutputStream(file);
219: int nValue;
220: while ((nValue = is.read()) != -1) {
221: fo.write(nValue);
222: }
223: } catch (FileNotFoundException e) {
224: e.printStackTrace();
225: } catch (IOException e) {
226: e.printStackTrace();
227: }
228:
229: file = new File(XMetaLEditor.m_sXMetalPath
230: + "\\rules\\harmonise-document.xac");
231:
232: is = XMetaLEditor.class
233: .getResourceAsStream("/org/openharmonise/him/icons/files/harmonise-document.xac");
234: fo = null;
235:
236: try {
237: fo = new FileOutputStream(file);
238: int nValue;
239: while ((nValue = is.read()) != -1) {
240: fo.write(nValue);
241: }
242: } catch (FileNotFoundException e) {
243: e.printStackTrace();
244: } catch (IOException e) {
245: e.printStackTrace();
246: }
247:
248: file = new File(this .m_sXMetalPath
249: + "\\rules\\harmonise-document.rld");
250:
251: is = XMetaLEditor.class
252: .getResourceAsStream("/org/openharmonise/him/icons/files/harmonise-document.rld");
253: fo = null;
254:
255: try {
256: fo = new FileOutputStream(file);
257: int nValue;
258: while ((nValue = is.read()) != -1) {
259: fo.write(nValue);
260: }
261: } catch (FileNotFoundException e) {
262: e.printStackTrace();
263: } catch (IOException e) {
264: e.printStackTrace();
265: }
266:
267: file = new File(this .m_sXMetalPath
268: + "\\rules\\harmonise-document.tbr");
269:
270: is = XMetaLEditor.class
271: .getResourceAsStream("/org/openharmonise/him/icons/files/harmonise-document.tbr");
272: fo = null;
273:
274: try {
275: fo = new FileOutputStream(file);
276: int nValue;
277: while ((nValue = is.read()) != -1) {
278: fo.write(nValue);
279: }
280: } catch (FileNotFoundException e) {
281: e.printStackTrace();
282: } catch (IOException e) {
283: e.printStackTrace();
284: }
285:
286: }
287: m_bTested = true;
288: }
289: return m_bXMetalAvailable;
290: }
291:
292: /* (non-Javadoc)
293: * @see com.simulacramedia.contentmanager.editors.AbstractEditor#createWorkingFile(com.simulacramedia.vfs.VirtualFile)
294: */
295: protected String createWorkingFile(VirtualFile vfFile) {
296: String sFilepath = super .createWorkingFile(vfFile);
297:
298: if (sFilepath == null) {
299: return null;
300: }
301:
302: File fFile = new File(sFilepath);
303:
304: FileOutputStream fos = null;
305:
306: Document xml = null;
307: try {
308: ByteArrayInputStream bais = new ByteArrayInputStream(vfFile
309: .getContent());
310: InputStreamReader isr = new InputStreamReader(bais, "UTF-8");
311:
312: xml = DocumentBuilderFactory.newInstance()
313: .newDocumentBuilder().parse(
314: new org.xml.sax.InputSource(isr));
315: } catch (SAXException e1) {
316: e1.printStackTrace();
317: } catch (ParserConfigurationException e1) {
318: e1.printStackTrace();
319: } catch (FactoryConfigurationError e1) {
320: e1.printStackTrace();
321: } catch (IOException e) {
322: e.printStackTrace();
323: }
324:
325: if (xml != null) {
326: Element rootEl = (Element) xml.getDocumentElement();
327:
328: String sXMPath = XMetaLEditor.m_sXMetalPath;
329: sXMPath = replace(sXMPath, "\\", "/");
330: sXMPath = replace(sXMPath, " ", "%20");
331:
332: String sValue = "http://www.simulacramedia.com/harmonise/document file:///"
333: + sXMPath + "/Rules/harmonise-document.xsd";
334:
335: rootEl.setAttribute("xsi:schemaLocation", sValue);
336: rootEl.setAttribute("xmlns",
337: "http://www.simulacramedia.com/harmonise/document");
338: rootEl.setAttribute("xmlns:"
339: + NamespaceType.XML_SCHEMA_INSTANCE.getPrefix(),
340: NamespaceType.XML_SCHEMA_INSTANCE.getURI());
341:
342: XMLPrettyPrint printer = new XMLPrettyPrint();
343: OutputStreamWriter osw = null;
344: try {
345:
346: fos = new FileOutputStream(fFile);
347:
348: osw = new OutputStreamWriter(fos, "UTF-8");
349: osw.write(printer.printNode(rootEl));
350: } catch (FileNotFoundException e) {
351: e.printStackTrace();
352: } catch (IOException e) {
353: e.printStackTrace();
354: } catch (NamespaceClashException e) {
355: e.printStackTrace();
356: } finally {
357: try {
358: osw.close();
359: fos.close();
360: } catch (IOException e1) {
361: e1.printStackTrace();
362: }
363: }
364: }
365:
366: return fFile.getAbsolutePath();
367: }
368:
369: String replace(String s, String one, String another) {
370: // In a string replace one substring with another
371: if (s == null || one == null || another == null || s.equals(""))
372: return "";
373: String res = "";
374: int i = s.indexOf(one, 0);
375: int lastpos = 0;
376: while (i != -1) {
377: res += s.substring(lastpos, i) + another;
378: lastpos = i + one.length();
379: i = s.indexOf(one, lastpos);
380: }
381: res += s.substring(lastpos); // the rest
382: return res;
383: }
384:
385: /* (non-Javadoc)
386: * @see com.simulacramedia.contentmanager.editors.Editor#export(java.lang.String, com.simulacramedia.vfs.AbstractVirtualFileSystem)
387: */
388: public StatusData export(String sPath, AbstractVirtualFileSystem vfs) {
389: VirtualFile vfFile = vfs.getVirtualFile(sPath).getResource();
390: String sFilename = super .getFileName(vfFile);
391:
392: String sTempPath = m_sEditFilePath + sFilename;
393: File fTempFile = new File(sTempPath);
394:
395: JFileChooser chooser = new JFileChooser();
396: chooser.setSelectedFile(fTempFile);
397:
398: chooser.setFileFilter(new XMLFilter());
399:
400: int returnVal = chooser.showSaveDialog(DisplayManager
401: .getInstance().getMainWindow());
402: if (returnVal == JFileChooser.APPROVE_OPTION) {
403: File fFile = chooser.getSelectedFile();
404: super .createWorkingFile(vfFile, fFile);
405: }
406:
407: return new VFSStatus();
408: }
409:
410: /* (non-Javadoc)
411: * @see com.simulacramedia.contentmanager.editors.Editor#createNew(java.lang.String, byte[], com.simulacramedia.vfs.AbstractVirtualFileSystem)
412: */
413: public PathStatusWrapper createNew(String sPath, byte[] content,
414: AbstractVirtualFileSystem vfs) {
415: ResourceStatusWrapper statusWrapper = new ResourceStatusWrapper(
416: null, new VFSStatus());
417:
418: VirtualFile vfFile = new VirtualFile(sPath);
419:
420: vfs.getVirtualFileSystemView().setContentType(vfFile,
421: "text/xml");
422:
423: vfFile.setContent(content);
424:
425: statusWrapper = vfs.addVirtualFile(sPath, vfFile);
426:
427: vfFile = vfs.getVirtualFile(sPath).getResource();
428: this .m_bResourceCreated = true;
429:
430: if (statusWrapper.getStatus().isOK()) {
431: this .m_bResourceCreated = true;
432: return new PathStatusWrapper(super
433: .createWorkingFile(vfFile), statusWrapper
434: .getStatus());
435: } else {
436: statusWrapper.getStatus().setStatusLevel(
437: StatusData.LEVEL_ERROR);
438: return new PathStatusWrapper(null, statusWrapper
439: .getStatus());
440: }
441: }
442:
443: /* (non-Javadoc)
444: * @see com.simulacramedia.contentmanager.editors.Editor#hasResourceBeenCreated()
445: */
446: public boolean hasResourceBeenCreated() {
447: return this .m_bResourceCreated;
448: }
449:
450: /* (non-Javadoc)
451: * @see com.simulacramedia.contentmanager.editors.Editor#preview(java.lang.String, com.simulacramedia.vfs.AbstractVirtualFileSystem)
452: */
453: public PathStatusWrapper preview(String sPath,
454: AbstractVirtualFileSystem vfs) {
455: VirtualFile vfFile = vfs.getVirtualFile(sPath).getResource();
456: PropertyInstance propInst = vfFile.getProperty(
457: NamespaceType.OHRM.getURI(), "harmonise-id");
458: String sId = getPropValue(propInst);
459:
460: System.out.println("Doc id: " + sId);
461:
462: String pagePath = getPreviewPagePath(vfFile, vfs);
463: if (pagePath != null) {
464: PageDefinitionEditor pde = new PageDefinitionEditor();
465: Document stateDoc = null;
466: try {
467: stateDoc = DocumentBuilderFactory.newInstance()
468: .newDocumentBuilder().newDocument();
469: Element stateRoot = stateDoc.createElement("State");
470: Element docEl = stateDoc.createElement("Document");
471: docEl.setAttribute("id", sId);
472: stateRoot.appendChild(docEl);
473: stateDoc.appendChild(stateRoot);
474: } catch (ParserConfigurationException e) {
475: e.printStackTrace();
476: } catch (FactoryConfigurationError e) {
477: e.printStackTrace();
478: }
479:
480: pde.preview(pagePath, vfs, stateDoc);
481: }
482: return new PathStatusWrapper(null, new VFSStatus());
483: }
484:
485: private String getPreviewPagePath(VirtualFile vfFile,
486: AbstractVirtualFileSystem vfs) {
487: String path = null;
488: PropertyInstance propInst = vfFile.getProperty(
489: NamespaceType.OHRM.getURI(), "PREVIEWPAGE");
490: if (propInst != null) {
491: List values = propInst.getValues();
492: Iterator iter = values.iterator();
493: if (iter.hasNext()) {
494: ResourceValue val = (ResourceValue) iter.next();
495: path = val.getValue();
496: }
497: }
498: if (path == null) {
499: String fullPath = ((VersionedVirtualFile) vfFile)
500: .getLiveVersionPath();
501: if (fullPath == null) {
502: fullPath = vfFile.getFilePath();
503: }
504: ((VersionedVirtualFile) vfFile).getLiveVersionPath();
505: //stop here if down to webdav root
506: System.out.println("Trying " + fullPath);
507: if (fullPath.equalsIgnoreCase("/webdav")) {
508: //try and get the default preview page
509: path = "/webdav/Website/Page Definition/defaultpreview";
510: VirtualFile vFile = vfs.getVirtualFile(path)
511: .getResource();
512: if (vFile == null) {
513: String sMsg = "You have no preview page for this collection and no default preview page set up.";
514: MessageHandler.getInstance().fireMessageEvent(sMsg,
515: MessageHandler.TYPE_ERROR);
516: return null;
517: }
518: } else {
519: path = getPreviewPagePath(vfs.getVirtualFile(fullPath)
520: .getResource(), vfs);
521: }
522: }
523: return path;
524: }
525:
526: private String getPropValue(PropertyInstance propInst) {
527: String sValue = null;
528: if (propInst != null && propInst.getValues().size() > 0) {
529: if (propInst.getValues().get(0) instanceof StringValue) {
530: StringValue value = (StringValue) propInst.getValues()
531: .get(0);
532: if (value.getValue() != null
533: && value.getValue().length() > 0) {
534: sValue = value.getValue();
535: }
536: }
537: }
538: return sValue;
539: }
540: }
|