Compiler.java
001 /*
002  *  Compiler.java - compile .jape files
003  *
004  *  Copyright (c) 1995-2010, The University of Sheffield. See the file
005  *  COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
006  *
007  *  This file is part of GATE (see http://gate.ac.uk/), and is free
008  *  software, licenced under the GNU Library General Public License,
009  *  Version 2, June 1991 (in the distribution as file licence.html,
010  *  and also available at http://gate.ac.uk/gate/licence.html).
011  *
012  *  Hamish Cunningham, 23/02/2000
013  *
014  *  $Id: Compiler.java 12006 2009-12-01 17:24:28Z thomas_heitz $
015  */
016 
017 package gate.jape;
018 
019 import java.io.*;
020 import java.util.ArrayList;
021 import java.util.Iterator;
022 
023 import gate.Factory;
024 import gate.jape.parser.ParseCpsl;
025 import gate.util.Err;
026 import gate.util.Out;
027 
028 /**
029   * Compiler for JAPE files.
030   */
031 public class Compiler {
032 
033   /** Debug flag */
034   private static final boolean DEBUG = false;
035 
036   /** How much noise to make. */
037   static private boolean verbose = false;
038 
039   static String defaultEncoding = "UTF-8";
040 
041   /** Take a list of .jape files names and compile them to .ser.
042     * Also recognises a -v option which makes it chatty.
043     */
044   static public void main(String[] args) {
045 
046     // process options
047     int argsIndex = 0;
048     while(args[argsIndex].toCharArray()[0== '-')
049       if(args[argsIndex++].equals("-v"))
050         verbose = true;
051 
052     // construct list of the files
053     ArrayList fileNames = new ArrayList();
054     for; argsIndex<args.length; argsIndex++)
055       fileNames.add(args[argsIndex]);
056 
057     // compile the files
058     compile(fileNames);
059 
060     message("done");
061   // main
062 
063   /** The main compile method, taking a file name. */
064   static public void compile(String japeFileName, String encoding) {
065     // parse
066     message("parsing " + japeFileName);
067     Transducer transducer = null;
068     try {
069       transducer = parseJape(japeFileName, encoding);
070     catch(JapeException e) {
071       emessage("couldn't compile " + japeFileName + ": " + e);
072       return;
073     }
074 
075     // save
076     message("saving " + japeFileName);
077     try {
078       saveJape(japeFileName, transducer);
079     catch (JapeException e) {
080       emessage("couldn't save " + japeFileName + ": " + e);
081     }
082 
083     message("finished " + japeFileName);
084   // compile(String japeFileName)
085 
086   /** The main compile method, taking a list of file names. */
087   static public void compile(ArrayList fileNames) {
088     // for each file, compile and save
089     for(Iterator i = fileNames.iterator(); i.hasNext())
090       compile((Stringi.next(), defaultEncoding);
091   // compile
092 
093   /** Parse a .jape and return a transducer, or throw exception. */
094   static public Transducer parseJape(String japeFileName, String encoding)
095   throws JapeException {
096     Transducer transducer = null;
097 
098     try {
099       ParseCpsl cpslParser = Factory.newJapeParser(new File(japeFileName).toURI().toURL(),
100                                            encoding);
101       transducer = cpslParser.MultiPhaseTransducer();
102     catch(gate.jape.parser.ParseException e) {
103       throw(new JapeException(e.toString()));
104     catch(IOException e) {
105       throw(new JapeException(e.toString()));
106     }
107 
108     return transducer;
109   // parseJape
110 
111   /** Save a .jape, or throw exception. */
112   static public void saveJape(String japeFileName, Transducer transducer)
113   throws JapeException {
114     String saveName = japeNameToSaveName(japeFileName);
115 
116     try {
117       FileOutputStream fos = new FileOutputStream(saveName);
118       ObjectOutputStream oos = new ObjectOutputStream (fos);
119       oos.writeObject(transducer);
120       oos.close();
121     catch (IOException e) {
122       throw(new JapeException(e.toString()));
123     }
124   // saveJape
125 
126   /** Convert a .jape file name to a .ser file name. */
127   static String japeNameToSaveName(String japeFileName) {
128     String base = japeFileName;
129     if(japeFileName.endsWith(".jape"|| japeFileName.endsWith(".JAPE"))
130       base = japeFileName.substring(0, japeFileName.length() 5);
131     return base + ".ser";
132   // japeNameToSaveName
133 
134   /** Hello? Anybody there?? */
135   public static void message(String mess) {
136     if(verboseOut.println("JAPE compiler: " + mess);
137   // message
138 
139   /** Ooops. */
140   public static void emessage(String mess) {
141     Err.println("JAPE compiler error: " + mess);
142   // emessage
143 
144 // class Compiler
145 
146 
147 // $Log$
148 // Revision 1.11  2005/06/21 14:09:51  valyt
149 // Ken Williams's patch for Factory and JAPE tranducers
150 //
151 // Revision 1.10  2005/01/11 13:51:36  ian
152 // Updating copyrights to 1998-2005 in preparation for v3.0
153 //
154 // Revision 1.9  2004/07/21 17:10:07  akshay
155 // Changed copyright from 1998-2001 to 1998-2004
156 //
157 // Revision 1.8  2004/03/25 13:01:14  valyt
158 // Imports optimisation throughout the Java sources
159 // (to get rid of annoying warnings in Eclipse)
160 //
161 // Revision 1.7  2001/09/13 12:09:49  kalina
162 // Removed completely the use of jgl.objectspace.Array and such.
163 // Instead all sources now use the new Collections, typically ArrayList.
164 // I ran the tests and I ran some documents and compared with keys.
165 // JAPE seems to work well (that's where it all was). If there are problems
166 // maybe look at those new structures first.
167 //
168 // Revision 1.6  2001/02/08 13:46:06  valyt
169 // Added full Unicode support for the gazetteer and Jape
170 // converted the gazetteer files to UTF-8
171 //
172 // Revision 1.5  2000/11/08 16:35:02  hamish
173 // formatting
174 //
175 // Revision 1.4  2000/10/26 10:45:30  oana
176 // Modified in the code style
177 //
178 // Revision 1.3  2000/10/16 16:44:33  oana
179 // Changed the comment of DEBUG variable
180 //
181 // Revision 1.2  2000/10/10 15:36:35  oana
182 // Changed System.out in Out and System.err in Err;
183 // Added the DEBUG variable seted on false;
184 // Added in the header the licence;
185 //
186 // Revision 1.1  2000/02/23 13:46:04  hamish
187 // added
188 //
189 // Revision 1.1.1.1  1999/02/03 16:23:01  hamish
190 // added gate2
191 //
192 // Revision 1.3  1998/10/29 12:07:27  hamish
193 // added compile method taking a file name
194 //
195 // Revision 1.2  1998/09/21 16:19:27  hamish
196 // don't catch *all* exceptions!
197 //
198 // Revision 1.1  1998/09/18 15:07:41  hamish
199 // a functioning compiler in two shakes of a rats tail