View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */ 
16  package org.apache.commons.betwixt.digester;
17  
18  import java.util.HashSet;
19  import java.util.Set;
20  
21  import javax.xml.parsers.SAXParser;
22  
23  import org.apache.commons.betwixt.XMLIntrospector;
24  import org.apache.commons.digester.Digester;
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.xml.sax.XMLReader;
28  
29  /*** <p><code>XMLBeanInfoDigester</code> is a digester of XML files
30    * containing XMLBeanInfo definitions for a JavaBean.</p>
31    *
32    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
33    * @version $Revision: 155402 $
34    */
35  public class XMLBeanInfoDigester extends Digester {
36  
37      /*** Logger */
38      private static final Log log = LogFactory.getLog( XMLBeanInfoDigester.class );
39      
40      /*** the beans class for this XML descriptor */
41      private Class beanClass;
42      
43      /*** should attributes or elements be used for primitive types */
44      private boolean attributesForPrimitives;
45      
46      /*** the set of property names processed so far */
47      private Set processedPropertyNameSet = new HashSet();
48  
49      /*** the introspector that is using me */
50      private XMLIntrospector introspector;
51      
52      /***
53       * Construct a new XMLBeanInfoDigester with default properties.
54       */
55      public XMLBeanInfoDigester() {
56      }
57  
58      /***
59       * Construct a new XMLBeanInfoDigester, allowing a SAXParser to be passed in.  This
60       * allows XMLBeanInfoDigester to be used in environments which are unfriendly to
61       * JAXP1.1 (such as WebLogic 6.0).  Thanks for the request to change go to
62       * James House (james@interobjective.com).  This may help in places where
63       * you are able to load JAXP 1.1 classes yourself.
64       *
65       * @param parser the <code>SAXParser</code> to be used to parse the xml
66       */
67      public XMLBeanInfoDigester(SAXParser parser) {
68          super(parser);
69      }
70  
71      /***
72       * Construct a new XMLBeanInfoDigester, allowing an XMLReader to be passed in.  This
73       * allows XMLBeanInfoDigester to be used in environments which are unfriendly to
74       * JAXP1.1 (such as WebLogic 6.0).  Note that if you use this option you
75       * have to configure namespace and validation support yourself, as these
76       * properties only affect the SAXParser and emtpy constructor.
77       *
78       * @param reader the <code>XMLReader</code> to be used to parse the xml
79       */
80      public XMLBeanInfoDigester(XMLReader reader) {
81          super(reader);
82      }
83      
84      /***
85       * Gets the class of the bean whose .betwixt file is being processed 
86       *
87       * @return the beans class for this XML descriptor 
88       */
89      public Class getBeanClass() {
90          return beanClass;
91      }
92      
93      /*** 
94       * Sets the beans class for this XML descriptor 
95       *
96       * @param beanClass the <code>Class</code> of the bean being processed
97       */
98      public void setBeanClass(Class beanClass) {
99          this.beanClass = beanClass;
100     }
101     
102     
103     /*** 
104      * Gets the property names already processed
105      *
106      * @return the set of property names that have been processed so far 
107      */
108     public Set getProcessedPropertyNameSet() {
109         return processedPropertyNameSet;
110     }
111     
112     /*** 
113      * Should attributes (or elements) be used for primitive types?
114      * @return true if primitive properties should be written as attributes in the xml
115      */
116     public boolean isAttributesForPrimitives() {
117         return attributesForPrimitives;
118     }
119 
120     /*** 
121      * Set whether attributes (or elements) should be used for primitive types. 
122      * @param attributesForPrimitives pass true if primitive properties should be 
123      * written as attributes
124      */
125     public void setAttributesForPrimitives(boolean attributesForPrimitives) {
126         this.attributesForPrimitives = attributesForPrimitives;
127         if ( introspector != null ) {
128             introspector.getConfiguration()
129                 .setAttributesForPrimitives( attributesForPrimitives );
130         }
131     }
132 
133     /*** 
134      * Gets the XMLIntrospector that's using this digester.
135      *
136      * @return the introspector that is using me 
137      */
138     public XMLIntrospector getXMLIntrospector() {
139         return introspector;
140     }
141     
142     /*** 
143      * Sets the introspector that is using me 
144      * @param introspector the <code>XMLIntrospector</code> that using this for .betwixt 
145      * digestion
146      */
147     public void setXMLIntrospector(XMLIntrospector introspector) {
148         this.introspector = introspector;
149     }
150     
151     // Implementation methods
152     //-------------------------------------------------------------------------        
153     /*** Reset configure for new digestion */
154     protected void configure() {
155         if (! configured) {
156             configured = true;
157          
158             // add the various rules
159             
160             addRule( "info", new InfoRule() );
161             addRule( "*/element", new ElementRule() );
162             addRule( "*/text", new TextRule() );
163             addRule( "*/attribute", new AttributeRule() );
164             addRule( "*/hide", new HideRule() );
165             addRule( "*/addDefaults", new AddDefaultsRule() );
166             
167             OptionRule optionRule = new OptionRule();
168             addRule( "*/option", optionRule );
169             addRule( "*/option/name", optionRule.getNameRule() );
170             addRule( "*/option/value", optionRule.getValueRule() );
171         }
172         
173         // now initialize
174         setAttributesForPrimitives(attributesForPrimitives);
175         processedPropertyNameSet.clear();
176     }
177     
178 }