📄 runtimeinstance.java
字号:
}
/*
* now, if we have a parser
*/
if (parser != null)
{
try
{
/*
* dump namespace if we are told to. Generally, you want to
* do this - you don't in special circumstances, such as
* when a VM is getting init()-ed & parsed
*/
if ( dumpNamespace )
{
dumpVMNamespace( templateName );
}
ast = parser.parse( reader, templateName );
}
finally
{
/*
* if this came from the pool, then put back
*/
if (!madeNew)
{
parserPool.put(parser);
}
}
}
else
{
error("Runtime : ran out of parsers and unable to create more.");
}
return ast;
}
/**
* Returns a <code>Template</code> from the resource manager.
* This method assumes that the character encoding of the
* template is set by the <code>input.encoding</code>
* property. The default is "ISO-8859-1"
*
* @param name The file name of the desired template.
* @return The template.
* @throws ResourceNotFoundException if template not found
* from any available source.
* @throws ParseErrorException if template cannot be parsed due
* to syntax (or other) error.
* @throws Exception if an error occurs in template initialization
*/
public Template getTemplate(String name)
throws ResourceNotFoundException, ParseErrorException, Exception
{
return getTemplate( name, getString( INPUT_ENCODING, ENCODING_DEFAULT) );
}
/**
* Returns a <code>Template</code> from the resource manager
*
* @param name The name of the desired template.
* @param encoding Character encoding of the template
* @return The template.
* @throws ResourceNotFoundException if template not found
* from any available source.
* @throws ParseErrorException if template cannot be parsed due
* to syntax (or other) error.
* @throws Exception if an error occurs in template initialization
*/
public Template getTemplate(String name, String encoding)
throws ResourceNotFoundException, ParseErrorException, Exception
{
return (Template) resourceManager.getResource(name, ResourceManager.RESOURCE_TEMPLATE, encoding);
}
/**
* Returns a static content resource from the
* resource manager. Uses the current value
* if INPUT_ENCODING as the character encoding.
*
* @param name Name of content resource to get
* @return parsed ContentResource object ready for use
* @throws ResourceNotFoundException if template not found
* from any available source.
*/
public ContentResource getContent(String name)
throws ResourceNotFoundException, ParseErrorException, Exception
{
/*
* the encoding is irrelvant as we don't do any converstion
* the bytestream should be dumped to the output stream
*/
return getContent( name, getString( INPUT_ENCODING, ENCODING_DEFAULT));
}
/**
* Returns a static content resource from the
* resource manager.
*
* @param name Name of content resource to get
* @param encoding Character encoding to use
* @return parsed ContentResource object ready for use
* @throws ResourceNotFoundException if template not found
* from any available source.
*/
public ContentResource getContent( String name, String encoding )
throws ResourceNotFoundException, ParseErrorException, Exception
{
return (ContentResource) resourceManager.getResource(name,ResourceManager.RESOURCE_CONTENT, encoding );
}
/**
* Determines is a template exists, and returns name of the loader that
* provides it. This is a slightly less hokey way to support
* the Velocity.templateExists() utility method, which was broken
* when per-template encoding was introduced. We can revisit this.
*
* @param resourceName Name of template or content resource
* @return class name of loader than can provide it
*/
public String getLoaderNameForResource( String resourceName )
{
return resourceManager.getLoaderNameForResource( resourceName );
}
/**
* Added this to check and make sure that the configuration
* is initialized before trying to get properties from it.
* This occurs when there are errors during initialization
* and the default properties have yet to be layed down.
*/
private boolean showStackTrace()
{
if (configuration.isInitialized())
{
return getBoolean(RUNTIME_LOG_WARN_STACKTRACE, false);
}
else
{
return false;
}
}
/**
* Handle logging.
*
* @param String message to log
*/
private void log(int level, Object message)
{
String out;
/*
* now, see if the logging stacktrace is on
* and modify the message to suit
*/
if ( showStackTrace() &&
(message instanceof Throwable || message instanceof Exception) )
{
out = StringUtils.stackTrace((Throwable)message);
}
else
{
out = message.toString();
}
/*
* just log it, as we are guaranteed now to have some
* kind of logger - save the if()
*/
logSystem.logVelocityMessage( level, out);
}
/**
* Log a warning message.
*
* @param Object message to log
*/
public void warn(Object message)
{
log(LogSystem.WARN_ID, message);
}
/**
* Log an info message.
*
* @param Object message to log
*/
public void info(Object message)
{
log(LogSystem.INFO_ID, message);
}
/**
* Log an error message.
*
* @param Object message to log
*/
public void error(Object message)
{
log(LogSystem.ERROR_ID, message);
}
/**
* Log a debug message.
*
* @param Object message to log
*/
public void debug(Object message)
{
log(LogSystem.DEBUG_ID, message);
}
/**
* String property accessor method with default to hide the
* configuration implementation.
*
* @param String key property key
* @param String defaultValue default value to return if key not
* found in resource manager.
* @return String value of key or default
*/
public String getString( String key, String defaultValue)
{
return configuration.getString(key, defaultValue);
}
/**
* Returns the appropriate VelocimacroProxy object if strVMname
* is a valid current Velocimacro.
*
* @param String vmName Name of velocimacro requested
* @return String VelocimacroProxy
*/
public Directive getVelocimacro( String vmName, String templateName )
{
return vmFactory.getVelocimacro( vmName, templateName );
}
/**
* Adds a new Velocimacro. Usually called by Macro only while parsing.
*
* @param String name Name of velocimacro
* @param String macro String form of macro body
* @param String argArray Array of strings, containing the
* #macro() arguments. the 0th is the name.
* @return boolean True if added, false if rejected for some
* reason (either parameters or permission settings)
*/
public boolean addVelocimacro( String name,
String macro,
String argArray[],
String sourceTemplate )
{
return vmFactory.addVelocimacro( name, macro, argArray, sourceTemplate );
}
/**
* Checks to see if a VM exists
*
* @param name Name of velocimacro
* @return boolean True if VM by that name exists, false if not
*/
public boolean isVelocimacro( String vmName, String templateName )
{
return vmFactory.isVelocimacro( vmName, templateName );
}
/**
* tells the vmFactory to dump the specified namespace. This is to support
* clearing the VM list when in inline-VM-local-scope mode
*/
public boolean dumpVMNamespace( String namespace )
{
return vmFactory.dumpVMNamespace( namespace );
}
/* --------------------------------------------------------------------
* R U N T I M E A C C E S S O R M E T H O D S
* --------------------------------------------------------------------
* These are the getXXX() methods that are a simple wrapper
* around the configuration object. This is an attempt
* to make a the Velocity Runtime the single access point
* for all things Velocity, and allow the Runtime to
* adhere as closely as possible the the Mediator pattern
* which is the ultimate goal.
* --------------------------------------------------------------------
*/
/**
* String property accessor method to hide the configuration implementation
* @param key property key
* @return value of key or null
*/
public String getString(String key)
{
return configuration.getString( key );
}
/**
* Int property accessor method to hide the configuration implementation.
*
* @param String key property key
* @return int value
*/
public int getInt( String key )
{
return configuration.getInt( key );
}
/**
* Int property accessor method to hide the configuration implementation.
*
* @param key property key
* @param int default value
* @return int value
*/
public int getInt( String key, int defaultValue )
{
return configuration.getInt( key, defaultValue );
}
/**
* Boolean property accessor method to hide the configuration implementation.
*
* @param String key property key
* @param boolean default default value if property not found
* @return boolean value of key or default value
*/
public boolean getBoolean( String key, boolean def )
{
return configuration.getBoolean( key, def );
}
/**
* Return the velocity runtime configuration object.
*
* @return ExtendedProperties configuration object which houses
* the velocity runtime properties.
*/
public ExtendedProperties getConfiguration()
{
return configuration;
}
/**
* Return the Introspector for this instance
*/
public Introspector getIntrospector()
{
return introspector;
}
public Object getApplicationAttribute( Object key)
{
return applicationAttributes.get( key );
}
public Object setApplicationAttribute( Object key, Object o )
{
return applicationAttributes.put( key, o );
}
public Uberspect getUberspect()
{
return uberSpect;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -