1 19 20 package org.netbeans.modules.project.ant; 21 22 import java.awt.Frame ; 23 import java.awt.event.WindowAdapter ; 24 import java.awt.event.WindowEvent ; 25 import java.io.CharArrayWriter ; 26 import java.io.StringReader ; 27 import java.util.logging.Level ; 28 import java.util.logging.Logger ; 29 import javax.swing.SwingUtilities ; 30 import javax.xml.parsers.DocumentBuilderFactory ; 31 import javax.xml.transform.Result ; 32 import javax.xml.transform.Source ; 33 import javax.xml.transform.Transformer ; 34 import javax.xml.transform.TransformerFactory ; 35 import javax.xml.transform.dom.DOMSource ; 36 import javax.xml.transform.stream.StreamResult ; 37 import javax.xml.transform.stream.StreamSource ; 38 import org.openide.DialogDisplayer; 39 import org.openide.LifecycleManager; 40 import org.openide.NotifyDescriptor; 41 import org.openide.modules.ModuleInstall; 42 import org.openide.util.NbBundle; 43 import org.openide.windows.WindowManager; 44 import org.openide.xml.XMLUtil; 45 import org.w3c.dom.Document ; 46 import org.xml.sax.InputSource ; 47 48 53 public class AntProjectModule extends ModuleInstall { 54 55 public void restored() { 56 super.restored(); 57 58 if (Boolean.getBoolean("netbeans.do.not.check.xalan")) return ; 60 61 long start = System.currentTimeMillis(); 62 boolean isBuggyXalan = checkForXalan(); 63 long end = System.currentTimeMillis(); 64 Logger.getLogger(AntProjectModule.class.getName()).log(Level.FINE, "check for buggy xalan took %d", new Long (end - start)); 66 if (isBuggyXalan) { 67 showWarning(); 68 } 69 } 70 71 private boolean checkForXalan() { 72 try { 75 try { 76 XMLUtil.class.getClassLoader().loadClass("org.apache.xalan.Version"); } catch (ClassNotFoundException ex) { 78 return false; 80 } 81 return !verifyWriterCorrect(); 82 } catch (Exception ex) { 83 Logger.getLogger(AntProjectModule.class.getName()).log(Level.INFO, "Cannot run JAXP", ex); 85 } catch (Error e) { 86 Logger.getLogger(AntProjectModule.class.getName()).log(Level.SEVERE, "Cannot run JAXP", e); 87 } 88 89 return false; 90 } 91 92 private boolean verifyWriterCorrect() throws Exception { 93 final String IDENTITY_XSLT_WITH_INDENT = 94 "<xsl:stylesheet version='1.0' " + "xmlns:xsl='http://www.w3.org/1999/XSL/Transform' " + "xmlns:xalan='http://xml.apache.org/xslt' " + "exclude-result-prefixes='xalan'>" + "<xsl:output method='xml' indent='yes' xalan:indent-amount='4'/>" + "<xsl:template match='@*|node()'>" + "<xsl:copy>" + "<xsl:apply-templates select='@*|node()'/>" + "</xsl:copy>" + "</xsl:template>" + "</xsl:stylesheet>"; String data = "<root xmlns='root'/>"; Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource (new StringReader (data))); 107 doc.getDocumentElement().appendChild(doc.createElementNS("child", "child")); Transformer t = TransformerFactory.newInstance().newTransformer( 109 new StreamSource (new StringReader (IDENTITY_XSLT_WITH_INDENT))); 110 Source source = new DOMSource (doc); 111 CharArrayWriter output = new CharArrayWriter (); 112 Result result = new StreamResult (output); 113 t.transform(source, result); 114 115 output.close(); 116 117 String text = output.toString(); 118 119 return text.indexOf("\"child\"") != (-1) || text.indexOf("'child'") != (-1); } 121 122 private void showWarning() { 123 NotifyDescriptor nd = new NotifyDescriptor.Message(NbBundle.getMessage(AntProjectModule.class, "LBL_Incompatible_Xalan")); 125 DialogDisplayer.getDefault().notify(nd); 126 127 SwingUtilities.invokeLater(new Runnable () { 130 public void run() { 131 Frame f = WindowManager.getDefault().getMainWindow(); 132 133 if (f == null || f.isShowing()) { 134 LifecycleManager.getDefault().exit(); 135 } else { 136 f.addWindowListener(new WindowAdapter () { 137 public void windowOpened(WindowEvent e) { 138 LifecycleManager.getDefault().exit(); 139 } 140 }); 141 } 142 } 143 }); 144 } 145 146 } 147 | Popular Tags |