Source Code Cross Referenced for TestUtil.java in  » IDE-Netbeans » ant » org » netbeans » modules » ant » grammar » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » IDE Netbeans » ant » org.netbeans.modules.ant.grammar 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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.ant.grammar;
043:
044:        import java.io.File;
045:        import java.io.StringReader;
046:        import java.lang.reflect.InvocationHandler;
047:        import java.lang.reflect.Method;
048:        import java.lang.reflect.Proxy;
049:        import java.util.ArrayList;
050:        import java.util.Enumeration;
051:        import java.util.HashSet;
052:        import java.util.List;
053:        import java.util.Set;
054:        import org.netbeans.junit.MockServices;
055:        import org.netbeans.modules.xml.api.model.GrammarResult;
056:        import org.netbeans.modules.xml.api.model.HintContext;
057:        import org.openide.modules.InstalledFileLocator;
058:        import org.openide.xml.XMLUtil;
059:        import org.w3c.dom.Attr;
060:        import org.w3c.dom.Document;
061:        import org.w3c.dom.Element;
062:        import org.w3c.dom.NamedNodeMap;
063:        import org.w3c.dom.Node;
064:        import org.w3c.dom.NodeList;
065:        import org.w3c.dom.Text;
066:        import org.xml.sax.InputSource;
067:
068:        /**
069:         * Helpers for AntGrammarTest.
070:         * @author Jesse Glick
071:         */
072:        final class TestUtil {
073:
074:            private TestUtil() {
075:            }
076:
077:            static {
078:                MockServices.setServices(IFL.class);
079:            }
080:
081:            public static final class IFL extends InstalledFileLocator {
082:                public File locate(String name, String module, boolean loc) {
083:                    String antHomeS = System.getProperty("test.ant.home");
084:                    if (antHomeS == null) {
085:                        throw new Error(
086:                                "Tests will not run unless test.ant.home and test.ant.bridge system properties are defined");
087:                    }
088:                    final File antHome = new File(antHomeS);
089:                    final File antBridge = new File(System
090:                            .getProperty("test.ant.bridge"));
091:                    final File antJar = new File(new File(antHome, "lib"),
092:                            "ant.jar");
093:                    if (name.equals("ant")) {
094:                        return antHome;
095:                    } else if (name.equals("ant/nblib/bridge.jar")) {
096:                        return antBridge;
097:                    } else if (name.equals("ant/nblib")) {
098:                        return antBridge.getParentFile();
099:                    } else if (name.equals("ant/lib/ant.jar")) {
100:                        return antJar;
101:                    } else {
102:                        return null;
103:                    }
104:                }
105:            }
106:
107:            private static HintContext createHintContext(final Node n,
108:                    final String prefix) {
109:                Set<Class> interfaces = new HashSet<Class>();
110:                findAllInterfaces(n.getClass(), interfaces);
111:                interfaces.add(HintContext.class);
112:                class Handler implements  InvocationHandler {
113:                    public Object invoke(Object proxy, Method method,
114:                            Object[] args) throws Throwable {
115:                        if (method.getDeclaringClass()
116:                                .equals(HintContext.class)) {
117:                            assert method.getName().equals("getCurrentPrefix");
118:                            return prefix;
119:                        } else {
120:                            return method.invoke(n, args);
121:                        }
122:                    }
123:                }
124:                return (HintContext) Proxy.newProxyInstance(TestUtil.class
125:                        .getClassLoader(), interfaces
126:                        .toArray(new Class[interfaces.size()]), new Handler());
127:            }
128:
129:            static void findAllInterfaces(Class c, Set<Class> interfaces) {
130:                if (c.isInterface()) {
131:                    interfaces.add(c);
132:                }
133:                Class s = c.getSuperclass();
134:                if (s != null) {
135:                    findAllInterfaces(s, interfaces);
136:                }
137:                Class[] is = c.getInterfaces();
138:                for (int i = 0; i < is.length; i++) {
139:                    findAllInterfaces(is[i], interfaces);
140:                }
141:            }
142:
143:            /**
144:             * Create a context for completing some XML.
145:             * The XML text must be a well-formed document.
146:             * It must contain exactly one element name, attribute name,
147:             * attribute value, or text node ending in the string <samp>HERE</samp>.
148:             * The context will be that node (Element, Attribute, or Text) with
149:             * the suffix stripped off and the prefix set to the text preceding that suffix.
150:             */
151:            public static HintContext createCompletion(String xml)
152:                    throws Exception {
153:                Document doc = XMLUtil.parse(new InputSource(new StringReader(
154:                        xml)), false, true, null, null);
155:                return findCompletion(doc.getDocumentElement(), doc);
156:            }
157:
158:            private static HintContext findCompletion(Node n, Document doc) {
159:                switch (n.getNodeType()) {
160:                case Node.ELEMENT_NODE:
161:                    Element el = (Element) n;
162:                    String name = el.getTagName();
163:                    if (name.endsWith("HERE")) {
164:                        String prefix = name.substring(0, name.length() - 4);
165:                        Node nue = doc.createElementNS(el.getNamespaceURI(),
166:                                prefix);
167:                        NodeList nl = el.getChildNodes();
168:                        while (nl.getLength() > 0) {
169:                            nue.appendChild(nl.item(0));
170:                        }
171:                        el.getParentNode().replaceChild(nue, el);
172:                        return createHintContext(nue, prefix);
173:                    }
174:                    break;
175:                case Node.TEXT_NODE:
176:                    Text text = (Text) n;
177:                    String contents = text.getNodeValue();
178:                    if (contents.endsWith("HERE")) {
179:                        String prefix = contents.substring(0,
180:                                contents.length() - 4);
181:                        text.setNodeValue(prefix);
182:                        return createHintContext(text, prefix);
183:                    }
184:                    break;
185:                case Node.ATTRIBUTE_NODE:
186:                    Attr attr = (Attr) n;
187:                    name = attr.getName();
188:                    if (name.endsWith("HERE")) {
189:                        String prefix = name.substring(0, name.length() - 4);
190:                        Attr nue = doc.createAttributeNS(
191:                                attr.getNamespaceURI(), prefix);
192:                        Element owner = attr.getOwnerElement();
193:                        owner.removeAttributeNode(attr);
194:                        owner.setAttributeNodeNS(nue);
195:                        return createHintContext(nue, prefix);
196:                    } else {
197:                        String value = attr.getNodeValue();
198:                        if (value.endsWith("HERE")) {
199:                            String prefix = value.substring(0,
200:                                    value.length() - 4);
201:                            attr.setNodeValue(prefix);
202:                            return createHintContext(attr, prefix);
203:                        }
204:                    }
205:                    break;
206:                default:
207:                    // ignore
208:                    break;
209:                }
210:                // Didn't find it, check children.
211:                NodeList nl = n.getChildNodes();
212:                for (int i = 0; i < nl.getLength(); i++) {
213:                    HintContext c = findCompletion(nl.item(i), doc);
214:                    if (c != null) {
215:                        return c;
216:                    }
217:                }
218:                // Element's attr nodes are listed separately.
219:                NamedNodeMap nnm = n.getAttributes();
220:                if (nnm != null) {
221:                    for (int i = 0; i < nnm.getLength(); i++) {
222:                        HintContext c = findCompletion(nnm.item(i), doc);
223:                        if (c != null) {
224:                            return c;
225:                        }
226:                    }
227:                }
228:                // Nope.
229:                return null;
230:            }
231:
232:            /**
233:             * Get a particular element in a test XML document.
234:             * Pass in a well-formed XML document and an element name to search for.
235:             * Must be exactly one such.
236:             */
237:            public static Element createElementInDocument(String xml,
238:                    String elementName, String elementNamespace)
239:                    throws Exception {
240:                Document doc = XMLUtil.parse(new InputSource(new StringReader(
241:                        xml)), false, true, null, null);
242:                NodeList nl = doc.getElementsByTagNameNS(elementNamespace,
243:                        elementName);
244:                if (nl.getLength() != 1) {
245:                    throw new IllegalArgumentException(
246:                            "Zero or more than one <" + elementName
247:                                    + ">s in \"" + xml + "\"");
248:                }
249:                return (Element) nl.item(0);
250:            }
251:
252:            /**
253:             * Given a list of XML nodes returned in GrammarResult's, return a list of their names.
254:             * For elements, you get the name; for attributes, the name;
255:             * for text nodes, the value.
256:             * (No namespaces returned.)
257:             */
258:            public static List<String> grammarResultValues(
259:                    Enumeration<GrammarResult> e) {
260:                List<String> l = new ArrayList<String>();
261:                while (e.hasMoreElements()) {
262:                    l.add(e.nextElement().toString());
263:                }
264:                return l;
265:            }
266:
267:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.