j-users@xerces.apache.org
[Top] [All Lists]

Re: Xerces DOM parser ignores EntityResolver providing local DTD for val

Subject: Re: Xerces DOM parser ignores EntityResolver providing local DTD for validation
From: Prashant Reddy
Date: Tue, 04 Mar 2008 11:31:55 +0530
I think xerces may be downloading from the internet, something the
LocalDTDResolver did not resolve.

How about putting a System.out.println() in the else{} of the
LocalDTDResolver to print the public Id, system Id.

public InputSource resolveEntity (String publicId, String systemId)
>    {
>      if (systemId.equals( mySystemIdToIntercept )) 
>      {
>        return new InputSource( localDtdFileAsUrl.toString() );
>      }
>      else
>      {
>        // use the default behaviour (?)
System.out.println("Could not resolve entity locally for
publicId :["+publicId+"] systemid :["+systemId+"]");
>        return null;
>      }
>    }

HTH
-Prashant

On Mon, 2008-03-03 at 07:37 -0800, Rob Davis-5 wrote:
> Hi, I want to read in and validate an XML file against a local DTD.
> 
> I have created a class based on EntityResolver which plugs into the Parser. 
> 
> However, this seems to be ignored because the parser still attempts to
> connect over the internet to w3c which I do not want. I know this because of
> the exception I get when it attempts (see below)
> 
> See also below my code.
> 
> Please advise
> 
> java.net.ConnectException: Connection refused: connect
>       at java.net.PlainSocketImpl.socketConnect(Native Method)
>       at java.net.PlainSocketImpl.doConnect(Unknown Source)
>       at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
>       at java.net.PlainSocketImpl.connect(Unknown Source)
>       at java.net.Socket.connect(Unknown Source)
>       at java.net.Socket.connect(Unknown Source)
>       at sun.net.NetworkClient.doConnect(Unknown Source)
>       at sun.net.www.http.HttpClient.openServer(Unknown Source)
>       at sun.net.www.http.HttpClient.openServer(Unknown Source)
>       at sun.net.www.http.HttpClient.<init>(Unknown Source)
>       at sun.net.www.http.HttpClient.New(Unknown Source)
>       at sun.net.www.http.HttpClient.New(Unknown Source)
>       at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown
> Source)
>       at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown 
> Source)
>       at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
>       at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown
> Source)
>       at
> org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:986)
>       at
> org.apache.xerces.impl.XMLEntityManager.startEntity(XMLEntityManager.java:897)
>       at
> org.apache.xerces.impl.XMLEntityManager.startDTDEntity(XMLEntityManager.java:864)
>       at
> org.apache.xerces.impl.XMLDTDScannerImpl.setInputSource(XMLDTDScannerImpl.java:241)
>       at
> org.apache.xerces.impl.XMLDocumentScannerImpl$DTDDispatcher.dispatch(XMLDocumentScannerImpl.java:1001)
>       at
> org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:324)
>       at
> org.apache.xerces.parsers.XML11Configuration.parse(XML11Configuration.java:845)
>       at
> org.apache.xerces.parsers.XML11Configuration.parse(XML11Configuration.java:768)
>       at org.apache.xerces.parsers.XMLParser.parse(XMLParser.java:108)
>       at org.apache.xerces.parsers.DOMParser.parse(DOMParser.java:230)
>       at SerializeTester.test(SerializeTester.java:30)
>       at SerializeTester.main(SerializeTester.java:62)
> 
> 
> 
> import java.io.File;
> import java.net.MalformedURLException;
> import java.net.URL;
> 
> import org.xml.sax.EntityResolver;
> import org.xml.sax.InputSource;
> 
> import java.io.File;
> import java.io.FileInputStream;
> import java.io.FileOutputStream;
> import java.io.OutputStream;
> import org.xml.sax.InputSource;
> import org.w3c.dom.Document;
> // Parser import
> import org.apache.xerces.parsers.DOMParser;
> 
> 
> 
> public class SerializeTester 
> {
>   // File to read XML from
>   private File inputXML;
>   
>   // File to serialize XML to
>   private File outputXML;
>   
>   public SerializeTester(File inputXML)
>   {
>       this.inputXML = inputXML;
>   }
>   
>   public void test(OutputStream outputStream) throws Exception
>   {
>       DOMParser parser = new DOMParser( );
>       
>       // Get the DOM tree as a Document object
>       parser.parse(new InputSource(
>       new FileInputStream(inputXML)));
>       
>     LocalDTDResolver LocalDTDResolver 
>     = new LocalDTDResolver(
> "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd";, 
>                                   new File("C:/svgdtd/svg11.dtd")
>                              ); 
> 
>   
>     parser.setEntityResolver( LocalDTDResolver );
>        
>       Document doc = parser.getDocument( );
>       // Serialize
>       
>       org.w3c.dom.Element w3cSvgTextElement = doc.getElementById( 
> "LABEL_OUTLINE"
> );
>         
>   }
>   
>   public static void main(String[] args)
>   {
>       if (args.length != 2)
>       {
>         System.out.println(
>           "Usage: java javaxml3.SerializeTester " +
>           "[XML document to read] " +
>           "[filename to write output to]");
>         return;
>       }
>       try
>       {
>         SerializeTester tester =
>           new SerializeTester(new File(args[0]));
>         tester.test(new FileOutputStream(new File(args[1])));
>       }
>       catch (Exception e)
>       {
>       e.printStackTrace( );
>       }
>   }
> }
>       
>       
> 
> 
> 
>  public class LocalDTDResolver implements EntityResolver {
>        
>    String mySystemIdToIntercept;
>    File myLocalDtdPath;
>    URL localDtdFileAsUrl;
>        
>    public LocalDTDResolver( String systemIdToIntercept, File localDtdPath )
> throws MalformedURLException
>    {
>          mySystemIdToIntercept = systemIdToIntercept;
>          myLocalDtdPath = localDtdPath;
>        
>        localDtdFileAsUrl = myLocalDtdPath.toURI().toURL();
>    }
>        
>    public InputSource resolveEntity (String publicId, String systemId)
>    {
>      if (systemId.equals( mySystemIdToIntercept )) 
>      {
>        return new InputSource( localDtdFileAsUrl.toString() );
>      }
>      else
>      {
>        // use the default behaviour (?)
>        return null;
>      }
>    }
>  }


---------------------------------------------------------------------
To unsubscribe, e-mail: j-users-unsubscribe@xxxxxxxxxxxxxxxxx
For additional commands, e-mail: j-users-help@xxxxxxxxxxxxxxxxx

<Prev in Thread] Current Thread [Next in Thread>