db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From arm...@apache.org
Subject cvs commit: db-ojb/src/java/org/apache/ojb/broker/metadata RepositoryPersistor.java
Date Thu, 11 Nov 2004 13:57:42 GMT
arminw      2004/11/11 05:57:42

  Modified:    src/java/org/apache/ojb/broker/metadata Tag: OJB_1_0_RELEASE
                        RepositoryPersistor.java
  Log:
  minor SAXParser instantiation refactoring
  
  Revision  Changes    Path
  No                   revision
  No                   revision
  1.24.2.1  +65 -37    db-ojb/src/java/org/apache/ojb/broker/metadata/RepositoryPersistor.java
  
  Index: RepositoryPersistor.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/metadata/RepositoryPersistor.java,v
  retrieving revision 1.24
  retrieving revision 1.24.2.1
  diff -u -r1.24 -r1.24.2.1
  --- RepositoryPersistor.java	1 Jun 2004 19:15:23 -0000	1.24
  +++ RepositoryPersistor.java	11 Nov 2004 13:57:42 -0000	1.24.2.1
  @@ -15,6 +15,20 @@
    * limitations under the License.
    */
   
  +import javax.xml.parsers.ParserConfigurationException;
  +import javax.xml.parsers.SAXParser;
  +import javax.xml.parsers.SAXParserFactory;
  +import java.io.File;
  +import java.io.FileInputStream;
  +import java.io.FileOutputStream;
  +import java.io.IOException;
  +import java.io.InputStream;
  +import java.io.OutputStream;
  +import java.io.PrintWriter;
  +import java.net.MalformedURLException;
  +import java.net.URL;
  +import java.util.Date;
  +
   import org.apache.commons.lang.SerializationUtils;
   import org.apache.commons.lang.SystemUtils;
   import org.apache.ojb.broker.util.ClassHelper;
  @@ -25,25 +39,12 @@
   import org.apache.ojb.broker.util.logging.Logger;
   import org.apache.ojb.broker.util.logging.LoggerFactory;
   import org.xml.sax.ContentHandler;
  +import org.xml.sax.ErrorHandler;
   import org.xml.sax.InputSource;
   import org.xml.sax.SAXException;
  -import org.xml.sax.SAXNotSupportedException;
  +import org.xml.sax.SAXParseException;
   import org.xml.sax.XMLReader;
   
  -import javax.xml.parsers.ParserConfigurationException;
  -import javax.xml.parsers.SAXParser;
  -import javax.xml.parsers.SAXParserFactory;
  -import java.io.File;
  -import java.io.FileInputStream;
  -import java.io.FileOutputStream;
  -import java.io.IOException;
  -import java.io.OutputStream;
  -import java.io.PrintWriter;
  -import java.io.InputStream;
  -import java.net.MalformedURLException;
  -import java.net.URL;
  -import java.util.Date;
  -
   /**
    * This class is responsible for reading and writing DescriptorRepository objects
    * from and to persistent media.
  @@ -246,7 +247,8 @@
       }
   
       /**
  -     * @todo We should re-design the configuration file reading
  +     *
  +     * TODO: We should re-design the configuration file reading
        */
       private Object buildRepository(String repositoryFileName, Class targetRepository)
               throws MalformedURLException, ParserConfigurationException, SAXException, IOException
  @@ -272,30 +274,21 @@
       private Object readMetadataFromXML(InputSource source, Class target)
               throws MalformedURLException, ParserConfigurationException, SAXException, IOException
       {
  -        // get a xml reader instance:
  -        SAXParser p = SAXParserFactory.newInstance().newSAXParser();
  +        // TODO: make this configurable
  +        boolean validate = false;
           
  -        log.debug("RespostoryPersistor using SAXParser : " + p.getClass().getName());
  -        XMLReader reader = p.getXMLReader();
  -        /**
  -         * MBAIRD: It's possible that the SAX implementation doesn't support this feature
  -         * so we try and catch the SAXNotSupportedException and just ignore it.
  -         *
  -         * arminw:
  -         * this ugly workaround - if() statement - was necessary, because when setFeature(..)
was
  -         * called on crimson parser, the parser does not find any relative path
  -         * TODO better solution
  -         */
  -        try
  +        // get a xml reader instance:
  +        SAXParserFactory factory = SAXParserFactory.newInstance();
  +        log.info("RespostoryPersistor using SAXParserFactory : " + factory.getClass().getName());
  +        if (validate)
           {
  -            if (reader.getClass().getName().indexOf("crimson") != -1)
  -            {
  -                reader.setFeature("http://xml.org/sax/features/validation", true);
  -            }
  +            factory.setValidating(true);
           }
  -        catch (SAXNotSupportedException snse)
  +        SAXParser p = factory.newSAXParser();
  +        XMLReader reader = p.getXMLReader();
  +        if (validate)
           {
  -            // ignore
  +            reader.setErrorHandler(new OJBErrorHandler());
           }
   
           Object result = null;
  @@ -354,5 +347,40 @@
               throw new MalformedURLException("did not find resource " + repositoryFileName);
           }
           return url;
  +    }
  +
  +    // inner class
  +    class OJBErrorHandler implements ErrorHandler
  +    {
  +        public void warning(SAXParseException exception)
  +                throws SAXException
  +        {
  +            logMessage(exception, false);
  +        }
  +
  +        public void error(SAXParseException exception)
  +                throws SAXException
  +        {
  +            logMessage(exception, false);
  +        }
  +
  +        public void fatalError(SAXParseException exception)
  +                throws SAXException
  +        {
  +            logMessage(exception, true);
  +        }
  +
  +        void logMessage(SAXParseException e, boolean isFatal)
  +        {
  +            String msg = e.getMessage();
  +            if (isFatal)
  +            {
  +                log.error("## " + e.getSystemId() + " - line " + e.getLineNumber() +  ":
" + msg + " ##");
  +            }
  +            else
  +            {
  +                log.warn(e.getSystemId() + " - line " + e.getLineNumber() + ": " + msg);
  +            }
  +        }
       }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org


Mime
View raw message