001: /*
002: * Copyright 1999 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025: /*
026: * COMPONENT_NAME: idl.parser
027: *
028: * ORIGINS: 27
029: *
030: * Licensed Materials - Property of IBM
031: * 5639-D57 (C) COPYRIGHT International Business Machines Corp. 1997, 1999
032: * RMI-IIOP v1.0
033: *
034: * @(#)Util.java 1.21 07/05/05
035: */
036:
037: package com.sun.tools.corba.se.idl;
038:
039: // NOTES:
040: // -capitalize and parseTypeModifier should probably be in the
041: // generators package.
042: // -D58319<daz> Add version() method.
043: // -D62023<daz> Add absDelta() method to support float computations.
044:
045: import java.io.DataInputStream;
046: import java.io.File;
047: import java.io.FileNotFoundException;
048: import java.io.IOException;
049:
050: import java.util.Enumeration;
051: import java.util.Hashtable;
052: import java.util.Properties;
053: import java.util.Vector;
054:
055: import com.sun.tools.corba.se.idl.som.cff.FileLocator;
056:
057: public class Util {
058: // <d58319>
059: /**
060: * Fetch the version number of this build of the IDL Parser Framework
061: * from the appropriate properties file.
062: * @return the version number contained within the appropriate properties
063: * file, which indicates the build of this IDL Parser Framework.
064: **/
065: public static String getVersion() {
066: return getVersion("com/sun/tools/corba/se/idl/idl.prp");
067: } // getVersion
068:
069: /**
070: * Fetch the version number of this build of the IDL Parser Framework.
071: * This method may be called before or after the framework has been
072: * initialized. If the framework is inititialized, the version information
073: * is extracted from the message properties object; otherwise, it is extracted
074: * from the indicated messages file.
075: * @return the version number.
076: **/
077: public static String getVersion(String filename) {
078: String version = "";
079: if (messages == null) // Use supplied file
080: {
081: Vector oldMsgFiles = msgFiles;
082: if (filename == null || filename.equals(""))
083: filename = "com/sun/tools/corba/se/idl/idl.prp";
084: filename = filename.replace('/', File.separatorChar);
085: registerMessageFile(filename);
086: version = getMessage("Version.product",
087: getMessage("Version.number"));
088: msgFiles = oldMsgFiles;
089: messages = null;
090: } else {
091: version = getMessage("Version.product",
092: getMessage("Version.number"));
093: }
094: return version;
095: } // getVersion
096:
097: public static boolean isAttribute(String name, Hashtable symbolTable) {
098: SymtabEntry entry = (SymtabEntry) symbolTable.get(name);
099: return entry == null ? false : entry instanceof AttributeEntry;
100: } // isAttribute
101:
102: public static boolean isConst(String name, Hashtable symbolTable) {
103: SymtabEntry entry = (SymtabEntry) symbolTable.get(name);
104: return entry == null ? false : entry instanceof ConstEntry;
105: } // isConst
106:
107: public static boolean isEnum(String name, Hashtable symbolTable) {
108: SymtabEntry entry = (SymtabEntry) symbolTable.get(name);
109: return entry == null ? false : entry instanceof EnumEntry;
110: } // isEnum
111:
112: public static boolean isException(String name, Hashtable symbolTable) {
113: SymtabEntry entry = (SymtabEntry) symbolTable.get(name);
114: return entry == null ? false : entry instanceof ExceptionEntry;
115: } // isException
116:
117: public static boolean isInterface(String name, Hashtable symbolTable) {
118: SymtabEntry entry = (SymtabEntry) symbolTable.get(name);
119: return entry == null ? false : entry instanceof InterfaceEntry;
120: } // isInterface
121:
122: public static boolean isMethod(String name, Hashtable symbolTable) {
123: SymtabEntry entry = (SymtabEntry) symbolTable.get(name);
124: return entry == null ? false : entry instanceof MethodEntry;
125: } // isMethod
126:
127: public static boolean isModule(String name, Hashtable symbolTable) {
128: SymtabEntry entry = (SymtabEntry) symbolTable.get(name);
129: return entry == null ? false : entry instanceof ModuleEntry;
130: } // isModule
131:
132: public static boolean isParameter(String name, Hashtable symbolTable) {
133: SymtabEntry entry = (SymtabEntry) symbolTable.get(name);
134: return entry == null ? false : entry instanceof ParameterEntry;
135: } // isParameter
136:
137: public static boolean isPrimitive(String name, Hashtable symbolTable) {
138: // Distinguish "string" because the name could be something like:
139: // string(25 + 1)
140: SymtabEntry entry = (SymtabEntry) symbolTable.get(name);
141: if (entry == null) {
142: // If it is null then it may be of the form string(<exp>).
143: // Don't just check for string because the name "string" may
144: // have been overridden.
145: int parenIndex = name.indexOf('(');
146: if (parenIndex >= 0)
147: entry = (SymtabEntry) symbolTable.get(name.substring(0,
148: parenIndex));
149: }
150: return entry == null ? false : entry instanceof PrimitiveEntry;
151: } // isPrimitive
152:
153: public static boolean isSequence(String name, Hashtable symbolTable) {
154: SymtabEntry entry = (SymtabEntry) symbolTable.get(name);
155: return entry == null ? false : entry instanceof SequenceEntry;
156: } // isSequence
157:
158: public static boolean isStruct(String name, Hashtable symbolTable) {
159: SymtabEntry entry = (SymtabEntry) symbolTable.get(name);
160: return entry == null ? false : entry instanceof StructEntry;
161: } // isStruct
162:
163: public static boolean isString(String name, Hashtable symbolTable) {
164: SymtabEntry entry = (SymtabEntry) symbolTable.get(name);
165: return entry == null ? false : entry instanceof StringEntry;
166: } // isString
167:
168: public static boolean isTypedef(String name, Hashtable symbolTable) {
169: SymtabEntry entry = (SymtabEntry) symbolTable.get(name);
170: return entry == null ? false : entry instanceof TypedefEntry;
171: } // isTypedef
172:
173: public static boolean isUnion(String name, Hashtable symbolTable) {
174: SymtabEntry entry = (SymtabEntry) symbolTable.get(name);
175: return entry == null ? false : entry instanceof UnionEntry;
176: } // isUnion
177:
178: //////////////
179: // Message-related methods
180:
181: public static String getMessage(String key) {
182: if (messages == null)
183: readMessages();
184: String message = messages.getProperty(key);
185: if (message == null)
186: message = getDefaultMessage(key);
187: return message;
188: } // getMessage
189:
190: public static String getMessage(String key, String fill) {
191: if (messages == null)
192: readMessages();
193: String message = messages.getProperty(key);
194: if (message == null)
195: message = getDefaultMessage(key);
196: else {
197: int index = message.indexOf("%0");
198: if (index >= 0)
199: message = message.substring(0, index) + fill
200: + message.substring(index + 2);
201: }
202: return message;
203: } // getMessage
204:
205: public static String getMessage(String key, String[] fill) {
206: if (messages == null)
207: readMessages();
208: String message = messages.getProperty(key);
209: if (message == null)
210: message = getDefaultMessage(key);
211: else
212: for (int i = 0; i < fill.length; ++i) {
213: int index = message.indexOf("%" + i);
214: if (index >= 0)
215: message = message.substring(0, index) + fill[i]
216: + message.substring(index + 2);
217: }
218: return message;
219: } // getMessage
220:
221: private static String getDefaultMessage(String keyNotFound) {
222: String message = messages.getProperty(defaultKey);
223: int index = message.indexOf("%0");
224: if (index > 0)
225: message = message.substring(0, index) + keyNotFound;
226: return message;
227: } // getDefaultMessage
228:
229: /*
230: findFile is no longer used now that FileLocator has been provided
231: by Larry Raper of the Shasta team.
232:
233: static File findFile (String name) throws FileNotFoundException
234: {
235: String classpath = System.getProperty ("java.class.path");
236: String separator = System.getProperty ("path.separator");
237: int end = -separator.length (); // so the first pass classpath == original classpath
238: File file;
239: do
240: {
241: classpath = classpath.substring (end + separator.length ());
242: end = classpath.indexOf (separator);
243: if (end < 0) end = classpath.length ();
244: file = new File (classpath.substring (0, end) + File.separator + "com" + File.separator + "ibm" + File.separator + "idl" + File.separator + name);
245: } while (!file.exists () && end != classpath.length ());
246: if (!file.exists ()) throw new FileNotFoundException ();
247: return file;
248: } // findFile
249: */
250:
251: private static void readMessages() {
252: messages = new Properties();
253: Enumeration fileList = msgFiles.elements();
254: DataInputStream stream;
255: while (fileList.hasMoreElements())
256: try {
257: stream = FileLocator
258: .locateLocaleSpecificFileInClassPath((String) fileList
259: .nextElement());
260: messages.load(stream);
261: } catch (IOException e) {
262: }
263: if (messages.size() == 0)
264: messages.put(defaultKey, "Error reading Messages File.");
265: } // readMessages
266:
267: /** Register a message file. This file will be searched for
268: in the CLASSPATH. */
269: public static void registerMessageFile(String filename) {
270: if (filename != null)
271: if (messages == null)
272: msgFiles.addElement(filename);
273: else
274: try {
275: DataInputStream stream = FileLocator
276: .locateLocaleSpecificFileInClassPath(filename);
277: messages.load(stream);
278: } catch (IOException e) {
279: }
280: } // registerMessageFile
281:
282: private static Properties messages = null;
283: private static String defaultKey = "default";
284: private static Vector msgFiles = new Vector();
285: static {
286: msgFiles.addElement("com/sun/tools/corba/se/idl/idl.prp");
287: }
288:
289: // Message-related methods
290: ///////////////
291:
292: public static String capitalize(String lc) {
293: String first = new String(lc.substring(0, 1));
294: first = first.toUpperCase();
295: return first + lc.substring(1);
296: } // capitalize
297:
298: ///////////////
299: // General file methods
300:
301: /** Searches the current user directory and a list of directories for
302: a given short file name and returns its absolute file specification.
303: @return Absolute file name of a given short filename
304: @throws FileNotFoundException The file does not exist in the
305: current user or specified directories.
306: @see java.io.File.getAbsolutePath */
307: public static String getAbsolutePath(String filename,
308: Vector includePaths) throws FileNotFoundException {
309: String filepath = null;
310: File file = new File(filename);
311: if (file.canRead())
312: filepath = file.getAbsolutePath();
313: else {
314: String fullname = null;
315: Enumeration pathList = includePaths.elements();
316: while (!file.canRead() && pathList.hasMoreElements()) {
317: fullname = (String) pathList.nextElement()
318: + File.separatorChar + filename;
319: file = new File(fullname);
320: }
321: if (file.canRead())
322: filepath = file.getPath();
323: else
324: throw new FileNotFoundException(filename);
325: }
326: return filepath;
327: } // getAbsolutePath
328:
329: // General file methods
330: ///////////////
331:
332: ///////////////
333: // Numeric computations
334:
335: // <d62023>
336: /**
337: * Compute the absolute value of the difference between two floating-point
338: * numbers having single precision.
339: * @return the absolute value of the difference between two floats.
340: **/
341: public static float absDelta(float f1, float f2) {
342: double delta = f1 - f2;
343: return (float) ((delta < 0) ? delta * -1.0 : delta);
344: } // absDelta
345:
346: // Numeric computations
347: ///////////////
348:
349: static RepositoryID emptyID = new RepositoryID();
350: } // class Util
|