Repost: Using EntityResolver to NOT resolve a PE Reference

Discussion in 'Java' started by =?ISO-8859-1?Q?Ricardo_Palomares_Mart=EDnez?=, Aug 19, 2006.

  1. [Resending, as I haven't got any answer and I can't find the message
    searching Google Groups]

    Hi,

    I'm a bit stuck with this problem. I'm modifying an existing program
    (let's call it "MT") to help in Mozilla Localization (L10n) process.
    Mozilla L10n is based in .properties and .dtd files, so the Java program
    must be able to parse them and generate the corresponding localized
    versions.

    Some DTD files use PE references, like this:

    <!ENTITY % brandDTD SYSTEM "chrome://branding/locale/brand.dtd">
    %brandDTD;

    This is supposed to locate and load "chrome://branding/locale/brand.dtd"
    and insert it replacing "%brandDTD;". However, there are two problems
    with this entity:

    - "chrome:" is an internal Mozilla protocol which allows the Mozilla
    built-in parser to load, in this case, the trademark and branding stuff
    in a localized way. Of course, it has no sense out of Mozilla environment.

    - actually, there is no need at all for MT (the L10n help program) to
    resolve the PE entity: the referenced file has been loaded in another
    part of the directory structure and, thus, their entities can be
    translated, so inserting again will result in duplicated work for the
    localizer, besides risking translation inconsistency.

    So, ideally, what I want is to NOT resolve this kind of entities.
    Instead, I should maintain a list of external entities in every parsed
    file and put them back on translation exporting.

    MT has a class extending DefaultHandler2 (which, in turns, implements
    EntityResolver), with this overriding method:

    public InputSource resolveEntity(java.lang.String name,
    java.lang.String publicId, java.lang.String baseURI,
    java.lang.String systemId) {

    if (name.startsWith("%")) {
    System.out.println("Let's ignore it!");
    // Tried also with return null;
    return new InputSource();
    } else {
    // Next line was original code for this method;
    // it's probably unneeded/wrong
    return new InputSource(is);
    }
    }


    But I keep getting this exception (sorry, it's long) because the parser
    tries to resolve the URL (and, of course, it fails):

    14-ago-2006 13:59:42 org.mozillatranslator.io.file.FileAccessAdapter load
    GRAVE: Unable to read file: Cannot read dtd file netError.dtd
    org.mozillatranslator.io.common.MozIOException: Cannot read dtd file
    netError.dtd
    at org.mozillatranslator.io.file.DTDAccess.beginRead(DTDAccess.java:221)
    at
    org.mozillatranslator.io.file.FileAccessAdapter.load(FileAccessAdapter.java:166)
    at org.mozillatranslator.datamodel.DTDFile.load(DTDFile.java:56)
    (...)
    Caused by: org.mozillatranslator.io.common.MozIOException: DTD load original
    at
    org.mozillatranslator.io.file.DTDReadHelper.loadOriginal(DTDReadHelper.java:119)
    at org.mozillatranslator.io.file.DTDAccess.beginRead(DTDAccess.java:185)
    ... 8 more
    Caused by: java.net.MalformedURLException
    at java.net.URL.<init>(URL.java:601)
    at java.net.URL.<init>(URL.java:464)
    at java.net.URL.<init>(URL.java:413)
    at
    com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:968)
    at
    com.sun.org.apache.xerces.internal.impl.XMLEntityManager.startEntity(XMLEntityManager.java:905)
    at
    com.sun.org.apache.xerces.internal.impl.XMLEntityManager.startEntity(XMLEntityManager.java:843)
    at
    com.sun.org.apache.xerces.internal.impl.XMLDTDScannerImpl.startPE(XMLDTDScannerImpl.java:686)
    at
    com.sun.org.apache.xerces.internal.impl.XMLDTDScannerImpl.skipSeparator(XMLDTDScannerImpl.java:2065)
    at
    com.sun.org.apache.xerces.internal.impl.XMLDTDScannerImpl.scanDecls(XMLDTDScannerImpl.java:2027)
    at
    com.sun.org.apache.xerces.internal.impl.XMLDTDScannerImpl.scanDTDExternalSubset(XMLDTDScannerImpl.java:319)
    at
    com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$DTDDispatcher.dispatch(XMLDocumentScannerImpl.java:1030)
    at
    com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:368)
    at
    com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:834)
    at
    com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764)
    at
    com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:148)
    at
    com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1242)
    at javax.xml.parsers.SAXParser.parse(SAXParser.java:375)
    at javax.xml.parsers.SAXParser.parse(SAXParser.java:176)
    at
    org.mozillatranslator.io.file.DTDReadHelper.loadOriginal(DTDReadHelper.java:117)
    ... 9 more

    and the bad part is that not only fails to resolve the external entity,
    it completely gives up on parsing the original file, so its own entities
    aren't loaded either.

    I've also implemented externalEntityDecl() to keep track of external
    entities:

    public void externalEntityDecl(String name, String publicId,
    String systemId) {
    System.out.println("We have found an external entity decl. with
    these values:");
    System.out.println("Name: " + name + " - Public ID: " + publicId +
    " - System ID: " + systemId);

    this.externalEntities.add(new ExternalEntity(name, publicId,
    systemId));
    }


    Searching the web, I've found this nice explanation of how to use
    EntityResolver:

    http://www-128.ibm.com/developerworks/library/x-tipent.html

    But I can't find the way to give the parser an empty InputSource so it
    doesn't insert anything nor tries to resolve the systemId.

    Thank you for reading up to here. :) If anybody can help me (wild ideas
    invited) :) or point me to further resources (I've STFW, I promise),
    I'd be very grateful.

    TIA

    --
    If it's true that we are here to help others,
    then what exactly are the OTHERS here for?
     
    =?ISO-8859-1?Q?Ricardo_Palomares_Mart=EDnez?=, Aug 19, 2006
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thomas Scheffler

    JAXP:SAX: EntityResolver never used

    Thomas Scheffler, Nov 12, 2003, in forum: XML
    Replies:
    0
    Views:
    648
    Thomas Scheffler
    Nov 12, 2003
  2. Mark Wright
    Replies:
    0
    Views:
    756
    Mark Wright
    May 12, 2004
  3. christof hoeke

    empty EntityResolver for SAX

    christof hoeke, Dec 21, 2003, in forum: Python
    Replies:
    0
    Views:
    349
    christof hoeke
    Dec 21, 2003
  4. chris
    Replies:
    1
    Views:
    1,031
    Ralf Schmitt
    Jun 11, 2004
  5. Replies:
    1
    Views:
    10,347
Loading...

Share This Page