/*
* Name: ProcessStage (Interface)
* Authors: Richard Rodger
*
* Copyright (c) 2004 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.process;
// import
import org.jostraca.Template;
import org.jostraca.util.Internal;
import org.jostraca.util.UserMessageHandler;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/** Support class for processing stage classes.
*/
public abstract class ProcessStageSupport implements ProcessStage {
// protected
protected UserMessageHandler iUserMessageHandler = null;
protected ArrayList iTemplateHandlers = null;
protected HashMap iTemplatesForHandler = null;
protected TemplateHandlerManager iTemplateHandlerManager = null;
// public methods
public void setUserMessageHandler( UserMessageHandler pUserMessageHandler ) {
iUserMessageHandler = (UserMessageHandler) Internal.null_arg( pUserMessageHandler) ;
}
public void process( List pTemplateList ) {
List tmlist = (List) Internal.null_arg( pTemplateList );
iTemplateHandlers = new ArrayList();
iTemplatesForHandler = new HashMap();
// process templates
int numT = tmlist.size();
for( int tI = 0; tI < numT; tI++ ) {
Object to = tmlist.get(tI);
if( null == to ) {
ProcessException.CODE_null_tm( "stage", getName(), "index", ""+tI );
}
if( !(to instanceof Template) ) {
ProcessException.CODE_not_tm( "stage", getName(), "index", ""+tI, "object", String.valueOf(to) );
}
Template tm = (Template) to;
process( tm );
}
// complete templates
int numTH = iTemplateHandlers.size();
for( int thI = 0; thI < numTH; thI++ ) {
TemplateHandler th = (TemplateHandler) iTemplateHandlers.get(thI);
ArrayList tmhlist = (ArrayList) iTemplatesForHandler.get(th);
th.complete( tmhlist );
}
}
public abstract String getName();
public String toString() {
return getName();
}
protected void process( Template pTemplate ) {
TemplateHandler th = getHandler( pTemplate );
th.process( pTemplate );
ArrayList tmlist = null;
if( !iTemplatesForHandler.containsKey( th ) ) {
iTemplateHandlers.add( th );
tmlist = new ArrayList();
iTemplatesForHandler.put( th, tmlist );
}
else {
tmlist = (ArrayList) iTemplatesForHandler.get( th );
}
tmlist.add( pTemplate );
}
// protected methods
protected TemplateHandler getHandler( Template pTemplate ) {
TemplateHandlerManager thm = getTemplateHandlerManager();
TemplateHandler th = thm.getTemplateHandler( pTemplate );
th.setUserMessageHandler( iUserMessageHandler );
return th;
}
protected TemplateHandlerManager getTemplateHandlerManager() {
return iTemplateHandlerManager;
}
}