if( pRecursionCount > MAX_RECURSION_COUNT_performIncludes ) {
throw new TemplateException( TemplateException.CODE_infinite_recursion, pRecursionCount+" > "+MAX_RECURSION_COUNT_performIncludes );
}
String source = pSource;
RegExpMatch includeMatch = null;
String includeText = null;
String includeFile = null;
String includeModifier = null;
int numIncludes = 0;
int numIncludeBlocks = 0;
// normal includes
String includeFilePath = Standard.EMPTY;
try {
RegExpMatch[] allincludes = iIncludeRegExp.matchAll( source );
numIncludes = allincludes.length;
for( int includeI = 0; includeI < numIncludes; includeI++ ) {
includeMatch = allincludes[ includeI ];
includeText = includeMatch.match();
includeFile = includeMatch.matchFirstSub();
includeFile = ( null == includeFile ? Standard.EMPTY : includeFile );
includeFile
= ( includeFile.startsWith( Standard.QUOTE ) && includeFile.endsWith( Standard.QUOTE ) )
? includeFile.substring( 1, includeFile.length()-1 ) : includeFile;
includeModifier = includeMatch.matchSecondSub();
includeModifier = ( null == includeModifier ? Standard.EMPTY : includeModifier.trim() );
// REVIEW: TemplatePath interaction with Template needs refactoring
// include the file
String includeBase = pIncludeBase;
if( -1 != includeModifier.indexOf( INCLUDE_MOD_template_relative ) ) {
includeBase = iTemplatePath.getTemplateFolder();
}
else if( -1 != includeModifier.indexOf( INCLUDE_MOD_output_relative ) ) {
includeBase = getMergedPropertySet().get( Property.main_OutputFolder );
}
// another HACK
File includeF = new File( includeFile );
if( includeF.isAbsolute() || includeFile.startsWith("/") || includeFile.startsWith("\\") ) {
includeFilePath = includeFile;
}
else {
// REVIEW: possible user info message that include file does not exist when "if-exists" used?
includeFilePath = includeBase + File.separatorChar + includeFile;
}
boolean onlyifexists = -1 != includeModifier.indexOf( 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( includeFile );
//includeRef.resolve( iParameters.getList( Property.main_TemplatePath, Standard.COMMA ));
includeRef.resolve( getMergedPropertySet().getList( Property.main_TemplatePath, Standard.COMMA ));
includeFilePath = includeRef.getTemplatePath();
includeContent = FileUtil.readFile( includeFilePath, ( onlyifexists ? FileUtil.EMPTY_IF_IO_ERROR : FileUtil.FAIL_ON_IO_ERROR ) );
}
int startOfInclude = source.indexOf( includeText );
if( 0 > startOfInclude ) { // paranoia ;)
continue;
}
int lengthOfInclude = includeText.length();
source
= source.substring( 0, startOfInclude )
+ includeContent
+ source.substring( startOfInclude + lengthOfInclude, source.length() )
;
FileRefBuildResource includeBuildResource = new FileRefBuildResource( new FileRef( new File( includeFilePath ) ), iCodeWriterRef );
iBuildResource.add( includeBuildResource );
}
}
catch( IOException ioe ) {
//ioe.printStackTrace();
throw new TemplateException( TemplateException.CODE_include,
new String[] { ValueCode.FILE, includeFilePath, ValueCode.TEMPLATE, iPath }, ioe );
}
catch( Exception e ) {
e.printStackTrace();
throw new TemplateException( TemplateException.CODE_include,
new String[] { ValueCode.FILE, includeFilePath, ValueCode.TEMPLATE, iPath }, e );
}
// block includes
RegExpMatch includeBlockMatch = null;
String includeBlockText = null;
String includeBlockFile = null;
String includeBlockMark = null;
try {
RegExpMatch[] allincludeblocks = iIncludeBlockRegExp.matchAll( source );
numIncludeBlocks = allincludeblocks.length;
for( int includeBlockI = 0; includeBlockI < numIncludeBlocks; includeBlockI++ ) {
includeBlockMatch = allincludeblocks[ includeBlockI ];
includeBlockText = includeBlockMatch.match();
includeBlockFile = includeBlockMatch.matchFirstSub();
includeBlockMark = includeBlockMatch.matchSecondSub();
RegExp markRegExp = RegExp.make( includeBlockMark, RegExp.ModeSet.DotMatchesNewline );
// include the file block
String includeFileContent = FileUtil.readFile( pIncludeBase + File.separatorChar + includeBlockFile );