01: /*
02: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
03: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
04: */
05: package com.sun.portal.rewriter;
06:
07: import com.sun.portal.rewriter.util.Debug;
08: import com.sun.portal.rewriter.util.uri.DecoratedURI;
09:
10: import java.util.ArrayList;
11: import java.util.List;
12:
13: /**
14: * Chain of Responsibility patten to chain the translators with series of
15: * TranslatorHooks
16: */
17: public class TranslatorChain extends AbstractTranslator {
18: private final Translator rootTranslator;
19: private final List hookChain = new ArrayList();
20: private boolean smartChaining = false;
21:
22: public TranslatorChain(final Translator aRootTranslator) {
23: this (aRootTranslator, aRootTranslator.getJSFunctionSpec());
24: }//constructor
25:
26: public TranslatorChain(final Translator aRootTranslator,
27: final JSFunctionSpec aJSFunctionSpec) {
28: super (aRootTranslator.getPageSpec(), aJSFunctionSpec);
29: rootTranslator = aRootTranslator;
30: }//constructor
31:
32: public Translator getRootTranslator() {
33: return rootTranslator;
34: }//getRootTranslator()
35:
36: public void addTranslatorHook(final TranslatorHook aTranslatorHook) {
37: if (aTranslatorHook != null) {
38: hookChain.add(aTranslatorHook);
39: }
40: }//addTranslatorHook()
41:
42: public boolean isSmartlyChained() {
43: return smartChaining;
44: }//isSmartlyChained()
45:
46: public void enableSmartChaining() {
47: smartChaining = true;
48: }//enableSmartChaining()
49:
50: public final String hook4Translate(final DecoratedURI aAbsoluteURI,
51: final DecoratedURI aTranslatedURI) {
52: final String rootTranslatedURI = rootTranslator.hook4Translate(
53: aAbsoluteURI, aTranslatedURI);
54:
55: if (smartChaining
56: && aAbsoluteURI.getInputString() == rootTranslatedURI) {
57: return aAbsoluteURI.getInputString();
58: }
59:
60: int chainLength = hookChain.size();
61:
62: String lResult = rootTranslatedURI;
63: try {
64: for (int i = 0; i < chainLength; i++) {
65: TranslatorHook nextTranslator = (TranslatorHook) (hookChain
66: .get(i));
67: lResult = nextTranslator.hook4Translate(aAbsoluteURI,
68: new DecoratedURI(lResult));
69: }
70: } catch (Exception e) {
71: if (Debug.isWarningEnabled()) {
72: //Debug.recordURIWarning( "Unable to create DecoratedURI for :" + lResult, e );
73: Debug.recordURIWarning("PSRW_CSPR_0014", e);
74: }
75: }
76:
77: return lResult;
78: }//hook4Translate()
79:
80: }//class TranslatorChain
|