KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jga > parser > GenericParser


1 // ============================================================================
2
// $Id: GenericParser.java,v 1.15 2006/09/02 02:25:09 davidahall Exp $
3
// Copyright (c) 2004-2005 David A. Hall
4
// ============================================================================
5
// The contents of this file are subject to the Common Development and
6
// Distribution License (CDDL), Version 1.0 (the License); you may not use this
7
// file except in compliance with the License. You should have received a copy
8
// of the the License along with this file: if not, a copy of the License is
9
// available from Sun Microsystems, Inc.
10
//
11
// http://www.sun.com/cddl/cddl.html
12
//
13
// From time to time, the license steward (initially Sun Microsystems, Inc.) may
14
// publish revised and/or new versions of the License. You may not use,
15
// distribute, or otherwise make this file available under subsequent versions
16
// of the License.
17
//
18
// Alternatively, the contents of this file may be used under the terms of the
19
// GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
20
// case the provisions of the LGPL are applicable instead of those above. If you
21
// wish to allow use of your version of this file only under the terms of the
22
// LGPL, and not to allow others to use your version of this file under the
23
// terms of the CDDL, indicate your decision by deleting the provisions above
24
// and replace them with the notice and other provisions required by the LGPL.
25
// If you do not delete the provisions above, a recipient may use your version
26
// of this file under the terms of either the CDDL or the LGPL.
27
//
28
// This library is distributed in the hope that it will be useful,
29
// but WITHOUT ANY WARRANTY; without even the implied warranty of
30
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
31
// ============================================================================
32

33 package net.sf.jga.parser;
34
35 import java.text.MessageFormat JavaDoc;
36 import net.sf.jga.fn.BinaryFunctor;
37 import net.sf.jga.fn.Generator;
38 import net.sf.jga.fn.UnaryFunctor;
39
40 /**
41  * FunctorParser wrapper that adds runtime type-safe entry points to the non-generified
42  * base parser. This class maps between the inherently type<b>un</b>safe
43  * baseclass and the generic functors that are returned: if the returned functor
44  * does not return the correct type, then a ClassCastException will be thrown by
45  * the entry point.
46  * <p>
47  * Copyright &copy; 2004-2005 David A. Hall
48  * @author <a HREF="mailto:davidahall@users.sf.net">David A. Hall</a>
49  */

50
51 public class GenericParser implements IParser {
52
53     // global instance
54
static private GenericParser _instance;
55
56     /**
57      * Returns a globally accessible default instance of a GenericParser.
58      */

59     static public synchronized GenericParser getInstance() {
60         if (_instance == null) {
61             _instance = new GenericParser(FunctorParser.getInstance());
62         }
63
64         return _instance;
65     }
66
67
68     private IParser _delegate;
69
70     public GenericParser() { _delegate = new JFXGParser(); }
71     public GenericParser(IParser delegate) { _delegate = delegate; }
72     
73     //======================
74
// Parser Entry Points
75
//======================
76

77     /**
78      * Parses the string to create a Generator.
79      */

80     public <R> Generator<R> parseGenerator(String JavaDoc str, Class JavaDoc<R> returnType)
81         throws ClassCastException JavaDoc, ParseException
82     {
83         Generator gen = parseGenerator(str);
84         if (returnType.isAssignableFrom(getReturnType()))
85             return (Generator<R>) gen;
86         
87         String JavaDoc err = "Expected Generator<{0}>, but expression returns {1}";
88         String JavaDoc msg= MessageFormat.format(err, new Object JavaDoc[] {returnType.getName(),
89                                                             getReturnType().getName()});
90         throw new ClassCastException JavaDoc(msg);
91     }
92
93     /**
94      * Parses the string to create a UnaryFunctor that takes an argument of the
95      * given type.
96      */

97     public <T,R> UnaryFunctor<T,R> parseUnary(String JavaDoc str, Class JavaDoc<T> argType, Class JavaDoc<R> returnType)
98         throws ClassCastException JavaDoc, ParseException
99     {
100         UnaryFunctor uf = parseUnary(str, argType);
101         if (returnType.isAssignableFrom(getReturnType()))
102             return (UnaryFunctor<T,R>) uf;
103
104         String JavaDoc err = "Expected UnaryFunctor<{0},{1}>, but expression returns {2}";
105         String JavaDoc msg= MessageFormat.format(err, new Object JavaDoc[] {argType.getName(),
106                                                             returnType.getName(),
107                                                             getReturnType().getName()});
108         throw new ClassCastException JavaDoc(msg);
109     }
110     
111     /**
112      * Parses the string to create a BinaryFunctor that takes arguments of the
113      * given types.
114      */

115     public <T1,T2,R> BinaryFunctor<T1,T2,R>
116     parseBinary(String JavaDoc str, Class JavaDoc<T1> arg1Type, Class JavaDoc<T2> arg2Type, Class JavaDoc<R> returnType)
117         throws ClassCastException JavaDoc, ParseException
118     {
119         BinaryFunctor bf = parseBinary(str, arg1Type, arg2Type);
120         if (returnType.isAssignableFrom(getReturnType()))
121             return (BinaryFunctor<T1,T2,R>) bf;
122
123         String JavaDoc err = "Expected BinaryFunctor<{0},{1},{2}>, but expression returns {3}";
124         String JavaDoc msg= MessageFormat.format(err, new Object JavaDoc[] {arg1Type.getName(),
125                                                             arg2Type.getName(),
126                                                             returnType.getName(),
127                                                             getReturnType().getName()});
128         throw new ClassCastException JavaDoc(msg);
129     }
130
131
132     //=======================
133
// IParser implementation
134
//=======================
135

136     /**
137      * Parses the string to create a Generator.
138      */

139     public Generator parseGenerator(String JavaDoc str) throws ParseException {
140         return _delegate.parseGenerator(str);
141     }
142     
143     /**
144      * Parses the string to create a UnaryFunctor that takes an argument of the
145      * given type.
146      */

147     public UnaryFunctor parseUnary(String JavaDoc str, Class JavaDoc argType) throws ParseException {
148         return _delegate.parseUnary(str, argType);
149     }
150
151     /**
152      * Parses the string to create a BinaryFunctor that takes arguments of the
153      * given types.
154      */

155     public BinaryFunctor parseBinary(String JavaDoc str, Class JavaDoc arg1Type, Class JavaDoc arg2Type)
156         throws ParseException
157     {
158         return _delegate.parseBinary(str, arg1Type, arg2Type);
159     }
160     
161     /**
162      * Returns the type of object returned by the last functor parsed.
163      * @throws IllegalStateException if the parser has not been used or if
164      * parsing the last functor resulted in an exception being thrown.
165      */

166     public Class JavaDoc getReturnType() {
167         return _delegate.getReturnType();
168     }
169     
170     //======================
171
// Static entry points
172
//======================
173

174    /**
175      * Parses the string to create a Generator.
176      */

177     static public <R> Generator<R> parse(String JavaDoc str, Class JavaDoc<R> returnType)
178         throws UncheckedParseException, ClassCastException JavaDoc
179     {
180         try {
181             return getInstance().parseGenerator(str);
182         }
183         catch(ParseException x) {
184             throw new UncheckedParseException(x);
185         }
186     }
187
188  
189     /**
190      * Parses the string to create a UnaryFunctor that takes an argument of the
191      * given type.
192      */

193     static public <T,R> UnaryFunctor<T,R> parse(String JavaDoc str, Class JavaDoc<T> argType, Class JavaDoc<R> returnType)
194             throws UncheckedParseException, ClassCastException JavaDoc
195     {
196         try {
197             return getInstance().parseUnary(str, argType, returnType);
198         }
199         catch(ParseException x) {
200             throw new UncheckedParseException(x);
201         }
202     }
203
204  
205     /**
206      * Parses the string to create a BinaryFunctor that takes arguments of the
207      * given types.
208      */

209     static public <T1,T2,R> BinaryFunctor<T1,T2,R>
210     parse(String JavaDoc str, Class JavaDoc<T1> arg1Type, Class JavaDoc<T2> arg2Type, Class JavaDoc<R> returnType)
211         throws UncheckedParseException, ClassCastException JavaDoc
212     {
213         try {
214             return getInstance().parseBinary(str, arg1Type, arg2Type, returnType);
215         }
216         catch(ParseException x) {
217             throw new UncheckedParseException(x);
218         }
219     }
220
221     //======================
222
// Deprecated methods
223
//======================
224

225     // All of these methods are deprecated, and will be removed in a future release.
226
// These were all part of the public interface of the base class, and had been
227
// available before this class was refactored to be a wrapper rather than a
228
// derived class. For one more release, we'll support them.
229

230     // The supported use for these methods is to build and configure the actual parser,
231
// then wrap it in a Generic wrapper.
232

233     /**
234      * @deprecated Configure the delegate parser prior to wrapping it
235      */

236     public void importClass(Class JavaDoc clasz) {
237         ((JFXGParser) _delegate).importClass(clasz);
238     }
239
240  
241     /**
242      * @deprecated Configure the delegate parser prior to wrapping it
243      */

244     public void importClass(String JavaDoc alias, Class JavaDoc clasz) {
245         ((JFXGParser) _delegate).importClass(alias, clasz);
246     }
247
248  
249     /**
250      * @deprecated Configure the delegate parser prior to wrapping it
251      */

252     public void deportClass(String JavaDoc alias) {
253         ((JFXGParser) _delegate).deportClass(alias);
254     }
255
256  
257     /**
258      * @deprecated Configure the delegate parser prior to wrapping it
259      */

260     public void importStatics(Class JavaDoc clasz) {
261         ((JFXGParser) _delegate).importStatics(clasz);
262     }
263
264  
265     /**
266      * @deprecated Configure the delegate parser prior to wrapping it
267      */

268     public void importField(Class JavaDoc clasz, String JavaDoc name) throws NoSuchFieldException JavaDoc {
269         ((JFXGParser) _delegate).importField(clasz, name);
270     }
271
272     /**
273      * @deprecated Configure the delegate parser prior to wrapping it
274      */

275     public void importField(java.lang.reflect.Field JavaDoc field) throws IllegalArgumentException JavaDoc {
276         ((JFXGParser) _delegate).importField(field);
277     }
278
279     /**
280      * @deprecated Configure the delegate parser prior to wrapping it
281      */

282     public java.lang.reflect.Field JavaDoc getImportedField(String JavaDoc name) {
283         return ((JFXGParser) _delegate).getImportedField(name);
284     }
285  
286     /**
287      * @deprecated Configure the delegate parser prior to wrapping it
288      */

289     public void importMethod(Class JavaDoc clasz, String JavaDoc name) throws NoSuchMethodException JavaDoc {
290         ((JFXGParser) _delegate).importMethod(clasz, name);
291     }
292
293     /**
294      * @deprecated Configure the delegate parser prior to wrapping it
295      */

296     public void importMethod(java.lang.reflect.Method JavaDoc meth) {
297         ((JFXGParser) _delegate).importMethod(meth);
298     }
299  
300     /**
301      * @deprecated Configure the delegate parser prior to wrapping it
302      */

303     public void importMethod(String JavaDoc name, java.lang.reflect.Method JavaDoc meth) {
304         ((JFXGParser) _delegate).importMethod(name, meth);
305     }
306
307
308     /**
309      * @deprecated Configure the delegate parser prior to wrapping it
310      */

311     public java.lang.reflect.Method JavaDoc[] getImportedMethods(String JavaDoc name) {
312         return ((JFXGParser) _delegate).getImportedMethods(name);
313     }
314  
315
316     /**
317      * @deprecated Configure the delegate parser prior to wrapping it
318      */

319     public void bindThis(Object JavaDoc thisBinding) {
320         ((JFXGParser) _delegate).bindThis(thisBinding);
321     }
322
323  
324     /**
325      * @deprecated Configure the delegate parser prior to wrapping it
326      */

327     protected Object JavaDoc getBoundObject() {
328         return ((JFXGParser) _delegate).getBoundObject();
329     }
330  
331  
332     /**
333      * @deprecated Configure the delegate parser prior to wrapping it
334      */

335     public void setUndecoratedDecimal(boolean flag) {
336         ((JFXGParser) _delegate).setUndecoratedDecimal(flag);
337     }
338
339  
340     /**
341      * @deprecated Configure the delegate parser prior to wrapping it
342      */

343     public boolean isUndecoratedDecimal() {
344         return ((JFXGParser) _delegate).isUndecoratedDecimal();
345     }
346
347 }
348
Popular Tags