/** {@inheritDoc} */
public void renderDocument( Writer writer, RenderingContext renderingContext, SiteRenderingContext siteContext )
throws RendererException, FileNotFoundException, UnsupportedEncodingException
{
SiteRendererSink sink = new SiteRendererSink( renderingContext );
File doc = new File( renderingContext.getBasedir(), renderingContext.getInputName() );
Reader reader = null;
try
{
String resource = doc.getAbsolutePath();
Parser parser = doxia.getParser( renderingContext.getParserId() );
// TODO: DOXIA-111: the filter used here must be checked generally.
if ( renderingContext.getAttribute( "velocity" ) != null )
{
try
{
SiteResourceLoader.setResource( resource );
Context vc = createVelocityContext( sink, siteContext );
StringWriter sw = new StringWriter();
velocity.getEngine().mergeTemplate( resource, siteContext.getInputEncoding(), vc, sw );
reader = new StringReader( sw.toString() );
if ( parser.getType() == Parser.XML_TYPE && siteContext.isValidate() )
{
reader = validate( reader, resource );
}
}
catch ( Exception e )
{
if ( getLogger().isDebugEnabled() )
{
getLogger().error( "Error parsing " + resource + " as a velocity template, using as text.", e );
}
else
{
getLogger().error( "Error parsing " + resource + " as a velocity template, using as text." );
}
}
}
else
{
switch ( parser.getType() )
{
case Parser.XML_TYPE:
reader = ReaderFactory.newXmlReader( doc );
if ( siteContext.isValidate() )
{
reader = validate( reader, resource );
}
break;
case Parser.TXT_TYPE:
case Parser.UNKNOWN_TYPE:
default:
reader = ReaderFactory.newReader( doc, siteContext.getInputEncoding() );
}
}
sink.enableLogging( new PlexusLoggerWrapper( getLogger() ) );
if ( reader == null ) // can happen if velocity throws above
{
throw new RendererException( "Error getting a parser for '" + doc + "'" );
}
doxia.parse( reader, renderingContext.getParserId(), sink );
}
catch ( ParserNotFoundException e )
{
throw new RendererException( "Error getting a parser for '" + doc + "': " + e.getMessage(), e );
}
catch ( ParseException e )
{
throw new RendererException( "Error parsing '"
+ doc + "': line [" + e.getLineNumber() + "] " + e.getMessage(), e );
}
catch ( IOException e )
{
throw new RendererException( "IOException when processing '" + doc + "'", e );
}
finally
{
sink.flush();
sink.close();
IOUtil.close( reader );
}
generateDocument( writer, sink, siteContext );