This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 136597
Collapse All | Expand All

(-)a/ant.freeform/nbproject/project.xml (-1 / +1 lines)
Lines 52-58 Link Here
52
                    <compile-dependency/>
52
                    <compile-dependency/>
53
                    <run-dependency>
53
                    <run-dependency>
54
                        <release-version>3</release-version>
54
                        <release-version>3</release-version>
55
                        <specification-version>3.11</specification-version>
55
                        <specification-version>3.31</specification-version>
56
                    </run-dependency>
56
                    </run-dependency>
57
                </dependency>
57
                </dependency>
58
                <dependency>
58
                <dependency>
(-)a/ant.freeform/src/org/netbeans/modules/ant/freeform/Util.java (-165 lines)
Lines 41-74 Link Here
41
41
42
package org.netbeans.modules.ant.freeform;
42
package org.netbeans.modules.ant.freeform;
43
43
44
import java.io.File;
45
import java.io.IOException;
46
import java.text.Collator;
47
import java.util.ArrayList;
44
import java.util.ArrayList;
48
import java.util.Collections;
45
import java.util.Collections;
49
import java.util.Iterator;
46
import java.util.Iterator;
50
import java.util.List;
47
import java.util.List;
51
import java.util.Set;
52
import java.util.SortedSet;
53
import java.util.TreeSet;
54
import java.util.regex.Pattern;
48
import java.util.regex.Pattern;
55
import javax.swing.event.ChangeListener;
56
import org.apache.tools.ant.module.api.AntProjectCookie;
57
import org.apache.tools.ant.module.api.support.TargetLister;
58
import org.netbeans.api.project.Project;
49
import org.netbeans.api.project.Project;
59
import org.netbeans.modules.ant.freeform.spi.HelpIDFragmentProvider;
50
import org.netbeans.modules.ant.freeform.spi.HelpIDFragmentProvider;
60
import org.openide.ErrorManager;
51
import org.openide.ErrorManager;
61
import org.openide.filesystems.FileObject;
62
import org.openide.filesystems.FileUtil;
63
import org.openide.loaders.DataObject;
64
import org.openide.loaders.DataObjectNotFoundException;
65
import org.openide.xml.XMLUtil;
66
import org.w3c.dom.Document;
67
import org.w3c.dom.Element;
68
import org.xml.sax.ErrorHandler;
69
import org.xml.sax.InputSource;
70
import org.xml.sax.SAXException;
71
import org.xml.sax.SAXParseException;
72
52
73
/**
53
/**
74
 * Miscellaneous utilities.
54
 * Miscellaneous utilities.
Lines 80-132 Link Here
80
    
60
    
81
    public static final ErrorManager err = ErrorManager.getDefault().getInstance("org.netbeans.modules.ant.freeform"); // NOI18N
61
    public static final ErrorManager err = ErrorManager.getDefault().getInstance("org.netbeans.modules.ant.freeform"); // NOI18N
82
    
62
    
83
    /**
84
     * Returns name of the Ant script represented by the given file object.
85
     * @param fo Ant script which name should be returned
86
     * @return name of the Ant script as specified in name attribute of
87
     *    project element or null if fo does not represent valid Ant script
88
     *    or the script is anonymous
89
     */
90
    public static String getAntScriptName(FileObject fo) {
91
        AntProjectCookie apc = getAntProjectCookie(fo);
92
        if (apc == null) {
93
            return null;
94
        }
95
        Element projEl = apc.getProjectElement();
96
        if (projEl == null) {
97
            return null;
98
        }
99
        String name = projEl.getAttribute("name"); // NOI18N
100
        // returns "" if no such attribute
101
        return name.length() > 0 ? name : null;
102
    }
103
    
104
    private static AntProjectCookie getAntProjectCookie(FileObject fo) {
105
        DataObject dob;
106
        try {
107
            dob = DataObject.find(fo);
108
        } catch (DataObjectNotFoundException ex) {
109
            Util.err.notify(ErrorManager.INFORMATIONAL, ex);
110
            return null;
111
        }
112
        assert dob != null;
113
        AntProjectCookie apc = dob.getCookie(AntProjectCookie.class);
114
        if (apc == null && /* #88430 */fo.isData()) {
115
            // Some file that *could* be an Ant script and just wasn't recognized
116
            // as such? Cf. also TargetLister.getAntProjectCookie, which has the
117
            // advantage of being inside the Ant module and therefore able to
118
            // directly instantiate AntProjectSupport.
119
            try {
120
                apc = forceParse(fo);
121
            } catch (IOException e) {
122
                err.notify(ErrorManager.INFORMATIONAL, e);
123
            } catch (SAXException e) {
124
                err.log("Parse error in " + fo + ": " + e);
125
            }
126
        }
127
        return apc;
128
    }
129
130
    private static final Pattern VALIDATION = Pattern.compile("([A-Za-z0-9])+"); // NOI18N
63
    private static final Pattern VALIDATION = Pattern.compile("([A-Za-z0-9])+"); // NOI18N
131
    
64
    
132
    public static String getMergedHelpIDFragments(Project p) {
65
    public static String getMergedHelpIDFragments(Project p) {
Lines 157-258 Link Here
157
        return result.toString();
90
        return result.toString();
158
    }
91
    }
159
    
92
    
160
    /**
161
     * Returns sorted list of targets name of the Ant script represented by the
162
     * given file object.
163
     * @param fo Ant script which target names should be returned
164
     * @return sorted list of target names or null if fo does not represent 
165
     * valid Ant script
166
     */
167
    public static List<String> getAntScriptTargetNames(FileObject fo) {
168
        if (fo == null) {
169
            throw new IllegalArgumentException("Cannot call Util.getAntScriptTargetNames with null"); // NOI18N
170
        }
171
        AntProjectCookie apc = getAntProjectCookie(fo);
172
        if (apc == null) {
173
            return null;
174
        }
175
        Set<TargetLister.Target> allTargets;
176
        try {
177
            allTargets = TargetLister.getTargets(apc);
178
        } catch (IOException e) {
179
            err.notify(ErrorManager.INFORMATIONAL, e);
180
            return null;
181
        }
182
        SortedSet<String> targetNames = new TreeSet<String>(Collator.getInstance());
183
        for (TargetLister.Target target : allTargets) {
184
            if (target.isOverridden()) {
185
                // Cannot call it directly.
186
                continue;
187
            }
188
            if (target.isInternal()) {
189
                // Should not be called from outside.
190
                continue;
191
            }
192
            targetNames.add(target.getName());
193
        }
194
        return new ArrayList<String>(targetNames);
195
    }
196
    
197
    /**
198
     * Try to parse a (presumably XML) file even though it is not known to be an Ant script.
199
     */
200
    private static AntProjectCookie forceParse(FileObject fo) throws IOException, SAXException {
201
        Document doc = XMLUtil.parse(new InputSource(fo.getURL().toExternalForm()), false, true, new ErrH(), null);
202
        return new TrivialAntProjectCookie(fo, doc);
203
    }
204
    
205
    private static final class ErrH implements ErrorHandler {
206
        public ErrH() {}
207
        public void fatalError(SAXParseException exception) throws SAXException {
208
            throw exception;
209
        }
210
        public void error(SAXParseException exception) throws SAXException {
211
            throw exception;
212
        }
213
        public void warning(SAXParseException exception) throws SAXException {
214
            // ignore that
215
        }
216
    }
217
    
218
    private static final class TrivialAntProjectCookie implements AntProjectCookie.ParseStatus {
219
        
220
        private final FileObject fo;
221
        private final Document doc;
222
        
223
        public TrivialAntProjectCookie(FileObject fo, Document doc) {
224
            this.fo = fo;
225
            this.doc = doc;
226
        }
227
228
        public FileObject getFileObject() {
229
            return fo;
230
        }
231
232
        public File getFile() {
233
            return FileUtil.toFile(fo);
234
        }
235
236
        public Document getDocument() {
237
            return doc;
238
        }
239
240
        public Element getProjectElement() {
241
            return doc.getDocumentElement();
242
        }
243
        
244
        public boolean isParsed() {
245
            return true;
246
        }
247
248
        public Throwable getParseException() {
249
            return null;
250
        }
251
252
        public void addChangeListener(ChangeListener l) {}
253
254
        public void removeChangeListener(ChangeListener l) {}
255
256
    }
257
    
258
}
93
}
(-)a/ant.freeform/src/org/netbeans/modules/ant/freeform/ui/BasicProjectInfoPanel.java (-5 / +10 lines)
Lines 42-56 Link Here
42
package org.netbeans.modules.ant.freeform.ui;
42
package org.netbeans.modules.ant.freeform.ui;
43
43
44
import java.io.File;
44
import java.io.File;
45
import java.io.IOException;
45
import java.text.MessageFormat;
46
import java.text.MessageFormat;
46
import javax.swing.JFileChooser;
47
import javax.swing.JFileChooser;
47
import javax.swing.event.ChangeListener;
48
import javax.swing.event.ChangeListener;
48
import javax.swing.event.DocumentEvent;
49
import javax.swing.event.DocumentEvent;
49
import javax.swing.event.DocumentListener;
50
import javax.swing.event.DocumentListener;
51
import org.apache.tools.ant.module.api.support.AntScriptUtils;
50
import org.netbeans.api.project.FileOwnerQuery;
52
import org.netbeans.api.project.FileOwnerQuery;
51
import org.netbeans.api.project.Project;
53
import org.netbeans.api.project.Project;
52
import org.netbeans.api.project.ProjectInformation;
54
import org.netbeans.api.project.ProjectInformation;
53
import org.netbeans.modules.ant.freeform.Util;
54
import org.netbeans.spi.project.ui.support.ProjectChooser;
55
import org.netbeans.spi.project.ui.support.ProjectChooser;
55
import org.openide.filesystems.FileObject;
56
import org.openide.filesystems.FileObject;
56
import org.openide.filesystems.FileUtil;
57
import org.openide.filesystems.FileUtil;
Lines 139-147 Link Here
139
        }
140
        }
140
        if (!antScriptValidityChecked) {
141
        if (!antScriptValidityChecked) {
141
            FileObject fo = FileUtil.toFileObject(getAntScript());
142
            FileObject fo = FileUtil.toFileObject(getAntScript());
142
            if (fo != null && Util.getAntScriptTargetNames(fo) != null) {
143
            if (fo != null) {
143
                antScriptValidityChecked = true;
144
                try {
144
            } else {
145
                    AntScriptUtils.getCallableTargetNames(fo);
146
                    antScriptValidityChecked = true;
147
                } catch (IOException x) {/* failed */}
148
            }
149
            if (!antScriptValidityChecked) {
145
                return org.openide.util.NbBundle.getMessage(BasicProjectInfoPanel.class, "LBL_BasicProjectInfoPanel_Error_5");
150
                return org.openide.util.NbBundle.getMessage(BasicProjectInfoPanel.class, "LBL_BasicProjectInfoPanel_Error_5");
146
            }
151
            }
147
        }
152
        }
Lines 250-256 Link Here
250
            if (as.exists()) {
255
            if (as.exists()) {
251
                FileObject fo = FileUtil.toFileObject(as);
256
                FileObject fo = FileUtil.toFileObject(as);
252
                assert fo != null : as;
257
                assert fo != null : as;
253
                String name = Util.getAntScriptName(fo);
258
                String name = AntScriptUtils.getAntScriptName(fo);
254
                if (name != null) {
259
                if (name != null) {
255
                    projectName.setText(name);
260
                    projectName.setText(name);
256
                    return;
261
                    return;
(-)a/ant.freeform/src/org/netbeans/modules/ant/freeform/ui/TargetMappingPanel.java (-2 / +9 lines)
Lines 44-55 Link Here
44
import java.awt.Frame;
44
import java.awt.Frame;
45
import java.awt.event.ActionEvent;
45
import java.awt.event.ActionEvent;
46
import java.awt.event.ActionListener;
46
import java.awt.event.ActionListener;
47
import java.io.IOException;
47
import java.util.ArrayList;
48
import java.util.ArrayList;
48
import java.util.Arrays;
49
import java.util.Arrays;
49
import java.util.Collections;
50
import java.util.Collections;
50
import java.util.Iterator;
51
import java.util.Iterator;
51
import java.util.List;
52
import java.util.List;
52
import java.util.StringTokenizer;
53
import java.util.StringTokenizer;
54
import java.util.logging.Level;
55
import java.util.logging.Logger;
53
import java.util.regex.Matcher;
56
import java.util.regex.Matcher;
54
import java.util.regex.Pattern;
57
import java.util.regex.Pattern;
55
import javax.swing.ComboBoxModel;
58
import javax.swing.ComboBoxModel;
Lines 60-67 Link Here
60
import javax.swing.JPanel;
63
import javax.swing.JPanel;
61
import javax.swing.WindowConstants;
64
import javax.swing.WindowConstants;
62
import javax.swing.table.AbstractTableModel;
65
import javax.swing.table.AbstractTableModel;
66
import org.apache.tools.ant.module.api.support.AntScriptUtils;
63
import org.netbeans.modules.ant.freeform.FreeformProjectGenerator;
67
import org.netbeans.modules.ant.freeform.FreeformProjectGenerator;
64
import org.netbeans.modules.ant.freeform.Util;
65
import org.netbeans.modules.ant.freeform.spi.ProjectConstants;
68
import org.netbeans.modules.ant.freeform.spi.ProjectConstants;
66
import org.netbeans.modules.ant.freeform.spi.TargetDescriptor;
69
import org.netbeans.modules.ant.freeform.spi.TargetDescriptor;
67
import org.netbeans.spi.project.support.ant.AntProjectHelper;
70
import org.netbeans.spi.project.support.ant.AntProjectHelper;
Lines 131-137 Link Here
131
        List<String> l = null;
134
        List<String> l = null;
132
        // #50933 - script can be null
135
        // #50933 - script can be null
133
        if (as != null) {
136
        if (as != null) {
134
            l = Util.getAntScriptTargetNames(as);
137
            try {
138
                l = AntScriptUtils.getCallableTargetNames(as);
139
            } catch (IOException x) {
140
                Logger.getLogger(TargetMappingPanel.class.getName()).log(Level.INFO, "Cannot parse: " + as, x);
141
            }
135
        }
142
        }
136
        if (l != null) {
143
        if (l != null) {
137
            setTargetNames(l, false);
144
            setTargetNames(l, false);
(-)a/ant.freeform/src/org/netbeans/modules/ant/freeform/ui/TargetMappingWizardPanel.java (-2 / +6 lines)
Lines 43-52 Link Here
43
43
44
import java.awt.Component;
44
import java.awt.Component;
45
import java.io.File;
45
import java.io.File;
46
import java.io.IOException;
46
import java.util.ArrayList;
47
import java.util.ArrayList;
47
import java.util.List;
48
import java.util.List;
48
import javax.swing.event.ChangeListener;
49
import javax.swing.event.ChangeListener;
49
import org.netbeans.modules.ant.freeform.Util;
50
import org.apache.tools.ant.module.api.support.AntScriptUtils;
50
import org.netbeans.modules.ant.freeform.spi.ProjectConstants;
51
import org.netbeans.modules.ant.freeform.spi.ProjectConstants;
51
import org.netbeans.modules.ant.freeform.spi.TargetDescriptor;
52
import org.netbeans.modules.ant.freeform.spi.TargetDescriptor;
52
import org.netbeans.modules.ant.freeform.spi.support.NewFreeformProjectSupport;
53
import org.netbeans.modules.ant.freeform.spi.support.NewFreeformProjectSupport;
Lines 109-115 Link Here
109
        FileObject fo = FileUtil.toFileObject(f);
110
        FileObject fo = FileUtil.toFileObject(f);
110
        // Util.getAntScriptTargetNames can return null when script is 
111
        // Util.getAntScriptTargetNames can return null when script is 
111
        // invalid but first panel checks script validity so it is OK here.
112
        // invalid but first panel checks script validity so it is OK here.
112
        List<String> l = Util.getAntScriptTargetNames(fo);
113
        List<String> l = null;
114
        try {
115
            l = AntScriptUtils.getCallableTargetNames(fo);
116
        } catch (IOException x) {/* ignore */}
113
        // #47784 - update panel only once or when Ant script has changed
117
        // #47784 - update panel only once or when Ant script has changed
114
        if (targetNames == null || !targetNames.equals(l)) {
118
        if (targetNames == null || !targetNames.equals(l)) {
115
            targetNames = new ArrayList<String>(l);
119
            targetNames = new ArrayList<String>(l);
(-)a/ant.freeform/src/org/netbeans/modules/ant/freeform/ui/UnboundTargetAlert.java (-4 / +5 lines)
Lines 51-62 Link Here
51
import java.util.StringTokenizer;
51
import java.util.StringTokenizer;
52
import javax.swing.DefaultComboBoxModel;
52
import javax.swing.DefaultComboBoxModel;
53
import javax.swing.JPanel;
53
import javax.swing.JPanel;
54
import org.apache.tools.ant.module.api.support.AntScriptUtils;
54
import org.netbeans.api.project.ProjectManager;
55
import org.netbeans.api.project.ProjectManager;
55
import org.netbeans.api.project.ProjectUtils;
56
import org.netbeans.api.project.ProjectUtils;
56
import org.netbeans.modules.ant.freeform.Actions;
57
import org.netbeans.modules.ant.freeform.Actions;
57
import org.netbeans.modules.ant.freeform.FreeformProject;
58
import org.netbeans.modules.ant.freeform.FreeformProject;
58
import org.netbeans.modules.ant.freeform.FreeformProjectGenerator;
59
import org.netbeans.modules.ant.freeform.FreeformProjectGenerator;
59
import org.netbeans.modules.ant.freeform.Util;
60
import org.netbeans.spi.project.ActionProvider;
60
import org.openide.DialogDescriptor;
61
import org.openide.DialogDescriptor;
61
import org.openide.DialogDisplayer;
62
import org.openide.DialogDisplayer;
62
import org.openide.NotifyDescriptor;
63
import org.openide.NotifyDescriptor;
Lines 114-124 Link Here
114
    private void listTargets() {
115
    private void listTargets() {
115
        FileObject script = FreeformProjectGenerator.getAntScript(project.helper(), project.evaluator());
116
        FileObject script = FreeformProjectGenerator.getAntScript(project.helper(), project.evaluator());
116
        if (script != null) {
117
        if (script != null) {
117
            List<String> targets = Util.getAntScriptTargetNames(script);
118
            try {
118
            if (targets != null) {
119
                List<String> targets = AntScriptUtils.getCallableTargetNames(script);
119
                selectCombo.setModel(new DefaultComboBoxModel(targets.toArray(new String[targets.size()])));
120
                selectCombo.setModel(new DefaultComboBoxModel(targets.toArray(new String[targets.size()])));
120
                selectCombo.setSelectedItem("");
121
                selectCombo.setSelectedItem("");
121
            }
122
            } catch (IOException x) {/* ignore */}
122
        }
123
        }
123
    }
124
    }
124
125
(-)a/o.apache.tools.ant.module/apichanges.xml (+16 lines)
Lines 106-111 Link Here
106
<!-- ACTUAL CHANGES BEGIN HERE: -->
106
<!-- ACTUAL CHANGES BEGIN HERE: -->
107
107
108
    <changes>
108
    <changes>
109
110
        <change id="AntScriptUtils">
111
            <api name="general"/>
112
            <summary>Added <code>AntScriptUtils</code></summary>
113
            <version major="3" minor="31"/>
114
            <date day="12" month="6" year="2008"/>
115
            <author login="jglick"/>
116
            <compatibility addition="yes"/>
117
            <description>
118
                <p>
119
                    Added a new convenience class for getting some common information from Ant scripts.
120
                </p>
121
            </description>
122
            <class package="org.apache.tools.ant.module.api.support" name="AntScriptUtils"/>
123
            <issue number="136597"/>
124
        </change>
109
125
110
        <change id="AntEvent-references">
126
        <change id="AntEvent-references">
111
            <api name="general"/>
127
            <api name="general"/>
(-)a/o.apache.tools.ant.module/nbproject/project.properties (-1 / +1 lines)
Lines 39-45 Link Here
39
39
40
javac.compilerargs=-Xlint:unchecked
40
javac.compilerargs=-Xlint:unchecked
41
javac.source=1.5
41
javac.source=1.5
42
spec.version.base=3.30.0
42
spec.version.base=3.31.0
43
compile.ant.jar=${ant.core.lib}
43
compile.ant.jar=${ant.core.lib}
44
src-bridge.cp.extra=build/classes:${compile.ant.jar}
44
src-bridge.cp.extra=build/classes:${compile.ant.jar}
45
extra.module.files=\
45
extra.module.files=\
(-)a/o.apache.tools.ant.module/src/org/apache/tools/ant/module/api/support/ActionUtils.java (-1 / +1 lines)
Lines 83-89 Link Here
83
        if (targetNames != null && targetNames.length == 0) {
83
        if (targetNames != null && targetNames.length == 0) {
84
            throw new IllegalArgumentException("No targets supplied"); // NOI18N
84
            throw new IllegalArgumentException("No targets supplied"); // NOI18N
85
        }
85
        }
86
        AntProjectCookie apc = TargetLister.getAntProjectCookie(buildXml);
86
        AntProjectCookie apc = AntScriptUtils.antProjectCookieFor(buildXml);
87
        AntTargetExecutor.Env execenv = new AntTargetExecutor.Env();
87
        AntTargetExecutor.Env execenv = new AntTargetExecutor.Env();
88
        if (properties != null) {
88
        if (properties != null) {
89
            Properties p = execenv.getProperties();
89
            Properties p = execenv.getProperties();
(-)a/o.apache.tools.ant.module/src/org/apache/tools/ant/module/api/support/AntScriptUtils.java (+136 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of either the GNU
7
 * General Public License Version 2 only ("GPL") or the Common
8
 * Development and Distribution License("CDDL") (collectively, the
9
 * "License"). You may not use this file except in compliance with the
10
 * License. You can obtain a copy of the License at
11
 * http://www.netbeans.org/cddl-gplv2.html
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
 * specific language governing permissions and limitations under the
14
 * License.  When distributing the software, include this License Header
15
 * Notice in each file and include the License file at
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
17
 * particular file as subject to the "Classpath" exception as provided
18
 * by Sun in the GPL Version 2 section of the License file that
19
 * accompanied this code. If applicable, add the following below the
20
 * License Header, with the fields enclosed by brackets [] replaced by
21
 * your own identifying information:
22
 * "Portions Copyrighted [year] [name of copyright owner]"
23
 *
24
 * If you wish your version of this file to be governed by only the CDDL
25
 * or only the GPL Version 2, indicate your decision by adding
26
 * "[Contributor] elects to include this software in this distribution
27
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
28
 * single choice of license, a recipient has the option to distribute
29
 * your version of this file under either the CDDL, the GPL Version 2 or
30
 * to extend the choice of license to its licensees as provided above.
31
 * However, if you add GPL Version 2 code and therefore, elected the GPL
32
 * Version 2 license, then the option applies only if the new code is
33
 * made subject to such option by the copyright holder.
34
 *
35
 * Contributor(s):
36
 *
37
 * Portions Copyrighted 2008 Sun Microsystems, Inc.
38
 */
39
40
package org.apache.tools.ant.module.api.support;
41
42
import java.io.IOException;
43
import java.text.Collator;
44
import java.util.ArrayList;
45
import java.util.List;
46
import java.util.Map;
47
import java.util.Set;
48
import java.util.SortedSet;
49
import java.util.TreeSet;
50
import java.util.WeakHashMap;
51
import org.apache.tools.ant.module.api.AntProjectCookie;
52
import org.apache.tools.ant.module.xml.AntProjectSupport;
53
import org.openide.filesystems.FileObject;
54
import org.openide.loaders.DataObject;
55
import org.openide.loaders.DataObjectNotFoundException;
56
import org.w3c.dom.Element;
57
58
/**
59
 * Convenience utilities for working with Ant scripts.
60
 * @since org.apache.tools.ant.module/3 3.31
61
 */
62
public class AntScriptUtils {
63
64
    private AntScriptUtils() {}
65
66
    /**
67
     * Loads a file believed to be an Ant script.
68
     * @param script a file assumed to be an Ant script
69
     * @return a handle for it (never null but may or may not be parsable)
70
     */
71
    public static AntProjectCookie antProjectCookieFor(FileObject script) {
72
        try {
73
            DataObject d = DataObject.find(script);
74
            AntProjectCookie apc = d.getCookie(AntProjectCookie.class);
75
            if (apc != null) {
76
                return apc;
77
            }
78
        } catch (DataObjectNotFoundException e) {
79
            assert false : e;
80
        }
81
        // AntProjectDataLoader probably not installed, e.g. from a unit test.
82
        // Or may not be recognized as an Ant script (e.g. root <project> element has no attributes).
83
        synchronized (antProjectCookies) {
84
            AntProjectCookie apc = antProjectCookies.get(script);
85
            if (apc == null) {
86
                apc = new AntProjectSupport(script);
87
                antProjectCookies.put(script, apc);
88
            }
89
            return apc;
90
        }
91
    }
92
    private static final Map<FileObject,AntProjectCookie> antProjectCookies = new WeakHashMap<FileObject,AntProjectCookie>();
93
94
    /**
95
     * Finds the name of an Ant script.
96
     * @param script Ant script to inspect
97
     * @return name of the Ant script as specified in the <code>name</code> attribute of
98
     *    the <code>project</code> element, or null if the file is not a valid Ant script
99
     *    or the script is anonymous
100
     */
101
    public static String getAntScriptName(FileObject script) {
102
        AntProjectCookie apc = antProjectCookieFor(script);
103
        Element projEl = apc.getProjectElement();
104
        if (projEl == null) {
105
            return null;
106
        }
107
        String name = projEl.getAttribute("name"); // NOI18N
108
        // returns "" if no such attribute
109
        return name.length() > 0 ? name : null;
110
    }
111
112
    /**
113
     * Finds the names of callable targets in an Ant script.
114
     * @param script Ant script to inspect
115
     * @return list of target names, sorted (by locale)
116
     * @throws IOException if the script cannot be inspected
117
     */
118
    public static List<String> getCallableTargetNames(FileObject script) throws IOException {
119
        AntProjectCookie apc = antProjectCookieFor(script);
120
        Set<TargetLister.Target> allTargets = TargetLister.getTargets(apc);
121
        SortedSet<String> targetNames = new TreeSet<String>(Collator.getInstance());
122
        for (TargetLister.Target target : allTargets) {
123
            if (target.isOverridden()) {
124
                // Cannot call it directly.
125
                continue;
126
            }
127
            if (target.isInternal()) {
128
                // Should not be called from outside.
129
                continue;
130
            }
131
            targetNames.add(target.getName());
132
        }
133
        return new ArrayList<String>(targetNames);
134
    }
135
136
}
(-)a/o.apache.tools.ant.module/src/org/apache/tools/ant/module/api/support/TargetLister.java (-31 / +2 lines)
Lines 56-69 Link Here
56
import java.util.Map;
56
import java.util.Map;
57
import java.util.Properties;
57
import java.util.Properties;
58
import java.util.Set;
58
import java.util.Set;
59
import java.util.WeakHashMap;
60
import org.apache.tools.ant.module.AntSettings;
59
import org.apache.tools.ant.module.AntSettings;
61
import org.apache.tools.ant.module.api.AntProjectCookie;
60
import org.apache.tools.ant.module.api.AntProjectCookie;
62
import org.apache.tools.ant.module.xml.AntProjectSupport;
63
import org.openide.filesystems.FileObject;
61
import org.openide.filesystems.FileObject;
64
import org.openide.filesystems.FileUtil;
62
import org.openide.filesystems.FileUtil;
65
import org.openide.loaders.DataObject;
66
import org.openide.loaders.DataObjectNotFoundException;
67
import org.openide.util.NbCollections;
63
import org.openide.util.NbCollections;
68
import org.openide.util.TopologicalSortException;
64
import org.openide.util.TopologicalSortException;
69
import org.openide.util.Union2;
65
import org.openide.util.Union2;
Lines 406-412 Link Here
406
                    if (file.canRead()) {
402
                    if (file.canRead()) {
407
                        FileObject fileObj = FileUtil.toFileObject(file);
403
                        FileObject fileObj = FileUtil.toFileObject(file);
408
                        assert fileObj != null : file;
404
                        assert fileObj != null : file;
409
                        AntProjectCookie importedApc = getAntProjectCookie(fileObj);
405
                        AntProjectCookie importedApc = AntScriptUtils.antProjectCookieFor(fileObj);
410
                        imports.add(new Script(this, importedApc, alreadyImported, propertyDefs, macroDefs));
406
                        imports.add(new Script(this, importedApc, alreadyImported, propertyDefs, macroDefs));
411
                    } else {
407
                    } else {
412
                        String optionalS = el.getAttribute("optional"); // NOI18N
408
                        String optionalS = el.getAttribute("optional"); // NOI18N
Lines 690-719 Link Here
690
        }
686
        }
691
        
687
        
692
    }
688
    }
693
    
689
694
    /**
695
     * Try to find an AntProjectCookie for a file.
696
     */
697
    static AntProjectCookie getAntProjectCookie(FileObject fo) {
698
        try {
699
            DataObject d = DataObject.find(fo);
700
            AntProjectCookie apc = d.getCookie(AntProjectCookie.class);
701
            if (apc != null) {
702
                return apc;
703
            }
704
        } catch (DataObjectNotFoundException e) {
705
            assert false : e;
706
        }
707
        // AntProjectDataLoader probably not installed, e.g. from a unit test.
708
        synchronized (antProjectCookies) {
709
            AntProjectCookie apc = antProjectCookies.get(fo);
710
            if (apc == null) {
711
                apc = new AntProjectSupport(fo);
712
                antProjectCookies.put(fo, apc);
713
            }
714
            return apc;
715
        }
716
    }
717
    private static final Map<FileObject,AntProjectCookie> antProjectCookies = new WeakHashMap<FileObject,AntProjectCookie>();
718
    
719
}
690
}
(-)a/o.apache.tools.ant.module/src/org/apache/tools/ant/module/run/LastTargetExecuted.java (-7 / +2 lines)
Lines 47-57 Link Here
47
import java.util.Map;
47
import java.util.Map;
48
import javax.swing.event.ChangeListener;
48
import javax.swing.event.ChangeListener;
49
import org.apache.tools.ant.module.api.AntProjectCookie;
49
import org.apache.tools.ant.module.api.AntProjectCookie;
50
import org.apache.tools.ant.module.api.support.AntScriptUtils;
50
import org.openide.execution.ExecutorTask;
51
import org.openide.execution.ExecutorTask;
51
import org.openide.filesystems.FileObject;
52
import org.openide.filesystems.FileObject;
52
import org.openide.filesystems.FileUtil;
53
import org.openide.filesystems.FileUtil;
53
import org.openide.loaders.DataObject;
54
import org.openide.loaders.DataObjectNotFoundException;
55
import org.openide.util.ChangeSupport;
54
import org.openide.util.ChangeSupport;
56
55
57
/**
56
/**
Lines 84-94 Link Here
84
        if (buildScript != null && buildScript.isFile()) {
83
        if (buildScript != null && buildScript.isFile()) {
85
            FileObject fo = FileUtil.toFileObject(buildScript);
84
            FileObject fo = FileUtil.toFileObject(buildScript);
86
            assert fo != null;
85
            assert fo != null;
87
            try {
86
            return AntScriptUtils.antProjectCookieFor(fo);
88
                return DataObject.find(fo).getCookie(AntProjectCookie.class);
89
            } catch (DataObjectNotFoundException e) {
90
                assert false : e;
91
            }
92
        }
87
        }
93
        return null;
88
        return null;
94
    }
89
    }
(-)a/profiler.freeform/nbproject/project.xml (-1 / +1 lines)
Lines 52-58 Link Here
52
                    <compile-dependency/>
52
                    <compile-dependency/>
53
                    <run-dependency>
53
                    <run-dependency>
54
                        <release-version>3</release-version>
54
                        <release-version>3</release-version>
55
                        <specification-version>3.27</specification-version>
55
                        <specification-version>3.31</specification-version>
56
                    </run-dependency>
56
                    </run-dependency>
57
                </dependency>
57
                </dependency>
58
                <dependency>
58
                <dependency>
(-)a/profiler.freeform/src/org/netbeans/modules/profiler/freeform/Bundle.properties (-2 lines)
Lines 54-58 Link Here
54
FreeFormProjectTypeProfiler_CreateNewTargetMsg=Please note that you have to create a <b>new</b> target for profiling, similar to the \"run\" target.<br>Click Help to get information about how to write the profiling targets.
54
FreeFormProjectTypeProfiler_CreateNewTargetMsg=Please note that you have to create a <b>new</b> target for profiling, similar to the \"run\" target.<br>Click Help to get information about how to write the profiling targets.
55
FreeFormProjectTypeProfiler_TargetBoxAccessName=List of targets defined in project build script.
55
FreeFormProjectTypeProfiler_TargetBoxAccessName=List of targets defined in project build script.
56
FreeFormProjectTypeProfiler_TargetBoxAccessDescr=Select target that will be used for profiling.
56
FreeFormProjectTypeProfiler_TargetBoxAccessDescr=Select target that will be used for profiling.
57
58
Util_ParseErrorMsg=Parse error in {0}\: {1}
(-)a/profiler.freeform/src/org/netbeans/modules/profiler/freeform/FreeFormProjectTypeProfiler.java (-3 / +5 lines)
Lines 66-71 Link Here
66
import java.util.List;
66
import java.util.List;
67
import java.util.Properties;
67
import java.util.Properties;
68
import javax.swing.*;
68
import javax.swing.*;
69
import org.apache.tools.ant.module.api.support.AntScriptUtils;
69
import org.netbeans.modules.profiler.projectsupport.utilities.SourceUtils;
70
import org.netbeans.modules.profiler.projectsupport.utilities.SourceUtils;
70
import org.netbeans.modules.profiler.utils.ProjectUtilities;
71
import org.netbeans.modules.profiler.utils.ProjectUtilities;
71
import org.openide.util.HelpCtx;
72
import org.openide.util.HelpCtx;
Lines 338-346 Link Here
338
    private String selectProfilingTarget(final Project project, final FileObject buildScript, final int type,
339
    private String selectProfilingTarget(final Project project, final FileObject buildScript, final int type,
339
            final String currentTarget) {
340
            final String currentTarget) {
340
        final List targets = Util.getAntScriptTargets(buildScript);
341
        final List targets = Util.getAntScriptTargets(buildScript);
341
        final List l = Util.getAntScriptTargetNames(buildScript);
342
        final List l;
342
343
        try {
343
        if (l == null) {
344
            l = AntScriptUtils.getCallableTargetNames(buildScript);
345
        } catch (IOException x) {
344
            Profiler.getDefault().displayError(MessageFormat.format(ERROR_PARSING_BUILDFILE_MSG,
346
            Profiler.getDefault().displayError(MessageFormat.format(ERROR_PARSING_BUILDFILE_MSG,
345
                    new Object[]{ProjectUtils.getInformation(project).getName()                    }));
347
                    new Object[]{ProjectUtils.getInformation(project).getName()                    }));
346
348
(-)a/profiler.freeform/src/org/netbeans/modules/profiler/freeform/Util.java (-188 / +3 lines)
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
}

Return to bug 136597