View Javadoc

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