001    package com.thaiopensource.xml.sax;
002    
003    import org.xml.sax.SAXException;
004    import org.xml.sax.XMLReader;
005    import org.xml.sax.SAXNotRecognizedException;
006    import org.xml.sax.SAXNotSupportedException;
007    import org.xml.sax.helpers.XMLReaderFactory;
008    
009    import com.thaiopensource.xml.sax.XMLReaderCreator;
010    
011    /**
012     * An <code>XMLReaderCreator</code> that creates <code>XMLReader</code>s using the SAX2 <code>XMLReaderFactory</code>.
013     * An instance of this class is safe for concurrent access by multiple threads.
014     *
015     * @see org.xml.sax.helpers.XMLReaderFactory
016     * @author <a href="mailto:jjc@jclark.com">James Clark</a>
017     */
018    public class Sax2XMLReaderCreator implements XMLReaderCreator {
019      private final String className;
020    
021      /**
022       * Constructs a <code>Sax2XMLReaderCreator</code> that uses system defaults to construct <code>XMLReader</code>s.
023       */
024      public Sax2XMLReaderCreator() {
025        this.className = null;
026      }
027    
028     /**
029      * Constructs a <code>Sax2XMLReaderCreator</code> that constructs <code>XMLReader</code>s with the specified
030      * class name.
031      *
032      * @param className the fully-qualified name of the class implementing <code>XMLReader</code>;
033      * if <code>null</code> equivalent to the no-argument constructor
034      *
035      */
036      public Sax2XMLReaderCreator(String className) {
037        this.className = className;
038      }
039    
040      public XMLReader createXMLReader() throws SAXException {
041        XMLReader xr;
042        if (className == null)
043          xr = XMLReaderFactory.createXMLReader();
044        else
045          xr = XMLReaderFactory.createXMLReader(className);
046        xr.setFeature("http://xml.org/sax/features/namespaces", true);
047        xr.setFeature("http://xml.org/sax/features/namespace-prefixes", false);
048        try {
049          xr.setFeature("http://xml.org/sax/features/validation", false);
050        }
051        catch (SAXNotRecognizedException e) {
052        }
053        catch (SAXNotSupportedException e) {
054        }
055        return xr;
056      }
057    }