KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > variables > NOPVariableResolver


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
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 org.apache.cocoon.components.variables;
17
18 import org.apache.cocoon.sitemap.PatternException;
19
20 /**
21  * No-op implementation of {@link VariableResolver} for constant expressions
22  *
23  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
24  *
25  * @version CVS $Id: NOPVariableResolver.java 125079 2005-01-13 14:56:12Z cziegeler $
26  */

27 public class NOPVariableResolver
28     implements VariableResolver {
29
30     protected String JavaDoc expression;
31
32     public NOPVariableResolver(String JavaDoc expression) {
33         this.expression = this.unescape(expression);
34     }
35
36     /**
37      * Unescape an expression that doesn't need to be resolved, but may contain
38      * escaped '{' characters.
39      *
40      * @param expression the expression to unescape.
41      * @return the unescaped result, or <code>expression</code> if unescaping isn't necessary.
42      */

43     protected String JavaDoc unescape(String JavaDoc expression) {
44         // Does it need escaping ?
45
if (expression == null || expression.indexOf("\\{") == -1) {
46             return expression;
47         }
48
49         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
50         for (int i = 0; i < expression.length(); i++) {
51             char ch = expression.charAt(i);
52             if (ch != '\\' || i >= (expression.length() - 1) || expression.charAt(i+1) != '{') {
53                 buf.append(ch);
54             }
55         }
56
57         return buf.toString();
58     }
59
60     /* (non-Javadoc)
61      * @see org.apache.cocoon.components.variables.VariableResolver#resolve()
62      */

63     public String JavaDoc resolve() throws PatternException {
64         return this.expression;
65     }
66
67 }
68
Popular Tags