001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.rewriter.engines;
006:
007: import com.sun.portal.rewriter.Rewriter;
008: import com.sun.portal.rewriter.Translator;
009: import com.sun.portal.rewriter.rom.RuleSet;
010: import com.sun.portal.rewriter.util.Debug;
011: import com.sun.portal.rewriter.util.Resource;
012: import com.sun.portal.rewriter.util.StringHelper;
013: import com.sun.portal.rewriter.util.uri.PageSpec;
014:
015: import java.io.Reader;
016: import java.io.StringReader;
017:
018: /**
019: * Abstact implementation for Rewriter interface. This also contain
020: * utility methods which can be used by other Rewriters
021: *
022: * @version 1.0 12/15/2001
023: * @author Raja Nagendra Kumar, Nagendra.Raja@sun.com
024: */
025: public abstract class AbstractRewriter implements Rewriter,
026: LanguageConstants {
027: private final RewriterBroker rewriterBroker;
028: private final String mime;
029:
030: protected AbstractRewriter(final RewriterBroker aRewriterBroker,
031: final String aMIME) {
032: rewriterBroker = aRewriterBroker;
033: mime = aMIME;
034: }//constructor
035:
036: public RuleSet getRuleSet() {
037: return rewriterBroker.getRuleSet();
038: }//getRuleSet()
039:
040: public String getMIME() {
041: return mime;
042: }//getMIME()
043:
044: public Reader rewrite(final Reader aReader,
045: final Translator aTranslator) {
046: return new StringReader(rewrite(Resource.read(aReader),
047: aTranslator));
048: }//rewrite()
049:
050: public final String rewrite(final String aContent,
051: final Translator aTranslator) {
052: final boolean root = aTranslator.getLookAheadInfo()
053: .isRootRewriter();
054: String lRewrittenConent = aContent;
055: try {
056: //pre-condition
057: if ( /*getRuleSet().isEmpty() ||*/
058: StringHelper.normalize(aContent).length() < 1) {
059: return aContent;
060: }
061:
062: PageContent lPageContent = new PageContent(aContent);
063: plugableRewriter(lPageContent, aTranslator);
064: //BugNo:4683016, 4747776, 4914257, 4915239
065: if (root) {
066: endRewriting(lPageContent, aTranslator);
067: }
068: lRewrittenConent = lPageContent.getResult();
069: } catch (Exception e) {
070: Debug.recordOriginalPageWarning("", e);
071: } finally //BugNo:4897060
072: {
073: if (root) {
074: recordPage(aTranslator.getPageSpec(), lRewrittenConent,
075: aContent);
076: }
077: }
078:
079: return lRewrittenConent;
080: }//rewrite()
081:
082: public abstract void plugableRewriter(
083: final PageContent aPageContent, final Translator aTranslator);
084:
085: public void endRewriting(final PageContent aPageContent,
086: final Translator aTranslator) {
087: }//endRewriting()
088:
089: protected final RewriterBroker getRewriterBroker() {
090: return rewriterBroker;
091: }//getRewriterBroker()
092:
093: private void recordPage(final PageSpec aPageSpec,
094: final String aRewrittenConent, final String aContent) {
095: if (Debug.isMessageEnabled()) {
096: String bRewrittenConent = aRewrittenConent;
097: boolean unaffected = false;
098: if (aRewrittenConent.trim().equals(aContent.trim())) {
099: bRewrittenConent = "This Page remains same as the Original Page even after rewritting";
100: unaffected = true;
101: }
102:
103: String bHeadder = "\n{\n"
104: + ((unaffected) ? "(IDENTICAL)" : "")
105: + //put the identical only when they are same
106: aPageSpec + "RuleSet ID: " + getRuleSet().getID()
107: + ",\n" + "Parser MIME: " + mime + ",\n";
108:
109: if (unaffected) {
110: /*Debug.recordUnaffectedPageMessage( bHeadder +
111: "Rewritten Content:\n" +
112: bRewrittenConent +
113: "\n}\n" );*/
114: Object[] param0 = { bHeadder };
115: Object[] param1 = { bRewrittenConent };
116: Debug.recordUnaffectedPageMessage("PSRW_CSPR_0016",
117: param0);
118: Debug.recordUnaffectedPageMessage("PSRW_CSPR_0015",
119: param1);
120: } else {
121: /*Debug.recordRewrittenPageMessage( bHeadder +
122: "Rewritten Content:\n" +
123: bRewrittenConent +
124: "\n}\n" );*/
125: Object[] param0 = { bHeadder };
126: Object[] param1 = { bRewrittenConent };
127: Debug.recordRewrittenPageMessage("PSRW_CSPR_0016",
128: param0);
129: Debug.recordRewrittenPageMessage("PSRW_CSPR_0015",
130: param1);
131: }
132:
133: /*Debug.recordOriginalPageMessage( bHeadder +
134: "Original Content:\n" +
135: aContent +
136: "\n}\n" );*/
137: Object[] param0 = { bHeadder };
138: Object[] param1 = { aContent };
139: Debug.recordOriginalPageMessage("PSRW_CSPR_0017", param0);
140: Debug.recordOriginalPageMessage("PSRW_CSPR_0015", param1);
141: }
142: }//recordPage()
143:
144: }//class AbstractRewriter
|