KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ant > grammar > TestUtil


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.ant.grammar;
21
22 import java.io.File JavaDoc;
23 import java.io.StringReader JavaDoc;
24 import java.lang.reflect.InvocationHandler JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
26 import java.lang.reflect.Proxy JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Set JavaDoc;
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 JavaDoc;
37 import org.w3c.dom.Document JavaDoc;
38 import org.w3c.dom.Element JavaDoc;
39 import org.w3c.dom.NamedNodeMap JavaDoc;
40 import org.w3c.dom.Node JavaDoc;
41 import org.w3c.dom.NodeList JavaDoc;
42 import org.w3c.dom.Text JavaDoc;
43 import org.xml.sax.InputSource JavaDoc;
44
45 /**
46  * Helpers for AntGrammarTest.
47  * @author Jesse Glick
48  */

49 final class TestUtil {
50     
51     private TestUtil() {}
52     
53     static {
54         MockServices.setServices(new Class JavaDoc[] {IFL.class});
55     }
56     
57     public static final class IFL extends InstalledFileLocator {
58         public File JavaDoc locate(String JavaDoc name, String JavaDoc module, boolean loc) {
59             String JavaDoc antHomeS = System.getProperty("test.ant.home");
60             if (antHomeS == null) {
61                 throw new Error JavaDoc("Tests will not run unless test.ant.home and test.ant.bridge system properties are defined");
62             }
63             final File JavaDoc antHome = new File JavaDoc(antHomeS);
64             final File JavaDoc antBridge = new File JavaDoc(System.getProperty("test.ant.bridge"));
65             final File JavaDoc antJar = new File JavaDoc(new File JavaDoc(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 JavaDoc n, final String JavaDoc prefix) {
81         Set JavaDoc/*<Class>*/ interfaces = new HashSet JavaDoc();
82         findAllInterfaces(n.getClass(), interfaces);
83         interfaces.add(HintContext.class);
84         class Handler implements InvocationHandler JavaDoc {
85             public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) throws Throwable JavaDoc {
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 JavaDoc[])interfaces.toArray(new Class JavaDoc[interfaces.size()]), new Handler JavaDoc());
96     }
97     
98     static void findAllInterfaces(Class JavaDoc c, Set JavaDoc/*<Class>*/ interfaces) {
99         if (c.isInterface()) {
100             interfaces.add(c);
101         }
102         Class JavaDoc s = c.getSuperclass();
103         if (s != null) {
104             findAllInterfaces(s, interfaces);
105         }
106         Class JavaDoc[] is = c.getInterfaces();
107         for (int i = 0; i < is.length; i++) {
108             findAllInterfaces(is[i], interfaces);
109         }
110     }
111     
112     /**
113      * Create a context for completing some XML.
114      * The XML text must be a well-formed document.
115      * It must contain exactly one element name, attribute name,
116      * attribute value, or text node ending in the string <samp>HERE</samp>.
117      * The context will be that node (Element, Attribute, or Text) with
118      * the suffix stripped off and the prefix set to the text preceding that suffix.
119      */

120     public static HintContext createCompletion(String JavaDoc xml) throws Exception JavaDoc {
121         Document JavaDoc doc = XMLUtil.parse(new InputSource JavaDoc(new StringReader JavaDoc(xml)), false, true, null, null);
122         return findCompletion(doc.getDocumentElement(), doc);
123     }
124     
125     private static HintContext findCompletion(Node JavaDoc n, Document JavaDoc doc) {
126         switch (n.getNodeType()) {
127             case Node.ELEMENT_NODE:
128                 Element JavaDoc el = (Element JavaDoc)n;
129                 String JavaDoc name = el.getTagName();
130                 if (name.endsWith("HERE")) {
131                     String JavaDoc prefix = name.substring(0, name.length() - 4);
132                     Node JavaDoc nue = doc.createElementNS(el.getNamespaceURI(), prefix);
133                     NodeList JavaDoc 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 JavaDoc text = (Text JavaDoc)n;
143                 String JavaDoc contents = text.getNodeValue();
144                 if (contents.endsWith("HERE")) {
145                     String JavaDoc 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 JavaDoc attr = (Attr JavaDoc)n;
152                 name = attr.getName();
153                 if (name.endsWith("HERE")) {
154                     String JavaDoc prefix = name.substring(0, name.length() - 4);
155                     Attr JavaDoc nue = doc.createAttributeNS(attr.getNamespaceURI(), prefix);
156                     Element JavaDoc owner = attr.getOwnerElement();
157                     owner.removeAttributeNode(attr);
158                     owner.setAttributeNodeNS(nue);
159                     return createHintContext(nue, prefix);
160                 } else {
161                     String JavaDoc value = attr.getNodeValue();
162                     if (value.endsWith("HERE")) {
163                         String JavaDoc prefix = value.substring(0, value.length() - 4);
164                         attr.setNodeValue(prefix);
165                         return createHintContext(attr, prefix);
166                     }
167                 }
168                 break;
169             default:
170                 // ignore
171
break;
172         }
173         // Didn't find it, check children.
174
NodeList JavaDoc 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         // Element's attr nodes are listed separately.
182
NamedNodeMap JavaDoc 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         // Nope.
192
return null;
193     }
194     
195     /**
196      * Get a particular element in a test XML document.
197      * Pass in a well-formed XML document and an element name to search for.
198      * Must be exactly one such.
199      */

200     public static Element JavaDoc createElementInDocument(String JavaDoc xml, String JavaDoc elementName, String JavaDoc elementNamespace) throws Exception JavaDoc {
201         Document JavaDoc doc = XMLUtil.parse(new InputSource JavaDoc(new StringReader JavaDoc(xml)), false, true, null, null);
202         NodeList JavaDoc nl = doc.getElementsByTagNameNS(elementNamespace, elementName);
203         if (nl.getLength() != 1) {
204             throw new IllegalArgumentException JavaDoc("Zero or more than one <" + elementName + ">s in \"" + xml + "\"");
205         }
206         return (Element JavaDoc)nl.item(0);
207     }
208     
209     /**
210      * Given a list of XML nodes returned in GrammarResult's, return a list of their names.
211      * For elements, you get the name; for attributes, the name;
212      * for text nodes, the value.
213      * (No namespaces returned.)
214      */

215     public static List JavaDoc/*<String>*/ grammarResultValues(Enumeration JavaDoc/*<Node>*/ e) {
216         List JavaDoc l = new ArrayList JavaDoc();
217         while (e.hasMoreElements()) {
218             Object JavaDoc o = e.nextElement();
219             String JavaDoc s;
220             if (o instanceof Element JavaDoc) {
221                 s = ((Element JavaDoc)o).getNodeName();
222             } else if (o instanceof Attr JavaDoc) {
223                 s = ((Attr JavaDoc)o).getName();
224             } else {
225                 s = ((Text JavaDoc)o).getData();
226             }
227             l.add(s);
228         }
229         return l;
230     }
231     
232 }
233
Popular Tags