Package org.jostraca.directive

Source Code of org.jostraca.directive.DirectiveManager

/*
* Name:    DirectiveManager
* Author:  Richard Rodger
*
* Copyright (c) 2000-2005 Richard Rodger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/


// package
package org.jostraca.directive;


// import
import org.jostraca.BlockList;
import org.jostraca.Template;
import org.jostraca.TemplateActionHandler;

import org.jostraca.unit.BasicUnitList;

import org.jostraca.transform.TextualTransformManagerTable;

import org.jostraca.util.Standard;
import org.jostraca.util.PropertySet;
import org.jostraca.util.ErrorUtil;
import org.jostraca.util.SimpleObjectManager;
import org.jostraca.util.ValueSet;
import org.jostraca.util.ValueCode;

import java.util.Hashtable;


/** Manages a keyed set of Directive objects.
*  Directives must have unique names and aliases.
*/
public class DirectiveManager extends SimpleObjectManager implements Directive {

  // public instance
  public static final String DEFAULT_JAVA_PACKAGE = "org.jostraca.directive.";




  // private instance
  private Hashtable iDirectiveTable = new Hashtable();




  // public methods

  /** Constructor.
   *  @see org.jostraca.util.SimpleObjectManager
   */
  public DirectiveManager() {
    super( Directive.class, DEFAULT_JAVA_PACKAGE );
  }



  // interface Directive

  public String perform(
      String                       pDirectiveName,
      String                       pArguments,

      BasicUnitList                pUnitList,
      TemplateActionHandler        pTemplateActionHandler,

      PropertySet                  pPropertySet,
      TextualTransformManagerTable pTextualTransformManagerTable,
                      
      Template pTemplate
  )
    throws DirectiveException
  {
    if( ErrorUtil.is_null( pTemplateActionHandler, "pTemplateActionHandler" ) ) {
      return null;
    }
    if( ErrorUtil.is_null( pPropertySet, "pPropertySet" ) ) {
      return null;
    }
    if( ErrorUtil.is_null( pTextualTransformManagerTable, "pTextualTransformManagerTable" ) ) {
      return null;
    }
    if( ErrorUtil.is_null( pTemplate, "pTemplate" ) ) {
      return null;
    }

    // REVIEW: handle this better
    String   refinedDirectiveName  = pDirectiveName;
    String[] directiveNameElements = DirectiveParser.getNameElements( refinedDirectiveName );
    if( null == directiveNameElements || 0 == directiveNameElements.length ) {
      throw new DirectiveException( DirectiveException.CODE_unknown, new ValueSet( ValueCode.NAME, pDirectiveName ) );
    }
    String directiveName = directiveNameElements[0];

    if( ! iDirectiveTable.containsKey( directiveName ) ) {
      throw new DirectiveException( DirectiveException.CODE_unknown, new ValueSet( ValueCode.NAME, pDirectiveName ) );
    }

    return performDirective(
        directiveName,
        refinedDirectiveName,
        pArguments,

        pUnitList,
        pTemplateActionHandler,

        pPropertySet,
        pTextualTransformManagerTable,
                     
        pTemplate
    );
  }


  public String getName() {
    throw new UnsupportedOperationException( ".getName() has no meaning for DirectiveManager" );
  }

  public String[] getAliases() {
    throw new UnsupportedOperationException( ".getAliases() has no meaning for DirectiveManager" );
  }



  // public methods

  public void loadClasses( String pClassList ) throws DirectiveException {
    try {
      super.loadClasses( pClassList );
     
      iDirectiveTable = new Hashtable();
      String    directiveName;
      Directive directive;
      int       numDirectives = super.iObjects.size();
      for( int directiveI = 0; directiveI < numDirectives; directiveI++ ) {
        directive     = (Directive) super.iObjects.elementAt( directiveI );
        directiveName = directive.getName();
        if( iDirectiveTable.containsKey( directiveName ) ) {
          throw new DirectiveException( "Directive "+directiveName+" uses already defined name." );
        }

        String   alias;
        String[] aliases    = directive.getAliases();
        int      numAliases = aliases.length;
        for( int aliasI = 0; aliasI < numAliases; aliasI++ ) {
          alias = aliases[aliasI];
          if( iDirectiveTable.containsKey( alias ) ) {
            throw new DirectiveException( "Directive "+directiveName+" uses already defined alias: "+alias+"." );
          }
          iDirectiveTable.put( alias, directive );
        }
      }
    }
    catch( DirectiveException de ) {
      throw de;
    }
    catch( Exception e ) {
      throw new DirectiveException( e.getMessage() );
    }
  }



  /** Includes name */
  public String toString() {
    return "Directives"+Standard.COLON_OPEN_SQUARE_BRACKET+super.toString()+Standard.CLOSE_SQUARE_BRACKET;
  }




  // private methods

  public String performDirective(
      String                       pDirectiveName,
      String                       pRefinedDirectiveName,
      String                       pArguments,

      BasicUnitList                pUnitList,
      TemplateActionHandler        pTemplateActionHandler,

      PropertySet                  pPropertySet,
      TextualTransformManagerTable pTextualTransformManagerTable,
                               
      Template pTemplate
  )
    throws DirectiveException
  {   
    Directive d = (Directive) iDirectiveTable.get( pDirectiveName );
    String newcontent
    = d.perform(
        pRefinedDirectiveName,
        pArguments,

        pUnitList,
        pTemplateActionHandler,

        pPropertySet,
        pTextualTransformManagerTable,
              
        pTemplate
    );
   
    return newcontent;
  }

}
TOP

Related Classes of org.jostraca.directive.DirectiveManager

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.