KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > yagga > util > AssociativeReader


1 /*
2  * This file is part of MiniInstaller, a self installer builder for Java
3  * Copyright (C) 2002 Walter Gamba
4  * mailto:walter@yagga.net
5  * http://www.yagga.net/java/miniinstaller
6  *
7  * MiniInstaller is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * MiniInstaller is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  *
21  * As the time of writing, the GNU General Public Licene can be
22  * found at http://www.gnu.org/licenses/gpl.txt
23  *
24  */

25
26 package net.yagga.util;
27
28 import java.util.*;
29 import net.yagga.util.Ut;
30 import java.io.*;
31
32 /**
33   base classs for associative strings and files
34     A stream containings pairs key = value, each pair on a single line
35     key can have spaces
36     Comments in the form #, // or as usual C-like /* ...
37 */

38 public abstract class AssociativeReader {
39
40     private int errorLine=-1; //error line number
41

42   public AssociativeReader() {
43   }
44
45   /**
46     does the real parsing
47   */

48   protected boolean parseAssociativeReader(HashMap map, Reader reader){
49         int lineNum=0;
50         map.clear();
51         try{
52             BufferedReader read=new BufferedReader(reader);
53             String JavaDoc line="";
54             boolean inComment=false;
55             boolean justComment=false;
56             int idx=-1;
57             while((line=read.readLine())!=null){
58                 lineNum++;
59                 //trimm spaces
60
line=myTrim(line);
61                 //empty lines
62
if(line.length()==0)
63                     continue;
64
65                 //comments like C++ "//"
66
if((idx=line.indexOf("//"))!=-1)
67                     line=line.substring(0,idx);
68
69                 //comments like Perl "#"
70
if((idx=line.indexOf("#"))!=-1)
71                     line=line.substring(0,idx);
72
73                 //comments like c "/*"
74
if((idx=line.indexOf("/*"))!=-1){
75                     inComment=true;
76                     justComment=true;
77                     line=line.substring(0,idx);
78                 }
79
80                 //comments like stile c "*/"
81
if((idx=line.indexOf("*/"))!=-1){
82                     inComment=false;
83                     line=line.substring(idx+2);
84                 }
85
86                 if(inComment && !justComment)
87                     continue;
88
89                 justComment=false;
90
91                 line=myTrim(line);
92
93                 //check for empty lines again
94
if(line.length()==0)
95                     continue;
96
97                 String JavaDoc key="",value="";
98                 if((idx=line.indexOf("="))!=-1){
99                     key=line.substring(0,idx);
100                     value=line.substring(idx+1);
101                     key=myTrim(key);
102                     value=myTrim(value);
103                     put(map,key,value);
104                     //System.out.println("<"+key+">='"+value+"'");
105
}
106                 else
107                 {
108                     errorLine=lineNum;
109                     return false;
110                 }
111             }
112         }catch(IOException e){}
113         return true;
114   }
115
116     public int getErrorLine(){
117         return errorLine;
118     }
119
120     public void addPair(HashMap map, String JavaDoc key, String JavaDoc value){
121             map.put(key,value);
122     }
123
124     public void addPair(HashMap map, String JavaDoc key, String JavaDoc[] values){
125             map.put(key,values);
126     }
127
128     void put(HashMap map, String JavaDoc key, String JavaDoc value)
129     {
130         map.put(key,value);
131     }
132
133     public static String JavaDoc myTrim(String JavaDoc orig)
134     {
135         char c;
136         int i=0;
137         int len=orig.length();
138         if(len==0)
139             return orig;
140
141         while(i<len){
142             c=orig.charAt(i++);
143             if(c=='\t' || c==' ')
144                 continue;
145             break;
146         }
147         int start=i;
148         i=orig.length()-1;
149         while(i>=0){
150             c=orig.charAt(i--);
151             if(c=='\t' || c==' ')
152                 continue;
153             break;
154         }
155         int end=i;
156         //System.out.println("TRIMMing:'"+orig+"' at:"+start+","+end+".");
157
String JavaDoc ret=orig.substring(start-1,end+2);
158         //System.out.println("TRIMMED:'"+ret+"'");
159
return ret;
160     }
161
162 }
163
Popular Tags