KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > xmlc > commands > options > OptionFileParser


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: OptionFileParser.java,v 1.2 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.xml.xmlc.commands.options;
25
26 import java.io.IOException JavaDoc;
27 import java.io.Reader JavaDoc;
28 import java.io.StreamTokenizer JavaDoc;
29 import java.util.Vector JavaDoc;
30
31 import org.enhydra.xml.io.InputSourceOps;
32 import org.enhydra.xml.xmlc.XMLCException;
33 import org.xml.sax.InputSource JavaDoc;
34
35 /**
36  * Parse an XMLC options file. This creates a vector of options and
37  * arguments. It does no other processing on options.
38  */

39 class OptionFileParser {
40     // Vector of entries in the file.
41
private Vector JavaDoc entries = new Vector JavaDoc();
42
43     /**
44      * Parse a line in the file.
45      *
46      * @return true if a line was parsed, false on EOF.
47      */

48     private boolean parseLine(StreamTokenizer JavaDoc in) throws IOException JavaDoc {
49         in.nextToken();
50         if (in.ttype == StreamTokenizer.TT_EOF) {
51             return false; // EOF
52
}
53
54         // Check for comment or blank line.
55
if (in.ttype == StreamTokenizer.TT_EOL) {
56             return true; // EOL
57
}
58         if (in.sval.startsWith("#")) {
59             // Eat comment line
60
do {
61                 in.nextToken();
62             } while (!((in.ttype == StreamTokenizer.TT_EOF)
63                      || (in.ttype == StreamTokenizer.TT_EOL)));
64             return true;
65         }
66         
67         // Parse option and arguments.
68
Vector JavaDoc tokens = new Vector JavaDoc();
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 JavaDoc[] optEntry = new String JavaDoc[tokens.size()];
76             tokens.copyInto(optEntry);
77             entries.addElement(optEntry);
78         }
79         return true;
80     }
81
82     /**
83      * Parse an open options file.
84      */

85     private void parse(Reader JavaDoc reader) throws IOException JavaDoc {
86         StreamTokenizer JavaDoc in = new StreamTokenizer JavaDoc(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     /**
100      * Construct, parsing contents of file.
101      */

102     public OptionFileParser(Reader JavaDoc reader)
103         throws XMLCException, IOException JavaDoc {
104         parse(reader);
105     }
106
107     /**
108      * Construct, parsing contents of file.
109      */

110     public OptionFileParser(InputSource JavaDoc inputSource)
111         throws XMLCException {
112         try {
113             Reader JavaDoc reader = InputSourceOps.open(inputSource);
114             try {
115                 parse(reader);
116             } finally {
117                 InputSourceOps.closeIfOpened(inputSource, reader);
118             }
119         } catch (IOException JavaDoc except) {
120             throw new XMLCException("parse of option failed" + inputSource,
121                                     except);
122         }
123     }
124
125     /**
126      * Get the options that were parsed.
127      *
128      * @return An array of string arrays, each representing an
129      * option and its arguments.
130      */

131     public String JavaDoc[][] getOptions() {
132         String JavaDoc[][] opts = new String JavaDoc[entries.size()][];
133         entries.copyInto(opts);
134         return opts;
135     }
136 }
137
Popular Tags