KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > jdbc > ParametersParser


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package scriptella.jdbc;
17
18 import scriptella.expression.Expression;
19 import scriptella.spi.DriverContext;
20 import scriptella.spi.ParametersCallback;
21
22 import java.net.MalformedURLException JavaDoc;
23 import java.net.URL JavaDoc;
24
25 /**
26  * Parses parameter expressions in SQL statements.
27  * This class use {@link Expression} parsing mechanism except the
28  * file reference case.
29  * <p>The syntax of the file reference is the following:
30  * <p>?{file &lt;Expression&gt;}
31  * <p>The result of expression evaluation must be of String or URL type.
32  * <p>Examples:
33  * <ul>
34  * <li>?{file 'http://site.com/img.gif')
35  * <li>?{file 'file:/path/img.gif')
36  * <li>?{file 'file:img.gif') - represents a reference relative to a directory where script file located.
37  * </ul>
38  * @see Expression
39  *
40  * @author Fyodor Kupolov
41  * @version 1.0
42  */

43 public class ParametersParser {
44     private DriverContext driverContext;
45
46
47     /**
48      * Creates a file reference parser.
49      * @param driverContext drivers content to use for URL resolution.
50      */

51     public ParametersParser(DriverContext driverContext) {
52         this.driverContext = driverContext;
53     }
54
55     /**
56      * Parses specified expression and returns the result of evaluation.
57      * @param expression expression to parse.
58      * @return result of parse.
59      */

60     public Object JavaDoc evaluate(final String JavaDoc expression, final ParametersCallback parameters) {
61         if (expression.startsWith("file ")) {
62             //now checking the syntax
63
try {
64                 final Expression ex = Expression.compile(expression.substring(5));//file prefix removed
65
final Object JavaDoc o = ex.evaluate(parameters);
66                 if (o==null) {
67                     throw new JdbcException("Failed to evaluate file URL", expression);
68                 }
69                 if (o instanceof URL JavaDoc) {
70                     return Lobs.newBlob((URL JavaDoc) o);
71                 } else {
72                     try {
73                         return Lobs.newBlob(driverContext.resolve(String.valueOf(o)));
74                     } catch (MalformedURLException JavaDoc e) {
75                         throw new JdbcException("Wrong file URL \""+o+"\"", e, expression);
76                     }
77                 }
78             } catch (Expression.ParseException e) {
79                 //If parsing fails try to evaluate the whole expression not the file reference
80
}
81         }
82         return Expression.compile(expression).evaluate(parameters);
83     }
84
85 }
86
Popular Tags