1 19 20 package org.netbeans.modules.ant.grammar; 21 22 import java.io.File ; 23 import java.io.StringReader ; 24 import java.lang.reflect.InvocationHandler ; 25 import java.lang.reflect.Method ; 26 import java.lang.reflect.Proxy ; 27 import java.util.ArrayList ; 28 import java.util.Enumeration ; 29 import java.util.HashSet ; 30 import java.util.List ; 31 import java.util.Set ; 32 import org.netbeans.junit.MockServices; 33 import org.netbeans.modules.xml.api.model.HintContext; 34 import org.openide.modules.InstalledFileLocator; 35 import org.openide.xml.XMLUtil; 36 import org.w3c.dom.Attr ; 37 import org.w3c.dom.Document ; 38 import org.w3c.dom.Element ; 39 import org.w3c.dom.NamedNodeMap ; 40 import org.w3c.dom.Node ; 41 import org.w3c.dom.NodeList ; 42 import org.w3c.dom.Text ; 43 import org.xml.sax.InputSource ; 44 45 49 final class TestUtil { 50 51 private TestUtil() {} 52 53 static { 54 MockServices.setServices(new Class [] {IFL.class}); 55 } 56 57 public static final class IFL extends InstalledFileLocator { 58 public File locate(String name, String module, boolean loc) { 59 String antHomeS = System.getProperty("test.ant.home"); 60 if (antHomeS == null) { 61 throw new Error ("Tests will not run unless test.ant.home and test.ant.bridge system properties are defined"); 62 } 63 final File antHome = new File (antHomeS); 64 final File antBridge = new File (System.getProperty("test.ant.bridge")); 65 final File antJar = new File (new File (antHome, "lib"), "ant.jar"); 66 if (name.equals("ant")) { 67 return antHome; 68 } else if (name.equals("ant/nblib/bridge.jar")) { 69 return antBridge; 70 } else if (name.equals("ant/nblib")) { 71 return antBridge.getParentFile(); 72 } else if (name.equals("ant/lib/ant.jar")) { 73 return antJar; 74 } else { 75 return null; 76 } 77 } 78 } 79 80 private static HintContext createHintContext(final Node n, final String prefix) { 81 Set interfaces = new HashSet (); 82 findAllInterfaces(n.getClass(), interfaces); 83 interfaces.add(HintContext.class); 84 class Handler implements InvocationHandler { 85 public Object invoke(Object proxy, Method method, Object [] args) throws Throwable { 86 if (method.getDeclaringClass().equals(HintContext.class)) { 87 assert method.getName().equals("getCurrentPrefix"); 88 return prefix; 89 } else { 90 return method.invoke(n, args); 91 } 92 } 93 } 94 return (HintContext)Proxy.newProxyInstance(TestUtil.class.getClassLoader(), 95 (Class [])interfaces.toArray(new Class [interfaces.size()]), new Handler ()); 96 } 97 98 static void findAllInterfaces(Class c, Set interfaces) { 99 if (c.isInterface()) { 100 interfaces.add(c); 101 } 102 Class s = c.getSuperclass(); 103 if (s != null) { 104 findAllInterfaces(s, interfaces); 105 } 106 Class [] is = c.getInterfaces(); 107 for (int i = 0; i < is.length; i++) { 108 findAllInterfaces(is[i], interfaces); 109 } 110 } 111 112 120 public static HintContext createCompletion(String xml) throws Exception { 121 Document doc = XMLUtil.parse(new InputSource (new StringReader (xml)), false, true, null, null); 122 return findCompletion(doc.getDocumentElement(), doc); 123 } 124 125 private static HintContext findCompletion(Node n, Document doc) { 126 switch (n.getNodeType()) { 127 case Node.ELEMENT_NODE: 128 Element el = (Element )n; 129 String name = el.getTagName(); 130 if (name.endsWith("HERE")) { 131 String prefix = name.substring(0, name.length() - 4); 132 Node nue = doc.createElementNS(el.getNamespaceURI(), prefix); 133 NodeList nl = el.getChildNodes(); 134 while (nl.getLength() > 0) { 135 nue.appendChild(nl.item(0)); 136 } 137 el.getParentNode().replaceChild(nue, el); 138 return createHintContext(nue, prefix); 139 } 140 break; 141 case Node.TEXT_NODE: 142 Text text = (Text )n; 143 String contents = text.getNodeValue(); 144 if (contents.endsWith("HERE")) { 145 String prefix = contents.substring(0, contents.length() - 4); 146 text.setNodeValue(prefix); 147 return createHintContext(text, prefix); 148 } 149 break; 150 case Node.ATTRIBUTE_NODE: 151 Attr attr = (Attr )n; 152 name = attr.getName(); 153 if (name.endsWith("HERE")) { 154 String prefix = name.substring(0, name.length() - 4); 155 Attr nue = doc.createAttributeNS(attr.getNamespaceURI(), prefix); 156 Element owner = attr.getOwnerElement(); 157 owner.removeAttributeNode(attr); 158 owner.setAttributeNodeNS(nue); 159 return createHintContext(nue, prefix); 160 } else { 161 String value = attr.getNodeValue(); 162 if (value.endsWith("HERE")) { 163 String prefix = value.substring(0, value.length() - 4); 164 attr.setNodeValue(prefix); 165 return createHintContext(attr, prefix); 166 } 167 } 168 break; 169 default: 170 break; 172 } 173 NodeList nl = n.getChildNodes(); 175 for (int i = 0; i < nl.getLength(); i++) { 176 HintContext c = findCompletion(nl.item(i), doc); 177 if (c != null) { 178 return c; 179 } 180 } 181 NamedNodeMap nnm = n.getAttributes(); 183 if (nnm != null) { 184 for (int i = 0; i < nnm.getLength(); i++) { 185 HintContext c = findCompletion(nnm.item(i), doc); 186 if (c != null) { 187 return c; 188 } 189 } 190 } 191 return null; 193 } 194 195 200 public static Element createElementInDocument(String xml, String elementName, String elementNamespace) throws Exception { 201 Document doc = XMLUtil.parse(new InputSource (new StringReader (xml)), false, true, null, null); 202 NodeList nl = doc.getElementsByTagNameNS(elementNamespace, elementName); 203 if (nl.getLength() != 1) { 204 throw new IllegalArgumentException ("Zero or more than one <" + elementName + ">s in \"" + xml + "\""); 205 } 206 return (Element )nl.item(0); 207 } 208 209 215 public static List grammarResultValues(Enumeration e) { 216 List l = new ArrayList (); 217 while (e.hasMoreElements()) { 218 Object o = e.nextElement(); 219 String s; 220 if (o instanceof Element ) { 221 s = ((Element )o).getNodeName(); 222 } else if (o instanceof Attr ) { 223 s = ((Attr )o).getName(); 224 } else { 225 s = ((Text )o).getData(); 226 } 227 l.add(s); 228 } 229 return l; 230 } 231 232 } 233 | Popular Tags |