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.markup;
006:
007: import com.sun.portal.rewriter.Translator;
008: import com.sun.portal.rewriter.TranslatorHelper;
009: import com.sun.portal.rewriter.engines.LanguageConstants;
010: import com.sun.portal.rewriter.util.ConfigManager;
011: import com.sun.portal.rewriter.util.Constants;
012: import com.sun.portal.rewriter.util.xml.Tag;
013: import com.sun.portal.rewriter.util.xml.TagParser;
014:
015: import java.util.List;
016:
017: final class MarkupRewriterHelper {
018: private static final boolean commentBaseHREF = ConfigManager
019: .getBoolean(MarkupRewriter.PROPERTY_IS_COMMENT_BASE_HREF);
020: private static final boolean commentBodyBackGround = ConfigManager
021: .getBoolean(MarkupRewriter.PROPERTY_IS_COMMENT_BODY_BACKGROUND);
022:
023: static boolean isEmptyTag(final String aContent, int aEndIndex) {
024: if (aContent.charAt(aEndIndex - 1) == '/') {
025: //BugNo: 4758709
026: char backByTwo = aContent.charAt(aEndIndex - 2);
027: //TODO:REGEXP
028: if (Character.isWhitespace(backByTwo) || backByTwo == '\"'
029: || backByTwo == '\'' || backByTwo == '\\') {
030: return true;
031: }
032: }
033: return false;
034: }//isEmptyTag()
035:
036: static void commentAttributes(final String aTagName,
037: final StringBuffer aTagResultBuffer,
038: final String aEndedWith, final Translator aTranslator) {
039: //BugNo: 4413145, 4767077, 4529513, 4696780, 4687285, 4692006
040: //comment the Base tag's href attribute only
041: if (commentBaseHREF
042: && aTagName.equalsIgnoreCase(LanguageConstants.BASE)) {
043: aTranslator.getLookAheadInfo().enableContentChanged();
044: commentBaseHREF(aTagResultBuffer, aEndedWith);
045: } else if (commentBodyBackGround
046: && aTagName.equalsIgnoreCase(LanguageConstants.BODY)) {
047: //BugNo:4870334
048: aTranslator.getLookAheadInfo().enableContentChanged();
049: commentAttribute(aTagResultBuffer,
050: LanguageConstants.BACKGROUND, aEndedWith);
051: }
052: }//commentAttributes()
053:
054: static void commentBaseHREF(final StringBuffer aTagBuff,
055: final String aEndedWith) {
056: //if href and target are present comment only href and not
057: //target
058: Tag lBaseTag = TagParser.parse(aTagBuff.toString());
059: String lHREF = lBaseTag.get(LanguageConstants.HREF);
060:
061: if (lHREF != null && lHREF.trim().length() != 0)//BugNo:4740650,4819702
062: {
063: if (lBaseTag.get(LanguageConstants.TARGET) == null)//only href present
064: {
065: aTagBuff.insert(0, "!-- ");
066: aTagBuff.append(" --");
067: } else //both href and target are present
068: {
069: aTagBuff.setLength(0);
070:
071: //comment only href and not target
072: aTagBuff.append(lBaseTag.getName());
073: aTagBuff.append(Constants.SINGLE_SPACE);
074: aTagBuff.append(LanguageConstants.TARGET);
075: aTagBuff.append(Constants.EQUALS);
076: aTagBuff.append(lBaseTag.get(LanguageConstants.TARGET));
077: aTagBuff.append(Constants.SINGLE_SPACE);//BugNo:4853610
078: aTagBuff.append(aEndedWith);
079: aTagBuff.append(" <!-- ");
080: aTagBuff.append(LanguageConstants.HREF);
081: aTagBuff.append(Constants.EQUALS);
082: aTagBuff.append(lHREF);
083: aTagBuff.append(" --");
084: }
085: }
086: }//commentBaseHREF()
087:
088: private static void commentAttribute(final StringBuffer aTagBuff,
089: final String aAttributeName, final String aEndedWith) {
090: Tag lTag = TagParser.parse(aTagBuff.toString());
091: String lBackGround = lTag.get(aAttributeName);
092:
093: if (lBackGround != null && lBackGround.trim().length() != 0)//BugNo:4740650, 4819702
094: {
095: aTagBuff.setLength(0);
096: aTagBuff.append(lTag.getName());
097: aTagBuff.append(Constants.SINGLE_SPACE);
098:
099: List lAttributeList = lTag.getAttributeMap().keyList();
100: for (int i = 0; i < lAttributeList.size(); i++) {
101: String bAttName = lAttributeList.get(i).toString();
102: if (bAttName.equalsIgnoreCase(aAttributeName)) {
103: continue;
104: }
105:
106: aTagBuff.append(bAttName);
107: aTagBuff.append(Constants.EQUALS);
108: aTagBuff.append(lTag.get(bAttName));
109: aTagBuff.append(Constants.SINGLE_SPACE);
110: }
111:
112: //make sure there is atleast a single space before endedWith tag
113: //BugNo:4853610
114: aTagBuff.append(aEndedWith);
115: aTagBuff.append(" <!-- ");
116: aTagBuff.append(aAttributeName);
117: aTagBuff.append(Constants.EQUALS);
118: aTagBuff.append(lBackGround);
119: aTagBuff.append(" --");
120: }
121: }//commentAttribute()
122:
123: static void rewriteMetaTag(final StringBuffer aTagBuff,
124: final String aAttributeValue, final Translator aTranslator) {
125: int index = aAttributeValue.indexOf(";");
126: if (index == -1) {
127: aTagBuff.append(aAttributeValue);
128: } else {
129: //Bug No: 4689375
130: index = aAttributeValue.toLowerCase()
131: .indexOf("url=", index);
132: if (index == -1) {
133: aTagBuff.append(aAttributeValue);
134: } else {
135: aTagBuff
136: .append(aAttributeValue.substring(0, index + 4));
137: aTagBuff.append(aTranslator.translate(aAttributeValue
138: .substring(index + 4)));
139: }
140: }//if/else
141: }//rewriteMetaTag()
142:
143: static String setBaseSpec(final Translator aTranslator,
144: final String aAttributeValue) {
145: final String lAbsoluteBASE = TranslatorHelper.convert2Absolute(
146: aTranslator.getPageSpec(), aAttributeValue);
147: aTranslator.getPageSpec().setBase(lAbsoluteBASE);
148: return lAbsoluteBASE;
149: }//setBaseSpec()
150:
151: static void setLookAheadCodeBaseInfo(final Translator aTranslator,
152: final Tag aTag) {
153: String lCodeBaseValue = aTag.get(LanguageConstants.CODEBASE);
154:
155: //set the codebase value only if this not a javascript call
156: //will a bug if one is asked to rewrite archive and code in this senario
157: //BugNo:4747776
158: if (lCodeBaseValue != null) {
159: if (!lCodeBaseValue.trim().regionMatches(true, 0,
160: LanguageConstants.JS_SCRIPT_PROTOCOL, 0, 11)) {
161: lCodeBaseValue = TranslatorHelper.convert2Absolute(
162: aTranslator.getPageSpec(), lCodeBaseValue);
163: }
164: aTranslator.getLookAheadInfo().setAppletCodeBase(
165: lCodeBaseValue);
166: }
167: }//setLookAheadCodeBaseInfo()
168:
169: static void checkAndSetAppletBegin(final TagContext aTagContext,
170: final Translator aTranslator) {
171: if (aTagContext.attributeName
172: .equals(LanguageConstants.CODEBASE)) {
173: aTagContext.enableCodeBase();
174: }
175:
176: if (aTagContext.attributeName.equals(LanguageConstants.CODE)
177: || aTagContext.attributeName
178: .equals(LanguageConstants.ARCHIVE)
179: || aTagContext.attributeName
180: .equals(LanguageConstants.CLASSID)) {
181: aTranslator.getLookAheadInfo().setInAppletAttributeBegin();
182: }
183: }//checkAndSetAppletBegin()
184:
185: static String getBlockTag(final String aContent,
186: final StringBuffer aBlockBuff) {
187: final int index = aContent.lastIndexOf('<');
188: final String tmptag;
189: if (index != -1) {
190: tmptag = aContent.substring(index + 1);
191: aBlockBuff.append(aContent.substring(0, index + 1));
192: } else {
193: tmptag = aContent;
194: }
195:
196: return tmptag;
197: }//getBlockTag()
198:
199: static int skip2EndOfTag(final String aLowerCaseContent,
200: final int aLookFromIndex, final String aTagName) {
201: final int length = aTagName.length();
202: final int k = aLowerCaseContent.indexOf(aTagName,
203: aLookFromIndex);
204: if (k == -1) {
205: return -1;
206: }
207:
208: int l = aLowerCaseContent.indexOf('>', k + length);
209:
210: if (l == -1) {
211: return -1;
212: }
213:
214: String gap = aLowerCaseContent.substring(k + length, l);
215: if (gap.trim().length() == 0) {
216: return l + 1;
217: } else {
218: return -1;
219: }
220: }//skip2EndOfTag()
221:
222: static boolean isValidNextTagChar(final String aPrevContent,
223: final String aTag) {
224: final int lPrevContentLength = aPrevContent.length();
225: final int lTagLength = aTag.length();
226:
227: char charNext2Tag = '>';
228:
229: if (lPrevContentLength > lTagLength) {
230: charNext2Tag = aPrevContent.charAt(lTagLength);
231: }
232:
233: //Bug No: 4742022
234: if (charNext2Tag == '>' || Character.isWhitespace(charNext2Tag)) {
235: return true;
236: }
237:
238: return false;
239: }//isValidNextTagChar()
240:
241: static final StringBuffer createBuffer() {
242: return new StringBuffer(200);
243: }//createBuffer()
244:
245: }//class RewriterHelper
|