View Javadoc
1 /*** 2 * Redistribution and use of this software and associated documentation 3 * ("Software"), with or without modification, are permitted provided 4 * that the following conditions are met: 5 * 6 * 1. Redistributions of source code must retain copyright 7 * statements and notices. Redistributions must also contain a 8 * copy of this document. 9 * 10 * 2. Redistributions in binary form must reproduce the 11 * above copyright notice, this list of conditions and the 12 * following disclaimer in the documentation and/or other 13 * materials provided with the distribution. 14 * 15 * 3. The name "Exolab" must not be used to endorse or promote 16 * products derived from this Software without prior written 17 * permission of Exoffice Technologies. For written permission, 18 * please contact tma@netspace.net.au. 19 * 20 * 4. Products derived from this Software may not be called "Exolab" 21 * nor may "Exolab" appear in their names without prior written 22 * permission of Exoffice Technologies. Exolab is a registered 23 * trademark of Exoffice Technologies. 24 * 25 * 5. Due credit should be given to the Exolab Project 26 * (http://www.exolab.org/). 27 * 28 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS 29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 32 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 39 * OF THE POSSIBILITY OF SUCH DAMAGE. 40 * 41 * Copyright 2003-2004 (C) Exoffice Technologies Inc. All Rights Reserved. 42 * 43 * $Id: ConfigExpander.java,v 1.2 2004/02/03 21:52:13 tanderson Exp $ 44 */ 45 package org.exolab.jmscts.util; 46 47 import java.io.IOException; 48 import java.io.Reader; 49 50 import org.xml.sax.AttributeList; 51 import org.xml.sax.DocumentHandler; 52 import org.xml.sax.Locator; 53 import org.xml.sax.InputSource; 54 import org.xml.sax.Parser; 55 import org.xml.sax.SAXException; 56 import org.xml.sax.helpers.AttributeListImpl; 57 58 import org.apache.log4j.Category; 59 60 import org.exolab.castor.util.Configuration; 61 import org.exolab.castor.xml.EventProducer; 62 63 64 /*** 65 * This class expands elements and attributes in XML documents as the document 66 * is being parsed. It is designed to be used in conjunction with the Castor 67 * unmarshalling framework. 68 * <p> 69 * To be expanded, values must contain text of the form <i>${property.name}</i> 70 * , where <i>property.name</i> is a property returned 71 * by System.getProperty().<br> 72 * If no property exists, the value remains unchanged. 73 * 74 * @version $Revision: 1.2 $ $Date: 2004/02/03 21:52:13 $ 75 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a> 76 * @see EventProducer 77 * @see org.exolab.castor.xml.Unmarshaller 78 */ 79 public class ConfigExpander implements EventProducer { 80 81 /*** 82 * The document handler 83 */ 84 private DocumentHandler _handler = null; 85 86 /*** 87 * The XML document reader 88 */ 89 private Reader _reader = null; 90 91 /*** 92 * The current element name 93 */ 94 private String _name = null; 95 96 /*** 97 * The logger 98 */ 99 private static final Category log = 100 Category.getInstance(ConfigExpander.class); 101 102 103 /*** 104 * Construct a new instance 105 * 106 * @param reader the XML document reader 107 */ 108 public ConfigExpander(Reader reader) { 109 _reader = reader; 110 } 111 112 /*** 113 * Sets the DocumentHandler to send SAX events to 114 * 115 * @param handler the handler to forward events to 116 */ 117 public void setDocumentHandler(DocumentHandler handler) { 118 _handler = handler; 119 } 120 121 /*** 122 * Signals to start producing events 123 * 124 * @throws SAXException if the document cannot be parsed 125 */ 126 public void start() throws SAXException { 127 Parser parser = Configuration.getDefaultParser(); 128 if (parser == null) { 129 throw new SAXException("Unable to create parser"); 130 } 131 132 DocumentHandler handler = new Expander(); 133 parser.setDocumentHandler(handler); 134 try { 135 parser.parse(new InputSource(_reader)); 136 } catch (IOException exception) { 137 throw new SAXException(exception.getMessage(), exception); 138 } 139 } 140 141 /*** 142 * Helper class to intercept {@link #startElement} calls, expanding 143 * any attributes. 144 */ 145 private class Expander implements DocumentHandler { 146 147 /*** 148 * Receive an object for locating the origin of SAX document events. 149 * 150 * @param locator an object that can return the location of any SAX 151 * document event 152 */ 153 public void setDocumentLocator(Locator locator) { 154 _handler.setDocumentLocator(locator); 155 } 156 157 /*** 158 * Receive notification of the beginning of a document. 159 * 160 * @throws SAXException any SAX exception, possibly wrapping another 161 * exception. 162 */ 163 public void startDocument() throws SAXException { 164 _handler.startDocument(); 165 } 166 167 /*** 168 * Receive notification of the end of a document. 169 * @throws SAXException any SAX exception, possibly wrapping another 170 * exception. 171 */ 172 public void endDocument() throws SAXException { 173 _handler.endDocument(); 174 } 175 176 /*** 177 * Receive notification of the beginning of an element. 178 * 179 * @param name the element name 180 * @param list the attributes attached to the element, if any 181 * @throws SAXException any SAX exception, possibly wrapping another 182 * exception. 183 */ 184 public void startElement(String name, AttributeList list) 185 throws SAXException { 186 187 AttributeListImpl replaced = new AttributeListImpl(); 188 for (int i = 0; i < list.getLength(); i++) { 189 String value = expand(list.getName(i), list.getValue(i)); 190 replaced.addAttribute(list.getName(i), list.getType(i), value); 191 } 192 _name = name; 193 _handler.startElement(name, replaced); 194 } 195 196 /*** 197 * Receive notification of the end of an element 198 * 199 * @param name the element name 200 * @throws SAXException any SAX exception, possibly wrapping another 201 * exception. 202 */ 203 public void endElement(String name) throws SAXException { 204 _handler.endElement(name); 205 _name = null; 206 } 207 208 /*** 209 * Receive notification of character data 210 * 211 * @param ch the characters from the XML document 212 * @param start the start position in the array 213 * @param length the number of characters to read from the array 214 * @throws SAXException any SAX exception, possibly wrapping another 215 * exception. 216 */ 217 public void characters(char[] ch, int start, int length) 218 throws SAXException { 219 220 String content = new String(ch, start, length); 221 content = expand(_name, content); 222 _handler.characters(content.toCharArray(), 0, content.length()); 223 } 224 225 /*** 226 * Receive notification of ignorable whitespace in element content. 227 * 228 * @param ch the characters from the XML document 229 * @param start the start position in the array 230 * @param length the number of characters to read from the array 231 * @throws SAXException any SAX exception, possibly wrapping another 232 * exception. 233 */ 234 public void ignorableWhitespace(char[] ch, int start, int length) 235 throws SAXException { 236 _handler.ignorableWhitespace(ch, start, length); 237 } 238 239 /*** 240 * Receive notification of a processing instruction. 241 * 242 * @param target the processing instruction target 243 * @param data the processing instruction data, or null if none was 244 * supplied 245 * @throws SAXException any SAX exception, possibly wrapping another 246 * exception. 247 */ 248 public void processingInstruction(String target, String data) 249 throws SAXException { 250 _handler.processingInstruction(target, data); 251 } 252 253 /*** 254 * Expand a value, if its contains embedded ${<property>} text 255 * 256 * @param name the attribute or element name 257 * @param value the content to expand 258 * @return the expanded value if it contained embedded text, 259 * and the property refers to a value system property. If not, returns 260 * value unchanged. 261 */ 262 private String expand(String name, String value) { 263 StringBuffer buffer = new StringBuffer(); 264 int prev = 0; 265 int pos; 266 while ((pos = value.indexOf("${", prev)) >= 0) { 267 if (pos > 0) { 268 buffer.append(value.substring(prev, pos)); 269 } 270 int index = value.indexOf('}', pos); 271 if (index < 0) { 272 // invalid format 273 log.debug( 274 "Cannot expand as format is invalid: " 275 + "name=" + name + ", value=" + value); 276 buffer.append("${"); 277 prev = pos + 2; 278 } else { 279 String propertyName = value.substring(pos + 2, index); 280 String propertyValue = System.getProperty(propertyName); 281 if (propertyValue != null) { 282 buffer.append(propertyValue); 283 } else { 284 log.debug("Cannot expand " + name + " as property=" 285 + propertyName + " is not defined"); 286 buffer.append("${"); 287 buffer.append(propertyName); 288 buffer.append("}"); 289 } 290 prev = index + 1; 291 } 292 } 293 if (prev < value.length()) { 294 buffer.append(value.substring(prev)); 295 } 296 String result = buffer.toString(); 297 return result; 298 } 299 300 } 301 302 }

This page was automatically generated by Maven