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