Update of /cvsroot/xdoclet/xdoclet/core/src/xdoclet/util
In directory 
sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv22118/core/src/xdoclet/util

Modified Files:
        XmlValidator.java XDocletUtilMessages.java 
Log Message:
Added support for XML Schema validation (XDT-1284)

Index: XmlValidator.java
===================================================================
RCS file: /cvsroot/xdoclet/xdoclet/core/src/xdoclet/util/XmlValidator.java,v
retrieving revision 1.17
retrieving revision 1.18
diff -C2 -r1.17 -r1.18
*** XmlValidator.java   14 Aug 2004 12:08:13 -0000      1.17
--- XmlValidator.java   1 Mar 2005 20:57:49 -0000       1.18
***************
*** 10,16 ****
--- 10,19 ----
  import java.io.StringReader;
  import java.net.URL;
+ import java.util.ArrayList;
+ import java.util.Collection;
  import java.util.HashMap;
  
  import javax.xml.parsers.ParserConfigurationException;
+ import javax.xml.parsers.SAXParser;
  import javax.xml.parsers.SAXParserFactory;
  import org.apache.commons.logging.Log;
***************
*** 46,49 ****
--- 49,55 ----
       */
      public final static String DEFAULT_XML_READER_CLASSNAME = 
"org.apache.crimson.parser.XMLReaderImpl";
+     private final static String JAXP_SCHEMA_SOURCE = 
"http://java.sun.com/xml/jaxp/properties/schemaSource";;
+     private final static String JAXP_SCHEMA_LANGUAGE = 
"http://java.sun.com/xml/jaxp/properties/schemaLanguage";;
+     private final static String W3C_XML_SCHEMA = 
"http://www.w3.org/2001/XMLSchema";;
  
      private static XmlValidator instance = new XmlValidator(null);
***************
*** 59,62 ****
--- 65,69 ----
  
      private final HashMap _dtds = new HashMap();
+     private final Collection _schemas = new ArrayList();
  
      /**
***************
*** 109,112 ****
--- 116,135 ----
  
      /**
+      * Registers a local XSD document by its public id. This is necessary to 
avoid loading XML Schemas over the net.
+      *
+      * @param schemaURL
+      */
+     public void registerSchema(URL schemaURL)
+     {
+         Log log = LogUtil.getLog(XmlValidator.class, "registerSchema");
+ 
+         if (log.isDebugEnabled()) {
+             log.debug("Schema '" + schemaURL + "' registered.");
+         }
+ 
+         _schemas.add(schemaURL.toString());
+     }
+ 
+     /**
       * Called by parser when a DTD declaration is encountered in the parsed 
XML document
       *
***************
*** 140,144 ****
          else {
              log.debug("dtdURL == null");
!             log.error(Translator.getString(XDocletMessages.class, 
XDocletMessages.COULDNT_LOAD_LOCAL_DTD, new String[]{publicId}));
              return null;
          }
--- 163,176 ----
          else {
              log.debug("dtdURL == null");
! 
!             String msg = Translator.getString(XDocletMessages.class, 
XDocletMessages.COULDNT_LOAD_LOCAL_DTD, new String[]{publicId});
! 
!             // no error if we are doing schema validation
!             if (isSchemaValidation()) {
!                 log.debug(msg);
!             }
!             else {
!                 log.error(msg);
!             }
              return null;
          }
***************
*** 190,193 ****
--- 222,235 ----
      }
  
+     /**
+      * Returns whether this validator should validate against a XML Schema
+      *
+      * @return   <code>true</code> it the validator should validate against a 
XSD
+      */
+     private boolean isSchemaValidation()
+     {
+         return !_schemas.isEmpty();
+     }
+ 
      /*
       * parse the file
***************
*** 220,230 ****
              xmlReader.parse(is);
          }
          catch (SAXException e) {
!             e.printStackTrace();
!             throw new XDocletException("Couldn't validate document " + 
xml_file);
          }
          catch (IOException e) {
!             e.printStackTrace();
!             throw new XDocletException(e, "Couldn't validate document " + 
xml_file);
          }
  
--- 262,282 ----
              xmlReader.parse(is);
          }
+         catch (SAXParseException e) {
+             String message = Translator.getString(XDocletUtilMessages.class, 
XDocletUtilMessages.GENERATED_XML_INVALID,
+                 new String[]{e.getSystemId(), 
Integer.toString(e.getLineNumber()), e.getMessage()});
+ 
+             throw new XDocletException(e, message);
+         }
          catch (SAXException e) {
!             String message = Translator.getString(XDocletUtilMessages.class, 
XDocletUtilMessages.PARSING_FAILED,
!                 new String[]{xml_file.getAbsolutePath(), e.getMessage()});
! 
!             throw new XDocletException(e, message);
          }
          catch (IOException e) {
!             String message = Translator.getString(XDocletUtilMessages.class, 
XDocletUtilMessages.PARSING_FAILED,
!                 new String[]{xml_file.getAbsolutePath(), e.getMessage()});
! 
!             throw new XDocletException(e, message);
          }
  
***************
*** 247,250 ****
--- 299,307 ----
          Log log = LogUtil.getLog(XmlValidator.class, "initValidator");
  
+         // The crimson parser does not support schema validation
+         if (isSchemaValidation()) {
+             log.warn(Translator.getString(XDocletUtilMessages.class, 
XDocletUtilMessages.PARSER_DOES_NOT_SUPPORT_XSD_VALIDATION));
+         }
+ 
          try {
              // load the parser class
***************
*** 329,345 ****
      private void initValidator() throws XDocletException
      {
          try {
              SAXParserFactory factory = SAXParserFactory.newInstance();
  
              factory.setValidating(true);
!             xmlReader = factory.newSAXParser().getXMLReader();
              xmlReader.setEntityResolver(this);
              xmlReader.setErrorHandler(this);
-         }
-         catch (SAXParseException e) {
-             String message = Translator.getString(XDocletUtilMessages.class, 
XDocletUtilMessages.INIT_FAILED,
-                 new String[]{e.getSystemId(), 
Integer.toString(e.getLineNumber()), e.getMessage()});
  
-             throw new XDocletException(e, message);
          }
          catch (SAXException e) {
--- 386,432 ----
      private void initValidator() throws XDocletException
      {
+         Log log = LogUtil.getLog(XmlValidator.class, "initValidator");
+ 
          try {
              SAXParserFactory factory = SAXParserFactory.newInstance();
  
              factory.setValidating(true);
!             factory.setNamespaceAware(isSchemaValidation());
! 
!             // try to get a SAX Parser
!             SAXParser parser;
! 
!             try {
!                 parser = factory.newSAXParser();
!             }
!             catch (ParserConfigurationException e) {
!                 // try to get a non-namespace-aware parser if a 
namespace-aware parser was
!                 // required and none could be found
!                 if (!factory.isNamespaceAware()) {
!                     throw e;
!                 }
!                 factory.setNamespaceAware(false);
!                 parser = factory.newSAXParser();
!                 log.warn(Translator.getString(XDocletUtilMessages.class, 
XDocletUtilMessages.NO_NAMESPACE_AWARE_SAX_PARSER));
!             }
! 
!             // try to setup XML Schema Validation if schema validation is 
required and
!             // a namespace-aware SAX parser is available
!             if (isSchemaValidation() && parser.isNamespaceAware()) {
!                 try {
!                     parser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
!                     parser.setProperty(JAXP_SCHEMA_SOURCE, 
_schemas.toArray(new String[_schemas.size()]));
!                 }
!                 catch (SAXNotRecognizedException e) {
!                     log.warn(Translator.getString(XDocletUtilMessages.class, 
XDocletUtilMessages.PARSER_DOES_NOT_SUPPORT_XSD_VALIDATION));
!                 }
!                 catch (SAXNotSupportedException e) {
!                     log.warn(Translator.getString(XDocletUtilMessages.class, 
XDocletUtilMessages.PARSER_DOES_NOT_SUPPORT_XSD_VALIDATION));
!                 }
!             }
!             xmlReader = parser.getXMLReader();
              xmlReader.setEntityResolver(this);
              xmlReader.setErrorHandler(this);
  
          }
          catch (SAXException e) {

Index: XDocletUtilMessages.java
===================================================================
RCS file: 
/cvsroot/xdoclet/xdoclet/core/src/xdoclet/util/XDocletUtilMessages.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -r1.3 -r1.4
*** XDocletUtilMessages.java    28 Jul 2002 23:31:45 -0000      1.3
--- XDocletUtilMessages.java    1 Mar 2005 20:57:50 -0000       1.4
***************
*** 18,29 ****
  
      /**
!      * @msg.bundle   msg="Generated file [{0}:line {1}] Message=[{2}] is not 
valid according to its DTD. This might be
!      *      due to some missing tags in your source."
       */
      public final static String GENERATED_XML_INVALID = 
"GENERATED_XML_INVALID";
  
      /**
       * @msg.bundle   msg="Exception reading merge file. {0}"
       */
      public final static String EXCEPTION_READING_MERGE_FILE = 
"EXCEPTION_READING_MERGE_FILE";
! }
\ No newline at end of file
--- 18,46 ----
  
      /**
!      * @msg.bundle   msg="Generated file [{0}:line {1}] Message=[{2}] is not 
valid according to its DTD or XML Schema.
!      *      This might be due to some missing tags in your source."
       */
      public final static String GENERATED_XML_INVALID = 
"GENERATED_XML_INVALID";
  
      /**
+      * @msg.bundle   msg="The validateion for the generated file [{0}] 
failed. Message=[{2}] ."
+      */
+     public final static String PARSING_FAILED = "PARSING_FAILED";
+ 
+     /**
       * @msg.bundle   msg="Exception reading merge file. {0}"
       */
      public final static String EXCEPTION_READING_MERGE_FILE = 
"EXCEPTION_READING_MERGE_FILE";
! 
!     /**
!      * @msg.bundle   msg="WARNING: Can't validate against the XML Schema 
because the SAX Parser does not support XML
!      *      Schema validation."
!      */
!     public final static String PARSER_DOES_NOT_SUPPORT_XSD_VALIDATION = 
"PARSER_DOES_NOT_SUPPORT_XSD_VALIDATION";
! 
!     /**
!      * @msg.bundle   msg="WARNING: Can't validate against the XML Schema 
because no namespace-aware SAX Parser could be
!      *      found."
!      */
!     public final static String NO_NAMESPACE_AWARE_SAX_PARSER = 
"NO_NAMESPACE_AWARE_SAX_PARSER";
! }



-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click
_______________________________________________
xdoclet-devel mailing list
xdoclet-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/xdoclet-devel

Reply via email to