Example usage for org.springframework.core.io.support EncodedResource getEncoding

List of usage examples for org.springframework.core.io.support EncodedResource getEncoding

Introduction

In this page you can find the example usage for org.springframework.core.io.support EncodedResource getEncoding.

Prototype

@Nullable
public final String getEncoding() 

Source Link

Document

Return the encoding to use for reading from the #getResource() resource , or null if none specified.

Usage

From source file:org.data.support.beans.factory.xml.XmlQueryDefinitionReader.java

/**
 * Load query definitions from the specified XML file.
 * @param encodedResource the resource descriptor for the XML file,
 * allowing to specify an encoding to use for parsing the file
 * @return the number of query definitions found
 * @throws QueryDefinitionStoreException in case of loading or parsing errors
 *///from  ww w . j  a v  a  2 s.com
public int loadQueryDefinitions(EncodedResource encodedResource) throws QueryDefinitionStoreException {
    Assert.notNull(encodedResource, "EncodedResource must not be null");
    if (logger.isInfoEnabled()) {
        logger.info("Loading XML bean definitions from " + encodedResource.getResource());
    }

    Set<EncodedResource> currentResources = this.resourcesCurrentlyBeingLoaded.get();
    if (currentResources == null) {
        currentResources = new HashSet<EncodedResource>(4);
        this.resourcesCurrentlyBeingLoaded.set(currentResources);
    }
    if (!currentResources.add(encodedResource)) {
        throw new QueryDefinitionStoreException(
                "Detected cyclic loading of " + encodedResource + " - check your import definitions!");
    }
    try {
        InputStream inputStream = encodedResource.getResource().getInputStream();
        try {
            InputSource inputSource = new InputSource(inputStream);
            if (encodedResource.getEncoding() != null) {
                inputSource.setEncoding(encodedResource.getEncoding());
            }
            return doLoadQueryDefinitions(inputSource, encodedResource.getResource());
        } finally {
            inputStream.close();
        }
    } catch (IOException ex) {
        throw new QueryDefinitionStoreException(
                "IOException parsing XML document from " + encodedResource.getResource(), ex);
    } finally {
        currentResources.remove(encodedResource);
        if (currentResources.isEmpty()) {
            this.resourcesCurrentlyBeingLoaded.remove();
        }
    }
}