1 19 package org.openharmonise.him.editors; 20 21 import java.beans.*; 22 import java.io.*; 23 import java.net.MalformedURLException ; 24 import java.util.*; 25 26 import javax.swing.JFileChooser ; 27 import javax.xml.parsers.*; 28 import javax.xml.transform.*; 29 import javax.xml.transform.dom.DOMSource ; 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 ; 39 import org.xml.sax.SAXException ; 40 41 42 49 public class SystemReportOutputEditor extends GenericEditor implements PropertyChangeListener { 50 51 54 private JFileChooser m_chooser = null; 55 56 59 private VirtualFile m_currVFile = null; 60 61 64 private String m_sCurrentName = null; 65 66 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 87 public SystemReportOutputEditor() { 88 super(); 89 } 90 91 94 public PathStatusWrapper open(String sPath, AbstractVirtualFileSystem vfs) { 95 return preview(sPath, vfs); 96 } 97 100 public PathStatusWrapper preview(String sPath, AbstractVirtualFileSystem vfs) { 101 VFSStatus vfsStatus = new VFSStatus(); 102 String sRealPath = null; 103 try { 104 VirtualFile vfFile = vfs.getVirtualFile(sPath).getResource(); 105 106 String 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 proc5 = Runtime.getRuntime().exec( "rundll32 url.dll,FileProtocolHandler file:/" + realFile.toURI().getPath() ); 116 } catch (MalformedURLException e1) { 117 e1.printStackTrace(); 118 } catch (IOException e1) { 119 e1.printStackTrace(); 120 } 121 } catch (Exception 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 133 public StatusData export(String sPath, AbstractVirtualFileSystem vfs) { 134 m_currVFile = vfs.getVirtualFile(sPath).getResource(); 135 136 m_chooser = new JFileChooser (); 137 StringBuffer sbuf = new StringBuffer (); 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 e) { 163 e.printStackTrace(); 164 } 165 } 166 167 m_chooser = null; 168 m_sCurrentName = null; 169 170 return new VFSStatus(); 171 } 172 173 176 public boolean hasResourceBeenCreated() { 177 return false; 178 } 179 180 187 private void exportContent(VirtualFile vfFile, File fFile) throws SAXException , IOException, ParserConfigurationException, FactoryConfigurationError, TransformerException { 188 this.exportContent(vfFile, fFile, getCurrentFormat()); 189 } 190 191 private void exportContent(VirtualFile vfFile, File fFile, FormatWrapper format) throws SAXException , 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 xmlInput = 200 DocumentBuilderFactory.newInstance().newDocumentBuilder().parse( 201 istream); 202 203 FileOutputStream ostream = new FileOutputStream(fFile); 204 205 InputStream is = null; 206 207 String 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 ds = new DOMSource (xmlInput.getDocumentElement()); 218 219 StreamResult res = new StreamResult(ostream); 220 221 trans.transform(ds, res); 222 223 ostream.close(); 224 } 225 } 226 } 227 228 235 static protected class FormatWrapper { 236 237 240 MimeTypeMapping.Mapping m_mapping = null; 241 242 245 String m_sXSLT = null; 246 247 250 String m_sLabel = null; 251 252 259 FormatWrapper(String sLabel,MimeTypeMapping.Mapping mapping, String sPath) { 260 m_mapping = mapping; 261 m_sXSLT = sPath; 262 m_sLabel = sLabel; 263 } 264 265 270 String getPath() { 271 return m_sXSLT; 272 } 273 274 279 MimeTypeMapping.Mapping getMapping() { 280 return m_mapping; 281 } 282 283 286 public String toString() { 287 return m_mapping.getDescription(); 288 } 289 } 290 291 292 295 public void propertyChange(PropertyChangeEvent evt) { 296 if(evt.getPropertyName().equals(JFileChooser.FILE_FILTER_CHANGED_PROPERTY)) { 297 String sDir = m_chooser.getCurrentDirectory().getAbsolutePath(); 298 299 StringBuffer sbuf = new StringBuffer (); 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 316 private FormatWrapper getCurrentFormat() { 317 String sFilterDesc = m_chooser.getFileFilter().getDescription(); 318 319 return (FormatWrapper)FORMATS.get(sFilterDesc); 320 321 } 322 323 324 } 325 | Popular Tags |