/*
* Copyright 2001 Sun Microsystems, Inc. All rights reserved.
* PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
*/
package com.sun.portal.rewriter;
import com.sun.portal.rewriter.util.Debug;
import com.sun.portal.rewriter.util.uri.DecoratedURI;
import java.util.ArrayList;
import java.util.List;
/**
* Chain of Responsibility patten to chain the translators with series of
* TranslatorHooks
*/
public class TranslatorChain extends AbstractTranslator
{
private final Translator rootTranslator;
private final List hookChain = new ArrayList();
private boolean smartChaining = false;
public TranslatorChain( final Translator aRootTranslator )
{
this( aRootTranslator, aRootTranslator.getJSFunctionSpec() );
}//constructor
public TranslatorChain( final Translator aRootTranslator,
final JSFunctionSpec aJSFunctionSpec )
{
super( aRootTranslator.getPageSpec(), aJSFunctionSpec );
rootTranslator = aRootTranslator;
}//constructor
public Translator getRootTranslator()
{
return rootTranslator;
}//getRootTranslator()
public void addTranslatorHook( final TranslatorHook aTranslatorHook )
{
if ( aTranslatorHook != null )
{
hookChain.add( aTranslatorHook );
}
}//addTranslatorHook()
public boolean isSmartlyChained()
{
return smartChaining;
}//isSmartlyChained()
public void enableSmartChaining()
{
smartChaining = true;
}//enableSmartChaining()
public final String hook4Translate( final DecoratedURI aAbsoluteURI,
final DecoratedURI aTranslatedURI )
{
final String rootTranslatedURI =
rootTranslator.hook4Translate( aAbsoluteURI,
aTranslatedURI );
if ( smartChaining &&
aAbsoluteURI.getInputString() == rootTranslatedURI )
{
return aAbsoluteURI.getInputString();
}
int chainLength = hookChain.size();
String lResult = rootTranslatedURI;
try
{
for ( int i = 0; i < chainLength; i++ )
{
TranslatorHook nextTranslator = (TranslatorHook) ( hookChain.get( i ) );
lResult = nextTranslator.hook4Translate( aAbsoluteURI,
new DecoratedURI( lResult ) );
}
}
catch ( Exception e )
{
if ( Debug.isWarningEnabled() )
{
//Debug.recordURIWarning( "Unable to create DecoratedURI for :" + lResult, e );
Debug.recordURIWarning("PSRW_CSPR_0014",e);
}
}
return lResult;
}//hook4Translate()
}//class TranslatorChain
|