1 23 24 package org.enhydra.xml.xmlc.commands.options; 25 26 import java.io.IOException ; 27 import java.io.Reader ; 28 import java.io.StreamTokenizer ; 29 import java.util.Vector ; 30 31 import org.enhydra.xml.io.InputSourceOps; 32 import org.enhydra.xml.xmlc.XMLCException; 33 import org.xml.sax.InputSource ; 34 35 39 class OptionFileParser { 40 private Vector entries = new Vector (); 42 43 48 private boolean parseLine(StreamTokenizer in) throws IOException { 49 in.nextToken(); 50 if (in.ttype == StreamTokenizer.TT_EOF) { 51 return false; } 53 54 if (in.ttype == StreamTokenizer.TT_EOL) { 56 return true; } 58 if (in.sval.startsWith("#")) { 59 do { 61 in.nextToken(); 62 } while (!((in.ttype == StreamTokenizer.TT_EOF) 63 || (in.ttype == StreamTokenizer.TT_EOL))); 64 return true; 65 } 66 67 Vector tokens = new Vector (); 69 do { 70 tokens.addElement(in.sval); 71 in.nextToken(); 72 } while (!((in.ttype == StreamTokenizer.TT_EOF) 73 || (in.ttype == StreamTokenizer.TT_EOL))); 74 if (tokens.size() >= 0) { 75 String [] optEntry = new String [tokens.size()]; 76 tokens.copyInto(optEntry); 77 entries.addElement(optEntry); 78 } 79 return true; 80 } 81 82 85 private void parse(Reader reader) throws IOException { 86 StreamTokenizer in = new StreamTokenizer (reader); 87 in.resetSyntax(); 88 in.eolIsSignificant(true); 89 in.whitespaceChars('\u0000', '\u0020'); 90 in.wordChars('\u0021', '\uffff'); 91 in.quoteChar('"'); 92 in.quoteChar('\''); 93 94 while (parseLine(in)) { 95 continue; 96 } 97 } 98 99 102 public OptionFileParser(Reader reader) 103 throws XMLCException, IOException { 104 parse(reader); 105 } 106 107 110 public OptionFileParser(InputSource inputSource) 111 throws XMLCException { 112 try { 113 Reader reader = InputSourceOps.open(inputSource); 114 try { 115 parse(reader); 116 } finally { 117 InputSourceOps.closeIfOpened(inputSource, reader); 118 } 119 } catch (IOException except) { 120 throw new XMLCException("parse of option failed" + inputSource, 121 except); 122 } 123 } 124 125 131 public String [][] getOptions() { 132 String [][] opts = new String [entries.size()][]; 133 entries.copyInto(opts); 134 return opts; 135 } 136 } 137 | Popular Tags |