1 23 24 package com.sun.enterprise.config.serverbeans.validation; 25 26 import org.xml.sax.Attributes ; 27 import org.xml.sax.SAXException ; 28 import org.xml.sax.XMLReader ; 29 import org.xml.sax.helpers.AttributesImpl ; 30 import org.xml.sax.helpers.XMLFilterImpl ; 31 32 55 56 class VariableExpander extends XMLFilterImpl 59 { 60 61 Framer framer = new Framer(); 62 63 private static final String START="${"; 64 private static final String END="}"; 65 66 private StringBuffer characters = new StringBuffer (); 73 private Frame frame; 74 75 VariableExpander(){ 76 super(); 77 } 78 79 80 VariableExpander(Framer f){ 81 framer = f; 82 } 84 85 void setFramer(Framer f){ 86 framer = f; 91 } 92 93 97 101 public void characters(char [] ch, int start, int len) throws SAXException { 102 characters.append(ch, start, len); 103 } 105 106 public void endDocument() throws SAXException { 107 processCharacters(); 108 framer.endDocument(); 109 super.endDocument(); 110 } 111 112 public void endElement(String namespaceURI, String localName, String qName) throws SAXException { 113 processCharacters(); 114 framer.endElement(namespaceURI, localName, qName); 115 super.endElement(namespaceURI, localName, qName); 116 } 117 118 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { 119 processCharacters(); 120 framer.startElement(namespaceURI, localName, qName, atts); 121 final Attributes newAtts = processAttributes(atts); 122 super.startElement(namespaceURI, localName, qName, newAtts); 123 } 124 private void processCharacters() throws SAXException { 125 if (characters.length() > 0){ 126 final String chars = expandVariables(characters.toString()); 127 super.characters(chars.toCharArray(), 0, chars.length()); 128 characters.setLength(0); 129 } 131 } 132 133 134 private String expandVariables(final String chars){ 135 return expandVariables(chars, framer.currentFrame()); 136 } 137 138 private String expandVariables(final String chars, final Frame f){ 139 return ((chars.indexOf(START) < chars.indexOf(END)) ? eval(chars, f) : chars); 140 } 141 142 private Attributes processAttributes(final Attributes atts){ 143 final AttributesImpl newAtts = new AttributesImpl (atts); 144 for (int i = 0; i < newAtts.getLength(); i++){ 145 newAtts.setValue(i, expandVariables(newAtts.getValue(i))); 146 } 147 return newAtts; 148 } 149 150 151 String eval(final String chars, final Frame frame){ 152 debug("expanding \""+chars+"\""); 153 StringBuffer sb = new StringBuffer (); 154 int vs, ve = 0; for (int i = 0; i < chars.length(); ){ 159 if (((vs = chars.indexOf(START, i)) != -1) && ((ve = chars.indexOf(END, i)) != -1)) { 160 sb.append(chars.substring(i, vs)); sb.append(frame.lookup(chars.substring(vs+2, ve))); 162 i = ve + 1; 163 } else { sb.append(chars.substring(i)); 165 i = chars.length(); 166 } 167 } 168 return sb.toString(); 169 } 170 171 private final boolean DEBUG = false; 172 173 private final void debug(final String s){ 174 if (DEBUG) System.err.println(s); 175 } 176 177 178 } 179 180 181 | Popular Tags |