001: /* $Id: VariableAttributes.java 471661 2006-11-06 08:09:25Z skitching $
002: *
003: * Licensed to the Apache Software Foundation (ASF) under one or more
004: * contributor license agreements. See the NOTICE file distributed with
005: * this work for additional information regarding copyright ownership.
006: * The ASF licenses this file to You under the Apache License, Version 2.0
007: * (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: package org.apache.commons.digester.substitution;
020:
021: import org.xml.sax.Attributes;
022:
023: import java.util.ArrayList;
024:
025: /**
026: * <p>Wrapper for an org.xml.sax.Attributes object which expands any
027: * "variables" referenced in the attribute value via ${foo} or similar.
028: * This is only done when something actually asks for the attribute value,
029: * thereby imposing no performance penalty if the attribute is not used.</p>
030: *
031: * @since 1.6
032: */
033:
034: public class VariableAttributes implements Attributes {
035:
036: // list of mapped attributes.
037: private ArrayList values = new ArrayList(10);
038:
039: private Attributes attrs;
040: private VariableExpander expander;
041:
042: // ------------------- Public Methods
043:
044: /**
045: * Specify which attributes class this object is a proxy for.
046: */
047: public void init(Attributes attrs, VariableExpander expander) {
048: this .attrs = attrs;
049: this .expander = expander;
050:
051: // I hope this doesn't release the memory for this array; for
052: // efficiency, this should just mark the array as being size 0.
053: values.clear();
054: }
055:
056: public String getValue(int index) {
057: if (index >= values.size()) {
058: // Expand the values array with null elements, so the later
059: // call to set(index, s) works ok.
060: //
061: // Unfortunately, there is no easy way to set the size of
062: // an arraylist; we must repeatedly add null elements to it..
063: values.ensureCapacity(index + 1);
064: for (int i = values.size(); i <= index; ++i) {
065: values.add(null);
066: }
067: }
068:
069: String s = (String) values.get(index);
070:
071: if (s == null) {
072: // we have never been asked for this value before.
073: // get the real attribute value and perform substitution
074: // on it.
075: s = attrs.getValue(index);
076: if (s != null) {
077: s = expander.expand(s);
078: values.set(index, s);
079: }
080: }
081:
082: return s;
083: }
084:
085: public String getValue(String qname) {
086: int index = attrs.getIndex(qname);
087: if (index == -1) {
088: return null;
089: }
090: return getValue(index);
091: }
092:
093: public String getValue(String uri, String localname) {
094: int index = attrs.getIndex(uri, localname);
095: if (index == -1) {
096: return null;
097: }
098: return getValue(index);
099: }
100:
101: // plain proxy methods follow : nothing interesting :-)
102: public int getIndex(String qname) {
103: return attrs.getIndex(qname);
104: }
105:
106: public int getIndex(String uri, String localpart) {
107: return attrs.getIndex(uri, localpart);
108: }
109:
110: public int getLength() {
111: return attrs.getLength();
112: }
113:
114: public String getLocalName(int index) {
115: return attrs.getLocalName(index);
116: }
117:
118: public String getQName(int index) {
119: return attrs.getQName(index);
120: }
121:
122: public String getType(int index) {
123: return attrs.getType(index);
124: }
125:
126: public String getType(String qname) {
127: return attrs.getType(qname);
128: }
129:
130: public String getType(String uri, String localname) {
131: return attrs.getType(uri, localname);
132: }
133:
134: public String getURI(int index) {
135: return attrs.getURI(index);
136: }
137:
138: }
|