You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by "Laxman, Vidya" <VL...@Tilion.com> on 2000/06/07 15:30:52 UTC

EntityResolver- question

Hi Folks,

  I am trying to reference another DTD instead of the one which is present
inside the XML and am using the EntityResolver to do it but unsuccessfully,
any help will be greatly appreciated. I am attaching my code too.

Thanks,
-Vidya

My code is:
public class SAXResolver
    extends DefaultHandler {

    //
    // Constants
    //

    /** Default parser name. */
    private static final String
        DEFAULT_PARSER_NAME = "org.apache.xerces.parsers.SAXParser";

    public static InputSource is;

    StringReader reader =new
StringReader("http://mycomp.comp.com/cXML/cXML.dtd");
    //
    // Data
    //

    /** Print writer. */
    protected PrintWriter out;

    /** Canonical output. */
    protected boolean canonical;

    //
    // Constructors
    //

    /** Default constructor. */
    public SAXResolver(boolean canonical) throws
UnsupportedEncodingException {
        this(null, canonical);
    }

    protected SAXResolver(String encoding, boolean canonical) throws
UnsupportedEncodingException {

        if (encoding == null) {
            encoding = "UTF8";
        }

        out = new PrintWriter(new OutputStreamWriter(System.out, encoding));
        this.canonical = canonical;

    } // <init>(String,boolean)

    //
    // Public static methods
    //

    /** Prints the output from the SAX callbacks. */
    public static void print(String parserName, String uri, boolean
canonical) {

        try {
            DefaultHandler handler = new SAXResolver(canonical);

            XMLReader parser =
(XMLReader)Class.forName(parserName).newInstance();
            parser.setContentHandler(handler);
            parser.setEntityResolver(handler);
            parser.setErrorHandler(handler);
 
parser.setFeature("http://xml.org/sax/features/namespaces", false);

            boolean validate = true;
            boolean warmup = false;
            if (validate)
                parser.setFeature("http://xml.org/sax/features/validation",
true);

            if (warmup) {
 
parser.setFeature("http://apache.org/xml/features/continue-after-fatal-error
", true);
                parser.parse(uri);
                warmup = false;
            }
            parser.parse(uri);
        }
        catch (Exception e) {
            e.printStackTrace(System.err);
        }

    } // print(String,String,boolean)


    public InputSource resolveEntity(String pubId, String systemId) throws
SAXException{
        if (systemId != null) {
            int index = systemId.lastIndexOf('/');
            if (index != -1){
                systemId = systemId.substring(index + 1);
                System.out.println("SystemId is: " + systemId + "\n");
            }
              return new InputSource(reader);
        }
    } // resolveEntity