The Java Program: ParseTestCases.java

  1 import java.io.File;
  2 import java.io.IOException;
  3 import java.util.ArrayList;
  4 import java.util.Iterator;
  5 import java.util.List;
  6 
  7 import javax.xml.parsers.ParserConfigurationException;
  8 import javax.xml.parsers.SAXParser;
  9 import javax.xml.parsers.SAXParserFactory;
 10 
 11 import org.xml.sax.Attributes;
 12 import org.xml.sax.SAXException;
 13 import org.xml.sax.helpers.DefaultHandler;
 14 
 15 /*
 16 <?xml version="1.0" encoding="UTF-8"?>
 17   <!DOCTYPE TESTS[
 18     <!ELEMENT TESTS (CASES*)>
 19     <!ELEMENT CASE (STDIN?,ARGUMENT*)>
 20     <!ELEMENT ARGUMENT (#PCDATA)>
 21     <!ATTLIST CASE NAME CDATA #REQUIRED>
 22     <!ATTLIST STDIN NAME CDATA #REQUIRED>
 23   ]>
 24 <tests>
 25   <case name="change-000">
 26     <stdin name="change-000.inp" />
 27     <argument>10234</argument>
 28     <argument>asdf</argument>
 29     <argument>as! adf</argument>
 30   </case>
 31   <case name="change-001">
 32     <stdin name="change-001.inp" />
 33     <argument>
 34     70234
 35     </argument>
 36   </case>
 37 </tests>
 38 
 39  */
 40 
 41 public class ParseTestCases extends DefaultHandler {
 42 
 43    final static String  fmt = System.getProperty ("format", "%1$s: %3$s < %2$s");
 44 
 45    static class TestCase {
 46       public String name;
 47       public String stdin = "/dev/null";
 48       public List<String> args = new ArrayList<String>();
 49       public String toString () {
 50          return String.format (fmt, name, stdin, inter());
 51       }
 52 
 53       private String inter () {
 54          return inter ("'");
 55       }
 56 
 57       private String inter (String x) {
 58          return inter (x, x, " ");
 59       }
 60       private String inter (String start, String end) {
 61          return inter (start, end, " ");
 62       }
 63       private String inter (String start, String end, String in) {
 64          if (args.size()==0) return "";
 65          StringBuilder sb = new StringBuilder ();
 66          sb.append (String.format ("%s%s%s", start, args.get(0), end));
 67          for (int i=1; i<args.size(); i++) {
 68             sb.append (String.format ("%s%s%s%s", in, start, args.get(i), end));
 69          }
 70          return sb.toString();
 71       }
 72    }
 73 
 74    List<TestCase> tests = new ArrayList<TestCase>();
 75         
 76    public void runExample (String file) {
 77       parseDocument (file);
 78       printData ();
 79    }
 80 
 81    private void parseDocument(String file) {
 82                 
 83       //get a factory
 84       SAXParserFactory spf = SAXParserFactory.newInstance();
 85       try {
 86                 
 87          //get a new instance of parser
 88          SAXParser sp = spf.newSAXParser();
 89                         
 90          //parse the file and also register this class for call backs
 91          sp.parse (file, this);
 92                         
 93       } catch (SAXException se) {
 94          se.printStackTrace();
 95       }catch (ParserConfigurationException pce) {
 96          pce.printStackTrace();
 97       }catch (IOException ie) {
 98          ie.printStackTrace();
 99       }
100    }
101 
102    // Print all the accumulated data
103    private void printData() {
104       for (TestCase tc: tests) System.out.println (tc);
105    }
106 
107    // Accumulate data
108    private TestCase temp;
109    private StringBuilder data = new StringBuilder ();
110          
111 
112    //Event Handlers
113    public void startElement (String uri, String localName, String qName, Attributes attributes) throws SAXException {
114       if (qName.equalsIgnoreCase("case")) {
115          temp = new TestCase();
116          temp.name = attributes.getValue("name"); 
117       } else if (qName.equalsIgnoreCase("stdin")) {
118          temp.stdin = attributes.getValue("name"); 
119       } else if (qName.equalsIgnoreCase("argument")) {
120          data.setLength(0);
121       }
122    }
123         
124    public void characters(char[] ch, int start, int length) throws SAXException {
125       data.append (new String(ch,start,length));
126    }
127         
128    public void endElement(String uri, String localName, String qName) throws SAXException {
129       if(qName.equalsIgnoreCase("case")) {
130          tests.add (temp);
131       } else if (qName.equalsIgnoreCase("argument")) {
132          temp.args.add (data.toString().trim());
133          data.setLength(0);
134       }
135    }
136         
137    public static void main (String[] args) {
138       main (args[0]);
139    }
140 
141    public static void main (String arg) {
142       new ParseTestCases().runExample(arg);
143    }
144 }