Example usage for org.springframework.beans.factory.xml XmlReaderContext getResource

List of usage examples for org.springframework.beans.factory.xml XmlReaderContext getResource

Introduction

In this page you can find the example usage for org.springframework.beans.factory.xml XmlReaderContext getResource.

Prototype

public final Resource getResource() 

Source Link

Usage

From source file:com.trigonic.utils.spring.beans.ImportBeanDefinitionParser.java

@Override
protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
    XmlReaderContext readerContext = parserContext.getReaderContext();
    String primaryLocation = element.getAttribute(RESOURCE_ATTRIBUTE);
    if (!StringUtils.hasText(primaryLocation)) {
        readerContext.error("Resource location must not be empty", element);
        return null;
    }//from   w  w  w .java 2 s  .c  o m

    String alternateLocation = element.getAttribute(ALTERNATE_ATTRIBUTE);
    boolean optional = Boolean.parseBoolean(element.getAttribute(OPTIONAL_ATTRIBUTE));

    String currentLocation = primaryLocation;
    try {
        Set<Resource> actualResources = ImportHelper.importResource(readerContext.getReader(),
                readerContext.getResource(), currentLocation);

        if (actualResources.isEmpty() && StringUtils.hasLength(alternateLocation)) {
            currentLocation = alternateLocation;
            actualResources = ImportHelper.importResource(readerContext.getReader(),
                    readerContext.getResource(), currentLocation);
        }

        if (actualResources.isEmpty() && !optional) {
            readerContext.error("Primary location [" + primaryLocation + "]"
                    + (alternateLocation == null ? "" : " and alternate location [" + alternateLocation + "]")
                    + " are not optional", element);
            return null;
        }

        Resource[] actResArray = actualResources.toArray(new Resource[actualResources.size()]);
        readerContext.fireImportProcessed(primaryLocation, actResArray, readerContext.extractSource(element));
    } catch (BeanDefinitionStoreException ex) {
        readerContext.error("Failed to import bean definitions from location [" + currentLocation + "]",
                element, ex);
    }

    return null;
}

From source file:org.impalaframework.module.beanset.ImportingBeanDefinitionDocumentReader.java

@Override
public void registerBeanDefinitions(Document doc, XmlReaderContext readerContext) {
    String filename = readerContext.getResource().getFilename();
    if (logger.isInfoEnabled())
        logger.info("Registering definitions for " + filename);

    if (!importedResources.contains(filename))
        namedResources.add(filename);//from w  ww. j  a v  a 2 s. c  o  m
    doRegisterBeanDefinitions(filename, doc, readerContext);
}