Util.java revision 608:7e06bf1dcb09
1/*
2 * Copyright (c) 1999, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25/*
26 * COMPONENT_NAME: idl.parser
27 *
28 * ORIGINS: 27
29 *
30 * Licensed Materials - Property of IBM
31 * 5639-D57 (C) COPYRIGHT International Business Machines Corp. 1997, 1999
32 * RMI-IIOP v1.0
33 *
34 */
35
36package com.sun.tools.corba.se.idl;
37
38// NOTES:
39// -capitalize and parseTypeModifier should probably be in the
40//  generators package.
41// -D58319<daz> Add version() method.
42// -D62023<daz> Add absDelta() method to support float computations.
43
44import java.io.DataInputStream;
45import java.io.File;
46import java.io.FileNotFoundException;
47import java.io.IOException;
48
49import java.util.Enumeration;
50import java.util.Hashtable;
51import java.util.Properties;
52import java.util.Vector;
53
54import com.sun.tools.corba.se.idl.som.cff.FileLocator;
55
56public class Util
57{
58  // <d58319>
59  /**
60   * Fetch the version number of this build of the IDL Parser Framework
61   * from the appropriate properties file.
62   * @return the version number contained within the appropriate properties
63   *  file, which indicates the build of this IDL Parser Framework.
64   **/
65  public static String getVersion ()
66  {
67    return getVersion ("com/sun/tools/corba/se/idl/idl.prp");
68  } // getVersion
69
70  /**
71   * Fetch the version number of this build of the IDL Parser Framework.
72   * This method may be called before or after the framework has been
73   * initialized. If the framework is inititialized, the version information
74   * is extracted from the message properties object; otherwise, it is extracted
75   * from the indicated messages file.
76   * @return the version number.
77   **/
78  public static String getVersion (String filename)
79  {
80    String version = "";
81    if (messages == null)  // Use supplied file
82    {
83      Vector oldMsgFiles = msgFiles;
84      if (filename == null || filename.equals (""))
85        filename = "com/sun/tools/corba/se/idl/idl.prp";
86      filename = filename.replace ('/', File.separatorChar);
87      registerMessageFile (filename);
88      version = getMessage ("Version.product", getMessage ("Version.number"));
89      msgFiles = oldMsgFiles;
90      messages = null;
91    }
92    else
93    {
94      version = getMessage ("Version.product", getMessage ("Version.number"));
95    }
96    return version;
97  } // getVersion
98
99  public static boolean isAttribute (String name, Hashtable symbolTable)
100  {
101    SymtabEntry entry = (SymtabEntry)symbolTable.get (name);
102    return entry == null ? false : entry instanceof AttributeEntry;
103  } // isAttribute
104
105  public static boolean isConst (String name, Hashtable symbolTable)
106  {
107    SymtabEntry entry = (SymtabEntry)symbolTable.get (name);
108    return entry == null ? false : entry instanceof ConstEntry;
109  } // isConst
110
111  public static boolean isEnum (String name, Hashtable symbolTable)
112  {
113    SymtabEntry entry = (SymtabEntry)symbolTable.get (name);
114    return entry == null ? false : entry instanceof EnumEntry;
115  } // isEnum
116
117  public static boolean isException (String name, Hashtable symbolTable)
118  {
119    SymtabEntry entry = (SymtabEntry)symbolTable.get (name);
120    return entry == null ? false : entry instanceof ExceptionEntry;
121  } // isException
122
123  public static boolean isInterface (String name, Hashtable symbolTable)
124  {
125    SymtabEntry entry = (SymtabEntry)symbolTable.get (name);
126    return entry == null ? false : entry instanceof InterfaceEntry;
127  } // isInterface
128
129  public static boolean isMethod (String name, Hashtable symbolTable)
130  {
131    SymtabEntry entry = (SymtabEntry)symbolTable.get (name);
132    return entry == null ? false : entry instanceof MethodEntry;
133  } // isMethod
134
135  public static boolean isModule (String name, Hashtable symbolTable)
136  {
137    SymtabEntry entry = (SymtabEntry)symbolTable.get (name);
138    return entry == null ? false : entry instanceof ModuleEntry;
139  } // isModule
140
141  public static boolean isParameter (String name, Hashtable symbolTable)
142  {
143    SymtabEntry entry = (SymtabEntry)symbolTable.get (name);
144    return entry == null ? false : entry instanceof ParameterEntry;
145  } // isParameter
146
147  public static boolean isPrimitive (String name, Hashtable symbolTable)
148  {
149    // Distinguish "string" because the name could be something like:
150    // string(25 + 1)
151    SymtabEntry entry = (SymtabEntry)symbolTable.get (name);
152    if (entry == null)
153    {
154      // If it is null then it may be of the form string(<exp>).
155      // Don't just check for string because the name "string" may
156      // have been overridden.
157      int parenIndex = name.indexOf ('(');
158      if (parenIndex >= 0)
159        entry = (SymtabEntry)symbolTable.get (name.substring (0, parenIndex));
160    }
161    return entry == null ? false : entry instanceof PrimitiveEntry;
162  } // isPrimitive
163
164  public static boolean isSequence (String name, Hashtable symbolTable)
165  {
166    SymtabEntry entry = (SymtabEntry)symbolTable.get (name);
167    return entry == null ? false : entry instanceof SequenceEntry;
168  } // isSequence
169
170  public static boolean isStruct (String name, Hashtable symbolTable)
171  {
172    SymtabEntry entry = (SymtabEntry)symbolTable.get (name);
173    return entry == null ? false : entry instanceof StructEntry;
174  } // isStruct
175
176  public static boolean isString (String name, Hashtable symbolTable)
177  {
178    SymtabEntry entry = (SymtabEntry)symbolTable.get (name);
179    return entry == null ? false : entry instanceof StringEntry;
180  } // isString
181
182  public static boolean isTypedef (String name, Hashtable symbolTable)
183  {
184    SymtabEntry entry = (SymtabEntry)symbolTable.get (name);
185    return entry == null ? false : entry instanceof TypedefEntry;
186  } // isTypedef
187
188  public static boolean isUnion (String name, Hashtable symbolTable)
189  {
190    SymtabEntry entry = (SymtabEntry)symbolTable.get (name);
191    return entry == null ? false : entry instanceof UnionEntry;
192  } // isUnion
193
194  //////////////
195  // Message-related methods
196
197  public static String getMessage (String key)
198  {
199    if (messages == null)
200      readMessages ();
201    String message = messages.getProperty (key);
202    if (message == null)
203      message = getDefaultMessage (key);
204    return message;
205  } // getMessage
206
207  public static String getMessage (String key, String fill)
208  {
209    if (messages == null)
210      readMessages ();
211    String message = messages.getProperty (key);
212    if (message == null)
213      message = getDefaultMessage (key);
214    else
215    {
216      int index = message.indexOf ("%0");
217      if (index >= 0)
218        message = message.substring (0, index) + fill + message.substring (index + 2);
219    }
220    return message;
221  } // getMessage
222
223  public static String getMessage (String key, String[] fill)
224  {
225    if (messages == null)
226      readMessages ();
227    String message = messages.getProperty (key);
228    if (message == null)
229      message = getDefaultMessage (key);
230    else
231      for (int i = 0; i < fill.length; ++i)
232      {
233        int index = message.indexOf ("%" + i);
234        if (index >= 0)
235          message = message.substring (0, index) + fill[i] + message.substring (index + 2);
236      }
237    return message;
238  } // getMessage
239
240  private static String getDefaultMessage (String keyNotFound)
241  {
242    String message = messages.getProperty (defaultKey);
243    int index = message.indexOf ("%0");
244    if (index > 0)
245      message = message.substring (0, index) + keyNotFound;
246    return message;
247  } // getDefaultMessage
248
249  /*
250  findFile is no longer used now that FileLocator has been provided
251  by Larry Raper of the Shasta team.
252
253  static File findFile (String name) throws FileNotFoundException
254  {
255    String classpath = System.getProperty ("java.class.path");
256    String separator = System.getProperty ("path.separator");
257    int end = -separator.length (); // so the first pass classpath == original classpath
258    File file;
259    do
260    {
261      classpath = classpath.substring (end + separator.length ());
262      end = classpath.indexOf (separator);
263      if (end < 0) end = classpath.length ();
264      file = new File (classpath.substring (0, end) + File.separator + "com" + File.separator + "ibm" + File.separator + "idl" + File.separator + name);
265    } while (!file.exists () && end != classpath.length ());
266    if (!file.exists ()) throw new FileNotFoundException ();
267    return file;
268  } // findFile
269  */
270
271  private static void readMessages ()
272  {
273    messages = new Properties ();
274    Enumeration fileList = msgFiles.elements ();
275    DataInputStream stream;
276    while (fileList.hasMoreElements ())
277      try
278      {
279        stream = FileLocator.locateLocaleSpecificFileInClassPath ((String)fileList.nextElement ());
280        messages.load (stream);
281      }
282      catch (IOException e)
283      {
284      }
285    if (messages.size () == 0)
286      messages.put (defaultKey, "Error reading Messages File.");
287  } // readMessages
288
289  /** Register a message file.  This file will be searched for
290      in the CLASSPATH. */
291  public static void registerMessageFile (String filename)
292  {
293    if (filename != null)
294      if (messages == null)
295        msgFiles.addElement (filename);
296      else
297        try
298        {
299          DataInputStream stream = FileLocator.locateLocaleSpecificFileInClassPath (filename);
300          messages.load (stream);
301        }
302        catch (IOException e)
303        {
304        }
305  } // registerMessageFile
306
307  private static Properties messages   = null;
308  private static String     defaultKey = "default";
309  private static Vector     msgFiles = new Vector ();
310  static
311  {
312    msgFiles.addElement ("com/sun/tools/corba/se/idl/idl.prp");
313  }
314
315  // Message-related methods
316  ///////////////
317
318  public static String capitalize (String lc)
319  {
320    String first = new String (lc.substring (0, 1));
321    first = first.toUpperCase ();
322    return first + lc.substring (1);
323  } // capitalize
324
325  ///////////////
326  // General file methods
327
328  /** Searches the current user directory and a list of directories for
329      a given short file name and returns its absolute file specification.
330      @return Absolute file name of a given short filename
331      @throws FileNotFoundException The file does not exist in the
332       current user or specified directories.
333      @see java.io.File.getAbsolutePath */
334  public static String getAbsolutePath (String filename, Vector includePaths) throws FileNotFoundException
335  {
336    String filepath = null;
337    File file = new File (filename);
338    if (file.canRead ())
339      filepath = file.getAbsolutePath ();
340    else
341    {
342      String fullname = null;
343      Enumeration pathList = includePaths.elements ();
344      while (!file.canRead () && pathList.hasMoreElements ())
345      {
346        fullname = (String)pathList.nextElement () + File.separatorChar + filename;
347        file = new File (fullname);
348      }
349      if (file.canRead ())
350        filepath = file.getPath ();
351      else
352        throw new FileNotFoundException (filename);
353    }
354    return filepath;
355  } // getAbsolutePath
356
357  // General file methods
358  ///////////////
359
360  ///////////////
361  // Numeric computations
362
363  // <d62023>
364  /**
365   * Compute the absolute value of the difference between two floating-point
366   * numbers having single precision.
367   * @return the absolute value of the difference between two floats.
368   **/
369  public static float absDelta (float f1, float f2)
370  {
371    double delta = f1 - f2;
372    return (float)((delta < 0) ? delta * -1.0 : delta);
373  } // absDelta
374
375  // Numeric computations
376  ///////////////
377
378  static RepositoryID emptyID = new RepositoryID ();
379} // class Util
380