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 info@exolab.org. 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 2001,2003 (C) Exoffice Technologies Inc. All Rights Reserved. 42 * 43 * $Id: AttributeExpander.java,v 1.7 2003/08/17 01:32:22 tanderson Exp $ 44 */ 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 /*** 66 * This class expands attributes in XML documents as the document is being 67 * parsed. It is designed to be used in conjunction with the Castor 68 * unmarshalling framework. 69 * <p> 70 * To be expanded, attribute values must contain text of the form 71 * <i>${property.name}</i>, where <i>property.name</i> is a property returned 72 * by <code>System.getProperty()</code>.<br> 73 * If no property exists, the attribute value remains unchanged. 74 * 75 * @version $Revision: 1.7 $ $Date: 2003/08/17 01:32:22 $ 76 * @author <a href="mailto:tima@intalio.com">Tim Anderson</a> 77 * @see EventProducer 78 * @see org.exolab.castor.xml.Unmarshaller 79 */ 80 public class AttributeExpander implements EventProducer { 81 82 private DocumentHandler _handler = null; 83 private Reader _reader = null; 84 85 /*** 86 * The logger 87 */ 88 private static final Log _log = 89 LogFactory.getLog(AttributeExpander.class); 90 91 92 /*** 93 * Construct a new instance 94 * 95 * @param reader the XML file reader 96 */ 97 public AttributeExpander(Reader reader) { 98 _reader = reader; 99 } 100 101 /*** 102 * Sets the DocumentHandler to send SAX events to 103 */ 104 public void setDocumentHandler(DocumentHandler handler) { 105 _handler = handler; 106 } 107 108 /*** 109 * Signals to start producing events. 110 */ 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 /*** 127 * Helper class to intercept {@link #startElement} calls, expanding 128 * any attributes. 129 */ 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 // invalid format 185 _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 // attribute cannot be expanded as the property is 196 // not defined 197 _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 } //-- AttributeInterceptor 215 216 } //-- AttributeExpander

This page was automatically generated by Maven