1 19 20 package org.netbeans.modules.project.ui.actions; 21 22 import java.io.IOException ; 23 import org.netbeans.api.project.Project; 24 import org.netbeans.spi.project.AuxiliaryConfiguration; 25 import org.netbeans.spi.project.ProjectFactory; 26 import org.netbeans.spi.project.ProjectState; 27 import org.openide.filesystems.FileObject; 28 import org.openide.util.Lookup; 29 import org.openide.util.lookup.Lookups; 30 import org.openide.util.lookup.ProxyLookup; 31 import org.openide.xml.XMLUtil; 32 import org.w3c.dom.Document ; 33 import org.w3c.dom.Element ; 34 import org.w3c.dom.Node ; 35 import org.w3c.dom.NodeList ; 36 37 41 public final class TestSupport { 42 43 public static FileObject createTestProject( FileObject workDir, String name ) throws IOException { 44 FileObject p = workDir.createFolder( name ); 45 p.createFolder( "testproject" ); 46 return p; 47 } 48 49 public static void notifyDeleted(Project p) { 50 ((TestProject) p).state.notifyDeleted(); 51 } 52 53 59 public static final class TestProjectFactory implements ProjectFactory { 60 61 public TestProjectFactory() {} 62 63 public Project loadProject(FileObject projectDirectory, ProjectState state) throws IOException { 64 FileObject testproject = projectDirectory.getFileObject("testproject"); 65 if (testproject != null && testproject.isFolder()) { 66 return new TestProject(projectDirectory, state); 67 } 68 else { 69 return null; 70 } 71 } 72 73 public void saveProject(Project project) throws IOException , ClassCastException { 74 TestProject p = (TestProject)project; 75 Throwable t = p.error; 76 if (t != null) { 77 p.error = null; 78 if (t instanceof IOException ) { 79 throw (IOException )t; 80 } else if (t instanceof Error ) { 81 throw (Error )t; 82 } else { 83 throw (RuntimeException )t; 84 } 85 } 86 } 87 88 public boolean isProject(FileObject dir) { 89 FileObject testproject = dir.getFileObject("testproject"); 90 return testproject != null && testproject.isFolder(); 91 } 92 93 } 94 95 public static final class TestProject implements Project { 96 97 private Lookup lookup; 98 private final FileObject dir; 99 final ProjectState state; 100 Throwable error; 101 int saveCount = 0; 102 103 public TestProject(FileObject dir, ProjectState state) { 104 this.dir = dir; 105 this.state = state; 106 } 107 108 public void setLookup( Lookup lookup ) { 109 this.lookup = lookup; 110 } 111 112 public Lookup getLookup() { 113 if ( lookup == null ) { 114 return Lookup.EMPTY; 115 } 116 else { 117 return lookup; 118 } 119 } 120 121 public FileObject getProjectDirectory() { 122 return dir; 123 } 124 125 public String toString() { 126 return "testproject:" + getProjectDirectory().getNameExt(); 127 } 128 129 } 130 131 public static class ChangeableLookup extends ProxyLookup { 132 133 public ChangeableLookup(Object ... objects) { 134 super( new Lookup[] { Lookups.fixed( objects ) } ); 135 } 136 137 public void change(Object ... objects) { 138 setLookups( new Lookup[] { Lookups.fixed( objects ) } ); 139 } 140 141 } 142 143 public static AuxiliaryConfiguration createAuxiliaryConfiguration () { 144 return new MemoryAuxiliaryConfiguration (); 145 } 146 147 private static class MemoryAuxiliaryConfiguration implements AuxiliaryConfiguration { 148 private Document xml = XMLUtil.createDocument ("private", "http://www.netbeans.org/ns/test-support-project-private/1", null, null); 149 150 public Element getConfigurationFragment (String elementName, String namespace, boolean shared) { 151 if (shared) { 152 assert false : "Shared not implemented"; 153 } 154 Element root = xml.getDocumentElement (); 155 Element data = findElement (root, elementName, namespace); 156 if (data != null) { 157 return (Element ) data.cloneNode (true); 158 } else { 159 return null; 161 } 162 } 163 164 public void putConfigurationFragment(Element fragment, boolean shared) throws IllegalArgumentException { 165 if (shared) { 166 assert false : "Shared not implemented"; 167 } 168 169 Element root = xml.getDocumentElement (); 170 Element existing = findElement (root, fragment.getLocalName (), fragment.getNamespaceURI ()); 171 if (existing != null) { 173 root.removeChild (existing); 174 } 175 Node ref = null; 177 NodeList list = root.getChildNodes (); 178 for (int i=0; i<list.getLength (); i++) { 179 Node node = list.item (i); 180 if (node.getNodeType () != Node.ELEMENT_NODE) { 181 continue; 182 } 183 int comparison = node.getNodeName ().compareTo (fragment.getNodeName ()); 184 if (comparison == 0) { 185 comparison = node.getNamespaceURI ().compareTo (fragment.getNamespaceURI ()); 186 } 187 if (comparison > 0) { 188 ref = node; 189 break; 190 } 191 } 192 root.insertBefore (root.getOwnerDocument ().importNode (fragment, true), ref); 193 } 194 195 public boolean removeConfigurationFragment (String elementName, String namespace, boolean shared) throws IllegalArgumentException { 196 if (shared) { 197 assert false : "Shared not implemented"; 198 } 199 200 Element root = xml.getDocumentElement (); 201 Element data = findElement (root, elementName, namespace); 202 if (data != null) { 203 root.removeChild (data); 204 return true; 205 } else { 206 return false; 207 } 208 } 209 } 210 211 private static Element findElement(Element parent, String name, String namespace) { 213 Element result = null; 214 NodeList l = parent.getChildNodes(); 215 int len = l.getLength(); 216 for (int i = 0; i < len; i++) { 217 if (l.item(i).getNodeType() == Node.ELEMENT_NODE) { 218 Element el = (Element )l.item(i); 219 if (name.equals(el.getLocalName()) && namespace.equals(el.getNamespaceURI())) { 220 if (result == null) { 221 result = el; 222 } else { 223 return null; 224 } 225 } 226 } 227 } 228 return result; 229 } 230 } 231 | Popular Tags |