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 136333
Collapse All | Expand All

(-)a/projectapi/src/org/netbeans/api/project/ProjectUtils.java (+19 lines)
Lines 49-54 Link Here
49
import java.util.Set;
49
import java.util.Set;
50
import javax.swing.Icon;
50
import javax.swing.Icon;
51
import javax.swing.ImageIcon;
51
import javax.swing.ImageIcon;
52
import org.netbeans.modules.projectapi.AuxiliaryConfigImpl;
53
import org.netbeans.spi.project.AuxiliaryConfiguration;
52
import org.netbeans.spi.project.SubprojectProvider;
54
import org.netbeans.spi.project.SubprojectProvider;
53
import org.netbeans.spi.project.support.GenericSources;
55
import org.netbeans.spi.project.support.GenericSources;
54
import org.openide.filesystems.FileStateInvalidException;
56
import org.openide.filesystems.FileStateInvalidException;
Lines 215-218 Link Here
215
        
217
        
216
    }
218
    }
217
    
219
    
220
    /**
221
     * Find a way of storing extra configuration in a project.
222
     * If the project's {@linkplain Project#getLookup lookup} does not provide an instance,
223
     * a fallback implementation using {@linkplain org.openide.filesystems.FileObject#setAttribute file attributes} is used.
224
     * @param project a project
225
     * @return an auxiliary configuration handle
226
     * @since XXX
227
     */
228
    public static AuxiliaryConfiguration getAuxiliaryConfiguration(Project project) {
229
        AuxiliaryConfiguration ac = project.getLookup().lookup(AuxiliaryConfiguration.class);
230
        if (ac != null) {
231
            return ac;
232
        } else {
233
            return new AuxiliaryConfigImpl(project);
234
        }
235
    }
236
218
}
237
}
(-)a/projectapi/src/org/netbeans/modules/projectapi/AuxiliaryConfigImpl.java (+185 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.netbeans.modules.projectapi;
41
42
import java.io.ByteArrayOutputStream;
43
import java.io.IOException;
44
import java.io.StringReader;
45
import org.netbeans.api.project.Project;
46
import org.netbeans.api.project.ProjectManager;
47
import org.netbeans.spi.project.AuxiliaryConfiguration;
48
import org.openide.util.Exceptions;
49
import org.openide.util.Mutex;
50
import org.openide.xml.XMLUtil;
51
import org.w3c.dom.Document;
52
import org.w3c.dom.Element;
53
import org.w3c.dom.Node;
54
import org.w3c.dom.NodeList;
55
import org.xml.sax.InputSource;
56
import org.xml.sax.SAXException;
57
58
/**
59
 * implementation of AuxiliaryConfiguration that relies on FileObject's attributes
60
 * @author mkleint
61
 */
62
public class AuxiliaryConfigImpl implements AuxiliaryConfiguration {
63
64
    private static final String AUX_CONFIG_PRIVATE = "AuxilaryConfigurationPrivate"; //NOI18N
65
    private static final String AUX_CONFIG_SHARED = "AuxilaryConfigurationShared"; //NOI18N
66
    private final Project project;
67
68
    public AuxiliaryConfigImpl(Project proj) {
69
        this.project = proj;
70
    }
71
72
    public Element getConfigurationFragment(final String elementName, final String namespace, final boolean shared) {
73
        return ProjectManager.mutex().readAccess(new Mutex.Action<Element>() {
74
            public Element run() {
75
                String str = (String)project.getProjectDirectory().getAttribute(shared ? AUX_CONFIG_SHARED : AUX_CONFIG_PRIVATE);
76
                if (str != null) {
77
                    Document doc;
78
                    try {
79
                        doc = XMLUtil.parse(new InputSource(new StringReader(str)), false, true, null, null);
80
                        return findElement(doc.getDocumentElement(), elementName, namespace);
81
                    } catch (SAXException ex) {
82
                        Exceptions.printStackTrace(ex);
83
                    } catch (IOException ex) {
84
                        Exceptions.printStackTrace(ex);
85
                    }
86
                }
87
                return null;
88
            }});
89
    }
90
91
    public void putConfigurationFragment(final Element fragment, final boolean shared) throws IllegalArgumentException {
92
        ProjectManager.mutex().writeAccess(new Mutex.Action<Void>() {
93
            public Void run() {
94
                String attr = shared ? AUX_CONFIG_SHARED : AUX_CONFIG_PRIVATE;
95
                String str = (String) project.getProjectDirectory().getAttribute(attr);
96
                Document doc = null;
97
                if (str != null) {
98
                    try {
99
                        doc = XMLUtil.parse(new InputSource(new StringReader(str)), false, true, null, null);
100
                    } catch (SAXException ex) {
101
                        ex.printStackTrace();
102
                    } catch (IOException ex) {
103
                        ex.printStackTrace();
104
                    }
105
                } else {
106
                    String element = "configuration"; // NOI18N
107
                    doc = XMLUtil.createDocument(element, null, null, null);
108
                }
109
                if (doc != null) {
110
                    Element el = findElement(doc.getDocumentElement(), fragment.getNodeName(), fragment.getNamespaceURI());
111
                    if (el != null) {
112
                        doc.getDocumentElement().removeChild(el);
113
                    }
114
                    doc.getDocumentElement().appendChild(doc.importNode(fragment, true));
115
                }
116
117
                try {
118
                    ByteArrayOutputStream wr = new ByteArrayOutputStream();
119
                    XMLUtil.write(doc, wr, "UTF-8");
120
                    project.getProjectDirectory().setAttribute(attr, wr.toString("UTF-8"));
121
                } catch (IOException ex) {
122
                    Exceptions.printStackTrace(ex);
123
                }
124
                return null;
125
            }
126
        });
127
128
    }
129
130
    public boolean removeConfigurationFragment(final String elementName, final String namespace, final boolean shared) throws IllegalArgumentException {
131
        return ProjectManager.mutex().writeAccess(new Mutex.Action<Boolean>() {
132
            public Boolean run() {
133
                String attr = shared ? AUX_CONFIG_SHARED : AUX_CONFIG_PRIVATE;
134
                String str = (String) project.getProjectDirectory().getAttribute(attr);
135
                Document doc = null;
136
                if (str != null) {
137
                    try {
138
                        doc = XMLUtil.parse(new InputSource(new StringReader(str)), false, true, null, null);
139
                    } catch (SAXException ex) {
140
                        Exceptions.printStackTrace(ex);
141
                    } catch (IOException ex) {
142
                        Exceptions.printStackTrace(ex);
143
                    }
144
                } else {
145
                    return false;
146
                }
147
                if (doc != null) {
148
                    Element el = findElement(doc.getDocumentElement(), elementName, namespace);
149
                    if (el != null) {
150
                        doc.getDocumentElement().removeChild(el);
151
                    }
152
                }
153
                try {
154
                    ByteArrayOutputStream wr = new ByteArrayOutputStream();
155
                    XMLUtil.write(doc, wr, "UTF-8");
156
                    project.getProjectDirectory().setAttribute(attr, wr.toString("UTF-8"));
157
                } catch (IOException ex) {
158
                    Exceptions.printStackTrace(ex);
159
                }
160
                return true;
161
            }
162
        });
163
    }
164
165
166
    private static Element findElement(Element parent, String name, String namespace) {
167
        Element result = null;
168
        NodeList l = parent.getChildNodes();
169
        int len = l.getLength();
170
        for (int i = 0; i < len; i++) {
171
            if (l.item(i).getNodeType() == Node.ELEMENT_NODE) {
172
                Element el = (Element)l.item(i);
173
                if (name.equals(el.getLocalName()) && namespace.equals(el.getNamespaceURI())) {
174
                    if (result == null) {
175
                        result = el;
176
                    } else {
177
                        return null;
178
                    }
179
                }
180
            }
181
        }
182
        return result;
183
    }
184
185
}
(-)a/projectapi/test/unit/src/org/netbeans/modules/projectapi/AuxiliaryConfigImplTest.java (+96 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.netbeans.modules.projectapi;
41
42
import javax.xml.parsers.DocumentBuilderFactory;
43
import org.netbeans.api.project.Project;
44
import org.netbeans.api.project.ProjectUtils;
45
import org.netbeans.junit.NbTestCase;
46
import org.netbeans.spi.project.AuxiliaryConfiguration;
47
import org.openide.filesystems.FileObject;
48
import org.openide.filesystems.FileUtil;
49
import org.openide.util.Lookup;
50
import org.w3c.dom.Document;
51
import org.w3c.dom.Element;
52
53
public class AuxiliaryConfigImplTest extends NbTestCase {
54
55
    public AuxiliaryConfigImplTest(String name) {
56
        super(name);
57
    }
58
59
    public void testFallbackAuxiliaryConfiguration() throws Exception {
60
        final FileObject prjdir = FileUtil.createMemoryFileSystem().getRoot();
61
        class Prj implements Project {
62
            public FileObject getProjectDirectory() {
63
                return prjdir;
64
            }
65
            public Lookup getLookup() {
66
                return Lookup.EMPTY;
67
            }
68
        }
69
        Project prj = new Prj();
70
        AuxiliaryConfiguration ac = ProjectUtils.getAuxiliaryConfiguration(prj);
71
        assertNotNull(ac);
72
        String namespace = "http://nowhere.net/test";
73
        assertNull(ac.getConfigurationFragment("x", namespace, true));
74
        ac.putConfigurationFragment(makeElement("x", namespace), true);
75
        Element e = ac.getConfigurationFragment("x", namespace, true);
76
        assertNotNull(e);
77
        assertEquals("x", e.getLocalName());
78
        assertEquals(namespace, e.getNamespaceURI());
79
        assertNull(ac.getConfigurationFragment("x", namespace, false));
80
        ac.removeConfigurationFragment("x", namespace, true);
81
        assertNull(ac.getConfigurationFragment("x", namespace, true));
82
        ac.putConfigurationFragment(makeElement("y", namespace), false);
83
        prj = new Prj();
84
        ac = ProjectUtils.getAuxiliaryConfiguration(prj);
85
        e = ac.getConfigurationFragment("y", namespace, false);
86
        assertNotNull(e);
87
        assertEquals("y", e.getLocalName());
88
        assertEquals(namespace, e.getNamespaceURI());
89
    }
90
91
    private static Element makeElement(String name, String namespace) throws Exception {
92
        Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
93
        return doc.createElementNS(namespace, name);
94
    }
95
96
}

Return to bug 136333