User: vharcq  
  Date: 02/03/12 23:31:19

  Modified:    core/test/src/xdoclet/retest/test XmlRegressionTestCase.java
  Log:
  Add a local entity resolver for DTDs (thanks to JBoss)
  
  Revision  Changes    Path
  1.2       +129 -1    
xdoclet/core/test/src/xdoclet/retest/test/XmlRegressionTestCase.java
  
  Index: XmlRegressionTestCase.java
  ===================================================================
  RCS file: 
/cvsroot/xdoclet/xdoclet/core/test/src/xdoclet/retest/test/XmlRegressionTestCase.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -w -r1.1 -r1.2
  --- XmlRegressionTestCase.java        5 Mar 2002 21:27:25 -0000       1.1
  +++ XmlRegressionTestCase.java        13 Mar 2002 07:31:18 -0000      1.2
  @@ -6,6 +6,9 @@
   import org.w3c.dom.Node;
   import org.xml.sax.SAXParseException;
   import org.xml.sax.SAXException;
  +import org.xml.sax.EntityResolver;
  +import org.xml.sax.InputSource;
  +import org.xml.sax.ErrorHandler;
   import xdoclet.XDocletException;
   
   import javax.xml.parsers.DocumentBuilderFactory;
  @@ -14,11 +17,12 @@
   import java.io.IOException;
   import java.io.File;
   import java.io.FileNotFoundException;
  +import java.util.Hashtable;
   
   /**
    * @author    Vincent Harcq ([EMAIL PROTECTED])
    * @created   Mars 5, 2002
  - * @version   $Revision: 1.1 $
  + * @version   $Revision: 1.2 $
    */
   public class XmlRegressionTestCase
           extends XDocletRegressionTestCase
  @@ -46,7 +50,12 @@
           Document doc;
           try{
               DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  +            dbf.setValidating(true);
               DocumentBuilder db = dbf.newDocumentBuilder();
  +            LocalResolver resolver = new LocalResolver();
  +            LocalErrorHandler error = new LocalErrorHandler("",resolver);
  +            db.setEntityResolver(resolver);
  +            db.setErrorHandler(error);
               doc = db.parse(uri);
               return doc;
           }catch (ParserConfigurationException e){
  @@ -78,5 +87,124 @@
           return readDocument(genXmlBase + File.separator + getClassName() + 
File.separator + file);
       }
   
  +
  +    private static class LocalResolver implements EntityResolver
  +    {
  +        private Hashtable dtds = new Hashtable();
  +        private boolean hasDTD = false;
  +
  +        public LocalResolver()
  +        {
  +            registerDTD("-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 
1.1//EN", "../resources/xdoclet/ejb/ejb11-jar.dtd");
  +            registerDTD("-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 
2.0//EN", "../resources/xdoclet/ejb/ejb20-jar.dtd");
  +            registerDTD("-//Sun Microsystems, Inc.//DTD J2EE Application 1.2//EN", 
"../resources/xdoclet/ejb/application_1_2.dtd");
  +            registerDTD("-//Sun Microsystems, Inc.//DTD Connector 1.0//EN", 
"../resources/xdoclet/ejb/connector_1_0.dtd");
  +            registerDTD("-//JBoss//DTD JAWS 2.4//EN", 
"../resources/xdoclet/ejb/vendor/jaws_2_4.dtd");
  +            registerDTD("-//JBoss//DTD JAWS 3.0//EN", 
"../resources/xdoclet/ejb/vendor/jaws_3_0.dtd");
  +            registerDTD("-//JBoss//DTD JBOSS 
2.4//EN","../resources/xdoclet/ejb/vendor/jboss_2_4.dtd");
  +            registerDTD("-//JBoss//DTD JBOSS 
3.0//EN","../resources/xdoclet/ejb/vendor/jboss_3_0.dtd");
  +        }
  +
  +        /**
  +         * Registers available DTDs
  +         * @param String publicId    - Public ID of DTD
  +         * @param String dtdFileName - the file name of DTD
  +         */
  +        public void registerDTD(String publicId, String dtdFileName)
  +        {
  +            dtds.put(publicId, dtdFileName);
  +        }
  +
  +        /**
  +         * Returns DTD inputSource. Is DTD was found in the hashtable and 
inputSource was created
  +         * flad hasDTD is ser to true.
  +         * @param String publicId    - Public ID of DTD
  +         * @param String dtdFileName - the file name of DTD
  +         * @return InputSource of DTD
  +         */
  +        public InputSource resolveEntity(String publicId, String systemId)
  +        {
  +            hasDTD = false;
  +            String dtd = (String)dtds.get(publicId);
  +
  +            if (dtd != null)
  +            {
  +                hasDTD = true;
  +                try
  +                {
  +                    InputSource aInputSource = new InputSource(dtd);
  +                    return aInputSource;
  +                } catch( Exception ex )
  +                {
  +                    // ignore
  +                }
  +            }
  +            return null;
  +        }
  +
  +        /**
  +         * Returns the boolean value to inform id DTD was found in the XML file or 
not
  +         * @return boolean - true if DTD was found in XML
  +         */
  +        public boolean hasDTD()
  +        {
  +            return hasDTD;
  +        }
  +
  +    }
  +
  +    private static class LocalErrorHandler implements ErrorHandler
  +    {
  +        private String theFileName;
  +        private LocalResolver localResolver;
  +
  +        public LocalErrorHandler( String inFileName, LocalResolver localResolver )
  +        {
  +            this.theFileName = inFileName;
  +            this.localResolver = localResolver;
  +        }
  +
  +        public void error(SAXParseException exception)
  +        {
  +            if ( localResolver.hasDTD() )
  +            {
  +                System.err.println("File "
  +                        + theFileName
  +                        + " process error. Line: "
  +                        + String.valueOf(exception.getLineNumber())
  +                        + ". Error message: "
  +                        + exception.getMessage()
  +                );
  +            }
  +        }
  +
  +        public void fatalError(SAXParseException exception)
  +        {
  +            if ( localResolver.hasDTD() )
  +            {
  +                System.err.println("File "
  +                        + theFileName
  +                        + " process fatal error. Line: "
  +                        + String.valueOf(exception.getLineNumber())
  +                        + ". Error message: "
  +                        + exception.getMessage()
  +                );
  +            }
  +        }
  +
  +        public void warning(SAXParseException exception)
  +        {
  +            if ( localResolver.hasDTD() )
  +            {
  +                System.err.println("File "
  +                        + theFileName
  +                        + " process warning. Line: "
  +                        + String.valueOf(exception.getLineNumber())
  +                        + ". Error message: "
  +                        + exception.getMessage()
  +                );
  +            }
  +        }
  +    }
   
   }
  
  
  

_______________________________________________
Xdoclet-devel mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/xdoclet-devel

Reply via email to