1 19 20 package org.netbeans.modules.schema2beansdev; 21 22 import java.io.*; 23 import java.util.*; 24 31 public class EntityParser { 32 private java.util.Map entityMap; 33 private File fileName; 34 35 public EntityParser(File fileName) { 36 this.fileName=fileName; 37 entityMap = new java.util.HashMap (); 38 } 39 41 public void parse() throws IOException { 42 BufferedReader br = new BufferedReader(new FileReader(fileName)); 43 String line = null; 44 while ((line=br.readLine())!=null) { 45 int startPos = line.indexOf("<!ENTITY "); 46 if (startPos>=0) addEntity(br,line.substring(startPos+9)); 47 } 48 br.close(); 49 } 50 51 private void addEntity(BufferedReader br, String line) throws IOException { 52 StringTokenizer tok = new StringTokenizer(line); 53 if (!tok.hasMoreTokens()) return; 54 String percentage = tok.nextToken(); 55 if (!"%".equals(percentage)) return; if (!tok.hasMoreTokens()) return; 58 String key = tok.nextToken(); 60 int valueStartPos = line.indexOf(key)+key.length(); 61 String rest = line.substring(valueStartPos); 62 63 valueStartPos = rest.indexOf("\""); 65 if (valueStartPos<0) return; 66 67 rest = rest.substring(valueStartPos+1); 69 String value = resolveValue (rest,br); 70 71 if (value!=null) { 73 int refStart = value.indexOf("%"); 74 int refEnd = value.indexOf(";"); 75 if (refStart>=0 && refEnd>refStart) { String entityKey = value.substring(refStart+1,refEnd); 77 String val = (String )entityMap.get(entityKey); 78 if (val!=null) { 79 String newValue = value.substring(0,refStart)+val+value.substring(refEnd+1); 80 System.out.println("found ENTITY: % "+key+" \""+newValue+"\""); 81 entityMap.put(key,newValue); 82 } 83 } else { 84 System.out.println("found ENTITY: % "+key+" \""+value+"\""); 85 entityMap.put(key,value); 86 } 87 } 88 } 89 90 private String resolveValue(String lineRest, BufferedReader br) throws IOException { 91 int index = lineRest.indexOf("\""); 93 if (index>=0) return lineRest.substring(0,index); 94 StringBuffer buf = new StringBuffer (lineRest); 96 buf.append("\n"); 97 int ch=br.read(); 98 while ( ch!=(int)'"' && ch!=(int)'>' && ch!=-1 ) { 99 buf.append((char)ch); 100 ch=br.read(); 101 } 102 return buf.toString(); 103 } 104 105 private boolean containsBlank(String s) { 106 for (int i=0;i<s.length();i++) { 107 if (' '==s.charAt(i)) return true; 108 } 109 return false; 110 } 111 112 115 public Reader getReader() throws IOException { 116 StringBuffer buf = new StringBuffer (); 117 BufferedReader br = new BufferedReader(new FileReader(fileName)); 118 String line = null; 119 while ((line=br.readLine())!=null) { 120 if (line.indexOf("<!ENTITY ")>=0) line = removeEntityDeclaration(line,br); 122 int pos = line.indexOf("%"); 124 if (pos>=0) { 125 StringTokenizer tok = new StringTokenizer(line.substring(pos),";%"); 126 while (tok.hasMoreTokens()) { 127 String key = tok.nextToken(); 128 if (key.length()>0 && !containsBlank(key)) { 129 String value = (String )entityMap.get(key); 130 if (value!=null) line = line.replaceAll("%"+key+";",value); 131 } 132 } 133 } 134 if (line.length()>0) buf.append(line); 135 } 136 br.close(); 137 return new StringReader(buf.toString()); 138 } 139 140 142 private String removeEntityDeclaration(String line,BufferedReader br) throws IOException { 143 int start = line.indexOf("<!ENTITY "); 144 StringBuffer buf = new StringBuffer (); 145 if (start>0) buf.append(line.substring(0, start)); 146 int endPos = line.indexOf(">", start); 147 if (endPos>0) { 148 buf.append(line.substring(endPos+1)); 149 return buf.toString(); 150 } 151 String ln=null; 152 while (endPos<0 && (ln=br.readLine())!=null) { 153 endPos = ln.indexOf(">"); 154 if (endPos>=0) { 155 buf.append(ln.substring(endPos+1)); 156 } 157 } 158 return buf.toString(); 159 } 160 161 } 162 | Popular Tags |