All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.enhydra.xml.xmlc.misc.SSIDirective Maven / Gradle / Ivy

The newest version!
/*
 * Enhydra Java Application Server Project
 * 
 * The contents of this file are subject to the Enhydra Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License on
 * the Enhydra web site ( http://www.enhydra.org/ ).
 * 
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 
 * the License for the specific terms governing rights and limitations
 * under the License.
 * 
 * The Initial Developer of the Enhydra Application Server is Lutris
 * Technologies, Inc. The Enhydra Application Server and portions created
 * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
 * All Rights Reserved.
 * 
 * Contributor(s):
 * 
 * $Id: SSIDirective.java,v 1.2 2005/01/26 08:29:24 jkjome Exp $
 */

package org.enhydra.xml.xmlc.misc;

import java.io.IOException;
import java.util.ArrayList;

//FIXME need to HTML decode values

/**
 * A parsed SSI directive.
 */
final class SSIDirective {
    /**
     * Class containing an argument name/value pair.
     */
    class Arg {
        /** Argument name */
        public final String name;

        /** Argument value */
        public final String value;

        /**
         * Constructor
         */
        public Arg(String argName,
                   String argValue) {
            name = argName;
            value = argValue;
        }
    }

    /** The SSI command */
    private final String fCmd;

    /** The source file containing the SSI directive */
    private final String fSystemId;

    /**
     * Arguments as Arg objects. SSI syntag allows argument names to be
     * repeated, so this isn't a hash.
     */
    private ArrayList fArgs = new ArrayList();

    /**
     * Constructor.
     */
    public SSIDirective(String cmd,
                        String systemId) {
        fCmd = cmd;
        fSystemId = systemId;
    }

    /**
     * Add an argument.
     */
    public void addArg(String name,
                       String value) {
        fArgs.add(new Arg(name, value));
    }

    /**
     * Get the command.
     */
    public String getCmd() {
        return fCmd;
    }

    /**
     * Get the system id for the file that contained this directive.
     */
    public String getSystemId() {
        return fSystemId;
    }

    /**
     * Get the number of arguments
     */
    public int getNumArgs() {
        return fArgs.size();
    }

    /**
     * Get an argument name.
     */
    public String getArgName(int idx) {
        return ((Arg)fArgs.get(idx)).name;
    }

    /**
     * Get an argument value by index.
     */
    public String getArgValue(int idx) {
        return ((Arg)fArgs.get(idx)).value;
    }

    /**
     * Get an argument value by name.
     */
    public String getArgValue(String name) {
        int len = fArgs.size();
        for (int i = 0; i < len; i++) {
            if (name.equals(getArgName(i))) {
                return getArgValue(i);
            }
        }
        return null;
    }

    /**
     * Validate the arguments names are in a list of valid arguments.
     */
    public void validateArgumentNames(String[] validNames) throws IOException {
        int len = fArgs.size();
        for (int i = 0; i < len; i++) {
            String name = getArgName(i);
            boolean valid = false;
            for (int j = 0; j < validNames.length; j++) {
                if (validNames[j].equals(name)) {
                    valid = true;
                    break;
                }
            }
            if (!valid) {
                throw new IOException("invalid argument name for SSI "
                                      + fCmd + " command \""
                                      + name + "\": " + fSystemId);
            }
        }
    }

    /**
     * Return object contents as a string for debugging.
     */
    public String toString() {
        StringBuffer buf = new StringBuffer(1024);
        buf.append("cmd=`");
        buf.append(fCmd);
        buf.append("' systemId=");
        buf.append(fSystemId);
        buf.append(" args:");
        int len = fArgs.size();
        for (int i = 0; i < len; i++) {
            buf.append(" ");
            buf.append(getArgName(i));
            buf.append("=`");
            buf.append(getArgValue(i));
            buf.append("'");
        }
        return buf.toString();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy