001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.project.ui.actions;
043:
044: import java.io.IOException;
045: import org.netbeans.api.project.Project;
046: import org.netbeans.spi.project.AuxiliaryConfiguration;
047: import org.netbeans.spi.project.ProjectFactory;
048: import org.netbeans.spi.project.ProjectState;
049: import org.openide.filesystems.FileObject;
050: import org.openide.util.Lookup;
051: import org.openide.util.lookup.Lookups;
052: import org.openide.util.lookup.ProxyLookup;
053: import org.openide.xml.XMLUtil;
054: import org.w3c.dom.Document;
055: import org.w3c.dom.Element;
056: import org.w3c.dom.Node;
057: import org.w3c.dom.NodeList;
058:
059: /**
060: * Help set up org.netbeans.api.project.*Test.
061: * @author Jesse Glick
062: */
063: public final class TestSupport {
064:
065: public static FileObject createTestProject(FileObject workDir,
066: String name) throws IOException {
067: FileObject p = workDir.createFolder(name);
068: p.createFolder("testproject");
069: return p;
070: }
071:
072: public static void notifyDeleted(Project p) {
073: ((TestProject) p).state.notifyDeleted();
074: }
075:
076: /**
077: * A testing project factory which recognizes directories containing
078: * a subdirectory called "testproject".
079: * If that subdirectory contains a file named "broken" then loading the project
080: * will fail with an IOException.
081: */
082: public static final class TestProjectFactory implements
083: ProjectFactory {
084:
085: public TestProjectFactory() {
086: }
087:
088: public Project loadProject(FileObject projectDirectory,
089: ProjectState state) throws IOException {
090: FileObject testproject = projectDirectory
091: .getFileObject("testproject");
092: if (testproject != null && testproject.isFolder()) {
093: return new TestProject(projectDirectory, state);
094: } else {
095: return null;
096: }
097: }
098:
099: public void saveProject(Project project) throws IOException,
100: ClassCastException {
101: TestProject p = (TestProject) project;
102: Throwable t = p.error;
103: if (t != null) {
104: p.error = null;
105: if (t instanceof IOException) {
106: throw (IOException) t;
107: } else if (t instanceof Error) {
108: throw (Error) t;
109: } else {
110: throw (RuntimeException) t;
111: }
112: }
113: }
114:
115: public boolean isProject(FileObject dir) {
116: FileObject testproject = dir.getFileObject("testproject");
117: return testproject != null && testproject.isFolder();
118: }
119:
120: }
121:
122: public static final class TestProject implements Project {
123:
124: private Lookup lookup;
125: private final FileObject dir;
126: final ProjectState state;
127: Throwable error;
128: int saveCount = 0;
129:
130: public TestProject(FileObject dir, ProjectState state) {
131: this .dir = dir;
132: this .state = state;
133: }
134:
135: public void setLookup(Lookup lookup) {
136: this .lookup = lookup;
137: }
138:
139: public Lookup getLookup() {
140: if (lookup == null) {
141: return Lookup.EMPTY;
142: } else {
143: return lookup;
144: }
145: }
146:
147: public FileObject getProjectDirectory() {
148: return dir;
149: }
150:
151: public String toString() {
152: return "testproject:" + getProjectDirectory().getNameExt();
153: }
154:
155: }
156:
157: public static class ChangeableLookup extends ProxyLookup {
158:
159: public ChangeableLookup(Object... objects) {
160: super (new Lookup[] { Lookups.fixed(objects) });
161: }
162:
163: public void change(Object... objects) {
164: setLookups(new Lookup[] { Lookups.fixed(objects) });
165: }
166:
167: }
168:
169: public static AuxiliaryConfiguration createAuxiliaryConfiguration() {
170: return new MemoryAuxiliaryConfiguration();
171: }
172:
173: private static class MemoryAuxiliaryConfiguration implements
174: AuxiliaryConfiguration {
175: private Document xml = XMLUtil
176: .createDocument(
177: "private",
178: "http://www.netbeans.org/ns/test-support-project-private/1",
179: null, null);
180:
181: public Element getConfigurationFragment(String elementName,
182: String namespace, boolean shared) {
183: if (shared) {
184: assert false : "Shared not implemented";
185: }
186: Element root = xml.getDocumentElement();
187: Element data = findElement(root, elementName, namespace);
188: if (data != null) {
189: return (Element) data.cloneNode(true);
190: } else {
191: //return xml.createElementNS (namespace, elementName);
192: return null;
193: }
194: }
195:
196: public void putConfigurationFragment(Element fragment,
197: boolean shared) throws IllegalArgumentException {
198: if (shared) {
199: assert false : "Shared not implemented";
200: }
201:
202: Element root = xml.getDocumentElement();
203: Element existing = findElement(root, fragment
204: .getLocalName(), fragment.getNamespaceURI());
205: // XXX first compare to existing and return if the same
206: if (existing != null) {
207: root.removeChild(existing);
208: }
209: // the children are alphabetize: find correct place to insert new node
210: Node ref = null;
211: NodeList list = root.getChildNodes();
212: for (int i = 0; i < list.getLength(); i++) {
213: Node node = list.item(i);
214: if (node.getNodeType() != Node.ELEMENT_NODE) {
215: continue;
216: }
217: int comparison = node.getNodeName().compareTo(
218: fragment.getNodeName());
219: if (comparison == 0) {
220: comparison = node.getNamespaceURI().compareTo(
221: fragment.getNamespaceURI());
222: }
223: if (comparison > 0) {
224: ref = node;
225: break;
226: }
227: }
228: root.insertBefore(root.getOwnerDocument().importNode(
229: fragment, true), ref);
230: }
231:
232: public boolean removeConfigurationFragment(String elementName,
233: String namespace, boolean shared)
234: throws IllegalArgumentException {
235: if (shared) {
236: assert false : "Shared not implemented";
237: }
238:
239: Element root = xml.getDocumentElement();
240: Element data = findElement(root, elementName, namespace);
241: if (data != null) {
242: root.removeChild(data);
243: return true;
244: } else {
245: return false;
246: }
247: }
248: }
249:
250: // copied from org.netbeans.modules.project.ant.Util
251: private static Element findElement(Element parent, String name,
252: String namespace) {
253: Element result = null;
254: NodeList l = parent.getChildNodes();
255: int len = l.getLength();
256: for (int i = 0; i < len; i++) {
257: if (l.item(i).getNodeType() == Node.ELEMENT_NODE) {
258: Element el = (Element) l.item(i);
259: if (name.equals(el.getLocalName())
260: && namespace.equals(el.getNamespaceURI())) {
261: if (result == null) {
262: result = el;
263: } else {
264: return null;
265: }
266: }
267: }
268: }
269: return result;
270: }
271: }
|