KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ant > freeform > Util


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.ant.freeform;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.text.Collator JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.util.SortedSet JavaDoc;
31 import java.util.TreeSet JavaDoc;
32 import java.util.regex.Pattern JavaDoc;
33 import javax.swing.event.ChangeListener JavaDoc;
34 import org.apache.tools.ant.module.api.AntProjectCookie;
35 import org.apache.tools.ant.module.api.support.TargetLister;
36 import org.netbeans.api.project.Project;
37 import org.netbeans.modules.ant.freeform.spi.HelpIDFragmentProvider;
38 import org.openide.ErrorManager;
39 import org.openide.filesystems.FileObject;
40 import org.openide.filesystems.FileUtil;
41 import org.openide.loaders.DataObject;
42 import org.openide.loaders.DataObjectNotFoundException;
43 import org.openide.xml.XMLUtil;
44 import org.w3c.dom.Document JavaDoc;
45 import org.w3c.dom.Element JavaDoc;
46 import org.xml.sax.ErrorHandler JavaDoc;
47 import org.xml.sax.InputSource JavaDoc;
48 import org.xml.sax.SAXException JavaDoc;
49 import org.xml.sax.SAXParseException JavaDoc;
50
51 /**
52  * Miscellaneous utilities.
53  * @author Jesse Glick
54  */

55 public class Util {
56     
57     private Util() {}
58     
59     public static final ErrorManager err = ErrorManager.getDefault().getInstance("org.netbeans.modules.ant.freeform"); // NOI18N
60

61     /**
62      * Returns name of the Ant script represented by the given file object.
63      * @param fo Ant script which name should be returned
64      * @return name of the Ant script as specified in name attribute of
65      * project element or null if fo does not represent valid Ant script
66      * or the script is anonymous
67      */

68     public static String JavaDoc getAntScriptName(FileObject fo) {
69         AntProjectCookie apc = getAntProjectCookie(fo);
70         if (apc == null) {
71             return null;
72         }
73         Element JavaDoc projEl = apc.getProjectElement();
74         if (projEl == null) {
75             return null;
76         }
77         String JavaDoc name = projEl.getAttribute("name"); // NOI18N
78
// returns "" if no such attribute
79
return name.length() > 0 ? name : null;
80     }
81     
82     private static AntProjectCookie getAntProjectCookie(FileObject fo) {
83         DataObject dob;
84         try {
85             dob = DataObject.find(fo);
86         } catch (DataObjectNotFoundException ex) {
87             Util.err.notify(ErrorManager.INFORMATIONAL, ex);
88             return null;
89         }
90         assert dob != null;
91         AntProjectCookie apc = dob.getCookie(AntProjectCookie.class);
92         if (apc == null && /* #88430 */fo.isData()) {
93             // Some file that *could* be an Ant script and just wasn't recognized
94
// as such? Cf. also TargetLister.getAntProjectCookie, which has the
95
// advantage of being inside the Ant module and therefore able to
96
// directly instantiate AntProjectSupport.
97
try {
98                 apc = forceParse(fo);
99             } catch (IOException JavaDoc e) {
100                 err.notify(ErrorManager.INFORMATIONAL, e);
101             } catch (SAXException JavaDoc e) {
102                 err.log("Parse error in " + fo + ": " + e);
103             }
104         }
105         return apc;
106     }
107
108     private static final Pattern JavaDoc VALIDATION = Pattern.compile("([A-Za-z0-9])+"); // NOI18N
109

110     public static String JavaDoc getMergedHelpIDFragments(Project p) {
111         List JavaDoc<String JavaDoc> fragments = new ArrayList JavaDoc<String JavaDoc>();
112         
113         for (HelpIDFragmentProvider provider : p.getLookup().lookupAll(HelpIDFragmentProvider.class)) {
114             String JavaDoc fragment = provider.getHelpIDFragment();
115             
116             if (fragment == null || !VALIDATION.matcher(fragment).matches()) {
117                 throw new IllegalStateException JavaDoc("HelpIDFragmentProvider \"" + provider + "\" provided invalid help ID fragment \"" + fragment + "\"."); // NOI18N
118
}
119             
120             fragments.add(fragment);
121         }
122         
123         Collections.sort(fragments);
124         
125         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
126
127         for (Iterator JavaDoc<String JavaDoc> i = fragments.iterator(); i.hasNext(); ) {
128             result.append(i.next());
129             
130             if (i.hasNext()) {
131                 result.append('.');
132             }
133         }
134         
135         return result.toString();
136     }
137     
138     /**
139      * Returns sorted list of targets name of the Ant script represented by the
140      * given file object.
141      * @param fo Ant script which target names should be returned
142      * @return sorted list of target names or null if fo does not represent
143      * valid Ant script
144      */

145     public static List JavaDoc<String JavaDoc> getAntScriptTargetNames(FileObject fo) {
146         if (fo == null) {
147             throw new IllegalArgumentException JavaDoc("Cannot call Util.getAntScriptTargetNames with null"); // NOI18N
148
}
149         AntProjectCookie apc = getAntProjectCookie(fo);
150         if (apc == null) {
151             return null;
152         }
153         Set JavaDoc<TargetLister.Target> allTargets;
154         try {
155             allTargets = TargetLister.getTargets(apc);
156         } catch (IOException JavaDoc e) {
157             err.notify(ErrorManager.INFORMATIONAL, e);
158             return null;
159         }
160         SortedSet JavaDoc<String JavaDoc> targetNames = new TreeSet JavaDoc<String JavaDoc>(Collator.getInstance());
161         for (TargetLister.Target target : allTargets) {
162             if (target.isOverridden()) {
163                 // Cannot call it directly.
164
continue;
165             }
166             if (target.isInternal()) {
167                 // Should not be called from outside.
168
continue;
169             }
170             targetNames.add(target.getName());
171         }
172         return new ArrayList JavaDoc<String JavaDoc>(targetNames);
173     }
174     
175     /**
176      * Try to parse a (presumably XML) file even though it is not known to be an Ant script.
177      */

178     private static AntProjectCookie forceParse(FileObject fo) throws IOException JavaDoc, SAXException JavaDoc {
179         Document JavaDoc doc = XMLUtil.parse(new InputSource JavaDoc(fo.getURL().toExternalForm()), false, true, new ErrH(), null);
180         return new TrivialAntProjectCookie(fo, doc);
181     }
182     
183     private static final class ErrH implements ErrorHandler JavaDoc {
184         public ErrH() {}
185         public void fatalError(SAXParseException JavaDoc exception) throws SAXException JavaDoc {
186             throw exception;
187         }
188         public void error(SAXParseException JavaDoc exception) throws SAXException JavaDoc {
189             throw exception;
190         }
191         public void warning(SAXParseException JavaDoc exception) throws SAXException JavaDoc {
192             // ignore that
193
}
194     }
195     
196     private static final class TrivialAntProjectCookie implements AntProjectCookie.ParseStatus {
197         
198         private final FileObject fo;
199         private final Document JavaDoc doc;
200         
201         public TrivialAntProjectCookie(FileObject fo, Document JavaDoc doc) {
202             this.fo = fo;
203             this.doc = doc;
204         }
205
206         public FileObject getFileObject() {
207             return fo;
208         }
209
210         public File JavaDoc getFile() {
211             return FileUtil.toFile(fo);
212         }
213
214         public Document JavaDoc getDocument() {
215             return doc;
216         }
217
218         public Element JavaDoc getProjectElement() {
219             return doc.getDocumentElement();
220         }
221         
222         public boolean isParsed() {
223             return true;
224         }
225
226         public Throwable JavaDoc getParseException() {
227             return null;
228         }
229
230         public void addChangeListener(ChangeListener JavaDoc l) {}
231
232         public void removeChangeListener(ChangeListener JavaDoc l) {}
233
234     }
235     
236 }
237
Popular Tags