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.beans.*;
022: import java.io.*;
023: import java.net.MalformedURLException;
024: import java.util.*;
025:
026: import javax.swing.JFileChooser;
027: import javax.xml.parsers.*;
028: import javax.xml.transform.*;
029: import javax.xml.transform.dom.DOMSource;
030: import javax.xml.transform.stream.*;
031:
032: import org.openharmonise.commons.net.*;
033: import org.openharmonise.him.actions.publish.ActionPreview;
034: import org.openharmonise.him.editors.filefilters.*;
035: import org.openharmonise.him.window.*;
036: import org.openharmonise.vfs.*;
037: import org.openharmonise.vfs.status.*;
038: import org.w3c.dom.Document;
039: import org.xml.sax.SAXException;
040:
041: /**
042: * Handles the export of system report outputs.
043: *
044: * @author Michael Bell
045: * @version $Revision: 1.3 $
046: *
047: */
048: public class SystemReportOutputEditor extends GenericEditor implements
049: PropertyChangeListener {
050:
051: /**
052: * File chooser.
053: */
054: private JFileChooser m_chooser = null;
055:
056: /**
057: * Report virtual file.
058: */
059: private VirtualFile m_currVFile = null;
060:
061: /**
062: * File name.
063: */
064: private String m_sCurrentName = null;
065:
066: /**
067: * Map of format identifier to {@link FormatWrapper} objects.
068: */
069: private static Map FORMATS = new Hashtable();
070:
071: static {
072: FormatWrapper wrapper = new FormatWrapper("HTML",
073: MimeTypeMapping.HTML,
074: "/org/openharmonise/him/icons/xsl/queryreport2html.xsl");
075:
076: FORMATS.put(HTMLFilter.DESCRIPTION, wrapper);
077:
078: wrapper = new FormatWrapper("XML", MimeTypeMapping.XML,
079: "/org/openharmonise/him/icons/xsl/queryreport2xml.xsl");
080:
081: FORMATS.put(XMLFilter.DESCRIPTION, wrapper);
082: wrapper = new FormatWrapper("CSV", MimeTypeMapping.CSV,
083: "/org/openharmonise/him/icons/xsl/queryreport2csv.xsl");
084:
085: FORMATS.put(CSVFilter.DESCRIPTION, wrapper);
086: }
087:
088: /**
089: *
090: */
091: public SystemReportOutputEditor() {
092: super ();
093: }
094:
095: /* (non-Javadoc)
096: * @see org.openharmonise.him.editors.Editor#open(java.lang.String, org.openharmonise.vfs.AbstractVirtualFileSystem)
097: */
098: public PathStatusWrapper open(String sPath,
099: AbstractVirtualFileSystem vfs) {
100: return preview(sPath, vfs);
101: }
102:
103: /* (non-Javadoc)
104: * @see org.openharmonise.him.editors.Editor#preview(java.lang.String, org.openharmonise.vfs.AbstractVirtualFileSystem)
105: */
106: public PathStatusWrapper preview(String sPath,
107: AbstractVirtualFileSystem vfs) {
108: VFSStatus vfsStatus = new VFSStatus();
109: String sRealPath = null;
110: try {
111: VirtualFile vfFile = vfs.getVirtualFile(sPath)
112: .getResource();
113:
114: String sFilename = vfFile.getFileName() + ".html";
115:
116: sRealPath = this .m_sPreviewFilePath + sFilename;
117:
118: File realFile = new File(sRealPath);
119:
120: this .exportContent(vfFile, realFile,
121: (FormatWrapper) FORMATS.get("HTML"));
122:
123: try {
124: Process proc5 = Runtime.getRuntime().exec(
125: "rundll32 url.dll,FileProtocolHandler file:/"
126: + realFile.toURI().getPath());
127: } catch (MalformedURLException e1) {
128: e1.printStackTrace();
129: } catch (IOException e1) {
130: e1.printStackTrace();
131: }
132: } catch (Exception e) {
133: e.printStackTrace();
134: vfsStatus.setMethodName(ActionPreview.ACTION_NAME);
135: vfsStatus
136: .setStatusCode(StatusData.STATUS_RESOURCE_NOT_FOUND);
137: vfsStatus.setStatusLevel(StatusData.LEVEL_ERROR);
138: }
139:
140: return new PathStatusWrapper(sRealPath, vfsStatus);
141: }
142:
143: /* (non-Javadoc)
144: * @see com.simulacramedia.contentmanager.editors.Editor#export(java.lang.String, com.simulacramedia.vfs.AbstractVirtualFileSystem)
145: */
146: public StatusData export(String sPath, AbstractVirtualFileSystem vfs) {
147: m_currVFile = vfs.getVirtualFile(sPath).getResource();
148:
149: m_chooser = new JFileChooser();
150: StringBuffer sbuf = new StringBuffer();
151: sbuf.append("C:\\ContentManager\\temp\\");
152:
153: m_sCurrentName = m_currVFile.getFileName();
154: sbuf.append(m_sCurrentName);
155: sbuf.append(".xml");
156: File fTempFile = new File(sbuf.toString());
157: m_chooser.setSelectedFile(fTempFile);
158: m_chooser.addChoosableFileFilter(new HTMLFilter());
159: m_chooser.addChoosableFileFilter(new CSVFilter());
160: m_chooser.setFileFilter(new XMLFilter());
161:
162: m_chooser.setAcceptAllFileFilterUsed(false);
163: m_chooser.addPropertyChangeListener(this );
164:
165: int returnVal = m_chooser.showSaveDialog(DisplayManager
166: .getInstance().getMainWindow());
167:
168: if (returnVal == JFileChooser.APPROVE_OPTION) {
169: try {
170: File fFile = m_chooser.getSelectedFile();
171:
172: if (fFile != null) {
173: exportContent(m_currVFile, fFile);
174: }
175:
176: } catch (Exception e) {
177: e.printStackTrace();
178: }
179: }
180:
181: m_chooser = null;
182: m_sCurrentName = null;
183:
184: return new VFSStatus();
185: }
186:
187: /* (non-Javadoc)
188: * @see com.simulacramedia.contentmanager.editors.Editor#hasResourceBeenCreated()
189: */
190: public boolean hasResourceBeenCreated() {
191: return false;
192: }
193:
194: /**
195: * Translates the content of the virtual file to the required format
196: * in a file located in the given file
197: *
198: * @param vfFile Report output virtual file
199: * @param fFile Local file to export to
200: */
201: private void exportContent(VirtualFile vfFile, File fFile)
202: throws SAXException, IOException,
203: ParserConfigurationException, FactoryConfigurationError,
204: TransformerException {
205: this .exportContent(vfFile, fFile, getCurrentFormat());
206: }
207:
208: private void exportContent(VirtualFile vfFile, File fFile,
209: FormatWrapper format) throws SAXException, IOException,
210: ParserConfigurationException, FactoryConfigurationError,
211: TransformerException {
212: if (fFile != null && vfFile != null) {
213:
214: byte[] bContent = vfFile.getContent();
215:
216: if (bContent != null && bContent.length > 0) {
217: ByteArrayInputStream istream = new ByteArrayInputStream(
218: bContent);
219:
220: Document xmlInput = DocumentBuilderFactory
221: .newInstance().newDocumentBuilder().parse(
222: istream);
223:
224: FileOutputStream ostream = new FileOutputStream(fFile);
225:
226: InputStream is = null;
227:
228: String sXSLLocation = format.getPath();
229:
230: is = SystemReportOutputEditor.class
231: .getResourceAsStream(sXSLLocation);
232:
233: StreamSource ssource = new StreamSource(is);
234:
235: TransformerFactory factory = TransformerFactory
236: .newInstance();
237:
238: Transformer trans = factory.newTransformer(ssource);
239:
240: DOMSource ds = new DOMSource(xmlInput
241: .getDocumentElement());
242:
243: StreamResult res = new StreamResult(ostream);
244:
245: trans.transform(ds, res);
246:
247: ostream.close();
248: }
249: }
250: }
251:
252: /**
253: * Wrapper for information about an output format.
254: *
255: * @author Matthew Large
256: * @version $Revision: 1.3 $
257: *
258: */
259: static protected class FormatWrapper {
260:
261: /**
262: * Mime-type information.
263: */
264: MimeTypeMapping.Mapping m_mapping = null;
265:
266: /**
267: * XSLT resource for export translation.
268: */
269: String m_sXSLT = null;
270:
271: /**
272: * Format label.
273: */
274: String m_sLabel = null;
275:
276: /**
277: * Constructs new format wrapper.
278: *
279: * @param sLabel Format label
280: * @param mapping Mime-type information
281: * @param sPath XSLT resource for export translation
282: */
283: FormatWrapper(String sLabel, MimeTypeMapping.Mapping mapping,
284: String sPath) {
285: m_mapping = mapping;
286: m_sXSLT = sPath;
287: m_sLabel = sLabel;
288: }
289:
290: /**
291: * Returns path to XSLT resource.
292: *
293: * @return Path
294: */
295: String getPath() {
296: return m_sXSLT;
297: }
298:
299: /**
300: * Returns the mime-type information.
301: *
302: * @return Mime-type information
303: */
304: MimeTypeMapping.Mapping getMapping() {
305: return m_mapping;
306: }
307:
308: /* (non-Javadoc)
309: * @see java.lang.Object#toString()
310: */
311: public String toString() {
312: return m_mapping.getDescription();
313: }
314: }
315:
316: /* (non-Javadoc)
317: * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
318: */
319: public void propertyChange(PropertyChangeEvent evt) {
320: if (evt.getPropertyName().equals(
321: JFileChooser.FILE_FILTER_CHANGED_PROPERTY)) {
322: String sDir = m_chooser.getCurrentDirectory()
323: .getAbsolutePath();
324:
325: StringBuffer sbuf = new StringBuffer();
326: sbuf.append(sDir).append(File.separator).append(
327: m_sCurrentName);
328: sbuf.append(".").append(
329: getCurrentFormat().getMapping().getExtension());
330: File newFile = new File(sbuf.toString());
331: m_chooser.setSelectedFile(newFile);
332: m_chooser.updateUI();
333:
334: } else if (evt.getPropertyName().equals(
335: JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)) {
336:
337: }
338: }
339:
340: /**
341: * Returns the current format wrapper.
342: *
343: * @return Format wrapper
344: */
345: private FormatWrapper getCurrentFormat() {
346: String sFilterDesc = m_chooser.getFileFilter().getDescription();
347:
348: return (FormatWrapper) FORMATS.get(sFilterDesc);
349:
350: }
351:
352: }
|