RiverRat  - An MIT iCampus project http://web.mit.edu/riverrat/

Main Page | Software Documentation | Hardware Documentation | People | Contact | Wiki

Main Page | Class Hierarchy | Class List | File List | Class Members

XMLFakeServer.java

00001 package riverrat.testing;
00002 
00003 import org.xml.sax.*;
00004 import org.xml.sax.helpers.*;
00005 
00010 public class XMLFakeServer extends FakeServer {
00012         protected static String file = "race.xml";
00013         
00015         protected boolean DELAY_INPUT = true;
00016         
00018         public XMLFakeServer() {
00019                 super();
00020         }
00021         
00023         public XMLFakeServer(int newport) {
00024                 super(newport);
00025         }
00026         
00030         public void mainServer() {
00031                 openFile(file, DELAY_INPUT);
00032                 
00033                 //initialize parser
00034                 DefaultHandler defaulthandler = (DefaultHandler) this;
00035                 XMLReader parser = null;
00036                 try { //instantiate parser
00037                         parser = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
00038                 } catch (Exception e) {
00039                         System.err.println("Error: Unable to instantiate parser.");
00040                 }
00041                 
00042                 //set parser features
00043                 try {
00044                         parser.setFeature("http://xml.org/sax/features/namespaces",true);
00045                         parser.setFeature("http://xml.org/sax/features/validation",false);
00046                         parser.setFeature("http://apache.org/xml/features/validation/schema",false);
00047                         parser.setFeature("http://apache.org/xml/features/validation/schema-full-checking",false);
00048                 } catch (SAXException e) {
00049                         System.err.println("warning: Parser doesn't support features... "+e);
00050                 }
00051                 
00052                 parser.setContentHandler(defaulthandler);
00053                 //tells the XML parser to use the defaulthandler (the BasicServer object) to handle callbacks
00054                 parser.setErrorHandler(defaulthandler);
00055                 //tells the XML parser to use the defaulthandler for errors
00056 
00057                 //parse, using callbacks in this class
00058                 InputSource source = new InputSource(fileInputStream);
00059                 source.setSystemId(""+filename);
00060                 if(source == null) {
00061                         System.err.println("InputSource is null!");
00062                         System.exit(1);
00063                 }
00064                 try {
00065                         parser.parse(source);
00066                 } catch(NullPointerException e) {
00067                         System.err.println("NullPointerException trying to parse. This probably means that the ContentHandler class is null.");
00068                 } catch(Exception e) {
00069                         System.err.println("Parse error.");
00070                 }
00071         }
00072         
00079         public static void main(String args[]) {
00080                 if(args.length != 0) {
00081                         //get filename from 1st argument
00082                         filename = args[0];
00083                 }
00084                 XMLFakeServer testserver = new XMLFakeServer();
00085                 Thread serverthread = new Thread(testserver);
00086                 serverthread.start();
00087                 
00088                 while(true) {
00089                         System.out.println("This is the main loop.");
00090                         try {
00091                                 Thread.sleep(10000);
00092                         } catch(InterruptedException e) {
00093                                 System.err.println("Interrupted.");
00094                         }
00095                 }
00096         }
00097 }
 

Brought to you by the RiverRat team.