1 45 package org.exolab.jms.config; 46 47 import java.io.IOException ; 48 import java.io.Reader ; 49 50 import org.apache.commons.logging.Log; 51 import org.apache.commons.logging.LogFactory; 52 53 import org.exolab.castor.util.Configuration; 54 import org.exolab.castor.xml.EventProducer; 55 import org.xml.sax.AttributeList ; 56 import org.xml.sax.DocumentHandler ; 57 import org.xml.sax.ErrorHandler ; 58 import org.xml.sax.InputSource ; 59 import org.xml.sax.Locator ; 60 import org.xml.sax.Parser ; 61 import org.xml.sax.SAXException ; 62 import org.xml.sax.helpers.AttributeListImpl ; 63 64 65 80 public class AttributeExpander implements EventProducer { 81 82 private DocumentHandler _handler = null; 83 private Reader _reader = null; 84 85 88 private static final Log _log = 89 LogFactory.getLog(AttributeExpander.class); 90 91 92 97 public AttributeExpander(Reader reader) { 98 _reader = reader; 99 } 100 101 104 public void setDocumentHandler(DocumentHandler handler) { 105 _handler = handler; 106 } 107 108 111 public void start() throws SAXException { 112 Parser parser = Configuration.getDefaultParser(); 113 if (parser == null) { 114 throw new SAXException ("Unable to create parser"); 115 } 116 117 DocumentHandler handler = new AttributeInterceptor(); 118 parser.setDocumentHandler(handler); 119 try { 120 parser.parse(new InputSource (_reader)); 121 } catch (IOException exception) { 122 throw new SAXException (exception.getMessage(), exception); 123 } 124 } 125 126 130 private class AttributeInterceptor implements DocumentHandler { 131 132 public void setDocumentLocator(Locator locator) { 133 _handler.setDocumentLocator(locator); 134 } 135 136 public void startDocument() throws SAXException { 137 _handler.startDocument(); 138 } 139 140 public void endDocument() throws SAXException { 141 _handler.endDocument(); 142 } 143 144 public void startElement(String name, AttributeList list) 145 throws SAXException { 146 147 AttributeListImpl replaced = new AttributeListImpl (); 148 for (int i = 0; i < list.getLength(); i++) { 149 String value = expand(list.getName(i), list.getValue(i)); 150 replaced.addAttribute(list.getName(i), list.getType(i), value); 151 } 152 _handler.startElement(name, replaced); 153 } 154 155 public void endElement(String name) throws SAXException { 156 _handler.endElement(name); 157 } 158 159 public void characters(char[] ch, int start, int length) 160 throws SAXException { 161 _handler.characters(ch, start, length); 162 } 163 164 public void ignorableWhitespace(char[] ch, int start, int length) 165 throws SAXException { 166 _handler.ignorableWhitespace(ch, start, length); 167 } 168 169 public void processingInstruction(String target, String data) 170 throws SAXException { 171 _handler.processingInstruction(target, data); 172 } 173 174 private String expand(String attribute, String value) { 175 StringBuffer buffer = new StringBuffer (); 176 int prev = 0; 177 int pos; 178 while ((pos = value.indexOf("${", prev)) >= 0) { 179 if (pos > 0) { 180 buffer.append(value.substring(prev, pos)); 181 } 182 int index = value.indexOf('}', pos); 183 if (index < 0) { 184 _log.warn("Cannot expand " + attribute 186 + " - invalid format: " + value); 187 buffer.append("${"); 188 prev = pos + 2; 189 } else { 190 String name = value.substring(pos + 2, index); 191 String property = System.getProperty(name); 192 if (property != null) { 193 buffer.append(property); 194 } else { 195 _log.warn("Cannot expand " + attribute 198 + " as property " + name 199 + " is not defined"); 200 buffer.append("${"); 201 buffer.append(name); 202 buffer.append("}"); 203 } 204 prev = index + 1; 205 } 206 } 207 if (prev < value.length()) { 208 buffer.append(value.substring(prev)); 209 } 210 String result = buffer.toString(); 211 return result; 212 } 213 214 } 216 } | Popular Tags |