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.getLog( XMLBeanInfoDigester.class );
040        
041        /** the beans class for this XML descriptor */
042        private Class beanClass;
043        
044        /** should attributes or elements be used for primitive types */
045        private boolean attributesForPrimitives;
046        
047        /** the set of property names processed so far */
048        private Set processedPropertyNameSet = new HashSet();
049    
050        /** the introspector that is using me */
051        private XMLIntrospector introspector;
052        
053        /**
054         * Construct a new XMLBeanInfoDigester with default properties.
055         */
056        public XMLBeanInfoDigester() {
057        }
058    
059        /**
060         * Construct a new XMLBeanInfoDigester, allowing a SAXParser to be passed in.  This
061         * allows XMLBeanInfoDigester to be used in environments which are unfriendly to
062         * JAXP1.1 (such as WebLogic 6.0).  Thanks for the request to change go to
063         * James House (james@interobjective.com).  This may help in places where
064         * you are able to load JAXP 1.1 classes yourself.
065         *
066         * @param parser the <code>SAXParser</code> to be used to parse the xml
067         */
068        public XMLBeanInfoDigester(SAXParser parser) {
069            super(parser);
070        }
071    
072        /**
073         * Construct a new XMLBeanInfoDigester, allowing an XMLReader to be passed in.  This
074         * allows XMLBeanInfoDigester to be used in environments which are unfriendly to
075         * JAXP1.1 (such as WebLogic 6.0).  Note that if you use this option you
076         * have to configure namespace and validation support yourself, as these
077         * properties only affect the SAXParser and emtpy constructor.
078         *
079         * @param reader the <code>XMLReader</code> to be used to parse the xml
080         */
081        public XMLBeanInfoDigester(XMLReader reader) {
082            super(reader);
083        }
084        
085        /**
086         * Gets the class of the bean whose .betwixt file is being processed 
087         *
088         * @return the beans class for this XML descriptor 
089         */
090        public Class getBeanClass() {
091            return beanClass;
092        }
093        
094        /** 
095         * Sets the beans class for this XML descriptor 
096         *
097         * @param beanClass the <code>Class</code> of the bean being processed
098         */
099        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    }