Error Handling

建议应用程序在设置新属性之一时catchorg\.xml\.sax\.SAXNotRecognizedException异常,以便应用程序可以在不支持它们的较旧版本上正常运行。例如,可下载的sample code包含以下方法isNewPropertySupported,该方法检测示例是否以支持JDK_GENERAL_ENTITY_SIZE_LIMIT属性的 JDK 版本运行:

public boolean isNewPropertySupported() {
    try {
        SAXParser parser = getSAXParser(false, false, false);
        parser.setProperty(JDK_GENERAL_ENTITY_SIZE_LIMIT, "10000");
    } catch (ParserConfigurationException ex) {
        fail(ex.getMessage());
    } catch (SAXException ex) {
        String err = ex.getMessage();
        if (err.indexOf("Property '" + JDK_GENERAL_ENTITY_SIZE_LIMIT +
                                       "' is not recognized.") > -1) {
            //expected before this patch
            debugPrint("New limit properties not supported. Samples not run.");
            return false;
        }
    }
    return true;
}

当 Importing 文件包含导致超出限制的异常的构造时,应用程序可能会检查错误代码以确定故障的性质。为限制定义了以下错误代码:

  • EntityExpansionLimit:JAXP00010001

  • ElementAttributeLimit:JAXP00010002

  • MaxEntitySizeLimit:JAXP00010003

  • TotalEntitySizeLimit:JAXP00010004

  • MaxXMLNameLimit:JAXP00010005

  • maxElementDepth:JAXP00010006

  • EntityReplacementLimit:JAXP00010007

错误代码具有以下格式:

"JAXP" + components (two digits) + error category (two digits) + sequence number

因此,代码 JAXP00010001 表示 JAXP 基本解析器安全限制EntityExpansionLimit