KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > digester > substitution > VariableAttributes


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

17
18
19 package org.apache.commons.digester.substitution;
20
21 import org.xml.sax.Attributes JavaDoc;
22
23 import java.util.ArrayList JavaDoc;
24
25
26 /**
27  * <p>Wrapper for an org.xml.sax.Attributes object which expands any
28  * "variables" referenced in the attribute value via ${foo} or similar.
29  * This is only done something actually asks for the attribute value,
30  * thereby imposing no performance penalty if the attribute is not used.</p>
31  *
32  * @since 1.6
33  */

34
35 public class VariableAttributes implements Attributes JavaDoc {
36
37     // list of mapped attributes.
38
private ArrayList JavaDoc values = new ArrayList JavaDoc(10);
39
40     private Attributes JavaDoc attrs;
41     private VariableExpander expander;
42     
43     // ------------------- Public Methods
44

45     /**
46      * Specify which attributes class this object is a proxy for.
47      */

48     public void init(Attributes JavaDoc attrs, VariableExpander expander) {
49         this.attrs = attrs;
50         this.expander = expander;
51
52         // I hope this doesn't release the memory for this array; for
53
// efficiency, this should just mark the array as being size 0.
54
values.clear();
55     }
56
57     public String JavaDoc getValue(int index) {
58         if (index >= values.size()) {
59             // Expand the values array with null elements, so the later
60
// call to set(index, s) works ok.
61
//
62
// Unfortunately, there is no easy way to set the size of
63
// an arraylist; we must repeatedly add null elements to it..
64
values.ensureCapacity(index+1);
65             for(int i = values.size(); i<= index; ++i) {
66                 values.add(null);
67             }
68         }
69         
70         String JavaDoc s = (String JavaDoc) values.get(index);
71         
72         if (s == null) {
73             // we have never been asked for this value before.
74
// get the real attribute value and perform substitution
75
// on it.
76
s = attrs.getValue(index);
77             if (s != null) {
78                 s = expander.expand(s);
79                 values.set(index, s);
80             }
81         }
82         
83         return s;
84     }
85     
86     public String JavaDoc getValue(String JavaDoc qname) {
87         int index = attrs.getIndex(qname);
88         if (index == -1) {
89             return null;
90         }
91         return getValue(index);
92     }
93     
94     public String JavaDoc getValue(String JavaDoc uri, String JavaDoc localname) {
95         int index = attrs.getIndex(uri, localname);
96         if (index == -1) {
97             return null;
98         }
99         return getValue(index);
100     }
101     
102     // plain proxy methods follow : nothing interesting :-)
103
public int getIndex(String JavaDoc qname) {
104         return attrs.getIndex(qname);
105     }
106     
107     public int getIndex(String JavaDoc uri, String JavaDoc localpart) {
108         return attrs.getIndex(uri, localpart);
109     }
110     
111     public int getLength() {
112         return attrs.getLength();
113     }
114     
115     public String JavaDoc getLocalName(int index) {
116         return attrs.getLocalName(index);
117     }
118     
119     public String JavaDoc getQName(int index) {
120         return attrs.getQName(index);
121     }
122     
123     public String JavaDoc getType(int index) {
124         return attrs.getType(index);
125     }
126
127     public String JavaDoc getType(String JavaDoc qname) {
128         return attrs.getType(qname);
129     }
130     
131     public String JavaDoc getType(String JavaDoc uri, String JavaDoc localname) {
132         return attrs.getType(uri, localname);
133     }
134     
135     public String JavaDoc getURI(int index) {
136         return attrs.getURI(index);
137     }
138  
139 }
140
Popular Tags