KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > transformation > helpers > InterpolatingConfigurationHandler


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.transformation.helpers;
17
18 import java.util.Map JavaDoc;
19 import java.util.StringTokenizer JavaDoc;
20
21 import org.apache.avalon.framework.configuration.SAXConfigurationHandler;
22 import org.xml.sax.Attributes JavaDoc;
23 import org.xml.sax.SAXException JavaDoc;
24 import org.xml.sax.helpers.AttributesImpl JavaDoc;
25
26 /**
27  * A SAX ContentHandler that builds Avalon <code>Configuration</code> objects,
28  * but also replaces variables of the form {var} with values from a map.
29  *
30  * @see VariableConfiguration
31  * @author <a HREF="jefft@apache.org">Jeff Turner</a>
32  * @version CVS $Id: InterpolatingConfigurationHandler.java 30932 2004-07-29 17:35:38Z vgritsenko $
33  */

34 public class InterpolatingConfigurationHandler extends SAXConfigurationHandler {
35     final private Map JavaDoc vars;
36     final private String JavaDoc location;
37
38     /** Constructor.
39      * @param vars The mappings from variable name to value.
40      */

41     public InterpolatingConfigurationHandler(Map JavaDoc vars) {
42         this.vars = vars;
43         this.location = "Unknown";
44     }
45
46     /** Constructor.
47      * @param vars The mappings from variable name to value.
48      * @param location The origin of this configuration info.
49      */

50     public InterpolatingConfigurationHandler(Map JavaDoc vars, String JavaDoc location) {
51         this.vars = vars;
52         this.location = location;
53     }
54
55     /** Replace {vars} in attributes. */
56     public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc attr) throws SAXException JavaDoc {
57         AttributesImpl JavaDoc newAttr = new AttributesImpl JavaDoc(attr);
58         for (int i=0; i<attr.getLength(); i++) {
59             newAttr.setValue(i, interp(attr.getValue(i)));
60         }
61         super.startElement(uri, localName, qName, newAttr);
62     }
63
64     /** Replace {vars} in element bodies. */
65     public void characters( final char[] ch, int start, int len )
66         throws SAXException JavaDoc
67     {
68         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
69         if (start!=0) buf.append(ch, 0, start-1);
70         String JavaDoc newVal = interp(new String JavaDoc(ch,start, len));
71         buf.append(newVal);
72         buf.append(ch, start+len, ch.length-(start+len));
73         super.characters(buf.toString().toCharArray(), start, newVal.length());
74     }
75
76     protected String JavaDoc getLocationString() {
77         return this.location;
78     }
79
80
81     /**
82      * Interpolate variable values into a string.
83      *
84      * @param str String with {var} tokens
85      * @return <code>str</code>, with {variables} replaced by values. If an
86      * unknown variable token is encountered it is ignored.
87      */

88     private String JavaDoc interp(String JavaDoc str) {
89         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(str.length()*2);
90         StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(str, "{}", true);
91         int state = 0; // 0=outside, 1=inside
92
while (tok.hasMoreTokens()) {
93             String JavaDoc token = tok.nextToken();
94             if (state == 0 && "{".equals(token)) {
95                 state = 1;
96             } else if (state == 1 && "}".equals(token)) {
97                 state = 0;
98             } else if (state == 0) {
99                 buf.append(token);
100             } else if (state == 1) {
101                 //System.out.println("Replacing "+token+" with "+vars.get(token));
102
String JavaDoc val = (String JavaDoc)vars.get(token);
103                 if (val == null) {
104                     buf.append("{").append(token).append("}");
105                 } else{ buf.append(val); }
106             }
107         }
108         return buf.toString();
109     }
110
111 }
112
Popular Tags