[jdom-interest] Re: No Validation

David Churchville dmc at clearlight.com
Thu Feb 22 10:19:51 PST 2001


>>
>> I have another problem now in that my firewall is quite slow and as a
>> result my server retrieves the dtd's too slowly and times out causing
>> build to fail.  What puzzles me is that I think I have
>> validation turned
>> off and therefore there should be no reason for build to get
>> the dtd at
>> all.

Yes, its not that the parser is trying to validate, its just making sure
that any entity references from another file are loaded.  To work around
this, you can define your own EntityResolver.  This is a class that SAX
defines to get the first crack at returning an "InputSource" whenever an
external reference is used. You can return a dummy InputSource to
essentially ignore all external references.  You could also *selectively*
ignore references and pass on others by returning null for data that you
want to be loaded.  See the SAX documentation for more details...

I've included an example resolver that passes on "normal" URL references,
and tries to load local file references if they are found.  You would just
have to look for specific URL patterns if you wanted to intercept specific
external references (i.e. those to cXML.org)

Code
------

    SAXBuilder builder = new SAXBuilder();
    builder.setEntityResolver( new LocalEntityResolver());

   // resolver for local files (Windows style file names)
   class LocalEntityResolver implements EntityResolver {
      // if the system id is a valid URL, do nothing, else look for a file
      public InputSource resolveEntity( String publicID, String systemID) {
         InputSource is = null;
         // is it a valid URL? reference??
         try {
            java.net.URL url = new java.net.URL( systemID);
         } catch( java.net.MalformedURLException mex) {
            // No, maybe its a local file reference - try it...
            try {
               is = new InputSource(new java.io.BufferedReader(new
FileReader(systemID)) );
            } catch (Exception e) {
               // file not found or some other problem...do nothing
            }
         }
         return is;
      }
   }









More information about the jdom-interest mailing list