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.util.clip;
006:
007: import com.sun.portal.rewriter.util.Constants;
008: import com.sun.portal.rewriter.util.i18n.LocaleHelper;
009: import com.sun.portal.rewriter.util.xml.Node;
010:
011: import java.util.ArrayList;
012: import java.util.Arrays;
013: import java.util.List;
014: import java.util.Locale;
015:
016: class CLIPSpecHelper {
017: static String[] removeFirstNArguments(final String[] args,
018: final int aCount) {
019: if (aCount > args.length) {
020: return Constants.EMPTY_STRING_ARRAY;
021: }
022:
023: List lResult = new ArrayList(Arrays.asList(args));
024: for (int i = 0; i < aCount; i++) {
025: lResult.remove(0);
026: }
027:
028: return (String[]) lResult.toArray(Constants.EMPTY_STRING_ARRAY);
029: }//removeFirstNArguments()
030:
031: static CLIPOption[] getCommonOptions(final Node aNode)
032: throws Exception {
033: final List v = new ArrayList();
034:
035: final Node[] children = aNode
036: .selectNodes(CLIPConstants.COMMON_OPTIONS_XPATH);
037:
038: for (int i = 0; i < children.length; i++) {
039: v.add(createOption(children[i]));
040: }//for loop
041:
042: return (CLIPOption[]) v.toArray(new CLIPOption[0]);
043: }//getCommonOptions()
044:
045: static int findType(final String aString) {
046: final String lString = aString.trim();
047: if (lString.equalsIgnoreCase("REGULAR")) {
048: return CLIPConstants.REGULAR;
049: }
050:
051: if (lString.equalsIgnoreCase("BOOLEAN")) {
052: return CLIPConstants.BOOLEAN;
053: }
054:
055: return -1;
056: }//findType()
057:
058: static Locale findLocale(final String[] args) {
059: for (int i = 0; i < args.length; i++) {
060: if (args[i].equals("-l")
061: || args[i].equals("--"
062: + CLIPConstants.OPTION_LOCALE)) {
063: if (i < args.length - 1) {
064: Locale locale = LocaleHelper.getLocale(args[i + 1]);
065: return locale;
066: }
067: }//if
068: }//for loop
069: return Locale.getDefault();
070: }//findLocale()
071:
072: static CLIPSubCommand createSubCommand(final Node aNode,
073: CLIPOption[] commonOptions) throws Exception {
074: return new CLIPSubCommand(
075: aNode.getAttributeValue(CLIPConstants.NAME),
076: createOptions(aNode, commonOptions),
077: Integer.parseInt(aNode
078: .getAttributeValue(CLIPConstants.MIN_OPERANDS)),
079: Integer.parseInt(aNode
080: .getAttributeValue(CLIPConstants.MAX_OPERANDS)),
081: aNode.getAttributeValue(CLIPConstants.OPERANDS_HELP),
082: aNode.getAttributeValue(CLIPConstants.HELP));
083: }//createSubCommand()
084:
085: static CLIPOption[] createOptions(final Node aNode,
086: CLIPOption[] commonOptions) throws Exception {
087: final List v = new ArrayList(Arrays.asList(commonOptions));
088: final Node[] options = Node.getChildNodes(aNode, "Option");
089: for (int i = 0; i < options.length; i++) {
090: v.add(createOption(options[i]));
091: }
092:
093: return (CLIPOption[]) v
094: .toArray(CLIPOption.EMTPY_CLIP_OPTION_ARRAY);
095: }//createOptions()
096:
097: private static CLIPOption createOption(final Node aNode)
098: throws Exception {
099: return new CLIPOption(aNode
100: .getAttributeValue(CLIPConstants.LONG_NAME), aNode
101: .getAttributeValue(CLIPConstants.SHORT_NAME),
102: findType(aNode.getAttributeValue(CLIPConstants.TYPE)),
103: aNode.getAttributeValue(CLIPConstants.DEFAULT_VALUES),
104: aNode.getAttributeValue(CLIPConstants.HELP));
105: }//createOption()
106: }//class CLIPSpecHelper
|