1   /*
2    *  Compiler.java - compile .jape files
3    *
4    *  Copyright (c) 1998-2005, The University of Sheffield.
5    *
6    *  This file is part of GATE (see http://gate.ac.uk/), and is free
7    *  software, licenced under the GNU Library General Public License,
8    *  Version 2, June 1991 (in the distribution as file licence.html,
9    *  and also available at http://gate.ac.uk/gate/licence.html).
10   *
11   *  Hamish Cunningham, 23/02/2000
12   *
13   *  $Id: Compiler.java,v 1.11 2005/06/21 14:09:51 valyt Exp $
14   */
15  
16  package gate.jape;
17  
18  import java.io.*;
19  import java.util.ArrayList;
20  import java.util.Iterator;
21  
22  import gate.Factory;
23  import gate.jape.parser.ParseCpsl;
24  import gate.util.Err;
25  import gate.util.Out;
26  
27  /**
28    * Compiler for JAPE files.
29    */
30  public class Compiler {
31  
32    /** Debug flag */
33    private static final boolean DEBUG = false;
34  
35    /** How much noise to make. */
36    static private boolean verbose = false;
37  
38    static String defaultEncoding = "UTF-8";
39  
40    /** Take a list of .jape files names and compile them to .ser.
41      * Also recognises a -v option which makes it chatty.
42      */
43    static public void main(String[] args) {
44  
45      // process options
46      int argsIndex = 0;
47      while(args[argsIndex].toCharArray()[0] == '-')
48        if(args[argsIndex++].equals("-v"))
49          verbose = true;
50  
51      // construct list of the files
52      ArrayList fileNames = new ArrayList();
53      for( ; argsIndex<args.length; argsIndex++)
54        fileNames.add(args[argsIndex]);
55  
56      // compile the files
57      compile(fileNames);
58  
59      message("done");
60    } // main
61  
62    /** The main compile method, taking a file name. */
63    static public void compile(String japeFileName, String encoding) {
64      // parse
65      message("parsing " + japeFileName);
66      Transducer transducer = null;
67      try {
68        transducer = parseJape(japeFileName, encoding);
69      } catch(JapeException e) {
70        emessage("couldn't compile " + japeFileName + ": " + e);
71        return;
72      }
73  
74      // save
75      message("saving " + japeFileName);
76      try {
77        saveJape(japeFileName, transducer);
78      } catch (JapeException e) {
79        emessage("couldn't save " + japeFileName + ": " + e);
80      }
81  
82      message("finished " + japeFileName);
83    } // compile(String japeFileName)
84  
85    /** The main compile method, taking a list of file names. */
86    static public void compile(ArrayList fileNames) {
87      // for each file, compile and save
88      for(Iterator i = fileNames.iterator(); i.hasNext(); )
89        compile((String) i.next(), defaultEncoding);
90    } // compile
91  
92    /** Parse a .jape and return a transducer, or throw exception. */
93    static public Transducer parseJape(String japeFileName, String encoding)
94    throws JapeException {
95      Transducer transducer = null;
96  
97      try {
98        ParseCpsl cpslParser = Factory.newJapeParser(new File(japeFileName).toURL(),
99                                             encoding);
100       transducer = cpslParser.MultiPhaseTransducer();
101     } catch(gate.jape.parser.ParseException e) {
102       throw(new JapeException(e.toString()));
103     } catch(IOException e) {
104       throw(new JapeException(e.toString()));
105     }
106 
107     return transducer;
108   } // parseJape
109 
110   /** Save a .jape, or throw exception. */
111   static public void saveJape(String japeFileName, Transducer transducer)
112   throws JapeException {
113     String saveName = japeNameToSaveName(japeFileName);
114 
115     try {
116       FileOutputStream fos = new FileOutputStream(saveName);
117       ObjectOutputStream oos = new ObjectOutputStream (fos);
118       oos.writeObject(transducer);
119       oos.close();
120     } catch (IOException e) {
121       throw(new JapeException(e.toString()));
122     }
123   } // saveJape
124 
125   /** Convert a .jape file name to a .ser file name. */
126   static String japeNameToSaveName(String japeFileName) {
127     String base = japeFileName;
128     if(japeFileName.endsWith(".jape") || japeFileName.endsWith(".JAPE"))
129       base = japeFileName.substring(0, japeFileName.length() - 5);
130     return base + ".ser";
131   } // japeNameToSaveName
132 
133   /** Hello? Anybody there?? */
134   public static void message(String mess) {
135     if(verbose) Out.println("JAPE compiler: " + mess);
136   } // message
137 
138   /** Ooops. */
139   public static void emessage(String mess) {
140     Err.println("JAPE compiler error: " + mess);
141   } // emessage
142 
143 } // class Compiler
144 
145 
146 // $Log: Compiler.java,v $
147 // Revision 1.11  2005/06/21 14:09:51  valyt
148 // Ken Williams's patch for Factory and JAPE tranducers
149 //
150 // Revision 1.10  2005/01/11 13:51:36  ian
151 // Updating copyrights to 1998-2005 in preparation for v3.0
152 //
153 // Revision 1.9  2004/07/21 17:10:07  akshay
154 // Changed copyright from 1998-2001 to 1998-2004
155 //
156 // Revision 1.8  2004/03/25 13:01:14  valyt
157 // Imports optimisation throughout the Java sources
158 // (to get rid of annoying warnings in Eclipse)
159 //
160 // Revision 1.7  2001/09/13 12:09:49  kalina
161 // Removed completely the use of jgl.objectspace.Array and such.
162 // Instead all sources now use the new Collections, typically ArrayList.
163 // I ran the tests and I ran some documents and compared with keys.
164 // JAPE seems to work well (that's where it all was). If there are problems
165 // maybe look at those new structures first.
166 //
167 // Revision 1.6  2001/02/08 13:46:06  valyt
168 // Added full Unicode support for the gazetteer and Jape
169 // converted the gazetteer files to UTF-8
170 //
171 // Revision 1.5  2000/11/08 16:35:02  hamish
172 // formatting
173 //
174 // Revision 1.4  2000/10/26 10:45:30  oana
175 // Modified in the code style
176 //
177 // Revision 1.3  2000/10/16 16:44:33  oana
178 // Changed the comment of DEBUG variable
179 //
180 // Revision 1.2  2000/10/10 15:36:35  oana
181 // Changed System.out in Out and System.err in Err;
182 // Added the DEBUG variable seted on false;
183 // Added in the header the licence;
184 //
185 // Revision 1.1  2000/02/23 13:46:04  hamish
186 // added
187 //
188 // Revision 1.1.1.1  1999/02/03 16:23:01  hamish
189 // added gate2
190 //
191 // Revision 1.3  1998/10/29 12:07:27  hamish
192 // added compile method taking a file name
193 //
194 // Revision 1.2  1998/09/21 16:19:27  hamish
195 // don't catch *all* exceptions!
196 //
197 // Revision 1.1  1998/09/18 15:07:41  hamish
198 // a functioning compiler in two shakes of a rats tail
199