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.rom.js;
006:
007: import com.sun.portal.rewriter.rom.Data;
008: import com.sun.portal.rewriter.rom.DataRule;
009: import com.sun.portal.rewriter.util.Constants;
010: import com.sun.portal.rewriter.util.Debug;
011: import com.sun.portal.rewriter.util.StringHelper;
012: import com.sun.portal.rewriter.util.re.Pattern;
013: import com.sun.portal.rewriter.util.xml.Node;
014:
015: import java.util.ArrayList;
016: import java.util.Arrays;
017: import java.util.Collection;
018: import java.util.Collections;
019: import java.util.List;
020:
021: /**
022: * Represents Function tag of JSRules
023: *
024: * @version 1.0 12/15/2001
025: * @author Raja Nagendra Kumar, Nagendra.Raja@sun.com
026: */
027: public final class FunctionRule extends JSDataRule {
028: private final Function function;
029: private final List argumentSpec;
030:
031: public FunctionRule(final Function aFunction) {
032: super (aFunction);
033: function = aFunction;
034:
035: argumentSpec = parseArgumentPatterns(function
036: .getArgumentPatterns());
037: }//constructor
038:
039: public FunctionRule(final Node aNode) {
040: this (new Function(aNode.getAttributeValue(NAME), aNode
041: .getAttributeValue(PARAM_PATTERNS), aNode
042: .getAttributeValue(TYPE), aNode
043: .getAttributeValue(SOURCE)));
044: }//constructor
045:
046: public Function getFunction() {
047: return function;
048: }//getFunction()
049:
050: private Collection getParsedPatterns() {
051: return argumentSpec;
052: }//getParsedValuePatterns()
053:
054: private static List parseArgumentPatterns(
055: final String aParamPatterns) {
056: if (aParamPatterns.length() == 0) {
057: return Collections.EMPTY_LIST;
058: }
059:
060: List result = new ArrayList();
061: Pattern[] tempPatterns = DataRule
062: .createValuePatternSpec(aParamPatterns);
063: for (int i = 0; i < tempPatterns.length; i++) {
064: result.add(StringHelper.tokenize(
065: tempPatterns[i].getInput(), ",", true));
066: }
067:
068: return result;
069: }//parseArgumentPatterns()
070:
071: public boolean plugableMatch(final Data aMache) {
072: if (!(aMache instanceof Function)) {
073: return false;
074: }
075:
076: Function valueObject = (Function) aMache;
077: return matchCommon(valueObject);
078: }//matchs()
079:
080: public boolean isValid() {
081: return doBasicValidation(new String[] { function.getName(),
082: function.getArgumentPatterns() });
083: }//isValid()
084:
085: public String[] getParamSpec(final int aLength) {
086: String[] result = Constants.EMPTY_STRING_ARRAY;
087: if (getParsedPatterns().size() > 0) {
088: result = (String[]) argumentSpec.get(0);
089: }
090:
091: //insert the dummy ones only if the lenght is less than the passed one
092: if (aLength > result.length) {
093: String[] temp = new String[aLength];
094: Arrays.fill(temp, Constants.EMPTY_STRING);
095: System.arraycopy(result, 0, temp, 0, result.length);
096: result = temp;
097: }
098:
099: return result;
100: }//getParamSpec()
101:
102: public static void main(String[] args) {
103: FunctionRule[] functions = com.sun.portal.rewriter.test.util.SampleRuleObjects.defaultJSFunctions;
104: for (int i = 0; i < functions.length; i++) {
105: Debug.println(functions[i].toXML());
106: }//for loop
107: }//main()
108: }//class Function
|