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 |
} |