Author: cbrisson Date: Sat Jan 28 16:49:32 2017 New Revision: 1780721 URL: http://svn.apache.org/viewvc?rev=1780721&view=rev Log: [engine] more slimming for o.a.v.utils.StringUtils
Modified: velocity/engine/trunk/velocity-engine-core/pom.xml velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/app/event/implement/EscapeReference.java velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/app/event/implement/IncludeNotFound.java velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeInstance.java velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/resource/ResourceManagerImpl.java velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/resource/loader/DataSourceResourceLoader.java velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/resource/loader/FileResourceLoader.java velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/resource/loader/JarResourceLoader.java velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/util/StringUtils.java velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/MiscTestCase.java Modified: velocity/engine/trunk/velocity-engine-core/pom.xml URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/pom.xml?rev=1780721&r1=1780720&r2=1780721&view=diff ============================================================================== --- velocity/engine/trunk/velocity-engine-core/pom.xml (original) +++ velocity/engine/trunk/velocity-engine-core/pom.xml Sat Jan 28 16:49:32 2017 @@ -52,6 +52,37 @@ </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-shade-plugin</artifactId> + <version>1.4</version> + <executions> + <execution> + <id>shade</id> + <phase>package</phase> + <goals> + <goal>shade</goal> + </goals> + <configuration> + <artifactSet> + <includes> + <include>commons-io:commons-io</include> + </includes> + <excludes> + <exclude>org.slf4j:slf4j-api</exclude> + </excludes> + </artifactSet> + <relocations> + <relocation> + <pattern>org.apache.commons.io</pattern> + <shadedPattern>org.apache.velocity.shaded.commons.io</shadedPattern> + </relocation> + </relocations> + <minimizeJar>true</minimizeJar> + </configuration> + </execution> + </executions> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>${surefire.plugin.version}</version> <configuration> @@ -147,7 +178,6 @@ <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.5</version> - <scope>test</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> Modified: velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/app/event/implement/EscapeReference.java URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/app/event/implement/EscapeReference.java?rev=1780721&r1=1780720&r2=1780721&view=diff ============================================================================== --- velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/app/event/implement/EscapeReference.java (original) +++ velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/app/event/implement/EscapeReference.java Sat Jan 28 16:49:32 2017 @@ -23,7 +23,8 @@ import org.apache.velocity.app.event.Ref import org.apache.velocity.context.Context; import org.apache.velocity.runtime.RuntimeServices; import org.apache.velocity.util.RuntimeServicesAware; -import org.apache.velocity.util.StringUtils; + +import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; @@ -123,7 +124,7 @@ public abstract class EscapeReference im log = rs.getLog("event"); // Get the regular expression pattern. - matchRegExp = StringUtils.nullTrim(rs.getString(getMatchAttribute())); + matchRegExp = StringUtils.trim(rs.getString(getMatchAttribute())); if (org.apache.commons.lang3.StringUtils.isEmpty(matchRegExp)) { matchRegExp = null; Modified: velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/app/event/implement/IncludeNotFound.java URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/app/event/implement/IncludeNotFound.java?rev=1780721&r1=1780720&r2=1780721&view=diff ============================================================================== --- velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/app/event/implement/IncludeNotFound.java (original) +++ velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/app/event/implement/IncludeNotFound.java Sat Jan 28 16:49:32 2017 @@ -24,7 +24,8 @@ import org.apache.velocity.context.Conte import org.apache.velocity.runtime.RuntimeServices; import org.apache.velocity.util.ContextAware; import org.apache.velocity.util.RuntimeServicesAware; -import org.apache.velocity.util.StringUtils; + +import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; @@ -110,7 +111,7 @@ public class IncludeNotFound implements { this.rs = rs; log = rs.getLog("event"); - notfound = StringUtils.nullTrim(rs.getString(PROPERTY_NOT_FOUND, DEFAULT_NOT_FOUND)); + notfound = StringUtils.trim(rs.getString(PROPERTY_NOT_FOUND, DEFAULT_NOT_FOUND)); } /** Modified: velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeInstance.java URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeInstance.java?rev=1780721&r1=1780720&r2=1780721&view=diff ============================================================================== --- velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeInstance.java (original) +++ velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeInstance.java Sat Jan 28 16:49:32 2017 @@ -46,10 +46,12 @@ import org.apache.velocity.runtime.resou import org.apache.velocity.util.ClassUtils; import org.apache.velocity.util.ExtProperties; import org.apache.velocity.util.RuntimeServicesAware; -import org.apache.velocity.util.StringUtils; import org.apache.velocity.util.introspection.ChainableUberspector; import org.apache.velocity.util.introspection.LinkingUberspector; import org.apache.velocity.util.introspection.Uberspect; + +import org.apache.commons.lang3.StringUtils; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -636,7 +638,7 @@ public class RuntimeInstance implements } if (o instanceof String) { - return StringUtils.nullTrim((String) o); + return StringUtils.trim((String) o); } else { @@ -1730,7 +1732,7 @@ public class RuntimeInstance implements */ public String getString(String key) { - return StringUtils.nullTrim(configuration.getString(key)); + return StringUtils.trim(configuration.getString(key)); } /** Modified: velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/resource/ResourceManagerImpl.java URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/resource/ResourceManagerImpl.java?rev=1780721&r1=1780720&r2=1780721&view=diff ============================================================================== --- velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/resource/ResourceManagerImpl.java (original) +++ velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/resource/ResourceManagerImpl.java Sat Jan 28 16:49:32 2017 @@ -28,12 +28,15 @@ import org.apache.velocity.runtime.resou import org.apache.velocity.runtime.resource.loader.ResourceLoaderFactory; import org.apache.velocity.util.ClassUtils; import org.apache.velocity.util.ExtProperties; -import org.apache.velocity.util.StringUtils; + +import org.apache.commons.lang3.StringUtils; + import org.slf4j.Logger; import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import java.util.ListIterator; import java.util.Vector; @@ -115,7 +118,7 @@ public class ResourceManagerImpl */ ExtProperties configuration = (ExtProperties) it.next(); - String loaderClass = StringUtils.nullTrim(configuration.getString("class")); + String loaderClass = StringUtils.trim(configuration.getString("class")); ResourceLoader loaderInstance = (ResourceLoader) configuration.get("instance"); if (loaderInstance != null) @@ -155,7 +158,7 @@ public class ResourceManagerImpl Object cacheObject = null; - if (org.apache.commons.lang3.StringUtils.isNotEmpty(cacheClassName)) + if (StringUtils.isNotEmpty(cacheClassName)) { try { @@ -210,11 +213,9 @@ public class ResourceManagerImpl private void assembleResourceLoaderInitializers() { Vector resourceLoaderNames = rsvc.getConfiguration().getVector(RuntimeConstants.RESOURCE_LOADER); - StringUtils.trimStrings(resourceLoaderNames); - for (Iterator it = resourceLoaderNames.iterator(); it.hasNext(); ) + for (ListIterator<String> it = resourceLoaderNames.listIterator(); it.hasNext(); ) { - /* * The loader id might look something like the following: * @@ -223,7 +224,8 @@ public class ResourceManagerImpl * The loader id is the prefix used for all properties * pertaining to a particular loader. */ - String loaderName = (String) it.next(); + String loaderName = StringUtils.trim(it.next()); + it.set(loaderName); StringBuilder loaderID = new StringBuilder(loaderName); loaderID.append(".").append(RuntimeConstants.RESOURCE_LOADER); @@ -529,7 +531,7 @@ public class ResourceManagerImpl * this strikes me as bad... */ - if (!org.apache.commons.lang3.StringUtils.equals(resource.getEncoding(), encoding)) + if (!StringUtils.equals(resource.getEncoding(), encoding)) { log.warn("Declared encoding for template '{}' is different on reload. Old = '{}' New = '{}'", resource.getName(), resource.getEncoding(), encoding); Modified: velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/resource/loader/DataSourceResourceLoader.java URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/resource/loader/DataSourceResourceLoader.java?rev=1780721&r1=1780720&r2=1780721&view=diff ============================================================================== --- velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/resource/loader/DataSourceResourceLoader.java (original) +++ velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/resource/loader/DataSourceResourceLoader.java Sat Jan 28 16:49:32 2017 @@ -23,7 +23,8 @@ import org.apache.velocity.exception.Res import org.apache.velocity.exception.VelocityException; import org.apache.velocity.runtime.resource.Resource; import org.apache.velocity.util.ExtProperties; -import org.apache.velocity.util.StringUtils; + +import org.apache.commons.lang3.StringUtils; import javax.naming.InitialContext; import javax.naming.NamingException; @@ -141,11 +142,11 @@ public class DataSourceResourceLoader ex */ public void init(ExtProperties configuration) { - dataSourceName = StringUtils.nullTrim(configuration.getString("resource.datasource")); - tableName = StringUtils.nullTrim(configuration.getString("resource.table")); - keyColumn = StringUtils.nullTrim(configuration.getString("resource.keycolumn")); - templateColumn = StringUtils.nullTrim(configuration.getString("resource.templatecolumn")); - timestampColumn = StringUtils.nullTrim(configuration.getString("resource.timestampcolumn")); + dataSourceName = StringUtils.trim(configuration.getString("resource.datasource")); + tableName = StringUtils.trim(configuration.getString("resource.table")); + keyColumn = StringUtils.trim(configuration.getString("resource.keycolumn")); + templateColumn = StringUtils.trim(configuration.getString("resource.templatecolumn")); + timestampColumn = StringUtils.trim(configuration.getString("resource.timestampcolumn")); if (dataSource != null) { @@ -209,7 +210,7 @@ public class DataSourceResourceLoader ex public synchronized Reader getResourceReader(final String name, String encoding) throws ResourceNotFoundException { - if (org.apache.commons.lang3.StringUtils.isEmpty(name)) + if (StringUtils.isEmpty(name)) { throw new ResourceNotFoundException("DataSourceResourceLoader: Template name was empty or null"); } Modified: velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/resource/loader/FileResourceLoader.java URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/resource/loader/FileResourceLoader.java?rev=1780721&r1=1780720&r2=1780721&view=diff ============================================================================== --- velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/resource/loader/FileResourceLoader.java (original) +++ velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/resource/loader/FileResourceLoader.java Sat Jan 28 16:49:32 2017 @@ -23,7 +23,9 @@ import org.apache.velocity.exception.Res import org.apache.velocity.exception.VelocityException; import org.apache.velocity.runtime.resource.Resource; import org.apache.velocity.util.ExtProperties; -import org.apache.velocity.util.StringUtils; + +import org.apache.commons.io.FilenameUtils; +import org.apache.commons.lang3.StringUtils; import java.io.File; import java.io.FileInputStream; @@ -35,6 +37,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; +import java.util.ListIterator; import java.util.Map; /** @@ -71,21 +74,14 @@ public class FileResourceLoader extends paths.addAll( configuration.getVector("path") ); - // unicode files may have a BOM marker at the start, - - if (log.isDebugEnabled()) + // trim spaces from all paths + for (ListIterator<String> it = paths.listIterator(); it.hasNext(); ) { - // trim spaces from all paths - StringUtils.trimStrings(paths); - - // this section lets tell people what paths we will be using - int sz = paths.size(); - for( int i=0; i < sz; i++) - { - log.debug("FileResourceLoader : adding path '{}'", (String)paths.get(i)); - } - log.trace("FileResourceLoader : initialization complete."); + String path = StringUtils.trim(it.next()); + it.set(path); + log.debug("FileResourceLoader : adding path '{}'", path); } + log.trace("FileResourceLoader : initialization complete."); } /** @@ -115,7 +111,7 @@ public class FileResourceLoader extends "Need to specify a file name or file path!"); } - String template = StringUtils.normalizePath(templateName); + String template = FilenameUtils.normalize( templateName, true ); if ( template == null || template.length() == 0 ) { String msg = "File resource error : argument " + template + @@ -179,7 +175,7 @@ public class FileResourceLoader extends { return false; } - name = StringUtils.normalizePath(name); + name = FilenameUtils.normalize(name); if (name == null || name.length() == 0) { return false; Modified: velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/resource/loader/JarResourceLoader.java URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/resource/loader/JarResourceLoader.java?rev=1780721&r1=1780720&r2=1780721&view=diff ============================================================================== --- velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/resource/loader/JarResourceLoader.java (original) +++ velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/resource/loader/JarResourceLoader.java Sat Jan 28 16:49:32 2017 @@ -23,15 +23,18 @@ import org.apache.velocity.exception.Res import org.apache.velocity.exception.VelocityException; import org.apache.velocity.runtime.resource.Resource; import org.apache.velocity.util.ExtProperties; -import org.apache.velocity.util.StringUtils; + +import org.apache.commons.io.FilenameUtils; +import org.apache.commons.lang3.StringUtils; import java.io.IOException; import java.io.InputStream; import java.io.Reader; import java.util.HashMap; import java.util.Hashtable; +import java.util.List; +import java.util.ListIterator; import java.util.Map; -import java.util.Vector; /** * <p> @@ -88,19 +91,17 @@ public class JarResourceLoader extends R { log.trace("JarResourceLoader : initialization starting."); - // rest of Velocity engine still use legacy Vector - // and Hashtable classes. Classes are implicitly - // synchronized even if we don't need it. - Vector paths = configuration.getVector("path"); - StringUtils.trimStrings(paths); + List paths = configuration.getList("path"); if (paths != null) { log.debug("JarResourceLoader # of paths : {}", paths.size() ); - for ( int i=0; i<paths.size(); i++ ) + for (ListIterator<String> it = paths.listIterator(); it.hasNext(); ) { - loadJar( (String)paths.get(i) ); + String jar = StringUtils.trim(it.next()); + it.set(jar); + loadJar(jar); } } @@ -184,7 +185,7 @@ public class JarResourceLoader extends R throw new ResourceNotFoundException("Need to have a resource!"); } - String normalizedPath = StringUtils.normalizePath( source ); + String normalizedPath = FilenameUtils.normalize( source, true ); if ( normalizedPath == null || normalizedPath.length() == 0 ) { Modified: velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/util/StringUtils.java URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/util/StringUtils.java?rev=1780721&r1=1780720&r2=1780721&view=diff ============================================================================== --- velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/util/StringUtils.java (original) +++ velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/util/StringUtils.java Sat Jan 28 16:49:32 2017 @@ -37,117 +37,6 @@ import java.util.List; public class StringUtils { /** - * Line separator for the OS we are operating on. - */ - private static final String EOL = System.getProperty("line.separator"); - - /** - * Return a context-relative path, beginning with a "/", that represents - * the canonical version of the specified path after ".." and "." elements - * are resolved out. If the specified path attempts to go outside the - * boundaries of the current context (i.e. too many ".." path elements - * are present), return <code>null</code> instead. - * - * @param path Path to be normalized - * @return String normalized path - */ - public static final String normalizePath(String path) - { - // Normalize the slashes and add leading slash if necessary - String normalized = path; - if (normalized.indexOf('\\') >= 0) - { - normalized = normalized.replace('\\', '/'); - } - - if (!normalized.startsWith("/")) - { - normalized = "/" + normalized; - } - - // Resolve occurrences of "//" in the normalized path - while (true) - { - int index = normalized.indexOf("//"); - if (index < 0) - break; - normalized = normalized.substring(0, index) + - normalized.substring(index + 1); - } - - // Resolve occurrences of "%20" in the normalized path - while (true) - { - int index = normalized.indexOf("%20"); - if (index < 0) - break; - normalized = normalized.substring(0, index) + " " + - normalized.substring(index + 3); - } - - // Resolve occurrences of "/./" in the normalized path - while (true) - { - int index = normalized.indexOf("/./"); - if (index < 0) - break; - normalized = normalized.substring(0, index) + - normalized.substring(index + 2); - } - - // Resolve occurrences of "/../" in the normalized path - while (true) - { - int index = normalized.indexOf("/../"); - if (index < 0) - break; - if (index == 0) - return (null); // Trying to go outside our context - int index2 = normalized.lastIndexOf('/', index - 1); - normalized = normalized.substring(0, index2) + - normalized.substring(index + 3); - } - - // Return the normalized path that we have completed - return (normalized); - } - - /** - * Trim all strings in a List. Changes the strings in the existing list. - * @param list - * @return List of trimmed strings. - * @since 1.5 - */ - public static List trimStrings(List list) - { - if (list == null) - return null; - - int sz = list.size(); - for (int i = 0; i < sz; i++) - list.set(i,nullTrim((String) list.get(i))); - return list; - } - - /** - * Trim the string, but pass a null through. - * @param s - * @return List of trimmed Strings. - * @since 1.5 - */ - public static String nullTrim(String s) - { - if (s == null) - { - return null; - } - else - { - return s.trim(); - } - } - - /** * Creates a string that formats the template filename with line number * and column of the given Directive. We use this routine to provide a cosistent format for displaying * file errors. Modified: velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/MiscTestCase.java URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/MiscTestCase.java?rev=1780721&r1=1780720&r2=1780721&view=diff ============================================================================== --- velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/MiscTestCase.java (original) +++ velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/MiscTestCase.java Sat Jan 28 16:49:32 2017 @@ -22,7 +22,8 @@ package org.apache.velocity.test; import junit.framework.Test; import junit.framework.TestSuite; import org.apache.velocity.runtime.RuntimeInstance; -import org.apache.velocity.util.StringUtils; + +import org.apache.commons.lang3.StringUtils; import java.util.ArrayList; import java.util.List; @@ -61,29 +62,16 @@ public class MiscTestCase extends BaseTe */ String arg = null; - String res = StringUtils.nullTrim(arg); + String res = StringUtils.trim(arg); assertNull(arg); arg = " test "; - res = StringUtils.nullTrim(arg); + res = StringUtils.trim(arg); assertEquals("test",res); arg = "test"; - res = StringUtils.nullTrim(arg); + res = StringUtils.trim(arg); assertEquals("test",res); - - List list = null; - assertNull(StringUtils.trimStrings(list)); - - list = new ArrayList(); - assertEquals(new ArrayList(),StringUtils.trimStrings(list)); - - list.add("test"); - list.add(" abc"); - StringUtils.trimStrings(list); - assertEquals("test",list.get(0)); - assertEquals("abc",list.get(1)); - } }