package org.jostraca.directive;
import java.io.File;
import java.util.*;
import org.jostraca.BasicTemplatePath;
import org.jostraca.Property;
import org.jostraca.Template;
import org.jostraca.TemplateException;
import org.jostraca.TemplatePath;
import org.jostraca.util.FileRef;
import org.jostraca.util.FileRefBuildResource;
import org.jostraca.util.FileUtil;
import org.jostraca.util.PropertySet;
import org.jostraca.util.Standard;
import org.jostraca.util.ValueCode;
public class BasicIncludeDirective extends IncludeDirectiveSupport {
protected String loadIncludeSource(String pPath, List pArguments, Template pTemplate ) throws DirectiveException {
File includeBaseFolder = (File) pTemplate.getAttribute(IncludeBaseDirective.class.getName()+":includeBase");
String includeFilePath
= null == includeBaseFolder ? pPath
: new File( includeBaseFolder, pPath).getAbsolutePath();
try {
TemplatePath tmpath = pTemplate.getTemplatePath();
PropertySet tmps = pTemplate.getMergedPropertySet();
// template relative is the default
String includeBase = tmpath.getTemplateFolder();
if( pArguments.contains(INCLUDE_MOD_output_relative) ) {
includeBase = tmps.get( Property.main_OutputFolder );
}
File includeF = new File( pPath );
if( includeF.isAbsolute() || pPath.startsWith("/") || pPath.startsWith("\\") ) {
includeFilePath = pPath;
}
else {
// REVIEW: possible user info message that include file does not exist when "if-exists" used?
includeFilePath = includeBase + File.separatorChar + pPath;
}
boolean onlyifexists = pArguments.contains( INCLUDE_MOD_if_exists );
String includeContent = Standard.EMPTY;
try {
includeContent = FileUtil.readFile( includeFilePath, ( onlyifexists ? FileUtil.EMPTY_IF_IO_ERROR : FileUtil.FAIL_ON_IO_ERROR ) );
}
// HACK
// try to load from template path
catch( Throwable t ) {
BasicTemplatePath includeRef = new BasicTemplatePath( pPath );
includeRef.resolve( tmps.getList( Property.main_TemplatePath, Standard.COMMA ));
includeFilePath = includeRef.getTemplatePath();
includeContent = FileUtil.readFile( includeFilePath, ( onlyifexists ? FileUtil.EMPTY_IF_IO_ERROR : FileUtil.FAIL_ON_IO_ERROR ) );
}
pTemplate.addFileBuildResource( includeFilePath );
return includeContent;
}
catch( Exception e ) {
throw new TemplateException( TemplateException.CODE_include,
new String[] { ValueCode.FILE, includeFilePath, ValueCode.TEMPLATE, pPath }, e );
}
}
}