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.AbsoluteTranslator;
008: import com.sun.portal.rewriter.LookAheadInfo;
009: import com.sun.portal.rewriter.Rewriter;
010: import com.sun.portal.rewriter.RewriterPool;
011: import com.sun.portal.rewriter.Translator;
012: import com.sun.portal.rewriter.TranslatorHelper;
013: import com.sun.portal.rewriter.engines.AbstractRewriter;
014: import com.sun.portal.rewriter.engines.LanguageConstants;
015: import com.sun.portal.rewriter.engines.PageContent;
016: import com.sun.portal.rewriter.engines.RewriterBroker;
017: import com.sun.portal.rewriter.engines.js.JSRewriter;
018: import com.sun.portal.rewriter.rom.Data;
019: import com.sun.portal.rewriter.rom.Rule;
020: import com.sun.portal.rewriter.rom.RuleSet;
021: import com.sun.portal.rewriter.rom.RuleSetHelper;
022: import com.sun.portal.rewriter.rom.common.Attribute;
023: import com.sun.portal.rewriter.rom.html.Applet;
024: import com.sun.portal.rewriter.rom.html.Form;
025: import com.sun.portal.rewriter.test.util.CreateRuleSet;
026: import com.sun.portal.rewriter.util.ConfigManager;
027: import com.sun.portal.rewriter.util.Constants;
028: import com.sun.portal.rewriter.util.Debug;
029: import com.sun.portal.rewriter.util.Resource;
030: import com.sun.portal.rewriter.util.StringHelper;
031: import com.sun.portal.rewriter.util.re.Pattern;
032: import com.sun.portal.rewriter.util.uri.PageSpec;
033: import com.sun.portal.rewriter.util.xml.TagParser;
034:
035: import java.util.Arrays;
036: import java.util.List;
037: import java.util.StringTokenizer;
038:
039: /**
040: * This class should rewrite any Markup content. Currently this class
041: * understands rewritting of only HTML pages
042: * @version 1.0 12/15/2001
043: * @author Modified by Raja Nagendra Kumar, Nagendra.Raja@sun.com
044: */
045: public final class MarkupRewriter extends AbstractRewriter {
046: public static final String PROPERTY_IS_COMMENT_BASE_HREF = "IS_COMMENT_BASE_HREF";
047: public static final String PROPERTY_IS_COMMENT_BODY_BACKGROUND = "IS_COMMENT_BODY_BACKGROUND";
048: public static final String PROPERTY_IS_INSERT_CODEBASE = "IS_INSERT_CODEBASE";
049: public static final String PROPERTY_EXCLUDED_FORM_INPUT_TYPES = "EXCLUDED_FORM_INPUT_TYPES";
050:
051: private static final boolean IS_INSERT_CODEBASE = ConfigManager
052: .getBoolean(PROPERTY_IS_INSERT_CODEBASE);
053: private static final List EXCLUDED_FORM_INPUT_TYPES = Arrays
054: .asList(StringHelper.tokenize(ConfigManager.getProperty(
055: PROPERTY_EXCLUDED_FORM_INPUT_TYPES).toLowerCase(),
056: ";"));
057:
058: public MarkupRewriter(final RewriterBroker aRewriterBroker) {
059: super (aRewriterBroker, MARKUP_MIME);
060: }//constructor
061:
062: public final void plugableRewriter(final PageContent aPageContent,
063: final Translator aTranslator) {
064: parseMarkupContent(aPageContent, aTranslator);
065: }//rewrite()
066:
067: private final void parseMarkupContent(
068: final PageContent aPageContent, final Translator aTranslator) {
069: final StringBuffer lPageBuff = aPageContent.getResultBuffer();
070: final PageContext lPageContext = new PageContext(lPageBuff);
071: final String aContent = aPageContent.getOriginalContent();
072:
073: StringBuffer jsb = MarkupRewriterHelper.createBuffer();
074: StringBuffer cssb = MarkupRewriterHelper.createBuffer();
075: StringBuffer xmlb = MarkupRewriterHelper.createBuffer();
076:
077: int startIndex = 0;
078: int endIndex = 0;
079: boolean needs = true;
080: boolean isS = false;
081: boolean isL = false;
082: boolean isTag = false;
083: String prev = null;
084: String endedWith = ""; //in DHTML Content this could be empty i.e it does not end with '>'
085:
086: boolean isTextArea = false;
087: boolean isJScript = false; // check if inside Java Script
088: boolean isCSS = false; // check if inside Cascade Style Sheet
089: boolean isSPAN = false; // check if inside SPAN
090: boolean isXML = false; // check if inside XML
091:
092: int length = aPageContent.length();
093: while (startIndex < length) {
094: if (isS) {
095: isS = false;
096: startIndex++;
097: isTag = true;
098: if (isJScript) {
099: jsb.append("<");
100: } else if (isXML) {
101: xmlb.append("<");
102: } else if (isCSS) {
103: cssb.append("<");
104: } else {
105: lPageBuff.append("<");
106: }
107: continue;
108: } else if (isL) {
109: isL = false;
110: startIndex++;
111: isTag = false;
112: if (isJScript) {
113: jsb.append(endedWith);
114: } else if (isXML) {
115: xmlb.append(endedWith);
116: } else if (isCSS) {
117: cssb.append(endedWith);
118: } else if (isTextArea) //Bug ID: 4672142
119: {
120: isTextArea = false;
121: lPageBuff.append(endedWith);
122: int endTextArea = MarkupRewriterHelper
123: .skip2EndOfTag(aPageContent.toLowerCase(),
124: startIndex, SLASH_TEXTAREA);
125: if (endTextArea != -1) {
126: lPageBuff.append(aContent.substring(startIndex,
127: endTextArea));
128: startIndex = endTextArea;
129: }
130: }//Bug ID: 4672142
131: else {
132: lPageBuff.append(endedWith);
133: }
134:
135: continue;
136: } else if (needs) {
137: endIndex = aContent.indexOf('<', startIndex);
138: if (endIndex == -1) {
139: prev = aContent.substring(startIndex);
140: startIndex = length;
141: } else {
142: prev = aContent.substring(startIndex, endIndex);
143: startIndex = endIndex;
144: }
145:
146: isS = true;
147: needs = false;
148: } else {
149: endIndex = aContent.indexOf('>', startIndex);
150: if (endIndex == -1) {
151: prev = aContent.substring(startIndex);
152: startIndex = length;
153: } else {
154: //aTag may end either with > or /> so take care of both
155: //Bug No:4696780
156: endedWith = Constants.GREATER_THAN;
157: int bIndex = endIndex;
158: if (MarkupRewriterHelper.isEmptyTag(aContent,
159: endIndex)) {
160: bIndex--;
161: endedWith = Constants.SLASH_GREATER_THAN;
162: }
163:
164: prev = aContent.substring(startIndex, bIndex);
165: startIndex = endIndex;
166: }
167:
168: isL = true;
169: needs = true;
170: }
171:
172: if (prev != null) {
173: if (prev.trim().length() == 0) {
174: if (isJScript) {
175: jsb.append(prev);
176: } else if (isXML) {
177: xmlb.append(prev);
178: } else if (isCSS) {
179: cssb.append(prev);
180: } else {
181: lPageBuff.append(prev);
182: }
183: } else if (isTag) {
184: if (isXML) {
185: String tmptag = MarkupRewriterHelper
186: .getBlockTag(prev, xmlb);
187:
188: if (tmptag.trim().equalsIgnoreCase(SLASH_XML)) {
189: isXML = false;
190: rewriteTagBlock(getRewriterBroker()
191: .getXMLRewriter(), aTranslator,
192: xmlb.toString().trim(), lPageBuff);
193:
194: lPageBuff.append(tmptag);
195: xmlb = MarkupRewriterHelper.createBuffer();
196: } else {
197: xmlb.append(tmptag);
198: }
199: } else if (isJScript) {
200: String tmptag = MarkupRewriterHelper
201: .getBlockTag(prev, jsb);
202:
203: if (tmptag.trim()
204: .equalsIgnoreCase(SLASH_SCRIPT)) {
205: isJScript = false;
206:
207: //Bug ID: 4636629
208: aTranslator.getLookAheadInfo()
209: .markJSStartIndex(lPageBuff);
210: rewriteTagBlock(getRewriterBroker()
211: .getJSRewriter(), aTranslator, jsb
212: .toString().trim(), lPageBuff);
213:
214: lPageBuff.append(tmptag);
215: jsb = MarkupRewriterHelper.createBuffer();
216: } else {
217: jsb.append(tmptag);
218: }
219: } else if (isCSS) {
220: String tmptag = MarkupRewriterHelper
221: .getBlockTag(prev, cssb);
222:
223: if (tmptag.trim().equalsIgnoreCase(SLASH_STYLE)) {
224: isCSS = false;
225: rewriteTagBlock(getRewriterBroker()
226: .getCSSRewriter(), aTranslator,
227: cssb.toString().trim(), lPageBuff);
228: lPageBuff.append(tmptag);
229: cssb = MarkupRewriterHelper.createBuffer();
230: } else {
231: cssb.append(tmptag);
232: }
233: } else if (prev.indexOf("=") == -1) {
234: lPageBuff.append(prev);
235: } else if (prev.startsWith("!--")
236: && prev.endsWith("--")) {
237: lPageBuff.append(prev);
238: } else {
239: //Bug No:4853610
240: String lResult = parseTag(prev, endedWith,
241: aTranslator, lPageContext);
242:
243: if (lResult.endsWith("--")) {
244: endedWith = Constants.GREATER_THAN;
245: }
246:
247: lPageBuff.append(lResult);
248: }//if/else
249:
250: String bPrevContent = prev.trim();
251: //Bug ID: 4672142 , 5032856
252: if (bPrevContent.regionMatches(true, 0, TEXTAREA,
253: 0, TEXTAREA.length())
254: && MarkupRewriterHelper.isValidNextTagChar(
255: bPrevContent, TEXTAREA)
256: && !isJScript) {
257: isTextArea = true;
258: }//Bug ID: 4672142
259: else if (bPrevContent.regionMatches(true, 0,
260: SCRIPT, 0, SCRIPT.length())
261: && MarkupRewriterHelper.isValidNextTagChar(
262: bPrevContent, SCRIPT)) {
263: isJScript = true;
264: } else if (bPrevContent.regionMatches(true, 0, XML,
265: 0, XML.length())
266: && MarkupRewriterHelper.isValidNextTagChar(
267: bPrevContent, XML)) {
268: if (!bPrevContent.regionMatches(true, 0,
269: "XML:namespace", 0, 13)
270: && !isJScript)//BugNo:4869744
271: {
272: isXML = true;
273: }
274: } else if (bPrevContent.regionMatches(true, 0,
275: SPAN, 0, SPAN.length())
276: && MarkupRewriterHelper.isValidNextTagChar(
277: bPrevContent, SPAN)) {
278: if (!isXML && !isJScript) {
279: isSPAN = true;
280: }
281: } else if (bPrevContent.regionMatches(true, 0,
282: STYLE, 0, STYLE.length())
283: && MarkupRewriterHelper.isValidNextTagChar(
284: bPrevContent, STYLE)) {
285: if (!isXML && !isJScript) {
286: isCSS = true;
287: }
288: }
289: } else if (isJScript) {
290: jsb.append(prev);
291: } else if (isXML) {
292: xmlb.append(prev);
293: } else if (isCSS) {
294: cssb.append(prev);
295: } else if (isSPAN) {
296: isSPAN = false;
297: lPageBuff.append(getRewriterBroker()
298: .getHTMLRewriter().parseSPAN(prev,
299: aTranslator));
300: } else {
301: lPageBuff.append(prev);
302: }
303: }
304: }//while loop
305:
306: if (jsb.length() > 0) {
307: lPageBuff.append(jsb.toString());
308: }
309:
310: if (cssb.length() > 0) {
311: lPageBuff.append(cssb.toString());
312: }
313: }//parseMarkupContent()
314:
315: public void endRewriting(final PageContent aPageContent,
316: final Translator aTranslator) {
317: //BugNo:4683016, 4747776
318: final LookAheadInfo lLookAheadInfo = aTranslator
319: .getLookAheadInfo();
320: final int jsStartIndex = lLookAheadInfo.getJSStartIndex();
321: final String jsWrapperFunctionsDefination = lLookAheadInfo
322: .readJSFunctions(aTranslator);
323:
324: if (jsStartIndex != -1
325: && jsWrapperFunctionsDefination.length() != 0) {
326: aPageContent.getResultBuffer().insert(jsStartIndex,
327: LanguageConstants.SCRIPT_TAG_END).insert(
328: jsStartIndex, jsWrapperFunctionsDefination).insert(
329: jsStartIndex, LanguageConstants.SCRIPT_TAG_BEGIN);
330: }
331: }//endRewriting()
332:
333: private String parseTag(final String aTagContent,
334: final String aEndedWith, final Translator aTranslator,
335: final PageContext aPageContext) {
336: final String lLowerTagContent = aTagContent.toLowerCase();
337: final StringBuffer lTagResultBuffer = new StringBuffer(
338: aTagContent.length());
339: final LookAheadInfo lLookAheadInfo = aTranslator
340: .getLookAheadInfo();
341: lLookAheadInfo.setTagContext(new TagContext());
342:
343: String lResult = doParseTag(aTagContent, lLowerTagContent,
344: lTagResultBuffer, aEndedWith, aPageContext, aTranslator);
345:
346: //reset value of lLookAheadInfo
347: lLookAheadInfo.setTagContext(null);
348: lLookAheadInfo.setAppletCodeBase(null);
349: return lResult;
350: }//parseTag()
351:
352: private String doParseTag(final String aTagContent,
353: final String aLowerTagContent,
354: final StringBuffer aTagResultBuffer,
355: final String aEndedWith, final PageContext aPageContext,
356: final Translator aTranslator) {
357: TagContext lTagContext = aTranslator.getLookAheadInfo()
358: .getTagContext();
359:
360: //TODO:Can we avoid ussage of StringTokenizer here
361: final StringTokenizer lStringTokenizer = new StringTokenizer(
362: aLowerTagContent);
363: if (lStringTokenizer.hasMoreTokens()) {
364: lTagContext.tagName = lStringTokenizer.nextToken();
365: aPageContext.currentTag(lTagContext.tagName);
366:
367: //Bug ID: 4631945 (if codebase not found in the applet tag, insert one)
368: if (lTagContext.tagName.equals(APPLET)
369: || lTagContext.tagName.equals(OBJECT)) {
370: //if it is document.write with incomple tag defination
371: //(can be found by seeing the ended with tag value)
372: //BugNo:4894078
373: if (IS_INSERT_CODEBASE && aEndedWith.length() != 0) {
374: String spaceRemovedTagStmt = StringHelper
375: .searchAndReplace(aLowerTagContent, " ", "");
376: if (spaceRemovedTagStmt.indexOf("codebase=") == -1) {
377: StringBuffer modSB = new StringBuffer(
378: aTagContent);
379: aTranslator.getLookAheadInfo()
380: .enableContentChanged();
381: //BugNo:4819702
382: modSB.insert(aLowerTagContent
383: .indexOf(lTagContext.tagName)
384: + lTagContext.tagName.length(),
385: " codebase="
386: + aTranslator.getPageSpec()
387: .getBaseURI()
388: .getBaseHREF());
389:
390: String bModifiedTagContent = modSB.toString();
391: return doParseTag(bModifiedTagContent,
392: bModifiedTagContent.toLowerCase(),
393: aTagResultBuffer, aEndedWith,
394: aPageContext, aTranslator);
395: }
396: }
397:
398: MarkupRewriterHelper.setLookAheadCodeBaseInfo(
399: aTranslator, TagParser.parse(aTagContent));
400: }
401: //Bug ID: 4631945
402: }
403:
404: int mContentLength = aLowerTagContent.length();
405:
406: int mStart = 0, mHolder = 0, mPrevStart = 0;
407: int mStartLink = 0, mEndLink = 0;
408: int mEqual2Index = -1;
409:
410: boolean inSQuote = false; //in Single Quotes
411: boolean inDQuote = false; //in Dobule Quotes
412: boolean inESQuote = false; //in EndOf Single Quotes
413: boolean inEDQuote = false; //in End Of Single Quotes
414:
415: boolean mEndOfString = false;
416: boolean mLookForString = false;
417:
418: int currentCharIndex;
419: char currentChar;
420: for (currentCharIndex = 0; currentCharIndex < mContentLength; currentCharIndex++) {
421: currentChar = aLowerTagContent.charAt(currentCharIndex);
422: if (Character.isWhitespace(currentChar) || mEndOfString) {
423: if (!inSQuote && !inDQuote) {
424: mLookForString = false;
425: mEndOfString = false;
426: if (Character.isWhitespace(currentChar)) {
427: while ((currentCharIndex + 1) < mContentLength
428: && Character
429: .isWhitespace(aLowerTagContent
430: .charAt(currentCharIndex + 1))) {
431: ++currentCharIndex;
432: }//while loop
433:
434: if ((currentCharIndex + 1) >= mContentLength) {
435: mStart = currentCharIndex + 1;
436: } else if (aLowerTagContent
437: .charAt(currentCharIndex + 1) != '=') {
438: mStart = currentCharIndex + 1;
439: } else {
440: continue;
441: }
442: } else {
443: mStart = currentCharIndex;
444: }
445:
446: if (mStartLink > mEndLink) {
447: mEndLink = currentCharIndex;
448: }
449:
450: rewriteAttribute(aTagContent, aTagResultBuffer,
451: lTagContext, aTranslator, aPageContext,
452: mStartLink, mEndLink, mPrevStart,
453: currentCharIndex);
454:
455: mPrevStart = mEndLink = currentCharIndex;
456: }
457: } else if (currentChar == '\\') {
458: if (currentCharIndex + 1 < mContentLength) {
459: char c2 = aLowerTagContent
460: .charAt(currentCharIndex + 1);
461: if (c2 == '\'') {
462: if ((currentCharIndex == mEqual2Index + 1)
463: || inESQuote) {
464: inESQuote = !inESQuote;
465: if (inESQuote) {
466: mStartLink = currentCharIndex + 2;
467: } else {
468: mEndLink = currentCharIndex;
469: mEndOfString = true;
470: }
471: currentCharIndex++;
472: }
473: } else if (c2 == '\"') {
474: if ((currentCharIndex == mEqual2Index + 1)
475: || inEDQuote) {
476: inEDQuote = !inEDQuote;
477: if (inEDQuote) {
478: mStartLink = currentCharIndex + 2;
479: } else {
480: mEndLink = currentCharIndex;
481: mEndOfString = true;
482: }
483: currentCharIndex++;
484: }
485: }
486: }
487: } else if (currentChar == '\'') {
488: if (currentCharIndex == mEqual2Index + 1 || inSQuote) {
489: if (!inDQuote) {
490: inSQuote = !inSQuote;
491: if (inSQuote) {
492: mStartLink = currentCharIndex + 1;
493: } else {
494: mEndLink = currentCharIndex;
495: mEndOfString = true;
496: }
497: }
498: }
499: } else if (currentChar == '\"') {
500: if (currentCharIndex == mEqual2Index + 1 || inDQuote) {
501: if (!inSQuote) {
502: inDQuote = !inDQuote;
503: if (inDQuote) {
504: mStartLink = currentCharIndex + 1;
505: } else {
506: mEndLink = currentCharIndex;
507: mEndOfString = true;
508: }
509: }
510: }
511: } else if (currentChar == '=') {
512: if (!mLookForString) {
513: if (!inDQuote && !inSQuote) {
514: mEqual2Index = currentCharIndex;
515: mLookForString = true;
516: mStartLink = currentCharIndex + 1;
517:
518: if (mStartLink >= mContentLength) {
519: aTagResultBuffer.append(aTagContent
520: .substring(mPrevStart));
521: return aTagResultBuffer.toString();
522: }
523:
524: mHolder = currentCharIndex;
525:
526: while (mStartLink < mContentLength
527: && Character
528: .isWhitespace(aLowerTagContent
529: .charAt(mStartLink))) {
530: ++mEqual2Index;
531: ++mStartLink;
532: ++mHolder;
533: }
534:
535: lTagContext.attributeName = aLowerTagContent
536: .substring(mStart, currentCharIndex)
537: .trim();
538: matchWithHTMLRules(lTagContext, aTranslator,
539: aPageContext);
540: currentCharIndex = mHolder;
541: }
542: }
543: }//if/else
544: }//for loop
545:
546: if (mStartLink > mEndLink) {
547: mEndLink = currentCharIndex;
548: }
549:
550: rewriteAttribute(aTagContent, aTagResultBuffer, lTagContext,
551: aTranslator, aPageContext, mStartLink, mEndLink,
552: mPrevStart, currentCharIndex);
553:
554: //comment the HREF of Base and also background of body Tag if the page being rewritten is
555: //HTML or DHTML content of Javascript
556: if (aTranslator.getPageSpec().getMIME().equalsIgnoreCase(
557: HTML_MIME)
558: || aTranslator.getPageSpec().getMIME()
559: .equalsIgnoreCase(JS_MIME)) {
560: MarkupRewriterHelper.commentAttributes(lTagContext.tagName,
561: aTagResultBuffer, aEndedWith, aTranslator);
562: }
563:
564: return aTagResultBuffer.toString();
565: }//parseTag()
566:
567: private void rewriteAttribute(final String aTagContent,
568: final StringBuffer aTagResultBuffer,
569: final TagContext aTagContext, final Translator aTranslator,
570: final PageContext aPageContext, final int aLinkStart,
571: final int aLinkEnd, final int aPrevStart,
572: final int aCursorIndex) {
573: aTagContext.attributeValue = aTagContent.substring(aLinkStart,
574: aLinkEnd);
575: storePageAndTagValues(aPageContext, aTagContext, aTranslator);
576: Data lMache = aTagContext.getMache();
577: if (lMache != null) {
578: aTagResultBuffer.append(aTagContent.substring(aPrevStart,
579: aLinkStart));
580:
581: Attribute bMatch = null;
582: if (lMache instanceof Attribute) {
583: bMatch = (Attribute) lMache;
584: }
585:
586: if (bMatch == null //for applet and form rules
587: || bMatch.getType().equals(Rule.URL)) {
588: rewriteURL(aTagResultBuffer, aPageContext, aTagContext,
589: aTranslator);
590: } else if (bMatch.getType().equals(Rule.DJS)) {
591: rewriteSCRIPT(aTagResultBuffer, aPageContext,
592: aTagContext.attributeValue, false, aTranslator);
593: } else if (bMatch.getType().equals(Rule.DHTML)) {
594: aTagResultBuffer.append(rewrite(
595: aTagContext.attributeValue, aTranslator));
596: }
597: } else {
598: //don't rewrite
599: aTagResultBuffer.append(aTagContent.substring(aPrevStart,
600: aLinkEnd));
601: }
602:
603: aTagResultBuffer.append(aTagContent.substring(aLinkEnd,
604: aCursorIndex));
605: aTagContext.reset();//Bug No: 4853610
606: }//rewriteAttribute()
607:
608: private void storePageAndTagValues(final PageContext aPageContext,
609: final TagContext aTagContext, final Translator aTranslator) {
610: if (aTagContext.attributeName.equals(NAME)
611: || aTagContext.attributeName.equals(Rule.ID)) {
612: if (aTagContext.tagName.equals(FORM)) {
613: aPageContext.formName = aTagContext.attributeValue;
614: } else if (aTagContext.tagName.equals(SELECT)) {
615: aPageContext.selectName = aTagContext.attributeValue;
616: } else {
617: aTagContext.inputFieldName = aTagContext.attributeValue;
618: }
619: } else if (aTagContext.attributeName.equals(HREF)) {
620: if (aTagContext.tagName.equals(BASE)) //BugNo:4740650-Relationship
621: {
622: aTagContext.attributeValue = MarkupRewriterHelper
623: .setBaseSpec(aTranslator,
624: aTagContext.attributeValue);
625: }
626: } else if (aTagContext.attributeName.equals(Rule.TYPE))//BugNo:4892642
627: {
628: if (aTagContext.tagName.equals(INPUT)) {
629: aTagContext.inputFieldType = aTagContext.attributeValue
630: .toLowerCase();
631: return;
632: }
633: } else {
634: dealWithApplet(aPageContext, aTagContext, aTranslator);
635: }
636: }//storePageAndTagValues()
637:
638: private final void rewriteURL(final StringBuffer aTagResultBuffer,
639: final PageContext aPageContext,
640: final TagContext aTagContext, final Translator aTranslator) {
641: if (aTagContext.attributeValue.trim().regionMatches(true, 0,
642: JS_SCRIPT_PROTOCOL, 0, 11)) {
643: int index = aTagContext.attributeValue.indexOf(":") + 1;
644: aTagResultBuffer.append(aTagContext.attributeValue
645: .substring(0, index));
646: rewriteSCRIPT(aTagResultBuffer, aPageContext,
647: aTagContext.attributeValue.substring(index),
648: aTagContext.isCodeBase(), aTranslator);
649: } else {
650: Pattern[] patterns = aTagContext.getPatterns();
651: if (patterns.length > 0) {
652: String result = TranslatorHelper.translateSubStrings(
653: patterns, aTagContext.attributeValue,
654: aTranslator);
655: aTagResultBuffer.append(result);
656: } else if ((aTagContext.tagName.equals(META))
657: && (aTagContext.attributeName.equals(CONTENT))) {
658: MarkupRewriterHelper.rewriteMetaTag(aTagResultBuffer,
659: aTagContext.attributeValue, aTranslator);
660: } else if (aTagContext.attributeName.equals(STYLE)) //Bug No: 4505529
661: {
662: aTagResultBuffer.append(getRewriterBroker()
663: .getCSSRewriter()
664: .rewrite(aTagContext.attributeValue,
665: aTranslator));
666: }//Bug No: 4505529
667: else {
668: aTagResultBuffer.append(aTranslator
669: .translate(aTagContext.attributeValue));
670: }
671: }
672: }//rewriteURL()
673:
674: private static void dealWithApplet(final PageContext aPageContext,
675: final TagContext aTagContext, final Translator aTranslator) {
676: if (aTagContext.tagName.equals(APPLET)
677: || aTagContext.tagName.equals(OBJECT)) {
678: if (aTagContext.attributeName.equals(CODE)
679: || aTagContext.attributeName.equals(CLASSID)) {
680: aPageContext.appletName = aTagContext.attributeValue;
681: }
682: MarkupRewriterHelper.checkAndSetAppletBegin(aTagContext,
683: aTranslator);
684: }
685: }//dealWithApplet()
686:
687: private void matchWithHTMLRules(final TagContext aTagContext,
688: final Translator aTranslator, final PageContext aPageContext) {
689: final PageSpec lPageSpec = aTranslator.getPageSpec();
690:
691: if (aTagContext.attributeName.length() != 0) {
692: aTranslator.getLookAheadInfo().setInAppletAttributeEnd();
693: }
694:
695: aTagContext.setMacheRule(null);
696: final String lTagName = aTagContext.tagName;
697: if (aTagContext.attributeName.equals(VALUE)) {
698: if (lTagName.equals(PARAM)) {
699: Applet bMache = aPageContext
700: .getAppletInstance()
701: .recycleMatchee(aPageContext.appletName,
702: aTagContext.inputFieldName,
703: lPageSpec.getPageURI().getFullFileURI());//BugNo:4898369
704:
705: aTagContext.setMacheRule(RuleSetHelper
706: .findHTMLAppletMatch(getRuleSet(), bMache));
707: return;
708: } else if (lTagName.equals(INPUT)) {
709: //BugNo:4892642
710: if (EXCLUDED_FORM_INPUT_TYPES
711: .contains(aTagContext.inputFieldType)) {
712: return;
713: }
714:
715: Form bMache = aPageContext
716: .getFormInstance()
717: .recycleMatchee(aPageContext.formName,
718: aTagContext.inputFieldName,
719: lPageSpec.getPageURI().getFullFileURI()); //BugNo:4898369
720:
721: aTagContext.setMacheRule(RuleSetHelper
722: .findHTMLFormMatch(getRuleSet(), bMache));
723: return;
724: } else if (lTagName.equals(OPTION)) {
725: Form bMache = aPageContext
726: .getFormInstance()
727: .recycleMatchee(aPageContext.formName,
728: aPageContext.selectName,
729: lPageSpec.getPageURI().getFullFileURI());//BugNo:4898369
730:
731: aTagContext.setMacheRule(RuleSetHelper
732: .findHTMLFormMatch(getRuleSet(), bMache));
733: return;
734: }
735: }
736:
737: //Treat it as Ordinary Attribute
738: Attribute bMache = aPageContext.getAttributeInstance()
739: .recycleMatchee(
740: aTagContext.attributeName,
741: lTagName,
742: Rule.URL,
743: aTranslator.getPageSpec().getPageURI()
744: .getFullFileURI());
745:
746: aTagContext.setMacheRule(RuleSetHelper.findHTMLAttributeMatch(
747: getRuleSet(), bMache));
748: }//matchWithHTMLRules()
749:
750: private void rewriteTagBlock(final Rewriter aRewriter,
751: final Translator aTranslator, final String aContent,
752: final StringBuffer aPageBuff) {
753:
754: String lContent = aContent.substring(">".length(), aContent
755: .length()
756: - "<".length());
757: aPageBuff.append(">");
758:
759: int bCommentEndIndex = lContent.length();
760: //BugNo:4896278
761: if ((!(aRewriter instanceof JSRewriter))
762: && lContent.trim().startsWith(COMMENT_TAG_BEGIN)) {
763: int startIndex = lContent.indexOf(COMMENT_TAG_BEGIN);
764: aPageBuff.append(lContent.substring(0, startIndex)); //retain the white space
765: aPageBuff.append(COMMENT_TAG_BEGIN);
766: lContent = lContent.substring(startIndex
767: + COMMENT_TAG_BEGIN.length());
768:
769: bCommentEndIndex = lContent.lastIndexOf(COMMENT_TAG_END);
770:
771: if (bCommentEndIndex != -1) {
772: //Bug No:4755898
773: if (lContent.substring(
774: bCommentEndIndex + COMMENT_TAG_END.length())
775: .trim().length() != 0) {
776: bCommentEndIndex = lContent.length();
777: }
778: //Bug No:4755898
779: } else {
780: //starting comment is present and not end comment
781: bCommentEndIndex = lContent.length();
782: }
783: }
784:
785: aPageBuff.append(aRewriter.rewrite(lContent.substring(0,
786: bCommentEndIndex), aTranslator));
787: aPageBuff.append(lContent.substring(bCommentEndIndex));
788:
789: aPageBuff.append("<");
790: }//rewriteTagBlock()
791:
792: private void rewriteSCRIPT(final StringBuffer aTagResultBuffer,
793: final PageContext aPageContext,
794: final String aAttributeValue, final boolean aISCodeBase,
795: final Translator aTranslator) {
796: aTranslator.getLookAheadInfo().markJSStartIndex(
797: aPageContext.getPageBuffer());
798: //if the value codebase is a javascript, then deal differently
799: //i.e by appending iplanet() function
800: //BugNo:4747776
801: if (aISCodeBase) {
802: String bExpressionFunctionName = aTranslator
803: .getJSFunctionSpec().getExpressionFunctionName();
804: aTranslator.getLookAheadInfo().addJSFunction(
805: bExpressionFunctionName);
806: aTagResultBuffer.append(bExpressionFunctionName)
807: .append('(');
808: }
809: //BugNo:4747776
810:
811: final String jsResult = getRewriterBroker().getJSRewriter()
812: .rewrite(aAttributeValue, aTranslator);
813: aTagResultBuffer.append(jsResult);
814:
815: //BugNo:4747776
816: if (aISCodeBase) {
817: int lastSemiColen = jsResult.lastIndexOf(';');
818:
819: if (lastSemiColen == -1) {
820: aTagResultBuffer.append(')');
821: } else {
822: lastSemiColen = jsResult.length() - lastSemiColen;
823: aTagResultBuffer.insert(aTagResultBuffer.length()
824: - lastSemiColen, ')');
825: }
826: }
827: //BugNo:4747776
828: }//rewriteSCRIPT()
829:
830: public static void main(String[] args) {
831: try {
832: RuleSet lRuleSet = CreateRuleSet.emptyRuleSet();
833: Rewriter uRewriter = RewriterPool.create(lRuleSet,
834: MARKUP_MIME);
835: Debug
836: .println(uRewriter
837: .rewrite(
838: Resource.read(args[0]),
839: new AbsoluteTranslator(
840: com.sun.portal.rewriter.test.util.SampleRuleObjects.testPageSpec)));
841: } catch (Exception e) {
842: e.printStackTrace();
843: }
844: }//main()
845:
846: }//class MarkupRewriter
|