KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JFlex > Skeleton


1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * JFlex 1.4.1 *
3  * Copyright (C) 1998-2004 Gerwin Klein <lsf@jflex.de> *
4  * All rights reserved. *
5  * *
6  * This program is free software; you can redistribute it and/or modify *
7  * it under the terms of the GNU General Public License. See the file *
8  * COPYRIGHT for more information. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License along *
16  * with this program; if not, write to the Free Software Foundation, Inc., *
17  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
18  * *
19  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

20 package JFlex;
21
22 import java.io.*;
23 import java.net.URL JavaDoc;
24 import java.util.Vector JavaDoc;
25
26
27 /**
28  * This class stores the skeleton of generated scanners.
29  *
30  * The skeleton consists of several parts that can be emitted to
31  * a file. Usually there is a portion of generated code
32  * (produced in class Emitter) between every two parts of skeleton code.
33  *
34  * There is a static part (the skeleton code) and state based iterator
35  * part to this class. The iterator part is used to emit consecutive skeleton
36  * sections to some <code>PrintWriter</code>.
37  *
38  * @see JFlex.Emitter
39  *
40  * @author Gerwin Klein
41  * @version JFlex 1.4.1, $Revision: 2.13 $, $Date: 2004/11/06 23:03:31 $
42  */

43 public class Skeleton {
44   /** expected number of sections in the skeleton file */
45   static final private int size = 21;
46
47   /** platform specific newline */
48   static final private String JavaDoc NL = System.getProperty("line.separator"); //$NON-NLS-1$
49

50   /** The skeleton */
51   public static String JavaDoc line[];
52   
53   /** initialization */
54   static { readDefault(); }
55   
56   // the state based, iterator part of Skeleton:
57

58   /**
59    * The current part of the skeleton (an index of nextStop[])
60    */

61   private int pos;
62   
63   /**
64    * The writer to write the skeleton-parts to
65    */

66   private PrintWriter out;
67
68
69   /**
70    * Creates a new skeleton (iterator) instance.
71    *
72    * @param out the writer to write the skeleton-parts to
73    */

74   public Skeleton(PrintWriter out) {
75     this.out = out;
76   }
77
78
79   /**
80    * Emits the next part of the skeleton
81    */

82   public void emitNext() {
83     out.print( line[pos++] );
84   }
85
86
87   /**
88    * Make the skeleton private.
89    *
90    * Replaces all occurences of " public " in the skeleton with " private ".
91    */

92   public static void makePrivate() {
93     for (int i=0; i < line.length; i++) {
94       line[i] = replace(" public ", " private ", line[i]); //$NON-NLS-1$ //$NON-NLS-2$
95
}
96   }
97
98
99   /**
100    * Reads an external skeleton file for later use with this class.
101    *
102    * @param skeletonFile the file to read (must be != null and readable)
103    */

104   public static void readSkelFile(File skeletonFile) {
105     if (skeletonFile == null)
106       throw new IllegalArgumentException JavaDoc("Skeleton file must not be null"); //$NON-NLS-1$
107

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   /**
127    * Reads an external skeleton file from a BufferedReader.
128    *
129    * @param reader the reader to read from (must be != null)
130    * @throws IOException if an IO error occurs
131    * @throws GeneratorException if the number of skeleton sections does not match
132    */

133   public static void readSkel(BufferedReader reader) throws IOException {
134     Vector JavaDoc lines = new Vector JavaDoc();
135     StringBuffer JavaDoc section = new StringBuffer JavaDoc();
136
137     String JavaDoc ln;
138     while ((ln = reader.readLine()) != null) {
139       if (ln.startsWith("---")) { //$NON-NLS-1$
140
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 JavaDoc[size];
157     for (int i = 0; i < size; i++)
158       line[i] = (String JavaDoc) lines.elementAt(i);
159   }
160   
161   /**
162    * Replaces a with b in c.
163    *
164    * @param a the String to be replaced
165    * @param b the replacement
166    * @param c the String in which to replace a by b
167    * @return a String object with a replaced by b in c
168    */

169   public static String JavaDoc replace(String JavaDoc a, String JavaDoc b, String JavaDoc c) {
170     StringBuffer JavaDoc result = new StringBuffer JavaDoc(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   /**
188    * (Re)load the default skeleton. Looks in the current system class path.
189    */

190   public static void readDefault() {
191     ClassLoader JavaDoc l = Skeleton.class.getClassLoader();
192     URL JavaDoc url = l.getResource("JFlex/skeleton.default"); //$NON-NLS-1$
193

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