Lines 45-67
Link Here
|
45 |
import org.netbeans.api.project.Project; |
45 |
import org.netbeans.api.project.Project; |
46 |
import org.openide.ErrorManager; |
46 |
import org.openide.ErrorManager; |
47 |
import org.openide.filesystems.FileObject; |
47 |
import org.openide.filesystems.FileObject; |
48 |
import org.openide.filesystems.FileUtil; |
|
|
49 |
import org.openide.loaders.DataObject; |
50 |
import org.openide.loaders.DataObjectNotFoundException; |
51 |
import org.openide.util.NbBundle; |
52 |
import org.openide.xml.XMLUtil; |
53 |
import org.w3c.dom.Document; |
54 |
import org.w3c.dom.Element; |
48 |
import org.w3c.dom.Element; |
55 |
import org.xml.sax.ErrorHandler; |
|
|
56 |
import org.xml.sax.InputSource; |
57 |
import org.xml.sax.SAXException; |
58 |
import org.xml.sax.SAXParseException; |
59 |
import java.io.File; |
60 |
import java.io.IOException; |
49 |
import java.io.IOException; |
61 |
import java.text.Collator; |
|
|
62 |
import java.text.MessageFormat; |
63 |
import java.util.*; |
50 |
import java.util.*; |
64 |
import javax.swing.event.ChangeListener; |
51 |
import org.apache.tools.ant.module.api.support.AntScriptUtils; |
65 |
|
52 |
|
66 |
|
53 |
|
67 |
/** |
54 |
/** |
Lines 71-153
Link Here
|
71 |
* @author Jesse Glick |
58 |
* @author Jesse Glick |
72 |
*/ |
59 |
*/ |
73 |
public final class Util { |
60 |
public final class Util { |
74 |
//~ Inner Classes ------------------------------------------------------------------------------------------------------------ |
|
|
75 |
|
76 |
private static final class ErrH implements ErrorHandler { |
77 |
//~ Constructors --------------------------------------------------------------------------------------------------------- |
78 |
|
79 |
public ErrH() { |
80 |
} |
81 |
|
82 |
//~ Methods -------------------------------------------------------------------------------------------------------------- |
83 |
|
84 |
public void error(final SAXParseException exception) |
85 |
throws SAXException { |
86 |
throw exception; |
87 |
} |
88 |
|
89 |
public void fatalError(final SAXParseException exception) |
90 |
throws SAXException { |
91 |
throw exception; |
92 |
} |
93 |
|
94 |
public void warning(final SAXParseException exception) |
95 |
throws SAXException { |
96 |
// ignore that |
97 |
} |
98 |
} |
99 |
|
100 |
private static final class TrivialAntProjectCookie implements AntProjectCookie.ParseStatus { |
101 |
//~ Instance fields ------------------------------------------------------------------------------------------------------ |
102 |
|
103 |
private final Document doc; |
104 |
private final FileObject fo; |
105 |
|
106 |
//~ Constructors --------------------------------------------------------------------------------------------------------- |
107 |
|
108 |
public TrivialAntProjectCookie(final FileObject fo, final Document doc) { |
109 |
this.fo = fo; |
110 |
this.doc = doc; |
111 |
} |
112 |
|
113 |
//~ Methods -------------------------------------------------------------------------------------------------------------- |
114 |
|
115 |
public Document getDocument() { |
116 |
return doc; |
117 |
} |
118 |
|
119 |
public File getFile() { |
120 |
return FileUtil.toFile(fo); |
121 |
} |
122 |
|
123 |
public FileObject getFileObject() { |
124 |
return fo; |
125 |
} |
126 |
|
127 |
public Throwable getParseException() { |
128 |
return null; |
129 |
} |
130 |
|
131 |
public boolean isParsed() { |
132 |
return true; |
133 |
} |
134 |
|
135 |
public Element getProjectElement() { |
136 |
return doc.getDocumentElement(); |
137 |
} |
138 |
|
139 |
public void addChangeListener(final ChangeListener l) { |
140 |
} |
141 |
|
142 |
public void removeChangeListener(final ChangeListener l) { |
143 |
} |
144 |
} |
145 |
|
146 |
//~ Static fields/initializers ----------------------------------------------------------------------------------------------- |
147 |
|
148 |
// ----- |
149 |
// I18N String constants |
150 |
private static final String PARSE_ERROR_MSG = NbBundle.getMessage(Util.class, "Util_ParseErrorMsg"); // NOI18N |
151 |
// ----- |
61 |
// ----- |
152 |
public static final ErrorManager err = ErrorManager.getDefault().getInstance("org.netbeans.modules.profiler.freeform"); // NOI18N |
62 |
public static final ErrorManager err = ErrorManager.getDefault().getInstance("org.netbeans.modules.profiler.freeform"); // NOI18N |
153 |
|
63 |
|
Lines 157-189
Link Here
|
157 |
} |
67 |
} |
158 |
|
68 |
|
159 |
//~ Methods ------------------------------------------------------------------------------------------------------------------ |
69 |
//~ Methods ------------------------------------------------------------------------------------------------------------------ |
160 |
|
|
|
161 |
/** |
162 |
* Returns name of the Ant script represented by the given file object. |
163 |
* |
164 |
* @param fo Ant script which name should be returned |
165 |
* @return name of the Ant script as specified in name attribute of |
166 |
* project element or null if fo does not represent valid Ant script |
167 |
* or the script is anonymous |
168 |
*/ |
169 |
public static String getAntScriptName(final FileObject fo) { |
170 |
final AntProjectCookie apc = getAntProjectCookie(fo); |
171 |
|
172 |
if (apc == null) { |
173 |
return null; |
174 |
} |
175 |
|
176 |
final Element projEl = apc.getProjectElement(); |
177 |
|
178 |
if (projEl == null) { |
179 |
return null; |
180 |
} |
181 |
|
182 |
final String name = projEl.getAttribute("name"); // NOI18N |
183 |
// returns "" if no such attribute |
184 |
|
185 |
return (name.length() > 0) ? name : null; |
186 |
} |
187 |
|
70 |
|
188 |
/** |
71 |
/** |
189 |
* Returns XML element representing the requested target or null if the target does not exist. |
72 |
* Returns XML element representing the requested target or null if the target does not exist. |
Lines 197-207
Link Here
|
197 |
throw new IllegalArgumentException("Cannot call Util.getAntScriptTargetNames with null"); // NOI18N |
80 |
throw new IllegalArgumentException("Cannot call Util.getAntScriptTargetNames with null"); // NOI18N |
198 |
} |
81 |
} |
199 |
|
82 |
|
200 |
final AntProjectCookie apc = getAntProjectCookie(fo); |
83 |
final AntProjectCookie apc = AntScriptUtils.antProjectCookieFor(fo); |
201 |
|
|
|
202 |
if (apc == null) { |
203 |
return null; |
204 |
} |
205 |
|
84 |
|
206 |
final Set /*TargetLister.Target*/ allTargets; |
85 |
final Set /*TargetLister.Target*/ allTargets; |
207 |
|
86 |
|
Lines 236-271
Link Here
|
236 |
return null; |
115 |
return null; |
237 |
} |
116 |
} |
238 |
|
117 |
|
239 |
/** |
|
|
240 |
* Returns sorted list of targets name of the Ant script represented by the |
241 |
* given file object. |
242 |
* |
243 |
* @param fo Ant script which target names should be returned |
244 |
* @return sorted list of target names or null if fo does not represent |
245 |
* valid Ant script |
246 |
*/ |
247 |
public static List /*<String>*/ getAntScriptTargetNames(final FileObject fo) { |
248 |
final List targets = getAntScriptTargets(fo); |
249 |
final SortedSet targetNames = new TreeSet(Collator.getInstance()); |
250 |
final Iterator it = targets.iterator(); |
251 |
|
252 |
while (it.hasNext()) { |
253 |
targetNames.add(((TargetLister.Target) it.next()).getName()); |
254 |
} |
255 |
|
256 |
return new ArrayList(targetNames); |
257 |
} |
258 |
|
259 |
public static List /*TargetLister.Target*/ getAntScriptTargets(final FileObject buildScript) { |
118 |
public static List /*TargetLister.Target*/ getAntScriptTargets(final FileObject buildScript) { |
260 |
if (buildScript == null) { |
119 |
if (buildScript == null) { |
261 |
throw new IllegalArgumentException("Cannot call Util.getAntScriptTargetNames with null"); // NOI18N |
120 |
throw new IllegalArgumentException("Cannot call Util.getAntScriptTargetNames with null"); // NOI18N |
262 |
} |
121 |
} |
263 |
|
122 |
|
264 |
final AntProjectCookie apc = getAntProjectCookie(buildScript); |
123 |
final AntProjectCookie apc = AntScriptUtils.antProjectCookieFor(buildScript); |
265 |
|
|
|
266 |
if (apc == null) { |
267 |
return null; |
268 |
} |
269 |
|
124 |
|
270 |
final Set /*TargetLister.Target*/ allTargets; |
125 |
final Set /*TargetLister.Target*/ allTargets; |
271 |
|
126 |
|
Lines 303-346
Link Here
|
303 |
return org.netbeans.modules.ant.freeform.spi.support.Util.getDefaultAntScript(project); |
158 |
return org.netbeans.modules.ant.freeform.spi.support.Util.getDefaultAntScript(project); |
304 |
} |
159 |
} |
305 |
|
160 |
|
306 |
private static AntProjectCookie getAntProjectCookie(final FileObject fo) { |
|
|
307 |
final DataObject dob; |
308 |
|
309 |
try { |
310 |
dob = DataObject.find(fo); |
311 |
} catch (DataObjectNotFoundException ex) { |
312 |
Util.err.notify(ErrorManager.INFORMATIONAL, ex); |
313 |
|
314 |
return null; |
315 |
} |
316 |
|
317 |
AntProjectCookie apc = (AntProjectCookie) dob.getCookie(AntProjectCookie.class); |
318 |
|
319 |
if ((apc == null) && fo.getMIMEType().equals("text/xml")) { // NOI18N |
320 |
// Some file that *could* be an Ant script and just wasn't recognized |
321 |
// as such? Cf. also TargetLister.getAntProjectCookie, which has the |
322 |
// advantage of being inside the Ant module and therefore able to |
323 |
// directly instantiate AntProjectSupport. |
324 |
|
325 |
try { |
326 |
apc = forceParse(fo); |
327 |
} catch (IOException e) { |
328 |
err.notify(ErrorManager.INFORMATIONAL, e); |
329 |
} catch (SAXException e) { |
330 |
err.log(MessageFormat.format(PARSE_ERROR_MSG, new Object[] { fo, e })); |
331 |
} |
332 |
} |
333 |
|
334 |
return apc; |
335 |
} |
336 |
|
337 |
/** |
338 |
* Try to parse a (presumably XML) file even though it is not known to be an Ant script. |
339 |
*/ |
340 |
private static AntProjectCookie forceParse(final FileObject fo) |
341 |
throws IOException, SAXException { |
342 |
final Document doc = XMLUtil.parse(new InputSource(fo.getURL().toExternalForm()), false, true, new ErrH(), null); |
343 |
|
344 |
return new TrivialAntProjectCookie(fo, doc); |
345 |
} |
346 |
} |
161 |
} |