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: import java.rmi.*;
025:
026: import javax.swing.*;
027: import javax.xml.parsers.*;
028: import javax.xml.rpc.*;
029:
030: import org.openharmonise.commons.xml.*;
031: import org.openharmonise.commons.xml.namespace.*;
032: import org.openharmonise.him.configuration.*;
033: import org.openharmonise.him.editors.preview.*;
034: import org.openharmonise.him.harmonise.*;
035: import org.openharmonise.him.publish.*;
036: import org.openharmonise.him.window.*;
037: import org.openharmonise.vfs.*;
038: import org.openharmonise.vfs.gui.*;
039: import org.openharmonise.vfs.status.*;
040: import org.w3c.dom.*;
041: import org.xml.sax.*;
042:
043: /**
044: * Handles editing of Page and Object template XML files.
045: *
046: * @author Matthew Large
047: * @version $Revision: 1.2 $
048: *
049: */
050: public class CompositionEditor extends GenericEditor implements Editor {
051:
052: private boolean m_bResourceCreated = false;
053:
054: private static final String TAG_HARMONISE_PUBLISH_SERVICE = "HarmonisePublishService";
055: protected String m_sBrowserPath = null;
056:
057: /**
058: *
059: */
060: public CompositionEditor() {
061: super ();
062: setup();
063: }
064:
065: /**
066: *
067: */
068: private void setup() {
069: String sValue = ConfigStore.getInstance().getPropertyValue(
070: "BROWSER_PATH");
071: if (sValue == null || sValue.length() < 1) {
072: // TODO remove windows hard coded paths
073: File fPath = new File(
074: "C:/Program Files/Internet Explorer/IEXPLORE.EXE");
075: if (fPath.exists()) {
076: this .m_sBrowserPath = fPath.getAbsolutePath();
077: ConfigStore.getInstance().setProperty("BROWSER_PATH",
078: this .m_sBrowserPath);
079: } else {
080: JFileChooser chooser = new JFileChooser();
081: chooser.setDialogTitle("Select Browser path");
082: int returnVal = chooser.showOpenDialog(DisplayManager
083: .getInstance().getMainWindow());
084:
085: this .m_sBrowserPath = chooser.getSelectedFile()
086: .getAbsolutePath();
087: ConfigStore.getInstance().setProperty("BROWSER_PATH",
088: this .m_sBrowserPath);
089:
090: }
091: } else {
092: this .m_sBrowserPath = sValue;
093: }
094:
095: }
096:
097: /* (non-Javadoc)
098: * @see com.simulacramedia.contentmanager.editors.Editor#createNew(java.lang.String, com.simulacramedia.vfs.AbstractVirtualFileSystem)
099: */
100: public PathStatusWrapper createNew(String sPath,
101: AbstractVirtualFileSystem vfs) {
102: ResourceStatusWrapper statusWrapper = new ResourceStatusWrapper(
103: null, new VFSStatus());
104:
105: VirtualFile vfFile = new VirtualFile(sPath);
106:
107: vfs.getVirtualFileSystemView().setContentType(vfFile,
108: "text/xml");
109:
110: StringBuffer sBuff = new StringBuffer();
111: InputStream is = null;
112:
113: if (sPath.startsWith(HarmonisePaths.PATH_OBJECT_TEMPLATES)) {
114: is = XMetaLEditor.class
115: .getResourceAsStream("/org/openharmonise/him/icons/xml/newObjectTemplate.xml");
116: } else {
117: is = XMetaLEditor.class
118: .getResourceAsStream("/org/openharmonise/him/icons/xml/newPageTemplate.xml");
119: }
120:
121: BufferedReader buff = new BufferedReader(new InputStreamReader(
122: is));
123:
124: String sLine = null;
125: try {
126: while ((sLine = buff.readLine()) != null) {
127: sBuff.append(sLine);
128: }
129: Document xml = null;
130: try {
131: xml = DocumentBuilderFactory.newInstance()
132: .newDocumentBuilder().parse(
133: new org.xml.sax.InputSource(
134: new StringReader(sBuff
135: .toString())));
136: } catch (SAXException e1) {
137: e1.printStackTrace();
138: } catch (ParserConfigurationException e1) {
139: e1.printStackTrace();
140: } catch (FactoryConfigurationError e1) {
141: e1.printStackTrace();
142: }
143:
144: if (xml != null) {
145: Element rootEl = (Element) xml.getDocumentElement();
146:
147: XMLPrettyPrint printer = new XMLPrettyPrint();
148:
149: vfFile.setContent(printer.printNode(rootEl).getBytes());
150: }
151:
152: statusWrapper = vfs.addVirtualFile(sPath, vfFile);
153:
154: vfFile = vfs.getVirtualFile(sPath).getResource();
155:
156: this .m_bResourceCreated = true;
157:
158: PathStatusWrapper pathStatus = new PathStatusWrapper(super
159: .createWorkingFile(vfFile), statusWrapper
160: .getStatus());
161:
162: return pathStatus;
163: } catch (IOException e) {
164: e.printStackTrace();
165: } catch (NamespaceClashException e) {
166: e.printStackTrace();
167: }
168: statusWrapper.getStatus()
169: .setStatusLevel(StatusData.LEVEL_ERROR);
170: return new PathStatusWrapper(null, statusWrapper.getStatus());
171: }
172:
173: /* (non-Javadoc)
174: * @see com.simulacramedia.contentmanager.editors.Editor#hasResourceBeenCreated()
175: */
176: public boolean hasResourceBeenCreated() {
177: return this .m_bResourceCreated;
178: }
179:
180: /* (non-Javadoc)
181: * @see com.simulacramedia.contentmanager.editors.Editor#preview(java.lang.String, com.simulacramedia.vfs.AbstractVirtualFileSystem)
182: */
183: public PathStatusWrapper preview(String sPath,
184: AbstractVirtualFileSystem vfs) {
185: VirtualFile vfFile = vfs.getVirtualFile(sPath).getResource();
186: String sFilename = null;
187: String sMessage = null;
188: try {
189: String sOut = invokePublishService(vfFile, vfs, null);
190: if (sOut != null) {
191: sFilename = this .getFileName(vfFile);
192: File fFile = super .createPreviewFile(sFilename, sOut
193: .getBytes());
194:
195: Runtime.getRuntime().exec(
196: "\"" + this .m_sBrowserPath + "\" " + "file:/"
197: + fFile.toURI().getPath());
198: }
199: } catch (SAXException e) {
200: e.getLocalizedMessage();
201: e.printStackTrace(System.err);
202: } catch (Exception e) {
203: e.printStackTrace(System.err);
204: }
205:
206: return new PathStatusWrapper(null, new VFSStatus());
207: }
208:
209: public static String invokePublishService(VirtualFile vfFile,
210: AbstractVirtualFileSystem vfs, Document state)
211: throws SAXException, IOException,
212: ParserConfigurationException, RemoteException,
213: ServiceException {
214: String sOutXml = null;
215: DocumentBuilderFactory factory = DocumentBuilderFactory
216: .newInstance();
217: factory.setNamespaceAware(true);
218: Document pageXml = factory.newDocumentBuilder().parse(
219: new org.xml.sax.InputSource(new StringReader(
220: new String(vfFile.getContent()))));
221: Document previewXml = DocumentBuilderFactory.newInstance()
222: .newDocumentBuilder().newDocument();
223: Element rootEl = previewXml
224: .createElement(TAG_HARMONISE_PUBLISH_SERVICE);
225: previewXml.appendChild(rootEl);
226: Document stateDoc = null;
227: if (state == null) {
228: stateDoc = getState(vfFile);
229: } else {
230: stateDoc = state;
231: }
232: Node node = null;
233: if (stateDoc != null) {
234: node = XMLUtils.copyNode(stateDoc.getDocumentElement(),
235: previewXml);
236: rootEl.appendChild(node);
237: node = XMLUtils.copyNode(pageXml.getDocumentElement(),
238: previewXml);
239: rootEl.appendChild(node);
240:
241: String sInXml = previewXml.getDocumentElement().toString();
242: //send the xml to the publish service
243: URI uri = vfs.getURI();
244: String sURL = uri.getScheme() + "://" + uri.getHost() + ":"
245: + uri.getPort() + "/webdav/services/PublishService";
246: URL endPointURL = new URL(sURL);
247: sOutXml = PublishServiceClient.getPublishXML(endPointURL,
248: sInXml);
249: }
250: return sOutXml;
251: }
252:
253: private static Document getState(VirtualFile vfFile) {
254: Frame frame = new Frame();
255: frame.setIconImage(((ImageIcon) IconManager.getInstance()
256: .getIcon("16-page-definition.gif")).getImage());
257: String sVFile = null;
258: if (vfFile.isVersionable()
259: && ((VersionedVirtualFile) vfFile).getState().equals(
260: VirtualFile.STATE_PENDING)
261: && ((VersionedVirtualFile) vfFile).getLiveVersionPath() != null) {
262: sVFile = ((VersionedVirtualFile) vfFile)
263: .getLiveVersionPath();
264: } else {
265: sVFile = vfFile.getFullPath();
266: }
267: StateDialog sd = new StateDialog(frame, "state selector",
268: sVFile);
269: Document doc = sd.getState();
270: if (doc == null) {
271: sd.show();
272: doc = sd.getState();
273: }
274: return doc;
275: }
276: }
|