KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quikj > server > framework > AceConfigTableFileParser


1 package com.quikj.server.framework;
2
3 import java.io.*;
4
5 public class AceConfigTableFileParser
6 {
7     public static final String JavaDoc ACE_GLOBALDATA_HOME_ENV_NAME = "ACE_GLOBAL_HOME";
8     public static final String JavaDoc ACE_LOCALDATA_HOME_ENV_NAME = "ACE_LOCAL_HOME";
9     public static final String JavaDoc DEFAULT_ACE_GLOBALDATA_HOME = "global";
10     public static final String JavaDoc DEFAULT_ACE_LOCALDATA_HOME = "local";
11     public static final String JavaDoc COMMENT_CHAR = "!";
12     public static final char QUOTE_CHAR = '\"';
13     
14     public static final int GLOBAL_DATA = 0;
15     public static final int LOCAL_DATA = 1;
16     
17     // the ordering of the following arrays must match with the above data
18
public static final String JavaDoc envNames[] =
19     {
20         ACE_GLOBALDATA_HOME_ENV_NAME,
21         ACE_LOCALDATA_HOME_ENV_NAME
22     };
23     
24     public static final String JavaDoc defaultEnvNames[] =
25     {
26         DEFAULT_ACE_GLOBALDATA_HOME,
27         DEFAULT_ACE_LOCALDATA_HOME
28     };
29     
30     public AceConfigTableFileParser(int data_type, String JavaDoc dir, String JavaDoc file)
31     throws FileNotFoundException, IOException, ArrayIndexOutOfBoundsException JavaDoc
32     {
33         absPath = AceConfigTableFileParser.getAcePath(data_type, dir, file);
34         
35         File config_file = new File(absPath);
36         lineReader = new LineNumberReader(new FileReader(config_file));
37         
38         String JavaDoc line;
39         do
40         {
41             line = lineReader.readLine();
42             if (line != null)
43             {
44                 String JavaDoc tline = line.trim();
45                 if (tline.length() > 0)
46                 {
47                     if (tline.startsWith(COMMENT_CHAR) == false)
48                     {
49                         numLines++;
50                     }
51                 }
52             }
53         }
54         while (line != null);
55         
56         lineReader.close();
57         lineReader = null;
58     }
59     
60     public void dispose()
61     {
62         try
63         {
64             if (lineReader != null)
65             {
66                 lineReader.close();
67                 lineReader = null;
68             }
69         }
70         catch (IOException ex)
71         {
72             System.err.println("AceConfigTableFileParser.dispose() -- Error occured while closing file :"
73             + absPath
74             + " : " + ex.getMessage());
75         }
76     }
77     
78     public static String JavaDoc getAcePath(int data_type, String JavaDoc dir, String JavaDoc file)
79     throws ArrayIndexOutOfBoundsException JavaDoc
80     {
81         String JavaDoc ace_home = System.getProperty(envNames[data_type]);
82         if (ace_home == null)
83         {
84             ace_home = defaultEnvNames[data_type];
85         }
86         
87         String JavaDoc path;
88         if (file.length() > 0)
89         {
90             path = new String JavaDoc(ace_home + File.separator
91             + dir + File.separator
92             + file);
93         }
94         else
95         {
96             path = new String JavaDoc(ace_home + File.separator
97             + dir);
98         }
99         return path;
100     }
101     
102     public static String JavaDoc getAcePath(int data_type, String JavaDoc dir)
103     throws ArrayIndexOutOfBoundsException JavaDoc
104     {
105         return getAcePath(data_type, dir, "");
106     }
107     
108     public static String JavaDoc convertAbsPathToURL(String JavaDoc path)
109     {
110         char[] array = path.toCharArray();
111         
112         for (int i = 0; i < array.length; i++)
113         {
114             if (array[i] == '\\') // Stupid DOS
115
{
116                 array[i] = '/';
117             }
118         }
119         
120         if (array[0] == '/')
121         {
122             return "file:" + new String JavaDoc(array);
123         }
124         else
125         {
126             return "file:/" + new String JavaDoc(array);
127         }
128     }
129     
130     public int numRecords()
131     {
132         return numLines;
133     }
134     
135     public AceConfigTableRecord nextRecord()
136     {
137         
138         if (lineReader == null) // first one
139
{
140             try
141             {
142                 lineReader = new LineNumberReader(new FileReader(absPath));
143             }
144             catch (FileNotFoundException ex)
145             {
146                 // should not happen
147
return null;
148             }
149         }
150         try
151         {
152             while (true)
153             {
154                 String JavaDoc line = lineReader.readLine();
155                 if (line != null)
156                 {
157                     AceConfigTableRecord cfg = new AceConfigTableRecord(line);
158                     if (cfg.numberOfTokens() > 0)
159                     {
160                         return cfg;
161                     }
162                 }
163                 else
164                 {
165                     // EOF reached
166
lineReader.close();
167                     lineReader = null;
168                     return null;
169                 }
170             }
171         }
172         catch (IOException ex)
173         {
174             return null;
175         }
176     }
177     
178     public String JavaDoc getAbsolutePath()
179     {
180         return new String JavaDoc(absPath);
181     }
182     
183     private int numLines = 0;
184     private LineNumberReader lineReader = null;
185     private String JavaDoc absPath;
186 }
187
188
189
190
191
192
193
Popular Tags