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 23:21:57 GMT
arminw      2004/11/11 15:21:57

  Modified:    src/java/org/apache/ojb/broker/metadata
                        RepositoryPersistor.java
  Log:
  minor SAXParser instantiation refactoring
  
  Revision  Changes    Path
  1.27      +50 -23    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.26
  retrieving revision 1.27
  diff -u -r1.26 -r1.27
  --- RepositoryPersistor.java	26 Oct 2004 15:27:40 -0000	1.26
  +++ RepositoryPersistor.java	11 Nov 2004 23:21:57 -0000	1.27
  @@ -28,8 +28,9 @@
   import org.xml.sax.ContentHandler;
   import org.xml.sax.InputSource;
   import org.xml.sax.SAXException;
  -import org.xml.sax.SAXNotSupportedException;
   import org.xml.sax.XMLReader;
  +import org.xml.sax.ErrorHandler;
  +import org.xml.sax.SAXParseException;
   
   import javax.xml.parsers.ParserConfigurationException;
   import javax.xml.parsers.SAXParser;
  @@ -255,7 +256,7 @@
       }
   
       /**
  -     * @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
  @@ -281,30 +282,21 @@
       private Object readMetadataFromXML(InputSource source, Class target)
               throws MalformedURLException, ParserConfigurationException, SAXException, IOException
       {
  +        // TODO: make this configurable
  +        boolean validate = false;
  +
           // get a xml reader instance:
  -        SAXParser p = SAXParserFactory.newInstance().newSAXParser();
  -        
  -        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
  +        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;
  @@ -363,5 +355,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