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.css;
006:
007: import com.sun.portal.rewriter.Translator;
008: import com.sun.portal.rewriter.engines.AbstractRewriter;
009: import com.sun.portal.rewriter.engines.PageContent;
010: import com.sun.portal.rewriter.engines.RewriterBroker;
011: import com.sun.portal.rewriter.util.Constants;
012: import com.sun.portal.rewriter.util.Debug;
013:
014: /**
015: * Translates the CSS content present in JavaScript
016: *
017: * @version 1.0 12/15/2001
018: * @author Modified by Raja Nagendra Kumar, Nagendra.Raja@sun.com
019: */
020: public final class CSSRewriter extends AbstractRewriter {
021: public CSSRewriter(final RewriterBroker aRewriterBroker) {
022: super (aRewriterBroker, CSS_MIME);
023: }//constructor
024:
025: public void plugableRewriter(final PageContent aPageContent,
026: final Translator aTranslator) {
027: //do not interchange these lines.. Need a better way...
028: handleCSSURLs(aPageContent, aTranslator);
029: handleCSSImports(aPageContent, aTranslator);
030: }//plugableRewriter()
031:
032: private static final void handleCSSImports(
033: final PageContent aPageContent, final Translator aTranslator) {
034: //RFE No:4887831
035: String lTranslatedURLContent = aPageContent.getResult();
036: final String lLowerTranslatedURLContent = lTranslatedURLContent
037: .toLowerCase();
038:
039: StringBuffer lResultBuffer = null;
040: int cursorIndex = 0;
041: int importURLBeginIndex, importURLEndIndex;
042: do {
043: importURLBeginIndex = lLowerTranslatedURLContent.indexOf(
044: CSS_IMPORT_NODE_START, cursorIndex);
045: if (importURLBeginIndex != -1) {
046: if (lResultBuffer == null) {
047: lResultBuffer = new StringBuffer();
048: }
049:
050: importURLBeginIndex += CSS_IMPORT_NODE_START.length();
051: char c;
052: while (Character.isWhitespace(c = lTranslatedURLContent
053: .charAt(importURLBeginIndex))) {
054: importURLBeginIndex++;
055: }
056:
057: lResultBuffer.append(lTranslatedURLContent.substring(
058: cursorIndex, importURLBeginIndex + 1));
059:
060: if (c == Constants.DOUBLE_QUOTES_CHAR
061: || c == Constants.SINGLE_QUOTES_CHAR) {
062: importURLEndIndex = lLowerTranslatedURLContent
063: .indexOf(c, importURLBeginIndex + 1);
064:
065: if (importURLEndIndex != -1) {
066: lResultBuffer
067: .append(
068: translateCSSURI(
069: lTranslatedURLContent
070: .substring(
071: importURLBeginIndex + 1,
072: importURLEndIndex),
073: aTranslator)).append(c);
074: cursorIndex = importURLEndIndex + 1;
075: continue;
076: }
077: }
078: cursorIndex = importURLBeginIndex + 1;
079: }
080: } while (importURLBeginIndex != -1);
081:
082: if (lResultBuffer != null) {
083: lResultBuffer.append(lTranslatedURLContent
084: .substring(cursorIndex));
085:
086: StringBuffer bResult = aPageContent.getResultBuffer();
087: bResult.setLength(0);
088: bResult.append(lResultBuffer);
089: }
090: }//handleCSSImports()
091:
092: private static final void handleCSSURLs(
093: final PageContent aPageContent, final Translator aTranslator) {
094: final StringBuffer lResultBuffer = aPageContent
095: .getResultBuffer();
096: final String lowerContent = aPageContent.toLowerCase();
097:
098: int cursorIndex = 0;
099: int urlBeginIndex, urlEndIndex;
100: do {
101: urlBeginIndex = lowerContent.indexOf("url(", cursorIndex);
102: if (urlBeginIndex != -1) {
103: urlBeginIndex += "url(".length();
104: lResultBuffer.append(aPageContent.substring(
105: cursorIndex, urlBeginIndex));
106: urlEndIndex = lowerContent.indexOf(")", urlBeginIndex);
107: if (urlEndIndex != -1) {
108: // rewrite the URL we found
109: lResultBuffer.append(
110: translateCSSURI(aPageContent.substring(
111: urlBeginIndex, urlEndIndex),
112: aTranslator)).append(")");
113: cursorIndex = urlEndIndex + ")".length();
114: } else {
115: if (Debug.isWarningEnabled()) {
116: /*Debug.recordOriginalPageWarning(
117: "Unable to traslate CSS:\n" +
118: aTranslator.getPageSpec() );*/
119: Object param[] = { aTranslator.getPageSpec() };
120: Debug.recordOriginalPageWarning(
121: "PSRW_CSPR_0018", param);
122: }
123: break;
124: }
125: }
126: } while (urlBeginIndex != -1);
127:
128: if (cursorIndex > 0) {
129: lResultBuffer.append(aPageContent.substring(cursorIndex));
130: }
131: }//handleCSSURLs()
132:
133: private static final String translateCSSURI(String aUrl,
134: final Translator aTranslator) {
135: //Don't rewrite CSS URL References - as they are part of IE DOM
136: if (!aUrl.trim().startsWith("#")) {
137: aUrl = aTranslator.translate(aUrl);
138: }
139: return aUrl;
140: }//translateCSSURI()
141:
142: }//class CSSRewriter
|