gu.sql2java.velocity.Sql2javaFileResourceLoader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sql2java-velocity Show documentation
Show all versions of sql2java-velocity Show documentation
template generate utilities based velocity
package gu.sql2java.velocity;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import org.apache.commons.collections.ExtendedProperties;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.exception.VelocityException;
import org.apache.velocity.io.UnicodeInputStream;
import org.apache.velocity.runtime.resource.Resource;
import org.apache.velocity.runtime.resource.loader.ResourceLoader;
import org.apache.velocity.util.StringUtils;
import com.google.common.base.Joiner;
/**
* 文件资源加载器
* 基于 {@link org.apache.velocity.runtime.resource.loader.FileResourceLoader}修改.
* 当指定资源名为文件夹时输出文件夹中的文件名列表('\n'分隔)
*
* @author guyadong
*
*/
public class Sql2javaFileResourceLoader extends ResourceLoader {
/**
* The paths to search for templates.
*/
private List paths = new ArrayList<>();
/**
* Used to map the path that a template was found on
* so that we can properly check the modification
* times of the files. This is synchronizedMap
* instance.
*/
private Map templatePaths = Collections.synchronizedMap(new HashMap());
/** Shall we inspect unicode files to see what encoding they contain?. */
private boolean unicode = false;
/**
* @see org.apache.velocity.runtime.resource.loader.ResourceLoader#init(org.apache.commons.collections.ExtendedProperties)
*/
@SuppressWarnings("unchecked")
public void init( ExtendedProperties configuration)
{
if (log.isTraceEnabled())
{
log.trace("Sql2javaFileResourceLoader : initialization starting.");
}
paths.addAll( (Vector)configuration.getVector("path") );
// unicode files may have a BOM marker at the start, but Java
// has problems recognizing the UTF-8 bom. Enabling unicode will
// recognize all unicode boms.
unicode = configuration.getBoolean("unicode", false);
if (log.isDebugEnabled())
{
log.debug("Do unicode file recognition: " + unicode);
}
if (log.isDebugEnabled())
{
// 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("Sql2javaFileResourceLoader : adding path '" + (String) paths.get(i) + "'");
}
log.trace("Sql2javaFileResourceLoader : initialization complete.");
}
}
/**
* Get an InputStream so that the Runtime can build a
* template with it.
*
* @param templateName name of template to get
* @return InputStream containing the template
* @throws ResourceNotFoundException if template not found
* in the file template path.
*/
public InputStream getResourceStream(String templateName)
throws ResourceNotFoundException
{
/*
* Make sure we have a valid templateName.
*/
if (org.apache.commons.lang.StringUtils.isEmpty(templateName))
{
/*
* If we don't get a properly formed templateName then
* there's not much we can do. So we'll forget about
* trying to search any more paths for the template.
*/
throw new ResourceNotFoundException(
"Need to specify a file name or file path!");
}
try {
InputStream inputStream = findTemplate("",templateName);
if(inputStream != null){
templatePaths.put(templateName, "");
return inputStream;
}
}
catch (IOException ioe)
{
String msg = "Exception while loading Resource " + templateName;
log.error(msg, ioe);
throw new VelocityException(msg, ioe);
}
String template = templateName;
int size = paths.size();
for (int i = 0; i < size; i++)
{
String path =paths.get(i);
InputStream inputStream = null;
try
{
inputStream = findTemplate(path, template);
}
catch (IOException ioe)
{
String msg = "Exception while loading Resource " + template;
log.error(msg, ioe);
throw new VelocityException(msg, ioe);
}
if (inputStream != null)
{
/*
* Store the path that this template came
* from so that we can check its modification
* time.
*/
templatePaths.put(templateName, path);
return inputStream;
}
}
/*
* We have now searched all the paths for
* templates and we didn't find anything so
* throw an exception.
*/
throw new ResourceNotFoundException("Sql2javaFileResourceLoader : cannot find " + template);
}
/**
* Overrides superclass for better performance.
* @since 1.6
*/
public boolean resourceExists(String name)
{
if (name == null)
{
return false;
}
name = StringUtils.normalizePath(name);
if (name == null || name.length() == 0)
{
return false;
}
int size = paths.size();
for (int i = 0; i < size; i++)
{
String path = (String)paths.get(i);
try
{
File file = getFile(path, name);
if (file.canRead())
{
return true;
}
}
catch (Exception ioe)
{
String msg = "Exception while checking for template " + name;
log.debug(msg, ioe);
}
}
return false;
}
/**
* Try to find a template given a normalized path.
*
* @param path a normalized path
* @param template name of template to find
* @return InputStream input stream that will be parsed
*
*/
private InputStream findTemplate(final String path, final String template)
throws IOException
{
try
{
File file = getFile(path,template);
InputStream is = null;
if (file.isFile())
{
is = new FileInputStream(file.getAbsolutePath());
}
else if (file.isDirectory())
{
// 资源路径为文件夹时输出文件夹中文件名列表('\n'分隔)
String[] dirEntires = file.list();
// 文件夹名后加'/'区分
for(int i=0; i