1 20 package JFlex; 21 22 import java.io.*; 23 import java.net.URL ; 24 import java.util.Vector ; 25 26 27 43 public class Skeleton { 44 45 static final private int size = 21; 46 47 48 static final private String NL = System.getProperty("line.separator"); 50 51 public static String line[]; 52 53 54 static { readDefault(); } 55 56 58 61 private int pos; 62 63 66 private PrintWriter out; 67 68 69 74 public Skeleton(PrintWriter out) { 75 this.out = out; 76 } 77 78 79 82 public void emitNext() { 83 out.print( line[pos++] ); 84 } 85 86 87 92 public static void makePrivate() { 93 for (int i=0; i < line.length; i++) { 94 line[i] = replace(" public ", " private ", line[i]); } 96 } 97 98 99 104 public static void readSkelFile(File skeletonFile) { 105 if (skeletonFile == null) 106 throw new IllegalArgumentException ("Skeleton file must not be null"); 108 if (!skeletonFile.isFile() || !skeletonFile.canRead()) { 109 Out.error(ErrorMessages.CANNOT_READ_SKEL, skeletonFile.toString()); 110 throw new GeneratorException(); 111 } 112 113 Out.println(ErrorMessages.READING_SKEL, skeletonFile.toString()); 114 115 try { 116 BufferedReader reader = new BufferedReader(new FileReader(skeletonFile)); 117 readSkel(reader); 118 } 119 catch (IOException e) { 120 Out.error(ErrorMessages.SKEL_IO_ERROR); 121 throw new GeneratorException(); 122 } 123 } 124 125 126 133 public static void readSkel(BufferedReader reader) throws IOException { 134 Vector lines = new Vector (); 135 StringBuffer section = new StringBuffer (); 136 137 String ln; 138 while ((ln = reader.readLine()) != null) { 139 if (ln.startsWith("---")) { lines.addElement(section.toString()); 141 section.setLength(0); 142 } else { 143 section.append(ln); 144 section.append(NL); 145 } 146 } 147 148 if (section.length() > 0) 149 lines.addElement(section.toString()); 150 151 if (lines.size() != size) { 152 Out.error(ErrorMessages.WRONG_SKELETON); 153 throw new GeneratorException(); 154 } 155 156 line = new String [size]; 157 for (int i = 0; i < size; i++) 158 line[i] = (String ) lines.elementAt(i); 159 } 160 161 169 public static String replace(String a, String b, String c) { 170 StringBuffer result = new StringBuffer (c.length()); 171 int i = 0; 172 int j = c.indexOf(a); 173 174 while (j >= i) { 175 result.append(c.substring(i,j)); 176 result.append(b); 177 i = j+a.length(); 178 j = c.indexOf(a,i); 179 } 180 181 result.append(c.substring(i,c.length())); 182 183 return result.toString(); 184 } 185 186 187 190 public static void readDefault() { 191 ClassLoader l = Skeleton.class.getClassLoader(); 192 URL url = l.getResource("JFlex/skeleton.default"); 194 if (url == null) { 195 Out.error(ErrorMessages.SKEL_IO_ERROR_DEFAULT); 196 throw new GeneratorException(); 197 } 198 199 try { 200 InputStreamReader reader = new InputStreamReader(url.openStream()); 201 readSkel(new BufferedReader(reader)); 202 } catch (IOException e) { 203 Out.error(ErrorMessages.SKEL_IO_ERROR_DEFAULT); 204 throw new GeneratorException(); 205 } 206 } 207 } 208
| Popular Tags
|