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