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. import java.util.HashSet;
  18. import java.util.Set;
  19. import javax.xml.parsers.SAXParser;
  20. import org.apache.commons.betwixt.XMLIntrospector;
  21. import org.apache.commons.digester.Digester;
  22. import org.apache.commons.logging.Log;
  23. import org.apache.commons.logging.LogFactory;
  24. import org.xml.sax.XMLReader;
  25. /** <p><code>XMLBeanInfoDigester</code> is a digester of XML files
  26. * containing XMLBeanInfo definitions for a JavaBean.</p>
  27. *
  28. * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
  29. * @version $Revision: 1.9 $
  30. */
  31. public class XMLBeanInfoDigester extends Digester {
  32. /** Logger */
  33. private static final Log log = LogFactory.getLog( XMLBeanInfoDigester.class );
  34. /** the beans class for this XML descriptor */
  35. private Class beanClass;
  36. /** should attributes or elements be used for primitive types */
  37. private boolean attributesForPrimitives;
  38. /** the set of property names processed so far */
  39. private Set processedPropertyNameSet = new HashSet();
  40. /** the introspector that is using me */
  41. private XMLIntrospector introspector;
  42. /**
  43. * Construct a new XMLBeanInfoDigester with default properties.
  44. */
  45. public XMLBeanInfoDigester() {
  46. }
  47. /**
  48. * Construct a new XMLBeanInfoDigester, allowing a SAXParser to be passed in. This
  49. * allows XMLBeanInfoDigester to be used in environments which are unfriendly to
  50. * JAXP1.1 (such as WebLogic 6.0). Thanks for the request to change go to
  51. * James House (james@interobjective.com). This may help in places where
  52. * you are able to load JAXP 1.1 classes yourself.
  53. *
  54. * @param parser the <code>SAXParser</code> to be used to parse the xml
  55. */
  56. public XMLBeanInfoDigester(SAXParser parser) {
  57. super(parser);
  58. }
  59. /**
  60. * Construct a new XMLBeanInfoDigester, allowing an XMLReader 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). Note that if you use this option you
  63. * have to configure namespace and validation support yourself, as these
  64. * properties only affect the SAXParser and emtpy constructor.
  65. *
  66. * @param reader the <code>XMLReader</code> to be used to parse the xml
  67. */
  68. public XMLBeanInfoDigester(XMLReader reader) {
  69. super(reader);
  70. }
  71. /**
  72. * Gets the class of the bean whose .betwixt file is being processed
  73. *
  74. * @return the beans class for this XML descriptor
  75. */
  76. public Class getBeanClass() {
  77. return beanClass;
  78. }
  79. /**
  80. * Sets the beans class for this XML descriptor
  81. *
  82. * @param beanClass the <code>Class</code> of the bean being processed
  83. */
  84. public void setBeanClass(Class beanClass) {
  85. this.beanClass = beanClass;
  86. }
  87. /**
  88. * Gets the property names already processed
  89. *
  90. * @return the set of property names that have been processed so far
  91. */
  92. public Set getProcessedPropertyNameSet() {
  93. return processedPropertyNameSet;
  94. }
  95. /**
  96. * Should attributes (or elements) be used for primitive types?
  97. * @return true if primitive properties should be written as attributes in the xml
  98. */
  99. public boolean isAttributesForPrimitives() {
  100. return attributesForPrimitives;
  101. }
  102. /**
  103. * Set whether attributes (or elements) should be used for primitive types.
  104. * @param attributesForPrimitives pass true if primitive properties should be
  105. * written as attributes
  106. */
  107. public void setAttributesForPrimitives(boolean attributesForPrimitives) {
  108. this.attributesForPrimitives = attributesForPrimitives;
  109. if ( introspector != null ) {
  110. introspector.getConfiguration()
  111. .setAttributesForPrimitives( attributesForPrimitives );
  112. }
  113. }
  114. /**
  115. * Gets the XMLIntrospector that's using this digester.
  116. *
  117. * @return the introspector that is using me
  118. */
  119. public XMLIntrospector getXMLIntrospector() {
  120. return introspector;
  121. }
  122. /**
  123. * Sets the introspector that is using me
  124. * @param introspector the <code>XMLIntrospector</code> that using this for .betwixt
  125. * digestion
  126. */
  127. public void setXMLIntrospector(XMLIntrospector introspector) {
  128. this.introspector = introspector;
  129. }
  130. // Implementation methods
  131. //-------------------------------------------------------------------------
  132. /** Reset configure for new digestion */
  133. protected void configure() {
  134. if (! configured) {
  135. configured = true;
  136. // add the various rules
  137. addRule( "info", new InfoRule() );
  138. addRule( "*/element", new ElementRule() );
  139. addRule( "*/text", new TextRule() );
  140. addRule( "*/attribute", new AttributeRule() );
  141. addRule( "*/hide", new HideRule() );
  142. addRule( "*/addDefaults", new AddDefaultsRule() );
  143. OptionRule optionRule = new OptionRule();
  144. addRule( "*/option", optionRule );
  145. addRule( "*/option/name", optionRule.getNameRule() );
  146. addRule( "*/option/value", optionRule.getValueRule() );
  147. }
  148. // now initialize
  149. setAttributesForPrimitives(attributesForPrimitives);
  150. processedPropertyNameSet.clear();
  151. }
  152. }