Search in sources :

Example 1 with SimpleSaxErrorHandler

use of org.springframework.util.xml.SimpleSaxErrorHandler in project spring-framework by spring-projects.

the class PersistenceUnitReader method readPersistenceUnitInfos.

/**
	 * Parse and build all persistence unit infos defined in the given XML files.
	 * @param persistenceXmlLocations the resource locations (can be patterns)
	 * @return the resulting PersistenceUnitInfo instances
	 */
public SpringPersistenceUnitInfo[] readPersistenceUnitInfos(String[] persistenceXmlLocations) {
    ErrorHandler handler = new SimpleSaxErrorHandler(logger);
    List<SpringPersistenceUnitInfo> infos = new LinkedList<>();
    String resourceLocation = null;
    try {
        for (String location : persistenceXmlLocations) {
            Resource[] resources = this.resourcePatternResolver.getResources(location);
            for (Resource resource : resources) {
                resourceLocation = resource.toString();
                InputStream stream = resource.getInputStream();
                try {
                    Document document = buildDocument(handler, stream);
                    parseDocument(resource, document, infos);
                } finally {
                    stream.close();
                }
            }
        }
    } catch (IOException ex) {
        throw new IllegalArgumentException("Cannot parse persistence unit from " + resourceLocation, ex);
    } catch (SAXException ex) {
        throw new IllegalArgumentException("Invalid XML in persistence unit from " + resourceLocation, ex);
    } catch (ParserConfigurationException ex) {
        throw new IllegalArgumentException("Internal error parsing persistence unit from " + resourceLocation);
    }
    return infos.toArray(new SpringPersistenceUnitInfo[infos.size()]);
}
Also used : SimpleSaxErrorHandler(org.springframework.util.xml.SimpleSaxErrorHandler) ErrorHandler(org.xml.sax.ErrorHandler) InputStream(java.io.InputStream) Resource(org.springframework.core.io.Resource) IOException(java.io.IOException) Document(org.w3c.dom.Document) LinkedList(java.util.LinkedList) SAXException(org.xml.sax.SAXException) SimpleSaxErrorHandler(org.springframework.util.xml.SimpleSaxErrorHandler) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Aggregations

IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 LinkedList (java.util.LinkedList)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 Resource (org.springframework.core.io.Resource)1 SimpleSaxErrorHandler (org.springframework.util.xml.SimpleSaxErrorHandler)1 Document (org.w3c.dom.Document)1 ErrorHandler (org.xml.sax.ErrorHandler)1 SAXException (org.xml.sax.SAXException)1