Java Code Examples for org.springframework.core.io.support.ResourcePatternResolver

Following code examples demonstrate how to use org.springframework.core.io.support.ResourcePatternResolverfrom spring-framework. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.springframework.core.io.support.ResourcePatternResolverand various code implementation of this class.

	public void registerCustomEditors(PropertyEditorRegistry registry) {
		ResourceEditor baseEditor = new ResourceEditor(this.resourceLoader);
		registry.registerCustomEditor(Resource.class, baseEditor);
		registry.registerCustomEditor(InputStream.class, new InputStreamEditor(baseEditor));
		registry.registerCustomEditor(File.class, new FileEditor(baseEditor));
		registry.registerCustomEditor(URL.class, new URLEditor(baseEditor));

		ClassLoader classLoader = this.resourceLoader.getClassLoader();
		registry.registerCustomEditor(Class.class, new ClassEditor(classLoader));
		registry.registerCustomEditor(URI.class, new URIEditor(classLoader));

		if (this.resourceLoader instanceof ResourcePatternResolver) {
			registry.registerCustomEditor(Resource[].class,
					new ResourceArrayPropertyEditor((ResourcePatternResolver) this.resourceLoader));
		}
	} 


    public static <Condition extends BaseQueryCondition> String getDynamicSql(Class<Condition> condition) {

        String text = null;
        String classpath = ReflectUtil.getClasspath(condition);
        String name = ReflectUtil.getName(condition);
        String queryName = name.substring(0, name.lastIndexOf("Condition"));
            try {
                String searchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
                        + org.springframework.util.ClassUtils.convertClassNameToResourcePath(classpath) + "/"
                        + name.substring(0, name.lastIndexOf("Condition")) + ".hbm.xml";
                org.springframework.core.io.support.ResourcePatternResolver resourcePatternResolver = new org.springframework.core.io.support.PathMatchingResourcePatternResolver();
                org.springframework.core.io.Resource[] resources = resourcePatternResolver.getResources(searchPath);
                InputStream is = resources[0].getInputStream();
                XmlParseUtil parseUtil = XmlParseUtil.getInstance(is);
                List<Element> children = parseUtil.getChildElement("sql-query");
                Element child = children.get(0);
                text = child.getText().trim();
                
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
            text = queryStringMap.get(queryName);
        } 

    public static <Condition extends BaseQueryCondition> String getSql(Class<Condition> condition) {
        NamedSQLQueryDefinition nsq;
        String classpath = ReflectUtil.getClasspath(condition);
        String name = ReflectUtil.getName(condition);
        String queryName = name.substring(0, name.lastIndexOf("Condition"));

            try {
                String searchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
                        + org.springframework.util.ClassUtils.convertClassNameToResourcePath(classpath) + "/"
                        + name.substring(0, name.lastIndexOf("Condition")) + ".hbm.xml";
                Configuration cfg = new Configuration();
                org.springframework.core.io.support.ResourcePatternResolver resourcePatternResolver = new org.springframework.core.io.support.PathMatchingResourcePatternResolver();
                org.springframework.core.io.Resource[] resources = resourcePatternResolver.getResources(searchPath);
                cfg.addInputStream(resources[0].getInputStream());
                cfg.buildMappings();
                Object obj = cfg.getNamedSQLQueries().get(queryName);
                nsq = (NamedSQLQueryDefinition) obj;
                String queryStr = nsq.getQuery();

                queryStringMap.put(queryName, queryStr);

                return queryStr;
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
        } 

Advertisement
Javadoc
Strategy interface for resolving a location pattern (for example, an Ant-style path pattern) into Re

source objects.

This is an extension to the org.springframework.core.io.ResourceLoader interface. A passed-in ResourceLoader (for example, an org.springframework.context.ApplicationContext passed in via org.springframework.context.ResourceLoaderAware when running in a context) can be checked whether it implements this extended interface too.

PathMatchingResourcePatternResolver is a standalone implementation that is usable outside an ApplicationContext, also used by ResourceArrayPropertyEditor for populating Resource array bean properties.

Can be used with any sort of location pattern (e.g. "/WEB-INF -context.xml"): Input patterns have to match the strategy implementation. This interface just specifies the conversion method rather than a specific pattern format.

This interface also suggests a new resource prefix "classpath :" for all matching resources from the class path. Note that the resource location is expected to be a path without placeholders in this case (e.g. "/beans.xml"); JAR files or classes directories can contain multiple files of the same name. @author Juergen Hoeller @since 1.0.2 @see org.springframework.core.io.Resource @see org.springframework.core.io.ResourceLoader @see org.springframework.context.ApplicationContext @see org.springframework.context.ResourceLoaderAware

Read More
Advertisement