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: /*
043: * Date: January 6, 2002 5:16 PM
044: *
045: * @author lm97939
046: */
047: package org.netbeans.xtest;
048:
049: import org.apache.tools.ant.*;
050: import org.apache.tools.ant.types.*;
051: import org.xml.sax.*;
052: import org.w3c.dom.*;
053: import javax.xml.parsers.*;
054: import java.io.File;
055: import java.util.*;
056: import java.io.IOException;
057: import java.io.BufferedInputStream;
058: import java.io.FileInputStream;
059: import org.netbeans.xtest.util.XMLFactoryUtil;
060:
061: /**
062: * This is a scanner of DOM tree.
063: */
064: public class NbTestConfig extends Task {
065:
066: private File file;
067: private String config;
068:
069: private Hashtable allSetups = new Hashtable();
070: private Vector cfg = new Vector();
071: private String config_setup = null;
072: //private Hashtable props = new Hashtable();
073:
074: private static MConfig mconfig;
075:
076: public static MConfig getMConfig() {
077: return mconfig;
078: }
079:
080: public void setFile(File f) {
081: file = f;
082: }
083:
084: public void setConfig(String c) {
085: config = c;
086: }
087:
088: public void execute() throws BuildException {
089: if (file == null)
090: throw new BuildException("Attribute 'file' is empty.");
091: if (config == null)
092: throw new BuildException("Attribute 'config' is empty.");
093:
094: Document document = null;
095: try {
096: DocumentBuilder db = XMLFactoryUtil.newDocumentBuilder();
097: db.setEntityResolver(new XTestEntityResolver());
098: document = db.parse(file);
099: } catch (SAXException saxe) {
100: throw new BuildException("Exception during parsing "
101: + file.getAbsolutePath() + ": " + saxe);
102: } catch (ParserConfigurationException pce) {
103: throw new BuildException("Exception during parsing "
104: + file.getAbsolutePath() + ": " + pce);
105: } catch (IOException ioe) {
106: throw new BuildException("Exception during parsing "
107: + file.getAbsolutePath() + ": " + ioe);
108: }
109:
110: org.w3c.dom.Element element = document.getDocumentElement();
111: if ((element != null)
112: && element.getTagName().equals("testconfig")) {
113: visitElement_testconfig(element);
114: } else
115: throw new BuildException("Element testconfig expected.");
116: }
117:
118: /** Scan through org.w3c.dom.Element named testconfig. */
119: void visitElement_testconfig(org.w3c.dom.Element element) { // <testconfig>
120: // element.getValue();
121: org.w3c.dom.NodeList nodes = element.getChildNodes();
122: for (int i = 0; i < nodes.getLength(); i++) {
123: org.w3c.dom.Node node = nodes.item(i);
124: if (node.getNodeType() == Node.ELEMENT_NODE) {
125: org.w3c.dom.Element nodeElement = (org.w3c.dom.Element) node;
126: if (nodeElement.getNodeName().equals("setup"))
127: visitElement_setup(allSetups, nodeElement);
128: }
129: }
130:
131: org.w3c.dom.Element rightConfig = null;
132: for (int i = 0; i < nodes.getLength(); i++) {
133: org.w3c.dom.Node node = nodes.item(i);
134: if (node.getNodeType() == Node.ELEMENT_NODE) {
135: org.w3c.dom.Element nodeElement = (org.w3c.dom.Element) node;
136: if (nodeElement.getNodeName().equals("config")) {
137: org.w3c.dom.Attr attr = nodeElement
138: .getAttributeNode("name");
139: if (attr == null)
140: throw new BuildException(
141: "Element 'config' has to have attribute 'name'.");
142: if (attr.getValue().equals(config)) {
143: if (rightConfig != null)
144: throw new BuildException(
145: "More than one config with name "
146: + config + " found.");
147: rightConfig = nodeElement;
148: }
149: }
150: }
151: }
152: if (rightConfig == null)
153: throw new BuildException("Config name " + config
154: + " not found.");
155: visitElement_config(rightConfig);
156:
157: mconfig = new MConfig(cfg);
158: if (config_setup != null) {
159: MConfig.Setup config_setup_object = (MConfig.Setup) allSetups
160: .get(config_setup);
161: if (config_setup_object == null)
162: throw new BuildException("Setup with name "
163: + config_setup + " not found.");
164: mconfig.setConfigSetup(config_setup_object);
165: }
166: }
167:
168: /** Scan through org.w3c.dom.Element named setup. */
169: void visitElement_setup(Hashtable allSetups,
170: org.w3c.dom.Element element) { // <setup>
171: // element.getValue();
172: org.w3c.dom.Attr attr = element.getAttributeNode("name");
173: if (attr == null)
174: throw new BuildException(
175: "Element 'setup' has to have attribute 'name'.");
176:
177: String name = attr.getValue();
178:
179: org.w3c.dom.Element start_node = null;
180: org.w3c.dom.Element stop_node = null;
181:
182: org.w3c.dom.NodeList nodes = element.getChildNodes();
183: for (int i = 0; i < nodes.getLength(); i++) {
184: org.w3c.dom.Node node = nodes.item(i);
185: if (node.getNodeType() == Node.ELEMENT_NODE) {
186: org.w3c.dom.Element nodeElement = (org.w3c.dom.Element) node;
187: if (nodeElement.getNodeName().equals("start")) {
188: if (start_node != null)
189: throw new BuildException(
190: "More than one start element found.");
191: start_node = nodeElement;
192: }
193: if (nodeElement.getNodeName().equals("stop")) {
194: if (stop_node != null)
195: throw new BuildException(
196: "More than one stop element found.");
197: stop_node = nodeElement;
198: }
199: }
200: }
201:
202: MConfig.Setup.StartStop start = null, stop = null;
203: if (start_node != null)
204: start = visitElement_start(start_node);
205: if (stop_node != null)
206: stop = visitElement_start(stop_node);
207:
208: MConfig.Setup setup = new MConfig.Setup();
209: setup.setName(name);
210: setup.setStart(start);
211: setup.setStop(stop);
212: allSetups.put(name, setup);
213: }
214:
215: /** Scan through org.w3c.dom.Element named start. */
216: MConfig.Setup.StartStop visitElement_start(
217: org.w3c.dom.Element element) { // <start>
218: // element.getValue();
219:
220: MConfig.Setup.StartStop ss = new MConfig.Setup.StartStop();
221: org.w3c.dom.NamedNodeMap attrs = element.getAttributes();
222: for (int i = 0; i < attrs.getLength(); i++) {
223: org.w3c.dom.Attr attr = (org.w3c.dom.Attr) attrs.item(i);
224: if (attr.getName().equals("dir")) { // <start dir="???">
225: ss.dir = getProject().resolveFile(
226: PropertyHelper.getPropertyHelper(getProject())
227: .replaceProperties("", attr.getValue(),
228: getProject().getProperties()));
229: }
230: if (attr.getName().equals("target")) { // <start target="???">
231: ss.target = PropertyHelper.getPropertyHelper(
232: getProject()).replaceProperties("",
233: attr.getValue(), getProject().getProperties());
234: }
235: if (attr.getName().equals("antfile")) { // <start antfile="???">
236: ss.antfile = PropertyHelper.getPropertyHelper(
237: getProject()).replaceProperties("",
238: attr.getValue(), getProject().getProperties());
239: }
240: if (attr.getName().equals("onBackground")) { // <start onBackground="???">
241: String ob = PropertyHelper.getPropertyHelper(
242: getProject()).replaceProperties("",
243: attr.getValue(), getProject().getProperties());
244: if (ob.equalsIgnoreCase("true")
245: || ob.equalsIgnoreCase("yes") || ob.equals("1"))
246: ss.onBackground = true;
247: else if (ob.equalsIgnoreCase("false")
248: || ob.equalsIgnoreCase("no") || ob.equals("0"))
249: ss.onBackground = false;
250: else
251: throw new BuildException(
252: "Unknown value of attribute onBackground: "
253: + ob);
254: }
255: if (attr.getName().equals("delay")) { // <start delay="???">
256: String d = PropertyHelper.getPropertyHelper(
257: getProject()).replaceProperties("",
258: attr.getValue(), getProject().getProperties());
259: int delay = 0;
260: try {
261: delay = Integer.parseInt(d);
262: ss.delay = delay;
263: } catch (NumberFormatException nfe) {
264: throw new BuildException(
265: "Attribute delay has bad number", nfe);
266: }
267: }
268: }
269: return ss;
270: }
271:
272: /** Scan through org.w3c.dom.Element named config. */
273: void visitElement_config(org.w3c.dom.Element element) { // <config>
274: String name = null;
275: String default_testtypes = null;
276: String default_attributes = null;
277:
278: Hashtable config_properties = new Hashtable();
279:
280: // element.getValue();
281: org.w3c.dom.NamedNodeMap attrs = element.getAttributes();
282: for (int i = 0; i < attrs.getLength(); i++) {
283: org.w3c.dom.Attr attr = (org.w3c.dom.Attr) attrs.item(i);
284: if (attr.getName().equals("name")) { // <config name="???">
285: name = attr.getValue();
286: } else if (attr.getName().equals("defaultAttributes")) { // <config attributes="???">
287: default_attributes = attr.getValue();
288: } else if (attr.getName().equals("defaultTesttypes")) { // <config testtypes="???">
289: default_testtypes = attr.getValue();
290: } else if (attr.getName().equals("setup")) { // <config setup="???">
291: config_setup = attr.getValue();
292: } else {
293: throw new BuildException("Unexpected attribute '"
294: + attr.getName() + "'.");
295: }
296: }
297:
298: if (name == null)
299: throw new BuildException("Attribute 'name' has to be set.");
300:
301: visitAllProperties(config_properties, element);
302:
303: MConfig.TestGroup group;
304:
305: org.w3c.dom.NodeList nodes = element.getChildNodes();
306: for (int i = 0; i < nodes.getLength(); i++) {
307: org.w3c.dom.Node node = nodes.item(i);
308: if (node.getNodeType() == Node.ELEMENT_NODE) {
309: org.w3c.dom.Element nodeElement = (org.w3c.dom.Element) node;
310: if (nodeElement.getNodeName().equals("module")) {
311: group = visitElement_module(nodeElement,
312: default_testtypes, default_attributes);
313: group.addProperties(config_properties);
314: cfg.add(group);
315: }
316: if (nodeElement.getNodeName().equals("testtype")) {
317: group = visitElement_testtype(nodeElement,
318: default_attributes);
319: group.addProperties(config_properties);
320: cfg.add(group);
321: }
322:
323: }
324: }
325: }
326:
327: /** Scan through org.w3c.dom.Element named module. */
328: MConfig.TestGroup visitElement_module(org.w3c.dom.Element element,
329: String default_testtypes, String default_attributes) { // <module>
330: // element.getValue();
331:
332: String name = null;
333: String testtypes = default_testtypes;
334: String attributes = default_attributes;
335: String setup = null;
336:
337: MConfig.TestGroup group = new MConfig.TestGroup();
338:
339: org.w3c.dom.NamedNodeMap attrs = element.getAttributes();
340: for (int i = 0; i < attrs.getLength(); i++) {
341: org.w3c.dom.Attr attr = (org.w3c.dom.Attr) attrs.item(i);
342: if (attr.getName().equals("attributes")) { // <module attributes="???">
343: attributes = attr.getValue();
344: } else if (attr.getName().equals("name")) { // <module name="???">
345: name = attr.getValue();
346: } else if (attr.getName().equals("testtypes")) { // <module testtypes="???">
347: testtypes = attr.getValue();
348: } else if (attr.getName().equals("setup")) { // <module setup="???">
349: setup = attr.getValue();
350: } else {
351: throw new BuildException("Unexpected attribute '"
352: + attr.getName() + "'.");
353: }
354: }
355:
356: if (name == null || testtypes == null || attributes == null)
357: throw new BuildException(
358: "Element 'module' has to have attriutes 'name', 'testtypes' and 'attributes'.");
359:
360: Hashtable module_props = new Hashtable();
361:
362: visitAllProperties(module_props, element);
363:
364: if (setup != null) {
365: MConfig.Setup module_setup = (MConfig.Setup) allSetups
366: .get(setup);
367: if (module_setup == null)
368: throw new BuildException("Setup with name " + setup
369: + " not found.");
370: group.setSetup(module_setup);
371: }
372: group.setProperties(module_props);
373: StringTokenizer testtypes_tokens = new StringTokenizer(
374: testtypes, ",");
375: while (testtypes_tokens.hasMoreTokens()) {
376: String testtype = testtypes_tokens.nextToken().trim();
377: updateTable(group, name, testtype, attributes);
378: }
379:
380: return group;
381: }
382:
383: /** Scan through org.w3c.dom.Element named testtype. */
384: MConfig.TestGroup visitElement_testtype(
385: org.w3c.dom.Element element, String default_attributes) { // <testtype>
386: // element.getValue();
387:
388: String name = null;
389: String modules = null;
390: String attributes = default_attributes;
391: String setup = null;
392:
393: MConfig.TestGroup group = new MConfig.TestGroup();
394:
395: org.w3c.dom.NamedNodeMap attrs = element.getAttributes();
396: for (int i = 0; i < attrs.getLength(); i++) {
397: org.w3c.dom.Attr attr = (org.w3c.dom.Attr) attrs.item(i);
398: if (attr.getName().equals("attributes")) { // <testtype attributes="???">
399: attributes = attr.getValue();
400: } else if (attr.getName().equals("modules")) { // <testtype modules="???">
401: modules = attr.getValue();
402: } else if (attr.getName().equals("name")) { // <testtype name="???">
403: name = attr.getValue();
404: } else if (attr.getName().equals("setup")) { // <testtype setup="???">
405: setup = attr.getValue();
406: } else {
407: throw new BuildException("Unexpected attribute '"
408: + attr.getName() + "'.");
409: }
410: }
411:
412: if (name == null || modules == null || attributes == null)
413: throw new BuildException(
414: "Element 'module' has to have attriutes 'name', 'modules' and 'attributes'.");
415:
416: Hashtable group_props = new Hashtable();
417:
418: visitAllProperties(group_props, element);
419:
420: if (setup != null) {
421: MConfig.Setup group_setup = (MConfig.Setup) allSetups
422: .get(setup);
423: group.setSetup(group_setup);
424: }
425: group.setProperties(group_props);
426: StringTokenizer modules_tokens = new StringTokenizer(modules,
427: ",");
428: while (modules_tokens.hasMoreTokens()) {
429: String module = modules_tokens.nextToken().trim();
430: updateTable(group, module, name, attributes);
431: }
432:
433: return group;
434: }
435:
436: void visitAllProperties(Hashtable table, Element element) {
437: org.w3c.dom.NodeList nodes = element.getChildNodes();
438: for (int i = 0; i < nodes.getLength(); i++) {
439: org.w3c.dom.Node node = nodes.item(i);
440: if (node.getNodeType() == Node.ELEMENT_NODE) {
441: org.w3c.dom.Element nodeElement = (org.w3c.dom.Element) node;
442: if (nodeElement.getNodeName().equals("property"))
443: visitElement_property(table, nodeElement);
444: }
445: }
446: }
447:
448: /** Scan through org.w3c.dom.Element named property. */
449: void visitElement_property(Hashtable properties,
450: org.w3c.dom.Element element) { // <property>
451: // element.getValue();
452:
453: String name = null;
454: String value = null;
455: String file = null;
456:
457: org.w3c.dom.NamedNodeMap attrs = element.getAttributes();
458: for (int i = 0; i < attrs.getLength(); i++) {
459: org.w3c.dom.Attr attr = (org.w3c.dom.Attr) attrs.item(i);
460: if (attr.getName().equals("file")) { // <property file="???">
461: file = PropertyHelper.getPropertyHelper(getProject())
462: .replaceProperties("", attr.getValue(),
463: getProject().getProperties());
464: } else if (attr.getName().equals("name")) { // <property name="???">
465: name = PropertyHelper.getPropertyHelper(getProject())
466: .replaceProperties("", attr.getValue(),
467: getProject().getProperties());
468: } else if (attr.getName().equals("value")) { // <property value="???">
469: value = PropertyHelper.getPropertyHelper(getProject())
470: .replaceProperties("", attr.getValue(),
471: getProject().getProperties());
472: } else {
473: throw new BuildException("Unexpected attribute '"
474: + attr.getName() + "'.");
475: }
476: }
477:
478: if (name == null && file == null)
479: throw new BuildException(
480: "Either 'name' or 'file' attribute is empty.",
481: getLocation());
482: if (name != null && value == null)
483: throw new BuildException("Attribute 'value' is empty.",
484: getLocation());
485: if (value != null && file != null)
486: throw new BuildException(
487: "Attributes 'file' and 'value' can't be defined together.",
488: getLocation());
489:
490: if (name != null)
491: properties.put(name, value);
492: if (file != null)
493: convertProperties(properties, file);
494:
495: }
496:
497: private void convertProperties(Hashtable table, String filename) {
498: try {
499: File file = getProject().resolveFile(filename);
500: if (!file.exists())
501: throw new BuildException("Property file "
502: + file.getAbsolutePath() + " not found.");
503: java.util.Properties javaprop = new java.util.Properties();
504: BufferedInputStream bis = new BufferedInputStream(
505: new FileInputStream(file));
506: javaprop.load(bis);
507: bis.close();
508: Enumeration en = javaprop.propertyNames();
509: while (en.hasMoreElements()) {
510: String name2 = (String) en.nextElement();
511: table.put(name2, javaprop.getProperty(name2));
512: }
513: } catch (IOException e) {
514: throw new BuildException(e);
515: }
516: }
517:
518: private void updateTable(MConfig.TestGroup group, String module,
519: String testtype, String attributes) {
520: MConfig.Test test = new MConfig.Test(module, testtype);
521: StringTokenizer ta = new StringTokenizer(attributes, ",");
522: String[] attrs = new String[ta.countTokens()];
523: int j = 0;
524: while (ta.hasMoreTokens()) {
525: String atr = ta.nextToken().trim();
526: attrs[j++] = atr;
527: }
528: test.setAttributes(attrs);
529: group.addTest(test);
530: }
531: }
|