KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > scenario > tools > util > MyBufferedReader


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Mathieu Peltier.
22  * Contributor(s): ______________________________________.
23  */

24
25 package org.objectweb.cjdbc.scenario.tools.util;
26
27 import java.io.BufferedReader JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.Reader JavaDoc;
30
31 /**
32  * I/O utilitary method.
33  *
34  * @author <a HREF="mailto:Mathieu.Peltier@inrialpes.fr">Mathieu Peltier</a>
35  */

36 public class MyBufferedReader extends BufferedReader JavaDoc
37 {
38   /** Begin request token. */
39   private static final String JavaDoc BEGIN_REQUEST = "#begin";
40
41   /** End request token. */
42   private static final String JavaDoc END_REQUEST = "#end";
43
44   /**
45    * File title (eg: 'requests'). Allow to contruct better error messages.
46    */

47   private String JavaDoc fileDescription;
48
49   /**
50    * Creates a new <code>MyBufferedReader</code> instance.
51    *
52    * @param reader a <code>Reader</code> instance.
53    * @param description description text.
54    */

55   public MyBufferedReader(Reader JavaDoc reader, String JavaDoc description)
56   {
57     super(reader);
58     fileDescription = description;
59   }
60
61   /**
62    * Convenient method to read a boolean value in a text file.
63    *
64    * @return the <code>boolean</code> read.
65    * @throws IOException if error occurs.
66    */

67   public boolean readBoolean() throws IOException JavaDoc
68   {
69     String JavaDoc line = readLine();
70     if ("true".equals(line))
71       return true;
72     else if ("false".equals(line))
73       return false;
74     throw new IOException JavaDoc(
75       "Syntax error in "
76         + fileDescription
77         + " file: unknow token '"
78         + line
79         + "' found, expected 'true' or 'false'");
80   }
81
82   /**
83    * Convenient method to read a <code>Strong</code> value in a text file.
84    *
85    * @param stringName name of the property to read.
86    * @return the <code>String</code> read.
87    * @throws IOException if error occurs.
88    */

89   public String JavaDoc readString(String JavaDoc stringName) throws IOException JavaDoc
90   {
91     String JavaDoc line = readLine();
92
93     if ((line == null) || line.equals(""))
94       throw new IOException JavaDoc(
95         "Syntax error in "
96           + fileDescription
97           + " file: "
98           + stringName
99           + " missing");
100     return line;
101   }
102
103   /**
104    * Convenient method to read a SQL request in a text file. The request must
105    * be delimited by the {@link #BEGIN_REQUEST}and {@link #END_REQUEST}
106    * tokens.
107    *
108    * @param line text file line.
109    * @return the <code>String</code> read.
110    * @throws IOException if error occurs.
111    */

112   public String JavaDoc readSQLRequest(String JavaDoc line) throws IOException JavaDoc
113   {
114     if (!BEGIN_REQUEST.equals(line))
115       throw new IOException JavaDoc(
116         "Syntax error in requests file: '"
117           + BEGIN_REQUEST
118           + "' token expected instead of '"
119           + line
120           + "')");
121
122     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
123     while (((line = readLine()) != null) && !line.equals(END_REQUEST))
124     {
125       buffer.append(line);
126       buffer.append(System.getProperty("line.separator"));
127     }
128     String JavaDoc request = buffer.toString();
129
130     if (!END_REQUEST.equals(line))
131       throw new IOException JavaDoc(
132         "Syntax error in requests file: '" + END_REQUEST + "' token not found");
133
134     return request;
135   }
136   
137   /**
138    * Get the next non-commented line
139    * @return the next non commented line
140    * @throws IOException if error occurs
141    */

142   public String JavaDoc readNextLine() throws IOException JavaDoc
143   {
144     String JavaDoc line = this.readLine();
145     while((line!=null)&&(line.startsWith("//")||line.startsWith("#")))
146       line = readLine();
147     return line;
148   }
149   
150 }
Popular Tags