001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.betwixt.digester;
018:
019: import java.util.HashSet;
020: import java.util.Set;
021:
022: import javax.xml.parsers.SAXParser;
023:
024: import org.apache.commons.betwixt.XMLIntrospector;
025: import org.apache.commons.digester.Digester;
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.xml.sax.XMLReader;
029:
030: /** <p><code>XMLBeanInfoDigester</code> is a digester of XML files
031: * containing XMLBeanInfo definitions for a JavaBean.</p>
032: *
033: * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
034: * @version $Revision: 438373 $
035: */
036: public class XMLBeanInfoDigester extends Digester {
037:
038: /** Logger */
039: private static final Log log = LogFactory
040: .getLog(XMLBeanInfoDigester.class);
041:
042: /** the beans class for this XML descriptor */
043: private Class beanClass;
044:
045: /** should attributes or elements be used for primitive types */
046: private boolean attributesForPrimitives;
047:
048: /** the set of property names processed so far */
049: private Set processedPropertyNameSet = new HashSet();
050:
051: /** the introspector that is using me */
052: private XMLIntrospector introspector;
053:
054: /**
055: * Construct a new XMLBeanInfoDigester with default properties.
056: */
057: public XMLBeanInfoDigester() {
058: }
059:
060: /**
061: * Construct a new XMLBeanInfoDigester, allowing a SAXParser to be passed in. This
062: * allows XMLBeanInfoDigester to be used in environments which are unfriendly to
063: * JAXP1.1 (such as WebLogic 6.0). Thanks for the request to change go to
064: * James House (james@interobjective.com). This may help in places where
065: * you are able to load JAXP 1.1 classes yourself.
066: *
067: * @param parser the <code>SAXParser</code> to be used to parse the xml
068: */
069: public XMLBeanInfoDigester(SAXParser parser) {
070: super (parser);
071: }
072:
073: /**
074: * Construct a new XMLBeanInfoDigester, allowing an XMLReader to be passed in. This
075: * allows XMLBeanInfoDigester to be used in environments which are unfriendly to
076: * JAXP1.1 (such as WebLogic 6.0). Note that if you use this option you
077: * have to configure namespace and validation support yourself, as these
078: * properties only affect the SAXParser and emtpy constructor.
079: *
080: * @param reader the <code>XMLReader</code> to be used to parse the xml
081: */
082: public XMLBeanInfoDigester(XMLReader reader) {
083: super (reader);
084: }
085:
086: /**
087: * Gets the class of the bean whose .betwixt file is being processed
088: *
089: * @return the beans class for this XML descriptor
090: */
091: public Class getBeanClass() {
092: return beanClass;
093: }
094:
095: /**
096: * Sets the beans class for this XML descriptor
097: *
098: * @param beanClass the <code>Class</code> of the bean being processed
099: */
100: public void setBeanClass(Class beanClass) {
101: this .beanClass = beanClass;
102: }
103:
104: /**
105: * Gets the property names already processed
106: *
107: * @return the set of property names that have been processed so far
108: */
109: public Set getProcessedPropertyNameSet() {
110: return processedPropertyNameSet;
111: }
112:
113: /**
114: * Should attributes (or elements) be used for primitive types?
115: * @return true if primitive properties should be written as attributes in the xml
116: */
117: public boolean isAttributesForPrimitives() {
118: return attributesForPrimitives;
119: }
120:
121: /**
122: * Set whether attributes (or elements) should be used for primitive types.
123: * @param attributesForPrimitives pass true if primitive properties should be
124: * written as attributes
125: */
126: public void setAttributesForPrimitives(
127: boolean attributesForPrimitives) {
128: this .attributesForPrimitives = attributesForPrimitives;
129: if (introspector != null) {
130: introspector.getConfiguration().setAttributesForPrimitives(
131: attributesForPrimitives);
132: }
133: }
134:
135: /**
136: * Gets the XMLIntrospector that's using this digester.
137: *
138: * @return the introspector that is using me
139: */
140: public XMLIntrospector getXMLIntrospector() {
141: return introspector;
142: }
143:
144: /**
145: * Sets the introspector that is using me
146: * @param introspector the <code>XMLIntrospector</code> that using this for .betwixt
147: * digestion
148: */
149: public void setXMLIntrospector(XMLIntrospector introspector) {
150: this .introspector = introspector;
151: }
152:
153: // Implementation methods
154: //-------------------------------------------------------------------------
155: /** Reset configure for new digestion */
156: protected void configure() {
157: if (!configured) {
158: configured = true;
159:
160: // add the various rules
161:
162: addRule("info", new InfoRule());
163: addRuleSet(new CommonRuleSet());
164:
165: }
166:
167: // now initialize
168: setAttributesForPrimitives(attributesForPrimitives);
169: processedPropertyNameSet.clear();
170: }
171:
172: }
|