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

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

Introduction

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

Prototype

public EncodedResource(Resource resource) 

Source Link

Document

Create a new EncodedResource for the given Resource , not specifying an explicit encoding or Charset .

Usage

From source file:com.porvak.bracket.social.jdbc.versioned.SqlDatabaseChange.java

private static String readScript(Resource resource) throws IOException {
    EncodedResource encoded = resource instanceof EncodedResource ? (EncodedResource) resource
            : new EncodedResource(resource);
    LineNumberReader lnr = new LineNumberReader(encoded.getReader());
    String currentStatement = lnr.readLine();
    StringBuilder scriptBuilder = new StringBuilder();
    while (currentStatement != null) {
        if (StringUtils.hasText(currentStatement)
                && (SQL_COMMENT_PREFIX != null && !currentStatement.startsWith(SQL_COMMENT_PREFIX))) {
            if (scriptBuilder.length() > 0) {
                scriptBuilder.append('\n');
            }/*from  w ww .  ja v a2s .  c  o  m*/
            scriptBuilder.append(currentStatement);
        }
        currentStatement = lnr.readLine();
    }
    return scriptBuilder.toString();
}

From source file:com.github.javarch.persistence.orm.test.DataBaseTestBuilder.java

private String readNextSqlScript(Resource rs) throws IOException {
    EncodedResource encoded = new EncodedResource(rs);
    LineNumberReader lnr = new LineNumberReader(encoded.getReader());
    String currentStatement = lnr.readLine();
    StringBuilder scriptBuilder = new StringBuilder();
    while (currentStatement != null) {
        if (StringUtils.hasText(currentStatement)
                && (SQL_COMMENT_PREFIX != null && !currentStatement.startsWith(SQL_COMMENT_PREFIX))) {
            if (scriptBuilder.length() > 0) {
                scriptBuilder.append('\n');
            }//from   w  w w  .j av  a2s  .c  om
            scriptBuilder.append(currentStatement);
        }
        currentStatement = lnr.readLine();
    }
    return scriptBuilder.toString();
}

From source file:de.codesourcery.spring.contextrewrite.ContextRewritingBootStrapper.java

@Override
public void setBootstrapContext(final BootstrapContext ctx) {
    final RewriteConfig config = new AnnotationParser().parse(ctx.getTestClass());

    final XMLRewrite rewrite = new XMLRewrite();

    super.setBootstrapContext(new BootstrapContext() {

        @Override//from   ww w  .  j a  v  a  2  s .c  om
        public Class<?> getTestClass() {
            return ctx.getTestClass();
        }

        @Override
        public CacheAwareContextLoaderDelegate getCacheAwareContextLoaderDelegate() {
            return new CacheAwareContextLoaderDelegate() {

                @Override
                public ApplicationContext loadContext(MergedContextConfiguration mergedContextConfiguration) {
                    final MergedContextConfiguration wrapper = new MergedContextConfiguration(
                            mergedContextConfiguration) {
                        @Override
                        public ContextLoader getContextLoader() {
                            return new AbstractGenericContextLoader() {

                                @Override
                                protected BeanDefinitionReader createBeanDefinitionReader(
                                        GenericApplicationContext context) {
                                    final XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(
                                            context) {
                                        private int loadBeanDefinitions() {
                                            try {
                                                return super.loadBeanDefinitions(new EncodedResource(
                                                        rewrite.filterResource(config.getResource(), config)));
                                            } catch (Exception e) {
                                                throw new BeanDefinitionStoreException(
                                                        "Failed to load from classpath:"
                                                                + config.getContextPath(),
                                                        e);
                                            }
                                        }

                                        @Override
                                        public int loadBeanDefinitions(String location)
                                                throws BeanDefinitionStoreException {
                                            return loadBeanDefinitions((EncodedResource) null);
                                        }

                                        @Override
                                        public int loadBeanDefinitions(String location,
                                                Set<Resource> actualResources)
                                                throws BeanDefinitionStoreException {
                                            if (StringUtils.isBlank(location)) {
                                                return loadBeanDefinitions();
                                            }
                                            return super.loadBeanDefinitions(location, actualResources);
                                        }

                                        @Override
                                        public int loadBeanDefinitions(String... locations)
                                                throws BeanDefinitionStoreException {
                                            return loadBeanDefinitions();
                                        }

                                        public int loadBeanDefinitions(InputSource inputSource)
                                                throws BeanDefinitionStoreException {
                                            return loadBeanDefinitions();
                                        }

                                        public int loadBeanDefinitions(InputSource inputSource,
                                                String resourceDescription)
                                                throws BeanDefinitionStoreException {
                                            return loadBeanDefinitions();
                                        }
                                    };
                                    return reader;
                                }

                                @Override
                                protected String getResourceSuffix() {
                                    return ".xml";
                                }
                            };
                        }
                    };
                    return ctx.getCacheAwareContextLoaderDelegate().loadContext(wrapper);
                }

                @Override
                public void closeContext(MergedContextConfiguration mergedContextConfiguration,
                        HierarchyMode hierarchyMode) {
                    ctx.getCacheAwareContextLoaderDelegate().closeContext(mergedContextConfiguration,
                            hierarchyMode);
                }
            };
        }
    });
}

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

/**
 * Load query definitions from the specified XML file.
 * @param resource the resource descriptor for the XML file
 * @return the number of query definitions found
 * @throws QueryDefinitionStoreException in case of loading or parsing errors
 *///from   w  ww.  j  av a  2s. c o m
public int loadQueryDefinitions(Resource resource) throws QueryDefinitionStoreException {
    return loadQueryDefinitions(new EncodedResource(resource));
}

From source file:be.jacobsvanroy.springsqlunit.util.ScriptUtils.java

/**
 * Execute the given SQL script using default settings for separator separators,
 * comment delimiters, and exception handling flags.
 * <p>Statement separators and comments will be removed before executing
 * individual statements within the supplied script.
 * <p><b>Do not use this method to execute DDL if you expect rollback.</b>
 *
 * @param dataSource the dataSource to use to execute the script; already
 *                   configured and ready to use
 * @param resource   the resource to load the SQL script from; encoded with the
 *                   current platform's default encoding
 * @see #executeSqlScript(javax.sql.DataSource, EncodedResource, String, String, String, String)
 * @see #DEFAULT_COMMENT_PREFIX//from ww  w  .  j  a va  2  s  .com
 * @see #DEFAULT_STATEMENT_SEPARATOR
 * @see #DEFAULT_BLOCK_COMMENT_START_DELIMITER
 * @see #DEFAULT_BLOCK_COMMENT_END_DELIMITER
 */
public static void executeSqlScript(DataSource dataSource, Resource resource) {
    executeSqlScript(dataSource, new EncodedResource(resource));
}

From source file:org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader.java

/**
 * Load bean definitions from the specified Groovy script or XML file.
 * <p>Note that {@code ".xml"} files will be parsed as XML content; all other kinds
 * of resources will be parsed as Groovy scripts.
 * @param resource the resource descriptor for the Groovy script or XML file
 * @return the number of bean definitions found
 * @throws BeanDefinitionStoreException in case of loading or parsing errors
 *//* ww w .  j a v a 2  s  .  c o m*/
public int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException {
    return loadBeanDefinitions(new EncodedResource(resource));
}

From source file:org.springframework.context.expression.ApplicationContextExpressionTests.java

@Test
public void resourceInjection() throws IOException {
    System.setProperty("logfile", "do_not_delete_me.txt");
    try (AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(
            ResourceInjectionBean.class)) {
        ResourceInjectionBean resourceInjectionBean = ac.getBean(ResourceInjectionBean.class);
        Resource resource = new ClassPathResource("do_not_delete_me.txt");
        assertEquals(resource, resourceInjectionBean.resource);
        assertEquals(resource.getURL(), resourceInjectionBean.url);
        assertEquals(resource.getURI(), resourceInjectionBean.uri);
        assertEquals(resource.getFile(), resourceInjectionBean.file);
        assertArrayEquals(FileCopyUtils.copyToByteArray(resource.getInputStream()),
                FileCopyUtils.copyToByteArray(resourceInjectionBean.inputStream));
        assertEquals(FileCopyUtils.copyToString(new EncodedResource(resource).getReader()),
                FileCopyUtils.copyToString(resourceInjectionBean.reader));
    } finally {//  w w  w  .j a v a  2  s  . c  om
        System.getProperties().remove("logfile");
    }
}

From source file:org.springframework.jdbc.datasource.init.ScriptUtils.java

/**
 * Execute the given SQL script using default settings for statement
 * separators, comment delimiters, and exception handling flags.
 * <p>Statement separators and comments will be removed before executing
 * individual statements within the supplied script.
 * <p><strong>Warning</strong>: this method does <em>not</em> release the
 * provided {@link Connection}./* w  w w . j  a v a 2 s  .  c  o  m*/
 * @param connection the JDBC connection to use to execute the script; already
 * configured and ready to use
 * @param resource the resource to load the SQL script from; encoded with the
 * current platform's default encoding
 * @throws ScriptException if an error occurred while executing the SQL script
 * @see #executeSqlScript(Connection, EncodedResource, boolean, boolean, String, String, String, String)
 * @see #DEFAULT_STATEMENT_SEPARATOR
 * @see #DEFAULT_COMMENT_PREFIX
 * @see #DEFAULT_BLOCK_COMMENT_START_DELIMITER
 * @see #DEFAULT_BLOCK_COMMENT_END_DELIMITER
 * @see org.springframework.jdbc.datasource.DataSourceUtils#getConnection
 * @see org.springframework.jdbc.datasource.DataSourceUtils#releaseConnection
 */
public static void executeSqlScript(Connection connection, Resource resource) throws ScriptException {
    executeSqlScript(connection, new EncodedResource(resource));
}

From source file:org.springframework.test.jdbc.SimpleJdbcTestUtils.java

/**
 * Execute the given SQL script. The script will normally be loaded by classpath.
 * <p>Statements should be delimited with a semicolon.  If statements are not delimited with
 * a semicolon then there should be one statement per line.  Statements are allowed to span
 * lines only if they are delimited with a semicolon.
 * <p><b>Do not use this method to execute DDL if you expect rollback.</b>
 * @param simpleJdbcTemplate the SimpleJdbcTemplate with which to perform JDBC operations
 * @param resource the resource to load the SQL script from.
 * @param continueOnError whether or not to continue without throwing an
 * exception in the event of an error.//  w w w  .  j av  a 2s  .c  om
 * @throws DataAccessException if there is an error executing a statement
 * and continueOnError was {@code false}
 */
public static void executeSqlScript(SimpleJdbcTemplate simpleJdbcTemplate, Resource resource,
        boolean continueOnError) throws DataAccessException {

    executeSqlScript(simpleJdbcTemplate, new EncodedResource(resource), continueOnError);
}