FileDocCategorySizeDatePackage
VariableExpander.javaAPI DocGlassfish v2 API7284Fri May 04 22:24:38 BST 2007com.sun.enterprise.config.serverbeans.validation

VariableExpander

public class VariableExpander extends XMLFilterImpl
This class provides a variable expansion mechanism on a sax stream. Variable references are strings that begin with "${" and end with "}", with the variable name lying between the braces. For example "${foo}" is a reference to a variable called "foo". From some given set of definitions for these variables this class expands these variables on the fly. For example, if the variable foo is defined as having the value "27", then the following element:

would be expanded to: In addition to providing a simple variable expansion mechanism the class has to cope with the fact that the variable definitions to be used depend upon where the variable reference is found. One definition can come into scope for a while, and then go out of scope. The scoping mechanism is handled by the {@link Framer} class.

Fields Summary
Framer
framer
private static final String
START
private static final String
END
private StringBuffer
characters
private Frame
frame
private final boolean
DEBUG
Constructors Summary
VariableExpander()


    
        super();
    
VariableExpander(Framer f)

        framer = f;
//         super.setParent(f);
    
Methods Summary
public voidcharacters(char[] ch, int start, int len)

        characters.append(ch, start, len);
//         frame = framer.currentFrame();
    
private final voiddebug(java.lang.String s)

    
         
       if (DEBUG) System.err.println(s);
    
public voidendDocument()

        processCharacters();
        framer.endDocument();
        super.endDocument();
    
public voidendElement(java.lang.String namespaceURI, java.lang.String localName, java.lang.String qName)

        processCharacters();
        framer.endElement(namespaceURI, localName, qName);
        super.endElement(namespaceURI, localName, qName);
    
java.lang.Stringeval(java.lang.String chars, Frame frame)

        debug("expanding \""+chars+"\"");
        StringBuffer sb = new StringBuffer();
        int vs, ve = 0;         // variable start, end
          // loop invariant - i is at start of next part of string to
          // be examined. sb contains all previous parts of string,
          // including the translated portions
        for (int  i = 0; i < chars.length(); ){
            if (((vs = chars.indexOf(START, i)) != -1) && ((ve = chars.indexOf(END, i)) != -1)) {
                sb.append(chars.substring(i, vs)); // copy prefix
                sb.append(frame.lookup(chars.substring(vs+2, ve)));
                i = ve + 1;
            } else {            // No variable to be expanded
                sb.append(chars.substring(i));
                i = chars.length();
            }
        }
        return sb.toString();
    
private java.lang.StringexpandVariables(java.lang.String chars, Frame f)

        return ((chars.indexOf(START) < chars.indexOf(END)) ? eval(chars, f) : chars);
    
private java.lang.StringexpandVariables(java.lang.String chars)

        return expandVariables(chars, framer.currentFrame());
    
private org.xml.sax.AttributesprocessAttributes(org.xml.sax.Attributes atts)

        final AttributesImpl newAtts = new AttributesImpl(atts);
        for (int i = 0; i < newAtts.getLength(); i++){
            newAtts.setValue(i, expandVariables(newAtts.getValue(i)));
        }
        return newAtts;
    
private voidprocessCharacters()

        if (characters.length() > 0){
            final String chars = expandVariables(characters.toString());
            super.characters(chars.toCharArray(), 0, chars.length());
            characters.setLength(0);
//             frame = null;
        }
    
voidsetFramer(Framer f)

//         if (framer != null && framer.getParent() != null) {
//             f.setParent(framer.getParent());
//         }
//         super.setParent(f);
        framer = f;
    
public voidstartElement(java.lang.String namespaceURI, java.lang.String localName, java.lang.String qName, org.xml.sax.Attributes atts)

        processCharacters();
        framer.startElement(namespaceURI, localName, qName, atts);
        final Attributes newAtts = processAttributes(atts);
        super.startElement(namespaceURI, localName, qName, newAtts);