KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > util > CatalinaDigester


1
2
3 /*
4  * The contents of this file are subject to the terms
5  * of the Common Development and Distribution License
6  * (the "License"). You may not use this file except
7  * in compliance with the License.
8  *
9  * You can obtain a copy of the license at
10  * glassfish/bootstrap/legal/CDDLv1.0.txt or
11  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
12  * See the License for the specific language governing
13  * permissions and limitations under the License.
14  *
15  * When distributing Covered Code, include this CDDL
16  * HEADER in each file and include the License file at
17  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
18  * add the following below this CDDL HEADER, with the
19  * fields enclosed by brackets "[]" replaced with your
20  * own identifying information: Portions Copyright [yyyy]
21  * [name of copyright owner]
22  *
23  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24  *
25  * Portions Copyright Apache Software Foundation.
26  */

27
28
29 package org.apache.catalina.util;
30
31
32 import com.sun.org.apache.commons.digester.Digester;
33
34 import org.xml.sax.Attributes JavaDoc;
35 import org.xml.sax.helpers.AttributesImpl JavaDoc;
36 import org.xml.sax.SAXException JavaDoc;
37
38 import org.apache.tomcat.util.IntrospectionUtils;
39
40 /**
41  * This extended digester filters out ${...} tokens to replace them with
42  * matching system properties.
43  *
44  * @author Simon Kitching
45  * @author Remy Maucherat
46  */

47 public class CatalinaDigester extends Digester {
48
49
50     // ---------------------------------------------------------- Static Fields
51

52
53     private static class SystemPropertySource
54         implements IntrospectionUtils.PropertySource {
55         public String JavaDoc getProperty( String JavaDoc key ) {
56             return System.getProperty(key);
57         }
58     }
59
60     protected static IntrospectionUtils.PropertySource source[] =
61         new IntrospectionUtils.PropertySource[] { new SystemPropertySource() };
62
63
64     // ---------------------------------------------------------------- Methods
65

66
67     /**
68      * Invoke inherited implementation after applying variable
69      * substitution to any attribute values containing variable
70      * references.
71      */

72     public void startElement(String JavaDoc namespaceURI, String JavaDoc localName,
73                              String JavaDoc qName, Attributes JavaDoc list)
74         throws SAXException JavaDoc {
75         list = updateAttributes(list);
76         super.startElement(namespaceURI, localName, qName, list);
77     }
78
79
80     /**
81      * Invoke inherited implementation after applying variable substitution
82      * to the character data contained in the current element.
83      */

84     public void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName)
85         throws SAXException JavaDoc {
86         bodyText = updateBodyText(bodyText);
87         super.endElement(namespaceURI, localName, qName);
88     }
89
90
91     /**
92      * Returns an attributes list which contains all the attributes
93      * passed in, with any text of form "${xxx}" in an attribute value
94      * replaced by the appropriate value from the system property.
95      */

96     private Attributes JavaDoc updateAttributes(Attributes JavaDoc list) {
97
98         if (list.getLength() == 0) {
99             return list;
100         }
101         
102         AttributesImpl JavaDoc newAttrs = new AttributesImpl JavaDoc(list);
103         int nAttributes = newAttrs.getLength();
104         for (int i = 0; i < nAttributes; ++i) {
105             String JavaDoc value = newAttrs.getValue(i);
106             try {
107                 String JavaDoc newValue =
108                     IntrospectionUtils.replaceProperties(value, null, source);
109                 if (value != newValue) {
110                     newAttrs.setValue(i, newValue);
111                 }
112             }
113             catch (Exception JavaDoc e) {
114                 // ignore - let the attribute have its original value
115
}
116         }
117
118         return newAttrs;
119
120     }
121
122
123     /**
124      * Return a new StringBuffer containing the same contents as the
125      * input buffer, except that data of form ${varname} have been
126      * replaced by the value of that var as defined in the system property.
127      */

128     private StringBuffer JavaDoc updateBodyText(StringBuffer JavaDoc bodyText) {
129         String JavaDoc in = bodyText.toString();
130         String JavaDoc out;
131         try {
132             out = IntrospectionUtils.replaceProperties(in, null, source);
133         } catch(Exception JavaDoc e) {
134             return bodyText; // return unchanged data
135
}
136
137         if (out == in) {
138             // No substitutions required. Don't waste memory creating
139
// a new buffer
140
return bodyText;
141         } else {
142             return new StringBuffer JavaDoc(out);
143         }
144     }
145
146
147 }
148
149
Popular Tags