Index: C:/workplace/.eclipse/thorn-workspace/castor/src/main/java/org/exolab/castor/xml/UnmarshalHandler.java =================================================================== --- C:/workplace/.eclipse/thorn-workspace/castor/src/main/java/org/exolab/castor/xml/UnmarshalHandler.java (revision 6971) +++ C:/workplace/.eclipse/thorn-workspace/castor/src/main/java/org/exolab/castor/xml/UnmarshalHandler.java (working copy) @@ -48,37 +48,49 @@ package org.exolab.castor.xml; //-- Castor imports -import org.castor.mapping.BindingType; -import org.castor.util.Base64Decoder; -import org.castor.util.HexDecoder; -import org.exolab.castor.util.Configuration; -import org.exolab.castor.util.ObjectFactory; -import org.exolab.castor.util.DefaultObjectFactory; -import org.exolab.castor.xml.descriptors.PrimitivesClassDescriptor; -import org.exolab.castor.xml.descriptors.StringClassDescriptor; -import org.exolab.castor.xml.util.*; -import org.exolab.castor.mapping.ClassDescriptor; -import org.exolab.castor.mapping.ExtendedFieldHandler; -import org.exolab.castor.mapping.FieldHandler; -import org.exolab.castor.mapping.MapItem; -import org.exolab.castor.mapping.loader.FieldHandlerImpl; - -//-- xml related imports -import org.xml.sax.*; - import java.io.PrintWriter; import java.io.Serializable; import java.io.StringWriter; - -import java.lang.reflect.*; - +import java.lang.reflect.Array; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; import java.util.ArrayList; +import java.util.Enumeration; import java.util.HashMap; import java.util.Hashtable; import java.util.Stack; -import java.util.Enumeration; import java.util.StringTokenizer; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.castor.mapping.BindingType; +import org.castor.util.Base64Decoder; +import org.castor.util.HexDecoder; +import org.exolab.castor.mapping.ClassDescriptor; +import org.exolab.castor.mapping.ExtendedFieldHandler; +import org.exolab.castor.mapping.FieldHandler; +import org.exolab.castor.mapping.MapItem; +import org.exolab.castor.mapping.loader.FieldHandlerImpl; +import org.exolab.castor.util.Configuration; +import org.exolab.castor.util.DefaultObjectFactory; +import org.exolab.castor.util.ObjectFactory; +import org.exolab.castor.xml.descriptors.PrimitivesClassDescriptor; +import org.exolab.castor.xml.descriptors.StringClassDescriptor; +import org.exolab.castor.xml.util.AttributeSetImpl; +import org.exolab.castor.xml.util.ContainerElement; +import org.exolab.castor.xml.util.SAX2ANY; +import org.exolab.castor.xml.util.XMLClassDescriptorImpl; +import org.exolab.castor.xml.util.XMLFieldDescriptorImpl; +import org.xml.sax.AttributeList; +import org.xml.sax.Attributes; +import org.xml.sax.ContentHandler; +import org.xml.sax.DocumentHandler; +import org.xml.sax.ErrorHandler; +import org.xml.sax.Locator; +import org.xml.sax.SAXException; +import org.xml.sax.SAXParseException; + /** * An unmarshaller to allowing unmarshaling of XML documents to * Java Objects. The Class must specify @@ -90,6 +102,10 @@ */ public final class UnmarshalHandler extends MarshalFramework implements ContentHandler, DocumentHandler, ErrorHandler { + /** + * Logger from commons-logging + */ + private static final Log LOG = LogFactory.getLog(UnmarshalHandler.class); //---------------------------/ //- Private Class Variables -/ @@ -145,12 +161,6 @@ private Object _topObject = null; /** - * A StringBuffer used to created Debug/Log messages - **/ - //private StringBuffer buf = null; - - - /** * Indicates whether or not collections should be cleared * upon first use (to remove default values, or old values). * False by default for backward compatibility. @@ -161,29 +171,13 @@ * The Castor configuration */ private Configuration _config = null; - - /** - * A flag to indicate whether or not to generate debug information - **/ - private boolean debug = false; /** - * A flag to indicate we need to kill the _logWriter when - * debug mode is false - **/ - private boolean killWriter = false; - - /** * The SAX Document Locator **/ private Locator _locator = null; /** - * The PrintWriter to print log information to - **/ - private PrintWriter _logWriter = null; - - /** * The ClassDescriptorResolver which is used to "resolve" * or find ClassDescriptors **/ @@ -390,28 +384,7 @@ _clearCollections = clear; } //-- setClearCollections - /** - * Turns debuging on or off. If no Log Writer has been set, then - * System.out will be used to display debug information - * @param debug the flag indicating whether to generate debug information. - * A value of true, will turn debuggin on. - * @see #setLogWriter - **/ - public void setDebug(boolean debug) { - this.debug = debug; - - if (this.debug && (_logWriter == null)) { - _logWriter = new PrintWriter(System.out, true); - killWriter = true; - } - if ((!this.debug) && killWriter) { - _logWriter = null; - killWriter = false; - } - } //-- setDebug - - /** * Sets the IDResolver to use when resolving IDREFs for * which no associated element may exist in XML document. * @@ -450,18 +423,7 @@ _strictElements = (!ignoreExtraElems); } //-- setIgnoreExtraElements - /** - * Sets the PrintWriter used for printing log messages - * @param printWriter the PrintWriter to use when printing - * log messages - **/ - public void setLogWriter(PrintWriter printWriter) { - this._logWriter = printWriter; - killWriter = false; - } //-- setLogWriter - - /** * Sets a boolean that when true indicates that objects * contained within the object model should be re-used * where appropriate. This is only valid when unmarshalling @@ -536,12 +498,12 @@ public void characters(char[] ch, int start, int length) throws SAXException { - if (debug) { + if (LOG.isDebugEnabled()) { StringBuffer sb = new StringBuffer(21 + length); sb.append("#characters: "); sb.append(ch, start, length); - message(sb.toString()); - } + LOG.debug(sb.toString()); + } //-- If we are skipping elements that have appeared in the XML but for //-- which we have no mapping, skip the text and return @@ -635,9 +597,8 @@ throws org.xml.sax.SAXException { - - if (debug) { - message("#endElement: " + name); + if (LOG.isDebugEnabled()) { + LOG.debug("#endElement: " + name); } //-- If we are skipping elements that have appeared in the XML but for @@ -721,7 +682,7 @@ //-- this message will only show up if debug //-- is turned on...how should we handle this case? //-- should it be a fatal error? - message("Ignoring " + state.elementName + " no descriptor was found"); + LOG.info("Ignoring " + state.elementName + " no descriptor was found"); } //-- handle possible location text content @@ -1214,11 +1175,11 @@ */ public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws org.xml.sax.SAXException { - if (debug) { + if (LOG.isDebugEnabled()) { if ((qName != null) && (qName.length() > 0)) - message("#startElement: " + qName); + LOG.debug("#startElement: " + qName); else - message("#startElement: " + localName); + LOG.debug("#startElement: " + localName); } //-- If we are skipping elements that have appeared in the XML but for @@ -1385,8 +1346,8 @@ */ public void startElement(String name, AttributeList attList) throws org.xml.sax.SAXException { - if (debug) { - message("#startElement: " + name); + if (LOG.isDebugEnabled()) { + LOG.debug("#startElement: " + name); } //-- If we are skipping elements that have appeared in the XML but for @@ -1904,8 +1865,8 @@ if (isWrapper) { state.classDesc = new XMLClassDescriptorImpl(ContainerElement.class, name); state.wrapper = true; - if (debug) { - message("wrapper-element: " + name); + if (LOG.isDebugEnabled()) { + LOG.debug("wrapper-element: " + name); } //-- process attributes processWrapperAttributes(atts); @@ -1927,8 +1888,8 @@ ++_ignoreElementDepth; //-- remove the StateInfo we just added _stateInfo.pop(); - if (debug) { - message(mesg + " - ignoring extra element."); + if (LOG.isDebugEnabled()) { + LOG.debug(mesg + " - ignoring extra element."); } return; } @@ -1936,8 +1897,7 @@ //the class descriptor was introspected //just log it else if (Introspector.introspected(classDesc)) { - //if (warn) - message(mesg); + LOG.warn(mesg); return; } //-- otherwise report error since we cannot find a suitable @@ -1961,8 +1921,8 @@ //create a new state to set the container as the object //don't save the current state, it will be recreated later - if (debug) { - message("#container: " + descriptor.getFieldName()); + if (LOG.isDebugEnabled()) { + LOG.debug("#container: " + descriptor.getFieldName()); } //-- clear current state and re-use for the container @@ -2291,7 +2251,7 @@ state.type = _class; } catch (java.lang.IllegalStateException ise) { - message(ise.toString()); + LOG.error(ise.toString()); throw new SAXException(ise); } @@ -2313,8 +2273,8 @@ //-- assign object, if incremental if (descriptor.isIncremental()) { - if (debug) { - message("debug: Processing incrementally for element: " + name); + if (LOG.isDebugEnabled()) { + LOG.debug("debug: Processing incrementally for element: " + name); } try { handler.setValue(parentState.object, state.object); @@ -2345,7 +2305,7 @@ buffer.append(name); buffer.append("\' is null. Processing attributes as location"); buffer.append("/wrapper only and ignoring all other attribtes."); - message(buffer.toString()); + LOG.warn(buffer.toString()); } } else { @@ -2639,7 +2599,7 @@ //-- Since many attributes represent primitive //-- fields, we add an extra validation check here //-- in case the class doesn't have a "has-method". - if (descriptor.isRequired() && (isValidating() || debug)) { + if (descriptor.isRequired() && (isValidating() || LOG.isDebugEnabled())) { String err = classDesc.getXMLName() + " is missing " + "required attribute: " + descriptor.getXMLName(); if (_locator != null) { @@ -2649,8 +2609,8 @@ if (isValidating()) { throw new SAXException(err); } - if (debug) { - message(err); + if (LOG.isDebugEnabled()) { + LOG.debug(err); } } } @@ -2736,11 +2696,11 @@ //-- XML specification specific attribute //-- It should be safe to ignore these...but //-- if you think otherwise...let use know! - if (debug) { + if (LOG.isDebugEnabled()) { String msg = "ignoring attribute '" + name + "' for class: " + state.classDesc.getJavaClass().getName(); - message(msg); + LOG.debug(msg); } continue; } @@ -3335,20 +3295,7 @@ } - /** - * Sends a message to all observers. Currently the only observer is - * the logger. - * @param msg the message to send to all message observers - **/ - private void message(String msg) { - if (_logWriter != null) { - _logWriter.println(msg); - _logWriter.flush(); - } - } //-- message - - /** * Finds and returns an XMLClassDescriptor for the given class name. * If a ClassDescriptor could not be found one will attempt to * be generated. @@ -3410,8 +3357,8 @@ return new InternalXMLClassDescriptor(classDesc); } - if (debug) { - message(ERROR_DID_NOT_FIND_CLASSDESCRIPTOR + _class.getName()); + if (LOG.isDebugEnabled()) { + LOG.debug(ERROR_DID_NOT_FIND_CLASSDESCRIPTOR + _class.getName()); } return classDesc; @@ -3446,8 +3393,8 @@ return new InternalXMLClassDescriptor(classDesc); } - if (debug) { - message(ERROR_DID_NOT_FIND_CLASSDESCRIPTOR + className); + if (LOG.isDebugEnabled()) { + LOG.debug(ERROR_DID_NOT_FIND_CLASSDESCRIPTOR + className); } return classDesc; Index: C:/workplace/.eclipse/thorn-workspace/castor/src/main/java/org/exolab/castor/xml/Unmarshaller.java =================================================================== --- C:/workplace/.eclipse/thorn-workspace/castor/src/main/java/org/exolab/castor/xml/Unmarshaller.java (revision 6971) +++ C:/workplace/.eclipse/thorn-workspace/castor/src/main/java/org/exolab/castor/xml/Unmarshaller.java (working copy) @@ -47,6 +47,13 @@ //-- castor imports +import java.io.Reader; +import java.util.HashMap; +import java.util.Iterator; +import java.util.StringTokenizer; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.castor.mapping.BindingType; import org.castor.mapping.MappingUnmarshaller; import org.exolab.castor.mapping.Mapping; @@ -56,9 +63,7 @@ import org.exolab.castor.util.LocalConfiguration; import org.exolab.castor.util.ObjectFactory; import org.exolab.castor.xml.location.FileLocation; -import org.exolab.castor.xml.util.*; - -//-- misc xml related imports +import org.exolab.castor.xml.util.DOMEventProducer; import org.w3c.dom.Node; import org.xml.sax.ContentHandler; import org.xml.sax.EntityResolver; @@ -66,13 +71,6 @@ import org.xml.sax.Parser; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; - -//-- Java imports -import java.io.Reader; -import java.io.PrintWriter; -import java.util.HashMap; -import java.util.Iterator; -import java.util.StringTokenizer; /** * An unmarshaller to allowing unmarshalling of XML documents to * Java Objects. The Class must specify @@ -83,6 +81,10 @@ * @version $Revision$ $Date: 2006-02-23 14:16:51 -0700 (Thu, 23 Feb 2006) $ */ public class Unmarshaller { + /** + * Logger from commons-logging + */ + private static final Log LOG = LogFactory.getLog(Unmarshaller.class); //----------------------------/ //- Private Member Variables -/ @@ -146,16 +148,6 @@ private ClassLoader _loader = null; /** - * The print writer used for log information - */ - private PrintWriter _pw = null; - - /** - * The flag indicating whether or not to display debug information - */ - private boolean _debug = false; - - /** * A boolean to indicate that objects should * be re-used where appropriate */ @@ -280,7 +272,6 @@ */ private void initConfig() { _config = LocalConfiguration.getInstance(); - _debug = _config.debug(); _validate = _config.marshallingValidation(); _ignoreExtraElements = (!_config.strictElements()); @@ -335,9 +326,7 @@ _cdResolver.setClassLoader(_loader); } handler.setResolver(_cdResolver); - handler.setLogWriter(_pw); handler.setClearCollections(_clearCollections); - handler.setDebug(_debug); handler.setReuseObjects(_reuseObjects); handler.setValidation(_validate); handler.setIgnoreExtraAttributes(_ignoreExtraAtts); @@ -437,17 +426,6 @@ } //-- setClearCollections /** - * Turns debuging on or off. If no Log Writer has been set, then - * System.out will be used to display debug information - * @param debug the flag indicating whether to generate debug information. - * A value of true, will turn debuggin on. - * @see #setLogWriter - **/ - public void setDebug(boolean debug) { - _debug = debug; - } //-- setDebug - - /** * Sets the EntityResolver to use when resolving system and * public ids with respect to entites and Document Type. * @param entityResolver the EntityResolver to use when @@ -496,14 +474,6 @@ } //-- setIgnoreExtraElements /** - * Sets the PrintWriter used for logging - * @param printWriter the PrintWriter to use for logging - **/ - public void setLogWriter(PrintWriter printWriter) { - _pw = printWriter; - } //-- setLogWriter - - /** * Sets the Mapping to use during unmarshalling. If the Mapping has a ClassLoader it * will be used during unmarshalling. * @@ -699,15 +669,7 @@ reader.setEntityResolver(entityResolver); } catch(RuntimeException rx) { - if (_debug) { - String err = "Unable to create SAX XMLReader, attempting SAX Parser."; - if (_pw != null) { - _pw.println(err); - } - else { - System.out.println(err); - } - } + LOG.debug("Unable to create SAX XMLReader, attempting SAX Parser."); } if (reader == null) { @@ -819,10 +781,10 @@ { Unmarshaller unmarshaller = new Unmarshaller(c); - if (unmarshaller._debug) { - String warning = "debug: *static* unmarshal method called, this will " + - "ignore any mapping files or changes made to an Unmarshaller instance."; - System.out.println(warning); + // TODO: Should this be at level WARN? + if (LOG.isDebugEnabled()) { + LOG.debug("*static* unmarshal method called, this will ignore any " + + "mapping files or changes made to an Unmarshaller instance."); } //-- for backward compatibility with Castor versions @@ -851,10 +813,11 @@ throws MarshalException, ValidationException { Unmarshaller unmarshaller = new Unmarshaller(c); - if (unmarshaller._debug) { - String warning = "debug: *static* unmarshal method called, this will " + - "ignore any mapping files or changes made to an Unmarshaller instance."; - System.out.println(warning); + + // TODO: Should this be at level WARN? + if (LOG.isDebugEnabled()) { + LOG.debug("*static* unmarshal method called, this will ignore any " + + "mapping files or changes made to an Unmarshaller instance."); } //-- for backward compatibility with Castor versions @@ -882,10 +845,11 @@ public static Object unmarshal(Class c, Node node) throws MarshalException, ValidationException { Unmarshaller unmarshaller = new Unmarshaller(c); - if (unmarshaller._debug) { - String warning = "debug: *static* unmarshal method called, this will " + - "ignore any mapping files or changes made to an Unmarshaller instance."; - System.out.println(warning); + + // TODO: Should this be at level WARN? + if (LOG.isDebugEnabled()) { + LOG.debug("*static* unmarshal method called, this will ignore any " + + "mapping files or changes made to an Unmarshaller instance."); } //-- for backward compatibility with Castor versions