1 19 package org.netbeans.modules.xsl.scenario; 20 21 import java.beans.PropertyChangeEvent ; 22 import java.beans.PropertyChangeListener ; 23 import java.beans.PropertyChangeSupport ; 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.io.OutputStream ; 27 import java.io.Serializable ; 28 import java.net.MalformedURLException ; 29 import java.net.URL ; 30 import java.util.Iterator ; 31 import java.util.StringTokenizer ; 32 import javax.xml.transform.Result ; 33 import javax.xml.transform.Source ; 34 import javax.xml.transform.TransformerException ; 35 import javax.xml.transform.stream.StreamResult ; 36 import org.netbeans.modules.xml.core.actions.InputOutputReporter; 37 import org.netbeans.modules.xml.core.lib.FileUtilities; 38 import org.netbeans.modules.xml.core.lib.GuiUtil; 39 import org.netbeans.modules.xsl.settings.TransformHistory; 40 import org.netbeans.modules.xsl.transform.TransformServlet; 41 import org.netbeans.modules.xsl.ui.TransformPanel; 42 import org.netbeans.modules.xsl.utils.TransformUtil; 43 import org.openide.ErrorManager; 44 import org.openide.awt.HtmlBrowser; 45 import org.openide.cookies.SaveCookie; 46 import org.openide.filesystems.FileAlreadyLockedException; 47 import org.openide.filesystems.FileLock; 48 import org.openide.filesystems.FileObject; 49 import org.openide.filesystems.FileStateInvalidException; 50 import org.openide.filesystems.FileUtil; 51 import org.openide.loaders.DataObject; 52 import org.openide.nodes.Node; 53 import org.openide.xml.XMLUtil; 54 import org.w3c.dom.Document ; 55 import org.xml.sax.InputSource ; 56 import org.xml.sax.SAXException ; 57 import org.netbeans.modules.xml.api.scenario.Scenario; 58 import org.netbeans.modules.xsl.api.XSLScenario; 59 60 70 public class FileXSLScenario implements Serializable , XSLScenario, PropertyChangeListener { 71 72 73 protected Data data; 74 75 76 protected String name = ""; 77 78 79 private transient Document sourceDoc; 80 81 82 private transient TransformPanel transPanel; 83 84 85 private transient DataObject lastDataObject; 86 87 88 private transient PropertyChangeSupport support; 89 90 91 public FileXSLScenario() { 92 data = new Data(); 93 } 94 95 99 public void execute(DataObject dataObject) { 100 InputOutputReporter cookieObserver = new InputOutputReporter(Util.THIS.getString("PROP_transformation_io_name")); 101 try { 102 SaveCookie saveCookie = (SaveCookie)dataObject.getCookie(SaveCookie.class); 104 if (saveCookie != null) { 105 try { 106 saveCookie.save(); 107 } catch(IOException e) { 108 cookieObserver.message("Could not save file: " + e.getMessage()); 109 } 110 } 111 112 113 FileObject baseFO = dataObject.getPrimaryFile(); 114 URL baseURL = preferFileURL(baseFO); 115 116 Source xmlSource = TransformUtil.createSource(baseURL, data.getSourceXML()); if ( Util.THIS.isLoggable() ) Util.THIS.debug(" xmlSource = " + xmlSource.getSystemId()); 118 119 String xslName = TransformUtil.getURLName(baseFO); 120 Source xslSource = TransformUtil.createSource(baseURL, xslName); if ( Util.THIS.isLoggable() ) Util.THIS.debug(" xslSource = " + xslSource.getSystemId()); 122 123 if ( data.getOutput() == null ) { TransformServlet.prepare(null, xmlSource, xslSource); 125 showURL(TransformServlet.getServletURL()); 126 } else { 127 String fileName = data.getOutput().replace('\\', '/'); 128 FileObject resultFO = FileUtilities.createFileObject(baseFO.getParent(), fileName, data.isOverwrite()); 130 if ( Util.THIS.isLoggable() ) Util.THIS.debug(" resultFO = " + resultFO); 131 132 OutputStream outputStream = null; 133 FileLock fileLock = null; 134 try { 135 fileLock = resultFO.lock(); 136 outputStream = resultFO.getOutputStream(fileLock); 137 138 Result outputResult = new StreamResult (outputStream); 140 if ( Util.THIS.isLoggable() ) { 141 Util.THIS.debug(" resultFO = " + resultFO); 142 Util.THIS.debug(" outputResult = " + outputResult); 143 } 144 145 String xmlName = data.getSourceXML(); 146 cookieObserver.message(Util.THIS.getString("MSG_transformation_1", xmlName, xslName)); 147 TransformUtil.transform(xmlSource, null, xslSource, outputResult, null); 149 if ( data.getProcess() == TransformHistory.APPLY_DEFAULT_ACTION ) { 150 GuiUtil.performDefaultAction(resultFO); 151 GuiUtil.performDefaultAction(resultFO); 152 } else if ( data.getProcess() == TransformHistory.OPEN_IN_BROWSER ) { 153 showURL(resultFO.getURL()); 154 } 155 156 GuiUtil.setStatusText(Util.THIS.getString("MSG_opening_browser")); 157 158 } catch (FileAlreadyLockedException exc) { 159 throw (FileAlreadyLockedException) ErrorManager.getDefault().annotate(exc, Util.THIS.getString("ERR_FileAlreadyLockedException_output")); 160 } finally { 161 if ( outputStream != null ) { 162 outputStream.close(); 163 } 164 if ( fileLock != null ) { 165 fileLock.releaseLock(); 166 } 167 } 168 } 169 } catch (TransformerException exc) { } catch(Exception e) { 172 ErrorManager.getDefault().notify(ErrorManager.WARNING, e); 173 } 174 175 cookieObserver.message(Util.THIS.getString("MSG_transformation_2")); 176 cookieObserver.moveToFront(true); 177 178 sourceDoc = null; 179 } 180 181 191 public java.awt.Component getCustomizer(DataObject dataObject, boolean activatePropertyChange) { 192 try { 193 transPanel = new TransformPanel(null, null, dataObject); 194 lastDataObject = dataObject; 195 196 String sourceXML = data.getSourceXML(); 197 if (sourceXML.length() > 0) { 198 FileObject baseFO = dataObject.getPrimaryFile(); 199 URL baseURL = preferFileURL(baseFO.getParent()); 200 URL sourceURL = new URL (baseURL, data.getSourceXML()); 201 sourceXML = sourceURL.toExternalForm(); 202 } 203 204 TransformPanel.Data transData = new TransformPanel.Data(sourceXML, 205 transPanel.getData().getXSL(), data.getOutput(), data.isOverwrite(), data.getProcess()); 206 transPanel.setData(transData); 207 if(activatePropertyChange) { 208 transPanel.addPropertyChangeListener(this); 209 } 210 } catch (Exception e) { 211 e.printStackTrace(); 213 transPanel = null; 214 } 215 216 return transPanel; 217 } 218 219 223 public String getName() { 224 return name; 225 } 226 227 231 public void setName(String name) { 232 String oldName = this.name; 233 this.name = name; 234 } 235 236 240 public void saveChanges() { 241 if (transPanel != null) { 242 TransformPanel.Data transData = transPanel.getData(); 243 244 String sourceXML = transData.getInput(); 245 FileObject baseFO = lastDataObject.getPrimaryFile(); 246 try { 247 String baseURL = preferFileURL(baseFO.getParent()).toExternalForm(); 248 sourceXML = relativize(baseURL, sourceXML); 249 } catch(Exception e) {} 250 251 data.setSourceXML(sourceXML); 252 String outputStr=null; 253 if(transData.getOutput()!=null) { 254 outputStr=transData.getOutput().toString(); 255 } 256 data.setOutput(outputStr); 257 data.setOverwrite(transData.isOverwriteOutput()); 258 data.setProcess(transData.getProcessOutput()); 259 sourceDoc = null; 260 } 261 } 262 263 public Document getSourceDocument(DataObject dataObject, boolean reload) throws SAXException , IOException { 264 if (sourceDoc == null || reload) { 265 String sourceXML = data.getSourceXML(); 266 if (sourceXML == null || sourceXML.length() == 0) { 267 return null; 268 } 269 270 FileObject baseFO = dataObject.getPrimaryFile(); 271 URL baseURL = preferFileURL(baseFO); 272 URL url = new URL (baseURL, sourceXML); 273 sourceDoc = XMLUtil.parse(new InputSource (url.toExternalForm()), false, true, 274 null, TransformUtil.getEntityResolver()); 275 } 276 277 return sourceDoc; 278 } 279 280 283 public String toString() { 284 return getName(); 285 } 286 287 288 292 protected URL preferFileURL(FileObject fileObject) throws MalformedURLException , FileStateInvalidException { 293 URL fileURL = null; 294 File file = FileUtil.toFile(fileObject); 295 296 if ( file != null ) { 297 fileURL = file.toURL(); 298 } else { 299 fileURL = fileObject.getURL(); 300 } 301 return fileURL; 302 } 303 304 private void showURL(URL url) { 305 HtmlBrowser.URLDisplayer.getDefault().showURL(url); 306 GuiUtil.setStatusText(Util.THIS.getString("MSG_opening_browser")); 307 } 308 309 313 public void addPropertyChangeListener(PropertyChangeListener listener) { 314 if(support==null) { 315 support = new PropertyChangeSupport (this); 316 } 317 support.addPropertyChangeListener(listener); 318 } 319 320 324 public void removePropertyChangeListener(PropertyChangeListener listener) { 325 if(support==null) { 326 support = new PropertyChangeSupport (this); 327 } 328 support.removePropertyChangeListener(listener); 329 } 330 331 336 public void propertyChange(PropertyChangeEvent evt) { 337 if(support==null) { 338 support = new PropertyChangeSupport (this); 339 } 340 if(evt.getPropertyName().equals(TransformPanel.DATA_XML_MODIFIED) || 341 evt.getPropertyName().equals(TransformPanel.DATA_XSL_MODIFIED) || 342 evt.getPropertyName().equals(TransformPanel.DATA_OUTPUT_MODIFIED) || 343 evt.getPropertyName().equals(TransformPanel.DATA_OVERWRITE_MODIFIED) || 344 evt.getPropertyName().equals(TransformPanel.DATA_PROCESS_MODIFIED)) { 345 support.firePropertyChange(Scenario.PROP_SCENARIO_MODIFIED,evt.getOldValue(), evt.getNewValue()); 346 } 347 } 348 349 private String relativize(String baseURI, String url) { 350 StringTokenizer baseTok = new StringTokenizer (baseURI, "/"); 351 StringTokenizer urlTok = new StringTokenizer (url, "/"); 352 353 String curUrlTok = null; 354 int parentTokens = baseTok.countTokens(); 355 int origParentTokens = parentTokens; 356 while (baseTok.hasMoreTokens()) { 357 if (!urlTok.hasMoreTokens()) { 358 curUrlTok = null; 359 break; 360 } 361 curUrlTok = urlTok.nextToken(); 362 if (!curUrlTok.equals(baseTok.nextToken())) { 363 break; 364 } 365 parentTokens--; 366 } 367 368 if (parentTokens == origParentTokens) { 369 return url; 371 } 372 373 String resUrl = ""; 374 for (int ind = 0; ind < parentTokens; ind++) { 375 resUrl += "../"; 376 } 377 378 if (curUrlTok != null) { 379 if (parentTokens != 0) { 380 resUrl += curUrlTok; 381 if (urlTok.hasMoreTokens()) { 382 resUrl += "/"; 383 } 384 } 385 386 while (urlTok.hasMoreTokens()) { 387 resUrl += urlTok.nextToken(); 388 if (urlTok.hasMoreTokens()) { 389 resUrl += "/"; 390 } 391 } 392 } 393 394 return resUrl; 395 } 396 397 public class Data implements Serializable { 398 399 400 private String sourceXML = ""; 401 402 403 private String output = null; 404 405 406 private boolean overwrite = true; 407 408 409 private int process = -1; 410 411 415 public String getSourceXML() { 416 return this.sourceXML; 417 } 418 419 423 public void setSourceXML(String sourceXML) { 424 this.sourceXML = sourceXML; 425 } 426 427 431 public String getOutput() { 432 return this.output; 433 } 434 435 439 public void setOutput(String output) { 440 this.output = output; 441 } 442 443 447 public boolean isOverwrite() { 448 return this.overwrite; 449 } 450 451 455 public void setOverwrite(boolean overwrite) { 456 this.overwrite = overwrite; 457 } 458 459 463 public int getProcess() { 464 return this.process; 465 } 466 467 471 public void setProcess(int process) { 472 this.process = process; 473 } 474 475 } 476 477 } 478 | Popular Tags |