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;
006:
007: import com.sun.portal.rewriter.engines.LanguageConstants;
008: import com.sun.portal.rewriter.util.Constants;
009: import com.sun.portal.rewriter.util.StringHelper;
010: import com.sun.portal.rewriter.util.re.Pattern;
011: import com.sun.portal.rewriter.util.re.RegExp;
012: import com.sun.portal.rewriter.util.uri.DecoratedURI;
013: import com.sun.portal.rewriter.util.uri.PageSpec;
014:
015: import java.net.MalformedURLException;
016:
017: /**
018: * This contain utility methods which can be used by
019: * any one including derived Rewriters
020: *
021: * @version 1.0 12/15/2001
022: * @author Raja Nagendra Kumar, Nagendra.Raja@sun.com
023: */
024: public final class TranslatorHelper {
025: public static final String convert2Absolute(
026: final PageSpec aPageSpec, final String aOrgURL) {
027: return AbstractTranslator.doTranslate(aOrgURL, aPageSpec, null);
028: }//convertURL2Absolute()
029:
030: /**
031: * Translates based on the patterns provided used in - Forms and Applets, TagText, Attributes
032: */
033: public static final String translateSubStrings(
034: final Pattern[] aRuleList, final String aSourceString,
035: final Translator aTranslator) {
036: Pattern rule;
037: int index;
038:
039: String result = aSourceString;
040: boolean translateZeroPosition = false;
041:
042: final int vpSize = aRuleList.length;
043: for (int j = vpSize - 1; j >= 0; j--) {
044: rule = aRuleList[j];
045: if (rule.getInput().equals(
046: Constants.ZERO_POSTITION_CHILD_PATTERN_INDICATOR)) {
047: translateZeroPosition = true;
048: continue;
049: }
050:
051: if (rule.getInput().length() == 0) {
052: // translate full string thinking as if this a simple URL
053: result = translateString(result, aTranslator);
054: } else {
055: //find SUB part of it, which is a Simple URL and translate
056: //only that portion of the String
057:
058: index = RegExp.indexOf(rule, result);
059: if (index == -1 || index >= result.length()) {
060: continue;
061: }
062: result = result.substring(0, index)
063: + translateString(rule.isRecursive() ? //BUG#4965568
064: translateSubStrings(aRuleList, result
065: .substring(index), aTranslator)
066: : result.substring(index), aTranslator);
067: }
068: }
069:
070: if (translateZeroPosition && !aSourceString.equals(result)) {
071: result = translateString(result, aTranslator);
072: }
073:
074: return result;
075: }//translateSubStrings()
076:
077: public static final String translateString(final String aString,
078: final Translator aTranslator) {
079: final int i = findLastValidURICharIndex(aString);
080:
081: if (i == 0) {
082: return aString;
083: }
084:
085: StringBuffer sb = new StringBuffer(aString.length());
086: sb.append(aTranslator.translate(aString.substring(0, i)));
087: sb.append(aString.substring(i));
088: return sb.toString();
089: }//translateString()
090:
091: private static final int findLastValidURICharIndex(
092: final String aString) {
093: int i;
094: char c;
095:
096: final String lScheme = StringHelper.normalize(aString);
097: final int strLen = lScheme.length();
098: for (i = 0; i < strLen; i++) {
099: c = lScheme.charAt(i);
100: //File Name can contain both letters and degits
101: if (!(Character.isLetterOrDigit(c)
102: || //i18n way
103: c == '.' || c == '/' || c == ' ' || c == ':'
104: || c == '\'' || c == '"')) {
105: return i;
106: }
107: }//for loop
108: return i;
109: }//findLastValidURICharIndex()
110:
111: public static final DecoratedURI handleScriptSRC(
112: final PageSpec aPageSpec, final DecoratedURI aURI)
113: throws MalformedURLException {
114: DecoratedURI lResult = aURI;
115:
116: String bScriptSRCBase = aURI.getBaseHREF();
117: String bScriptReferrerBase = aPageSpec.getBaseURI()
118: .getBaseHREF();
119: if (!bScriptSRCBase.equalsIgnoreCase(bScriptReferrerBase)) {
120: String bURIString = lResult.toExternalForm();
121: if (aURI.hasQuery()) {
122: //check if the URI already has script referer appended. This
123: //could happen in URLScrapper pages
124: if (aURI.getQuery().indexOf(
125: LanguageConstants.SCRIPT_REFERRER) != -1) {
126: return lResult;
127: }
128:
129: //already get param present and assumption is the key with scritrefferer is never present
130: if (!bURIString.endsWith(Constants.AND)) {
131: bURIString += Constants.AND;
132: }
133: } else {
134: bURIString += Constants.QUESTION_MARK;
135: }
136:
137: bURIString += LanguageConstants.SCRIPT_REFERRER
138: + Constants.EQUALS + bScriptReferrerBase + ".END.";
139: lResult = new DecoratedURI(bURIString);
140: }
141:
142: return lResult;
143: }//handleScriptSRC()
144:
145: public static void main(String[] args) throws Exception {
146: PageSpec samplePageSpec = new PageSpec(
147: "http://rajanagendra.sun.com/Base/Raja/raja.html?name=raja",
148: "");
149: AbsoluteTranslator defaultTranslator = new AbsoluteTranslator(
150: samplePageSpec);
151: String result = defaultTranslator.translate("..");
152: System.out.println(result);
153: }//main()
154:
155: }//class TranslatorHelper
|