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

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

Introduction

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

Prototype

@Nullable
public Object extractSource(Object sourceCandidate) 

Source Link

Document

Call the source extractor for the given source object.

Usage

From source file:org.codehaus.grepo.core.config.GenericRepositoryScanBeanDefinitionParser.java

/**
 * @param configContext The config context.
 * @param parserContext The parser context.
 *///from   w  w  w  . j  a v  a2  s . c  o  m
protected void parseBeanNameGenerator(GenericRepositoryConfigContext configContext,
        ParserContext parserContext) {
    XmlReaderContext readerContext = parserContext.getReaderContext();
    try {
        if (configContext.hasNameGenerator()) {
            BeanNameGenerator generator = (BeanNameGenerator) instantiateUserDefinedStrategy(
                    configContext.getNameGenerator(), BeanNameGenerator.class,
                    parserContext.getReaderContext().getResourceLoader().getClassLoader());
            setBeanNameGenerator(generator);
        }
    } catch (Exception ex) {
        readerContext.error(ex.getMessage(), readerContext.extractSource(configContext.getElement()),
                ex.getCause());
    }
}

From source file:com.bstek.dorado.web.servlet.DefaultDoradoAppContextImporter.java

protected void importBeanDefinitionResource(String location, Element element, ParserContext parserContext)
        throws Exception {
    XmlReaderContext readerContext = parserContext.getReaderContext();
    try {//  w  w  w .j av  a  2s  .  c o m
        ResourceLoader resourceLoader = readerContext.getResourceLoader();
        Resource relativeResource = resourceLoader.getResource(location);

        int importCount = readerContext.getReader().loadBeanDefinitions(relativeResource);
        if (logger.isDebugEnabled()) {
            logger.debug(
                    "Imported " + importCount + " bean definitions from dorado-context [" + location + "]");
        }
    } catch (Exception ex) {
        readerContext.error("Invalid dorado-context [" + location + "] to import bean definitions from",
                element, null, ex);
    }

    readerContext.fireImportProcessed(location, readerContext.extractSource(element));
}

From source file:info.sargis.eventbus.config.EventBusHandlerBeanDefinitionParser.java

protected void parseTypeFilters(Element element, ClassPathBeanDefinitionScanner scanner,
        XmlReaderContext readerContext, ParserContext parserContext) {

    // Parse exclude and include filter elements.
    ClassLoader classLoader = scanner.getResourceLoader().getClassLoader();
    NodeList nodeList = element.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            String localName = parserContext.getDelegate().getLocalName(node);
            try {
                if (INCLUDE_FILTER_ELEMENT.equals(localName)) {
                    TypeFilter typeFilter = createTypeFilter((Element) node, classLoader);
                    scanner.addIncludeFilter(typeFilter);
                } else if (EXCLUDE_FILTER_ELEMENT.equals(localName)) {
                    TypeFilter typeFilter = createTypeFilter((Element) node, classLoader);
                    scanner.addExcludeFilter(typeFilter);
                }//  w  ww.j a v  a2s . co m
            } catch (Exception ex) {
                readerContext.error(ex.getMessage(), readerContext.extractSource(element), ex.getCause());
            }
        }
    }
}

From source file:org.mybatis.spring.config.MapperScannerBeanDefinitionParser.java

/**
 * {@inheritDoc}/*from   w  ww .  ja v a2  s . co  m*/
 */
@Override
public synchronized BeanDefinition parse(Element element, ParserContext parserContext) {
    ClassPathMapperScanner scanner = new ClassPathMapperScanner(parserContext.getRegistry());
    ClassLoader classLoader = scanner.getResourceLoader().getClassLoader();
    XmlReaderContext readerContext = parserContext.getReaderContext();
    scanner.setResourceLoader(readerContext.getResourceLoader());
    try {
        String annotationClassName = element.getAttribute(ATTRIBUTE_ANNOTATION);
        if (StringUtils.hasText(annotationClassName)) {
            @SuppressWarnings("unchecked")
            Class<? extends Annotation> markerInterface = (Class<? extends Annotation>) classLoader
                    .loadClass(annotationClassName);
            scanner.setAnnotationClass(markerInterface);
        }
        String markerInterfaceClassName = element.getAttribute(ATTRIBUTE_MARKER_INTERFACE);
        if (StringUtils.hasText(markerInterfaceClassName)) {
            Class<?> markerInterface = classLoader.loadClass(markerInterfaceClassName);
            scanner.setMarkerInterface(markerInterface);
        }
        String nameGeneratorClassName = element.getAttribute(ATTRIBUTE_NAME_GENERATOR);
        if (StringUtils.hasText(nameGeneratorClassName)) {
            Class<?> nameGeneratorClass = classLoader.loadClass(nameGeneratorClassName);
            BeanNameGenerator nameGenerator = BeanUtils.instantiateClass(nameGeneratorClass,
                    BeanNameGenerator.class);
            scanner.setBeanNameGenerator(nameGenerator);
        }
    } catch (Exception ex) {
        readerContext.error(ex.getMessage(), readerContext.extractSource(element), ex.getCause());
    }
    String sqlSessionTemplateBeanName = element.getAttribute(ATTRIBUTE_TEMPLATE_REF);
    scanner.setSqlSessionTemplateBeanName(sqlSessionTemplateBeanName);
    String sqlSessionFactoryBeanName = element.getAttribute(ATTRIBUTE_FACTORY_REF);
    scanner.setSqlSessionFactoryBeanName(sqlSessionFactoryBeanName);
    scanner.registerFilters();
    String basePackage = element.getAttribute(ATTRIBUTE_BASE_PACKAGE);
    scanner.scan(StringUtils.tokenizeToStringArray(basePackage,
            ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS));
    return null;
}

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  ww . j a  va2 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.codehaus.grepo.core.config.GenericRepositoryScanBeanDefinitionParser.java

/**
 * @param element The element.//from w  ww  .j  av  a2s.c o  m
 * @param scanner The scanner.
 * @param readerContext The reader context.
 */
protected void parseTypeFilters(Element element, GenericRepositoryBeanDefinitionScanner scanner,
        XmlReaderContext readerContext) {

    // Parse exclude and include filter elements.
    ClassLoader classLoader = scanner.getResourceLoader().getClassLoader();
    NodeList nodeList = element.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            String localName = node.getLocalName();
            try {
                if (INCLUDE_FILTER_ELEMENT.equals(localName)) {
                    // Note: that we use the composite type filter for include-filter,
                    // because we always want repositories of appropriate interface type...
                    CompositeTypeFilter typeFilter = new CompositeTypeFilter();
                    typeFilter.addTypeFilter(new AssignableTypeFilter(requiredGenericRepositoryType));
                    typeFilter.addTypeFilter(createTypeFilter((Element) node, classLoader));
                    scanner.addIncludeFilter(typeFilter);
                } else if (EXCLUDE_FILTER_ELEMENT.equals(localName)) {
                    TypeFilter typeFilter = createTypeFilter((Element) node, classLoader);
                    scanner.addExcludeFilter(typeFilter);
                }
            } catch (Exception ex) {
                readerContext.error(ex.getMessage(), readerContext.extractSource(element), ex.getCause());
            }
        }
    }
}