User: ara_e_w
Date: 02/04/12 14:01:58
Modified: core/src/xdoclet/util XmlValidator.java
Log:
fork=false works fine :-)
Revision Changes Path
1.11 +237 -33 xdoclet/core/src/xdoclet/util/XmlValidator.java
Index: XmlValidator.java
===================================================================
RCS file: /cvsroot/xdoclet/xdoclet/core/src/xdoclet/util/XmlValidator.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -w -r1.10 -r1.11
--- XmlValidator.java 9 Apr 2002 00:07:17 -0000 1.10
+++ XmlValidator.java 12 Apr 2002 21:01:58 -0000 1.11
@@ -5,23 +5,17 @@
package xdoclet.util;
import org.apache.log4j.Category;
-
-import org.xml.sax.InputSource;
-import org.xml.sax.SAXException;
-import org.xml.sax.SAXParseException;
+import org.apache.tools.ant.AntClassLoader;
+import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
-
+import org.xml.sax.helpers.ParserAdapter;
import xdoclet.XDocletException;
-import javax.xml.parsers.ParserConfigurationException;
-import javax.xml.parsers.SAXParser;
-import javax.xml.parsers.SAXParserFactory;
-
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
+import java.io.FileReader;
import java.util.HashMap;
-import java.net.MalformedURLException;
/**
* This handler implementation is capable of providing dtds from a local storage
@@ -30,22 +24,72 @@
*
* @author <a href="mailto:[EMAIL PROTECTED]">Aslak Helles�y</a>
* @created September 18, 2001
- * @version $Revision: 1.10 $
+ * @version $Revision: 1.11 $
* @todo Deal with Translator.getString()'s exception better in
* resolveEntity(String, String)
*/
public class XmlValidator extends DefaultHandler
{
+
+ /**
+ * The crimson implementation is shipped with ant.
+ */
+ public static String DEFAULT_XML_READER_CLASSNAME =
"org.apache.crimson.parser.XMLReaderImpl";
+ /**
+ * @todo-javadoc Describe the field
+ */
+ private static XmlValidator instance;
+
+ /**
+ * @todo-javadoc Describe the field
+ */
+ protected ClassLoader classLoader;
+
+ /**
+ * XMLReader used to validation process
+ */
+ protected XMLReader xmlReader = null;
+
+ /**
+ * @todo-javadoc Describe the field
+ */
+ protected String readerClassName = DEFAULT_XML_READER_CLASSNAME;
+
/**
* @todo-javadoc Describe the field
*/
private final HashMap _dtds = new HashMap();
/**
- * Creates a new XmlValidator.
+ * Describe what the XmlValidator constructor does
+ *
+ * @param classLoader Describe what the parameter does
+ * @todo-javadoc Write javadocs for constructor
+ * @todo-javadoc Write javadocs for method parameter
+ */
+ public XmlValidator( ClassLoader classLoader )
+ {
+ this.classLoader = classLoader;
+ }
+
+ /**
+ * Gets the Instance attribute of the XmlValidator class
+ *
+ * @return The Instance value
*/
- public XmlValidator()
+ public static XmlValidator getInstance()
{
+ return instance;
+ }
+
+ /**
+ * Sets the Instance attribute of the XmlValidator class
+ *
+ * @param instance The new Instance value
+ */
+ public static void setInstance( XmlValidator instance )
+ {
+ XmlValidator.instance = instance;
}
/**
@@ -156,42 +200,202 @@
*/
public void validate( File xmlFile ) throws XDocletException
{
- Category cat = Log.getCategory( XmlValidator.class, "validate" );
- // instantiate a validating parser
+ initValidator();
+ doValidate( xmlFile );
+
+// try
+// {
+// SAXParser parser = factory.newSAXParser();
+//
+// cat.debug( "Parsing " + xmlFile.getAbsolutePath() );
+// parser.parse( xmlFile, this );
+// cat.debug( "Parsing OK" );
+// }
+// catch( SAXParseException e )
+// {
+// String message = Translator.getString( "generated_xml_invalid",
+// new String[]{e.getSystemId(), Integer.toString( e.getLineNumber()
), e.getMessage()} );
+//
+// throw new XDocletException( e, message );
+// }
+// catch( SAXException e )
+// {
+// throw new XDocletException( e, Translator.getString(
"couldnt_init_xml_parser" ) );
+// }
+// catch( IOException e )
+// {
+// throw new XDocletException( e, Translator.getString(
"couldnt_load_xml_file" ) );
+// }
+// catch( ParserConfigurationException e )
+// {
+// throw new XDocletException( e, Translator.getString(
"couldnt_conf_xml_parser" ) );
+// }
+// catch( NullPointerException e )
+// {
+// throw new XDocletException( e, Translator.getString( "couldnt_load_dtd"
) );
+// }
+ }
- SAXParserFactory factory = SAXParserFactory.newInstance();
+ /*
+ * parse the file
+ */
+ /**
+ * Describe what the method does
+ *
+ * @param xml_file Describe what the parameter does
+ * @exception XDocletException Describe the exception
+ * @todo-javadoc Write javadocs for method
+ * @todo-javadoc Write javadocs for method parameter
+ * @todo-javadoc Write javadocs for exception
+ */
+ private void doValidate( File xml_file ) throws XDocletException
+ {
+ Category cat = Log.getCategory( XmlValidator.class, "doValidate" );
- factory.setValidating( true );
try
{
- SAXParser parser = factory.newSAXParser();
-
- cat.debug( "Parsing " + xmlFile.getAbsolutePath() );
- parser.parse( xmlFile, this );
- cat.debug( "Parsing OK" );
+ if( cat.isDebugEnabled() )
+ {
+ cat.debug( "Validating " + xml_file.getName() + "... "
);
}
- catch( SAXParseException e )
+
+ //errorHandler.init( afile );
+ InputSource is = new InputSource( new FileReader( xml_file ) );
+ String uri = "file:" + xml_file.getAbsolutePath().replace(
'\\', '/' );
+
+ for( int index = uri.indexOf( '#' ); index != -1; index =
uri.indexOf( '#' ) )
{
- String message = Translator.getString( "generated_xml_invalid",
- new String[]{e.getSystemId(), Integer.toString(
e.getLineNumber() ), e.getMessage()} );
+ uri = uri.substring( 0, index ) + "%23" +
uri.substring( index + 1 );
+ }
- throw new XDocletException( e, message );
+ is.setSystemId( uri );
+
+ xmlReader.parse( is );
}
catch( SAXException e )
{
- throw new XDocletException( e, Translator.getString(
"couldnt_init_xml_parser" ) );
+ e.printStackTrace();
+ throw new XDocletException( "Could'nt validate document " +
xml_file );
}
catch( IOException e )
{
- throw new XDocletException( e, Translator.getString(
"couldnt_load_xml_file" ) );
+ e.printStackTrace();
+ throw new XDocletException( e, "Could'nt validate document " +
xml_file );
+ }
+
+// if( errorHandler.getFailure() )
+// {
+// if( failOnError )
+// throw new BuildException( xml_file + " is not a valid XML document."
);
+// else
+// log( xml_file + " is not a valid XML document", Project.MSG_ERR );
+// }
+ }
+
+ /**
+ * Describe what the method does
+ *
+ * @exception XDocletException Describe the exception
+ * @todo-javadoc Write javadocs for method
+ * @todo-javadoc Write javadocs for exception
+ */
+ private void initValidator() throws XDocletException
+ {
+ Category cat = Log.getCategory( XmlValidator.class, "initValidator" );
+
+ try
+ {
+ // load the parser class
+ // with JAXP, we would use a SAXParser factory
+ Class readerClass = null;
+
+ if( classLoader != null )
+ {
+ readerClass = classLoader.loadClass( readerClassName );
+ AntClassLoader.initializeClass( readerClass );
+ }
+ else
+ {
+ readerClass = Class.forName( readerClassName );
+ }
+
+ // then check it implements XMLReader
+ if( XMLReader.class.isAssignableFrom( readerClass ) )
+ {
+ xmlReader = ( XMLReader ) readerClass.newInstance();
+
+ if( cat.isDebugEnabled() )
+ {
+ cat.debug( "Using SAX2 reader " +
readerClassName );
}
- catch( ParserConfigurationException e )
+ }
+ else
+ {
+ // see if it is a SAX1 Parser
+ if( Parser.class.isAssignableFrom( readerClass ) )
+ {
+ Parser parser = ( Parser )
readerClass.newInstance();
+
+ xmlReader = new ParserAdapter( parser );
+
+ if( cat.isDebugEnabled() )
{
- throw new XDocletException( e, Translator.getString(
"couldnt_conf_xml_parser" ) );
+ cat.debug( "Using SAX1 parser " +
readerClassName );
}
- catch( NullPointerException e )
+ }
+ else
{
- throw new XDocletException( e, Translator.getString(
"couldnt_load_dtd" ) );
+ String message = Translator.getString(
"init_failed", new String[]{readerClassName} );
+
+ System.out.println( "init_failed" );
+
+ throw new XDocletException( message );
+ }
+ }
+ }
+ catch( ClassNotFoundException e )
+ {
+ e.printStackTrace();
+
+ String message = Translator.getString( "init_failed", new
String[]{readerClassName} );
+
+ throw new XDocletException( e, message );
+ }
+ catch( InstantiationException e )
+ {
+ e.printStackTrace();
+
+ String message = Translator.getString( "init_failed", new
String[]{readerClassName} );
+
+ throw new XDocletException( e, message );
+ }
+ catch( IllegalAccessException e )
+ {
+ e.printStackTrace();
+
+ String message = Translator.getString( "init_failed", new
String[]{readerClassName} );
+
+ throw new XDocletException( e, message );
+ }
+
+ //\\xmlReader.setErrorHandler( errorHandler );
+ xmlReader.setEntityResolver( this );
+
+ if( !( xmlReader instanceof ParserAdapter ) )
+ {
+ // turn validation on
+ try
+ {
+ xmlReader.setFeature(
"http://xml.org/sax/features/validation", true );
+ }
+ catch( SAXNotRecognizedException e )
+ {
+ e.printStackTrace();
+ }
+ catch( SAXNotSupportedException e )
+ {
+ e.printStackTrace();
+ }
}
}
}
_______________________________________________
Xdoclet-devel mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/xdoclet-devel