KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exolab > jms > config > AttributeExpander


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.1 2004/11/26 01:50:41 tanderson Exp $
44  */

45 package org.exolab.jms.config;
46
47 import java.io.IOException JavaDoc;
48 import java.io.Reader JavaDoc;
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 JavaDoc;
56 import org.xml.sax.DocumentHandler JavaDoc;
57 import org.xml.sax.ErrorHandler JavaDoc;
58 import org.xml.sax.InputSource JavaDoc;
59 import org.xml.sax.Locator JavaDoc;
60 import org.xml.sax.Parser JavaDoc;
61 import org.xml.sax.SAXException JavaDoc;
62 import org.xml.sax.helpers.AttributeListImpl JavaDoc;
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.1 $ $Date: 2004/11/26 01:50:41 $
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 JavaDoc _handler = null;
83     private Reader JavaDoc _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 JavaDoc reader) {
98         _reader = reader;
99     }
100
101     /**
102      * Sets the DocumentHandler to send SAX events to
103      */

104     public void setDocumentHandler(DocumentHandler JavaDoc handler) {
105         _handler = handler;
106     }
107
108     /**
109      * Signals to start producing events.
110      */

111     public void start() throws SAXException JavaDoc {
112         Parser JavaDoc parser = Configuration.getDefaultParser();
113         if (parser == null) {
114             throw new SAXException JavaDoc("Unable to create parser");
115         }
116
117         DocumentHandler JavaDoc handler = new AttributeInterceptor();
118         parser.setDocumentHandler(handler);
119         try {
120             parser.parse(new InputSource JavaDoc(_reader));
121         } catch (IOException JavaDoc exception) {
122             throw new SAXException JavaDoc(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 JavaDoc {
131
132         public void setDocumentLocator(Locator JavaDoc locator) {
133             _handler.setDocumentLocator(locator);
134         }
135
136         public void startDocument() throws SAXException JavaDoc {
137             _handler.startDocument();
138         }
139
140         public void endDocument() throws SAXException JavaDoc {
141             _handler.endDocument();
142         }
143
144         public void startElement(String JavaDoc name, AttributeList JavaDoc list)
145             throws SAXException JavaDoc {
146
147             AttributeListImpl JavaDoc replaced = new AttributeListImpl JavaDoc();
148             for (int i = 0; i < list.getLength(); i++) {
149                 String JavaDoc 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 JavaDoc name) throws SAXException JavaDoc {
156             _handler.endElement(name);
157         }
158
159         public void characters(char[] ch, int start, int length)
160             throws SAXException JavaDoc {
161             _handler.characters(ch, start, length);
162         }
163
164         public void ignorableWhitespace(char[] ch, int start, int length)
165             throws SAXException JavaDoc {
166             _handler.ignorableWhitespace(ch, start, length);
167         }
168
169         public void processingInstruction(String JavaDoc target, String JavaDoc data)
170             throws SAXException JavaDoc {
171             _handler.processingInstruction(target, data);
172         }
173
174         private String JavaDoc expand(String JavaDoc attribute, String JavaDoc value) {
175             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
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 JavaDoc name = value.substring(pos + 2, index);
191                     String JavaDoc 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 JavaDoc result = buffer.toString();
211             return result;
212         }
213
214     } //-- AttributeInterceptor
215

216 } //-- AttributeExpander
217
Popular Tags