// Date: 4/25/02 // Author: Paul Robertson, Copyright (c) DOLL, Inc. // File: XMLStream.java // Description: XML Basic stream I/O support // Stream for reading and writing XML package mit16_410_13; import java.util.Vector; import java.io.InputStream; import java.io.PrintStream; import java.io.EOFException; import java.io.IOException; import java.io.FileInputStream; public class XMLStream { InputStream readfrom; PrintStream writeto; int ch; // one character lookahead public XMLStream (InputStream istrm) { readfrom=istrm; try { ch=istrm.read(); } catch (IOException e) { System.out.println("Can't read from XML input stream."); System.exit(1); } writeto=null; } public XMLStream (PrintStream ostrm) { writeto=ostrm; readfrom=null; } public XMLStream (InputStream istrm, PrintStream ostrm) { readfrom=istrm; try { ch=istrm.read(); } catch (IOException e) { System.out.println("Can't read from XML input stream."); System.exit(1); } writeto=ostrm; } public InputStream getInputStream () { return readfrom; } public PrintStream getPrintStream () { return writeto; } void readnextcharacter () throws IOException, EOFException { ch=readfrom.read(); if (ch==-1) throw new EOFException(); } XMLAttribute readNextAttribute () throws IOException { String attributestr=new String(); String valuestr=new String(); boolean eofreached=false; while (ch==' ') readnextcharacter(); // skip over whitespace try { while ((ch!=' ')&&(ch!='=')) { attributestr=attributestr+(char)ch; readnextcharacter(); } } catch (EOFException e) { eofreached=true; } if (!eofreached) { while (ch==' ') readnextcharacter(); // skip over whitespace if (ch=='=') readnextcharacter(); while (ch==' ') readnextcharacter(); // skip over whitespace if (ch=='\"') readnextcharacter(); try { while (ch!='\"') { valuestr=valuestr+(char)ch; readnextcharacter(); } } catch (EOFException e) { eofreached=true; } readnextcharacter(); return new XMLAttribute(attributestr, valuestr); } return null; } public XMLTag readNextTag () throws IOException { String tagname=new String(); XMLTag result; // Skip over any leading non tag characters try { while (ch!='<') readnextcharacter(); int pos=0; readnextcharacter(); while ((ch!=' ')&&(ch!='>')) { pos++; tagname=tagname+(char)ch; readnextcharacter(); } result=new XMLTag(tagname); while (ch==' ') readnextcharacter(); // skip over whitespace while (ch!='>') { XMLAttribute anattribute=readNextAttribute(); result.addAttribute(anattribute); } if (ch=='>') readnextcharacter(); if (ch=='\r') readnextcharacter(); if (ch=='\n') readnextcharacter(); } catch (EOFException e) { return null; } return result; } public String readText () throws IOException { String flattext=new String(); int pos=0; try { while (ch!='<') { pos++; flattext=flattext+(char)ch; readnextcharacter(); } } catch (EOFException e) { } if (pos==0) return null; return flattext; } } // Fin