Package org.jostraca.directive

Source Code of org.jostraca.directive.IncludeDirectiveSupport

package org.jostraca.directive;

import java.util.*;

import org.jostraca.Template;
import org.jostraca.TemplateActionHandler;
import org.jostraca.TemplateException;
import org.jostraca.transform.InsertSectionTransform;
import org.jostraca.transform.TextualTransformManagerTable;
import org.jostraca.unit.BasicUnit;
import org.jostraca.unit.BasicUnitList;
import org.jostraca.unit.BasicUnitOrigin;
import org.jostraca.unit.BasicUnitProcessor;
import org.jostraca.util.PropertySet;
import org.jostraca.util.RegExp;
import org.jostraca.util.RegExpMatch;

public abstract class IncludeDirectiveSupport extends DirectiveSupport {

  public final static String NAME = "include";

  public static final String INCLUDE_MOD_if_exists         = "if-exists";
  public static final String INCLUDE_MOD_template_relative = "template-relative";
  public static final String INCLUDE_MOD_output_relative   = "output-relative";

  public static final String REGEXP_IncludeArgs  = "\\s*(\".+?\"|[^ ]+)\\s*([a-z-\\s]*)";
 
  public static final int MAX_INCLUDE_COUNT = 111;
 
   
  protected static RegExp sIncludeArgsRegExp          = null;
 
 
  static {
    createRegExps();
  }
 
 
  public String perform(String pDirectiveSupportName, String pArguments, BasicUnitList pUnitList, TemplateActionHandler pTemplateActionHandler, PropertySet pPropertySet, TextualTransformManagerTable pTextualTransformManagerTable, Template pTemplate ) throws DirectiveException {
   
    ArrayList args = new ArrayList();
    String path = parseArguments( args, pArguments );
 
    HashMap includeFileMap = (HashMap) pTemplate.getAttribute( getClass().getName()+":includeFileMap");
    if( null == includeFileMap ) {
      includeFileMap = new HashMap();
      pTemplate.setAttribute( getClass().getName()+":includeFileMap", includeFileMap );
    }
   
    Integer count = (Integer) includeFileMap.get( path );
    if( null == count ) {
      count = new Integer(0);
      includeFileMap.put(path, count);
    }
    if( MAX_INCLUDE_COUNT < count.intValue() ) {
      throw new TemplateException( TemplateException.CODE_infinite_recursion, path+" included "+count.intValue()+" times, max allowed: "+MAX_INCLUDE_COUNT );     
    }
   
    String incsrc = loadIncludeSource(path, args, pTemplate);
   
    return incsrc;
  }

 
  public String getName() {
    return NAME;
  }

 
  protected String parseArguments( List pArgs, String pArguments ) {
    RegExpMatch match = sIncludeArgsRegExp.matchFirst( pArguments );
    String path = match.matchFirstSub();

    String arg = match.matchSecondSub();
    String[] args = arg.split("\\s");
    pArgs.addAll(Arrays.asList(args));
   
    return path;
  }
 
 
  /** Create regexps used to implement preprocessing directives */
  private static void createRegExps() {
    String regexpFailed = "RegExp invalid for ";

    try {
      sIncludeArgsRegExp = RegExp.make( REGEXP_IncludeArgs, RegExp.ModeSet.DotMatchesNewline );
    }
    catch( Exception e ) {
      throw new TemplateException( TemplateException.CODE_re_include, e );
    }

  }

 
  protected abstract String loadIncludeSource( String pPath, List pArgs, Template pTemplate ) throws DirectiveException;
}
TOP

Related Classes of org.jostraca.directive.IncludeDirectiveSupport

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.