KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > editors > SystemReportOutputEditor


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.him.editors;
20
21 import java.beans.*;
22 import java.io.*;
23 import java.net.MalformedURLException JavaDoc;
24 import java.util.*;
25
26 import javax.swing.JFileChooser JavaDoc;
27 import javax.xml.parsers.*;
28 import javax.xml.transform.*;
29 import javax.xml.transform.dom.DOMSource JavaDoc;
30 import javax.xml.transform.stream.*;
31
32 import org.openharmonise.commons.net.*;
33 import org.openharmonise.him.actions.publish.ActionPreview;
34 import org.openharmonise.him.editors.filefilters.*;
35 import org.openharmonise.him.window.*;
36 import org.openharmonise.vfs.*;
37 import org.openharmonise.vfs.status.*;
38 import org.w3c.dom.Document JavaDoc;
39 import org.xml.sax.SAXException JavaDoc;
40
41
42 /**
43  * Handles the export of system report outputs.
44  *
45  * @author Michael Bell
46  * @version $Revision: 1.3 $
47  *
48  */

49 public class SystemReportOutputEditor extends GenericEditor implements PropertyChangeListener {
50
51     /**
52      * File chooser.
53      */

54     private JFileChooser JavaDoc m_chooser = null;
55     
56     /**
57      * Report virtual file.
58      */

59     private VirtualFile m_currVFile = null;
60     
61     /**
62      * File name.
63      */

64     private String JavaDoc m_sCurrentName = null;
65     
66     /**
67      * Map of format identifier to {@link FormatWrapper} objects.
68      */

69     private static Map FORMATS = new Hashtable();
70
71         static {
72             FormatWrapper wrapper = new FormatWrapper("HTML",MimeTypeMapping.HTML,"/org/openharmonise/him/icons/xsl/queryreport2html.xsl");
73         
74             FORMATS.put(HTMLFilter.DESCRIPTION,wrapper);
75             
76             wrapper = new FormatWrapper("XML",MimeTypeMapping.XML,"/org/openharmonise/him/icons/xsl/queryreport2xml.xsl");
77             
78             FORMATS.put(XMLFilter.DESCRIPTION,wrapper);
79             wrapper = new FormatWrapper("CSV",MimeTypeMapping.CSV,"/org/openharmonise/him/icons/xsl/queryreport2csv.xsl");
80         
81             FORMATS.put(CSVFilter.DESCRIPTION,wrapper);
82         }
83
84     /**
85      *
86      */

87     public SystemReportOutputEditor() {
88         super();
89     }
90
91     /* (non-Javadoc)
92      * @see org.openharmonise.him.editors.Editor#open(java.lang.String, org.openharmonise.vfs.AbstractVirtualFileSystem)
93      */

94     public PathStatusWrapper open(String JavaDoc sPath, AbstractVirtualFileSystem vfs) {
95         return preview(sPath, vfs);
96     }
97     /* (non-Javadoc)
98      * @see org.openharmonise.him.editors.Editor#preview(java.lang.String, org.openharmonise.vfs.AbstractVirtualFileSystem)
99      */

100     public PathStatusWrapper preview(String JavaDoc sPath, AbstractVirtualFileSystem vfs) {
101         VFSStatus vfsStatus = new VFSStatus();
102         String JavaDoc sRealPath = null;
103         try {
104             VirtualFile vfFile = vfs.getVirtualFile(sPath).getResource();
105             
106             String JavaDoc sFilename = vfFile.getFileName() + ".html";
107             
108             sRealPath = this.m_sPreviewFilePath + sFilename;
109             
110             File realFile = new File(sRealPath);
111             
112             this.exportContent(vfFile, realFile, (FormatWrapper) FORMATS.get("HTML"));
113             
114             try {
115                 Process JavaDoc proc5 = Runtime.getRuntime().exec( "rundll32 url.dll,FileProtocolHandler file:/" + realFile.toURI().getPath() );
116             } catch (MalformedURLException JavaDoc e1) {
117                 e1.printStackTrace();
118             } catch (IOException e1) {
119                 e1.printStackTrace();
120             }
121         } catch (Exception JavaDoc e) {
122             e.printStackTrace();
123             vfsStatus.setMethodName(ActionPreview.ACTION_NAME);
124             vfsStatus.setStatusCode(StatusData.STATUS_RESOURCE_NOT_FOUND);
125             vfsStatus.setStatusLevel(StatusData.LEVEL_ERROR);
126         }
127         
128         return new PathStatusWrapper(sRealPath, vfsStatus);
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 JavaDoc sPath, AbstractVirtualFileSystem vfs) {
134         m_currVFile = vfs.getVirtualFile(sPath).getResource();
135         
136         m_chooser = new JFileChooser JavaDoc();
137         StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
138         sbuf.append("C:\\ContentManager\\temp\\");
139         
140         m_sCurrentName = m_currVFile.getFileName();
141         sbuf.append(m_sCurrentName);
142         sbuf.append(".xml");
143         File fTempFile = new File(sbuf.toString());
144         m_chooser.setSelectedFile(fTempFile);
145         m_chooser.addChoosableFileFilter(new HTMLFilter());
146                 m_chooser.addChoosableFileFilter(new CSVFilter());
147         m_chooser.setFileFilter(new XMLFilter());
148         
149         m_chooser.setAcceptAllFileFilterUsed(false);
150         m_chooser.addPropertyChangeListener(this);
151         
152         int returnVal = m_chooser.showSaveDialog(DisplayManager.getInstance().getMainWindow());
153
154         if (returnVal == JFileChooser.APPROVE_OPTION) {
155             try {
156                 File fFile = m_chooser.getSelectedFile();
157                 
158                 if(fFile != null) {
159                     exportContent(m_currVFile,fFile);
160                 }
161                 
162             } catch (Exception JavaDoc e) {
163                 e.printStackTrace();
164             }
165         }
166         
167         m_chooser = null;
168         m_sCurrentName = null;
169         
170         return new VFSStatus();
171     }
172
173     /* (non-Javadoc)
174      * @see com.simulacramedia.contentmanager.editors.Editor#hasResourceBeenCreated()
175      */

176     public boolean hasResourceBeenCreated() {
177         return false;
178     }
179     
180     /**
181      * Translates the content of the virtual file to the required format
182      * in a file located in the given file
183      *
184      * @param vfFile Report output virtual file
185      * @param fFile Local file to export to
186      */

187     private void exportContent(VirtualFile vfFile, File fFile) throws SAXException JavaDoc, IOException, ParserConfigurationException, FactoryConfigurationError, TransformerException {
188         this.exportContent(vfFile, fFile, getCurrentFormat());
189     }
190     
191     private void exportContent(VirtualFile vfFile, File fFile, FormatWrapper format) throws SAXException JavaDoc, IOException, ParserConfigurationException, FactoryConfigurationError, TransformerException {
192         if(fFile != null && vfFile != null) {
193             
194                 byte[] bContent = vfFile.getContent();
195         
196                 if(bContent != null && bContent.length > 0) {
197                     ByteArrayInputStream istream = new ByteArrayInputStream(bContent);
198             
199                     Document JavaDoc xmlInput =
200                         DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(
201                             istream);
202             
203                     FileOutputStream ostream = new FileOutputStream(fFile);
204             
205                     InputStream is = null;
206                     
207                     String JavaDoc sXSLLocation = format.getPath();
208             
209                     is = SystemReportOutputEditor.class.getResourceAsStream(sXSLLocation);
210             
211                     StreamSource ssource = new StreamSource(is);
212             
213                     TransformerFactory factory = TransformerFactory.newInstance();
214             
215                     Transformer trans = factory.newTransformer(ssource);
216             
217                     DOMSource JavaDoc ds = new DOMSource JavaDoc(xmlInput.getDocumentElement());
218             
219                     StreamResult res = new StreamResult(ostream);
220             
221                     trans.transform(ds, res);
222             
223                     ostream.close();
224                 }
225             }
226     }
227     
228     /**
229      * Wrapper for information about an output format.
230      *
231      * @author Matthew Large
232      * @version $Revision: 1.3 $
233      *
234      */

235     static protected class FormatWrapper {
236         
237         /**
238          * Mime-type information.
239          */

240         MimeTypeMapping.Mapping m_mapping = null;
241         
242         /**
243          * XSLT resource for export translation.
244          */

245         String JavaDoc m_sXSLT = null;
246         
247         /**
248          * Format label.
249          */

250         String JavaDoc m_sLabel = null;
251     
252         /**
253          * Constructs new format wrapper.
254          *
255          * @param sLabel Format label
256          * @param mapping Mime-type information
257          * @param sPath XSLT resource for export translation
258          */

259         FormatWrapper(String JavaDoc sLabel,MimeTypeMapping.Mapping mapping, String JavaDoc sPath) {
260             m_mapping = mapping;
261             m_sXSLT = sPath;
262             m_sLabel = sLabel;
263         }
264     
265         /**
266          * Returns path to XSLT resource.
267          *
268          * @return Path
269          */

270         String JavaDoc getPath() {
271             return m_sXSLT;
272         }
273     
274         /**
275          * Returns the mime-type information.
276          *
277          * @return Mime-type information
278          */

279         MimeTypeMapping.Mapping getMapping() {
280             return m_mapping;
281         }
282     
283         /* (non-Javadoc)
284          * @see java.lang.Object#toString()
285          */

286         public String JavaDoc toString() {
287             return m_mapping.getDescription();
288         }
289     }
290
291
292     /* (non-Javadoc)
293      * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
294      */

295     public void propertyChange(PropertyChangeEvent evt) {
296         if(evt.getPropertyName().equals(JFileChooser.FILE_FILTER_CHANGED_PROPERTY)) {
297             String JavaDoc sDir = m_chooser.getCurrentDirectory().getAbsolutePath();
298             
299             StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
300             sbuf.append(sDir).append(File.separator).append(m_sCurrentName);
301             sbuf.append(".").append(getCurrentFormat().getMapping().getExtension());
302             File newFile = new File(sbuf.toString() );
303             m_chooser.setSelectedFile(newFile);
304             m_chooser.updateUI();
305         
306         } else if(evt.getPropertyName().equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)) {
307             
308         }
309     }
310     
311     /**
312      * Returns the current format wrapper.
313      *
314      * @return Format wrapper
315      */

316     private FormatWrapper getCurrentFormat() {
317         String JavaDoc sFilterDesc = m_chooser.getFileFilter().getDescription();
318         
319         return (FormatWrapper)FORMATS.get(sFilterDesc);
320         
321     }
322
323
324 }
325
Popular Tags