001: /**
002: * JTestGen.java
003: * TestGen4J is licensed under Open Software License 2.1
004: * For details, please refer to:
005: * http://www.opensource.org/licenses/osl-2.1.php
006: */package com.spikesource.spiketestgen;
007:
008: import org.w3c.dom.Document;
009: import org.w3c.dom.NodeList;
010: import org.w3c.dom.Element;
011: import org.xml.sax.SAXException;
012: import org.xml.sax.SAXParseException;
013:
014: //import java.io.File;
015: import java.io.InputStream;
016:
017: import java.util.Properties;
018:
019: import javax.xml.parsers.DocumentBuilder;
020: import javax.xml.parsers.DocumentBuilderFactory;
021:
022: /**
023: * Parses the rules XML file and retrives the boundary
024: * conditions of a data type. Currently it supports
025: * only primitive data types.
026: *
027: * @version 0.1.4-alpha
028: * @author Manish Marathe
029: */
030: public class RulesEngine {
031: /**
032: * Factory variable.
033: */
034: private static Document doc = null;
035:
036: /**
037: * The function uses DocumentBuilderFactory parser to
038: * parse the rules file and get boundary conditions of
039: * the data type.
040: *
041: * @param type
042: * The data type in String form.
043: * @return
044: * Returns boundary conditions of the data
045: * type as String, with each condition as
046: * a token separated by a comma.
047: */
048: public final String getConditions(final String type) {
049: String conditions = null;
050:
051: try {
052: if (doc == null) {
053: DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
054: .newInstance();
055: DocumentBuilder docBuilder = docBuilderFactory
056: .newDocumentBuilder();
057: Properties p = new Properties(System.getProperties());
058:
059: /*File file = new File("src" + p.getProperty("file.separator") +
060: "com" + p.getProperty("file.separator") +
061: "spikesource" + p.getProperty("file.separator") +
062: "spiketestgen", "rules.xml");*/
063:
064: InputStream is = ClassLoader
065: .getSystemResourceAsStream("src"
066: + p.getProperty("file.separator")
067: + "com"
068: + p.getProperty("file.separator")
069: + "spikesource"
070: + p.getProperty("file.separator")
071: + "spiketestgen"
072: + p.getProperty("file.separator")
073: + "rules.xml");
074:
075: doc = docBuilder.parse(is);
076:
077: // normalize text representation
078: doc.getDocumentElement().normalize();
079: }
080:
081: NodeList t = doc.getElementsByTagName("DataType");
082:
083: for (int j = 0; j < t.getLength(); j++) {
084: if (t.item(j).getAttributes().getNamedItem("name")
085: .getNodeValue().equals(type)) {
086: Element d = (Element) t.item(j);
087: NodeList n = d.getElementsByTagName("Case");
088:
089: for (int k = 0; k < n.getLength(); k++) {
090: Element r = (Element) n.item(k);
091:
092: if (k == 0) {
093: conditions = r.getAttribute("value");
094: } else if (k > 0) {
095: conditions = conditions + ','
096: + r.getAttribute("value");
097: }
098: }
099: }
100: }
101: } catch (SAXParseException err) {
102: System.out.println("** Parsing error" + ", line "
103: + err.getLineNumber() + ", uri "
104: + err.getSystemId());
105: System.out.println(" " + err.getMessage());
106: } catch (SAXException e) {
107: Exception x = e.getException();
108: if (x == null) {
109: e.printStackTrace();
110: } else {
111: x.printStackTrace();
112: }
113: } catch (Throwable t) {
114: t.printStackTrace();
115: }
116:
117: return conditions;
118: }
119: }
|