Kumaran
Kumaran

Reputation: 31

Sax Parser - Catalog Resolver Entity is resolving each and every time while it validating XML

I got a task to validate xml against dtd and we'll get more than 20k files to validate.

While testing the performance, we have found the below discrepancies

I have kept the dtd and it's supporting files under resources, created catalogManager.properties file and referred the catalog file.
Is there any way to resolving catalog once and validate all the files?

    private List<ValidationResult> validateWithDTDUsingCR(final List<Path> validateFileList) throws IOException { 
        LOGGER.logEvent("validateWithDTDUsingDOM", "File Parsing started : "); 
        long start = System.currentTimeMillis(); 
        final List<ValidationResult> response = new ArrayList<>(); 
        try { 
        final SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setNamespaceAware(true); 
        factory.setValidating(true);
        final SAXParser parser = factory.newSAXParser();
        
        parser.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, ""); 
        parser.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
        
        for (Path targetFolderPath : validateFileList) {
            final List<String> exceptions = new LinkedList<>();
            final File localXMLFile = new File(targetFolderPath.toString());
            try {
                parser.parse(new FileInputStream(localXMLFile), new MyHandler());
            } catch (SAXException | IOException e) {
                addResponse(response, localXMLFile, exceptions, e);
            } catch (Exception e) {
                addResponse(response, localXMLFile, exceptions, e);
            }
        }
    } catch (ParserConfigurationException | SAXException exception) {
        LOGGER.logEvent("validateWithDTDUsingDOM", "ERROR Occurred: " + exception);
    }
    long stop = System.currentTimeMillis();
    System.out.println("validateWithDTDUsingDOM Elapsed: " + (stop - start) + " ms");
    return response;
}

final class MyHandler extends DefaultHandler {
    final CatalogResolver catalogResolver = new CatalogResolver();
    @Override
    public InputSource resolveEntity(String publicId, String systemId) throws IOException, SAXException {
        System.out.println("resolve: "+ systemId);
        return catalogResolver.resolveEntity(publicId, systemId);
    }

    @Override
    public void warning(SAXParseException e) throws SAXException {
        System.out.println("WARNING : " + e);
    }

    @Override
    public void error(SAXParseException e) throws SAXException {
        System.out.println("ERROR : " + e);
    }
}

Upvotes: 0

Views: 51

Answers (0)

Related Questions