KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > project > ant > AntProjectModule


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.project.ant;
21
22 import java.awt.Frame JavaDoc;
23 import java.awt.event.WindowAdapter JavaDoc;
24 import java.awt.event.WindowEvent JavaDoc;
25 import java.io.CharArrayWriter JavaDoc;
26 import java.io.StringReader JavaDoc;
27 import java.util.logging.Level JavaDoc;
28 import java.util.logging.Logger JavaDoc;
29 import javax.swing.SwingUtilities JavaDoc;
30 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
31 import javax.xml.transform.Result JavaDoc;
32 import javax.xml.transform.Source JavaDoc;
33 import javax.xml.transform.Transformer JavaDoc;
34 import javax.xml.transform.TransformerFactory JavaDoc;
35 import javax.xml.transform.dom.DOMSource JavaDoc;
36 import javax.xml.transform.stream.StreamResult JavaDoc;
37 import javax.xml.transform.stream.StreamSource JavaDoc;
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 JavaDoc;
46 import org.xml.sax.InputSource JavaDoc;
47
48 /**
49  * Checks for buggy versions of Xalan.
50  * @author Jan Lahoda
51  * @see "issue #70130"
52  */

53 public class AntProjectModule extends ModuleInstall {
54     
55     public void restored() {
56         super.restored();
57         
58         if (Boolean.getBoolean("netbeans.do.not.check.xalan")) // NOI18N
59
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 JavaDoc(end - start)); // NOI18N
65

66         if (isBuggyXalan) {
67             showWarning();
68         }
69     }
70     
71     private boolean checkForXalan() {
72         //check for a buggy xalan on the classpath and warn if necessary:
73
//try to load org.apache.xalan.Version class, OK if it does not exist:
74
try {
75             try {
76                 XMLUtil.class.getClassLoader().loadClass("org.apache.xalan.Version"); // NOI18N
77
} catch (ClassNotFoundException JavaDoc ex) {
78                 //ok, no xalan, everything is OK.
79
return false;
80             }
81             return !verifyWriterCorrect();
82         } catch (Exception JavaDoc ex) {
83             //should not happen, but probably OK:
84
Logger.getLogger(AntProjectModule.class.getName()).log(Level.INFO, "Cannot run JAXP", ex);
85         } catch (Error JavaDoc 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 JavaDoc {
93         final String JavaDoc IDENTITY_XSLT_WITH_INDENT =
94                 "<xsl:stylesheet version='1.0' " + // NOI18N
95
"xmlns:xsl='http://www.w3.org/1999/XSL/Transform' " + // NOI18N
96
"xmlns:xalan='http://xml.apache.org/xslt' " + // NOI18N
97
"exclude-result-prefixes='xalan'>" + // NOI18N
98
"<xsl:output method='xml' indent='yes' xalan:indent-amount='4'/>" + // NOI18N
99
"<xsl:template match='@*|node()'>" + // NOI18N
100
"<xsl:copy>" + // NOI18N
101
"<xsl:apply-templates select='@*|node()'/>" + // NOI18N
102
"</xsl:copy>" + // NOI18N
103
"</xsl:template>" + // NOI18N
104
"</xsl:stylesheet>"; // NOI18N
105
String JavaDoc data = "<root xmlns='root'/>"; // NOI18N
106
Document JavaDoc doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource JavaDoc(new StringReader JavaDoc(data)));
107         doc.getDocumentElement().appendChild(doc.createElementNS("child", "child")); // NOI18N
108
Transformer JavaDoc t = TransformerFactory.newInstance().newTransformer(
109                 new StreamSource JavaDoc(new StringReader JavaDoc(IDENTITY_XSLT_WITH_INDENT)));
110         Source JavaDoc source = new DOMSource JavaDoc(doc);
111         CharArrayWriter JavaDoc output = new CharArrayWriter JavaDoc();
112         Result JavaDoc result = new StreamResult JavaDoc(output);
113         t.transform(source, result);
114         
115         output.close();
116         
117         String JavaDoc text = output.toString();
118         
119         return text.indexOf("\"child\"") != (-1) || text.indexOf("'child'") != (-1); // NOI18N
120
}
121
122     private void showWarning() {
123         NotifyDescriptor nd = new NotifyDescriptor.Message(NbBundle.getMessage(AntProjectModule.class, "LBL_Incompatible_Xalan")); // NOI18N
124

125         DialogDisplayer.getDefault().notify(nd);
126         
127         //the IDE cannot be closed here (the window system data are corrupted then), wait until the main window appears
128
//and close then:
129
SwingUtilities.invokeLater(new Runnable JavaDoc() {
130             public void run() {
131                 Frame JavaDoc f = WindowManager.getDefault().getMainWindow();
132                 
133                 if (f == null || f.isShowing()) {
134                     LifecycleManager.getDefault().exit();
135                 } else {
136                     f.addWindowListener(new WindowAdapter JavaDoc() {
137                         public void windowOpened(WindowEvent JavaDoc e) {
138                             LifecycleManager.getDefault().exit();
139                         }
140                     });
141                 }
142             }
143         });
144     }
145     
146 }
147
Popular Tags