I've added some code for local resolving of entities to the optional
XMLValidateTask. Following is an example, how to use it:

  <xmlvalidate failonerror="false" lenient="false" warn="true">
    <fileset dir="${etc}" includes="*.xml"/>
    <dtd publicId="-//DCTSS//DTD service//EN"
         location="${lib}/dtd/service_1_0.dtd"/>
    <dtd publicId="-//DCTSS//DTD juna-service//EN"
         location="${lib}/dtd/juna-service_1_0.dtd"/>
    <dtd publicId="-//Sun Microsystems, Inc.//DTD J2EE Client 1.2//EN"
         location="${lib}/dtd/application-client_1_2.dtd"/>
    <dtd publicId="-//DCTSS//DTD juna-client//EN"
         location="${lib}/dtd/juna-client_1_0.dtd"/>
  </xmlvalidate>


I'de love committing it myself, but I don't have write access to the
repository ..

Holger Engels
56,58c56
< import java.io.File;
< import java.io.FileReader;
< import java.io.IOException;
---
> import java.io.*;

61,63c59
< import java.util.Enumeration;
< import java.util.Hashtable;
< import java.util.Vector;
---
> import java.util.*;

72,79c68
< import org.xml.sax.ErrorHandler;
< import org.xml.sax.InputSource;
< import org.xml.sax.Parser;
< import org.xml.sax.SAXException;
< import org.xml.sax.SAXNotRecognizedException;
< import org.xml.sax.SAXNotSupportedException;
< import org.xml.sax.SAXParseException;
< import org.xml.sax.XMLReader;
---
> import org.xml.sax.*;

120a110,113
>     /**

>      * The list of configured DTD locations

>      */

>     public ArrayList dtdLocations = new ArrayList();

213a207,227
>     /**

>      * Create a DTD location record. This stores the location of a DTD. The 
> DTD is identified

>      * by its public Id. The location may either be a file location or a 
> resource location.

>      */

>     public DTDLocation createDTD() {

>         DTDLocation dtdLocation = new DTDLocation();

>         dtdLocations.add(dtdLocation);

>         

>         return dtdLocation;

>     }

> 

>     protected EntityResolver getEntityResolver() {

>       LocalResolver resolver = new LocalResolver();

> 

>         for (Iterator i = dtdLocations.iterator(); i.hasNext();) {

>             DTDLocation location = (DTDLocation)i.next();

>           resolver.registerDTD(location);

>         }

>         return resolver; 

>     }

> 

297a312
>       xmlReader.setEntityResolver(getEntityResolver());

399d413
< 
405d418
< 
437a451,555
> 

>     public static class DTDLocation {

>         private String publicId = null;

>         private String location = null;

>         

>         public void setPublicId(String publicId) {

>             this.publicId = publicId;

>         }

>         

>         public void setLocation(String location) {

>             this.location = location;

>         }

>         

>         public String getPublicId() {

>             return publicId;

>         }

>         

>         public String getLocation() {

>             return location;

>         }

>     }

> 

>     private class LocalResolver

>       implements EntityResolver

>     {

>       private Hashtable fileDTDs = new Hashtable();

>       private Hashtable resourceDTDs = new Hashtable();

>       private Hashtable urlDTDs = new Hashtable();

> 

>       public LocalResolver() {}

>       

>       public void registerDTD(String publicId, String location) {

>           if (location == null) {

>               return;

>           }

>         

>           File fileDTD = new File(location);

>           if (fileDTD.exists()) {

>               if (publicId != null) {

>                   fileDTDs.put(publicId, fileDTD);

>                   log("Mapped publicId " + publicId + " to file " + fileDTD, 
> Project.MSG_VERBOSE);

>               }

>               return;

>           }

>         

>           if (getClass().getResource(location) != null) {

>               if (publicId != null) {

>                   resourceDTDs.put(publicId, location);

>                   log("Mapped publicId " + publicId + " to resource " + 
> location, Project.MSG_VERBOSE);

>               }

>           }

> 

>           try {

>               if (publicId != null) {

>                   URL urldtd = new URL(location);

>                   urlDTDs.put(publicId, urldtd);

>               }            

>           } catch ( java.net.MalformedURLException   e) {

>               //ignored

>           } 

>       }

> 

>       public InputSource resolveEntity(String publicId, String systemId)

>           throws SAXException

>       {

>           File dtdFile = (File) fileDTDs.get(publicId);

>           if (dtdFile != null) {

>               try {

>                   log("Resolved " + publicId + " to local file " + dtdFile, 
> Project.MSG_VERBOSE);

>                   return new InputSource(new FileInputStream(dtdFile));

>               } catch( FileNotFoundException ex ) {

>                   // ignore

>               }

>           }

> 

>           String dtdResourceName = (String)resourceDTDs.get(publicId); 

>           if (dtdResourceName != null) {

>               InputStream is = 
> this.getClass().getResourceAsStream(dtdResourceName);

>               if (is != null) {

>                   log("Resolved " + publicId + " to local resource " + 
> dtdResourceName, Project.MSG_VERBOSE);

>                   return new InputSource(is);

>               }

>           }

> 

>           URL dtdUrl = (URL) urlDTDs.get(publicId);

>           if ( dtdUrl != null ) {

>               try {

>                   InputStream is = dtdUrl.openStream();

>                   log("Resolved " + publicId + " to url " + dtdUrl, 
> Project.MSG_VERBOSE);

>                   return new InputSource(is);

>               } catch ( IOException ioe) {

>                   //ignore

>               }            

>           } 

>                 

>           log("Could not resolve ( publicId: " + publicId + ", systemId: " + 
> systemId + ") to a local entity", 

>               Project.MSG_INFO);

>         

>           return null;

>       }

> 

>       public void registerDTD(DTDLocation location) {

>           registerDTD(location.getPublicId(), location.getLocation());

>       }

>     }

Reply via email to