1 25 26 package net.yagga.util; 27 28 import java.util.*; 29 import net.yagga.util.Ut; 30 import java.io.*; 31 32 38 public abstract class AssociativeReader { 39 40 private int errorLine=-1; 42 public AssociativeReader() { 43 } 44 45 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 line=""; 54 boolean inComment=false; 55 boolean justComment=false; 56 int idx=-1; 57 while((line=read.readLine())!=null){ 58 lineNum++; 59 line=myTrim(line); 61 if(line.length()==0) 63 continue; 64 65 if((idx=line.indexOf("//"))!=-1) 67 line=line.substring(0,idx); 68 69 if((idx=line.indexOf("#"))!=-1) 71 line=line.substring(0,idx); 72 73 if((idx=line.indexOf("/*"))!=-1){ 75 inComment=true; 76 justComment=true; 77 line=line.substring(0,idx); 78 } 79 80 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 if(line.length()==0) 95 continue; 96 97 String 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 } 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 key, String value){ 121 map.put(key,value); 122 } 123 124 public void addPair(HashMap map, String key, String [] values){ 125 map.put(key,values); 126 } 127 128 void put(HashMap map, String key, String value) 129 { 130 map.put(key,value); 131 } 132 133 public static String myTrim(String 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 String ret=orig.substring(start-1,end+2); 158 return ret; 160 } 161 162 } 163 | Popular Tags |