KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > xmlc > misc > SSIDirective


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: SSIDirective.java,v 1.2 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.xml.xmlc.misc;
25
26 import java.io.IOException JavaDoc;
27 import java.util.ArrayList JavaDoc;
28
29 //FIXME need to HTML decode values
30

31 /**
32  * A parsed SSI directive.
33  */

34 final class SSIDirective {
35     /**
36      * Class containing an argument name/value pair.
37      */

38     class Arg {
39         /** Argument name */
40         public final String JavaDoc name;
41
42         /** Argument value */
43         public final String JavaDoc value;
44
45         /**
46          * Constructor
47          */

48         public Arg(String JavaDoc argName,
49                    String JavaDoc argValue) {
50             name = argName;
51             value = argValue;
52         }
53     }
54
55     /** The SSI command */
56     private final String JavaDoc fCmd;
57
58     /** The source file containing the SSI directive */
59     private final String JavaDoc fSystemId;
60
61     /**
62      * Arguments as Arg objects. SSI syntag allows argument names to be
63      * repeated, so this isn't a hash.
64      */

65     private ArrayList JavaDoc fArgs = new ArrayList JavaDoc();
66
67     /**
68      * Constructor.
69      */

70     public SSIDirective(String JavaDoc cmd,
71                         String JavaDoc systemId) {
72         fCmd = cmd;
73         fSystemId = systemId;
74     }
75
76     /**
77      * Add an argument.
78      */

79     public void addArg(String JavaDoc name,
80                        String JavaDoc value) {
81         fArgs.add(new Arg(name, value));
82     }
83
84     /**
85      * Get the command.
86      */

87     public String JavaDoc getCmd() {
88         return fCmd;
89     }
90
91     /**
92      * Get the system id for the file that contained this directive.
93      */

94     public String JavaDoc getSystemId() {
95         return fSystemId;
96     }
97
98     /**
99      * Get the number of arguments
100      */

101     public int getNumArgs() {
102         return fArgs.size();
103     }
104
105     /**
106      * Get an argument name.
107      */

108     public String JavaDoc getArgName(int idx) {
109         return ((Arg)fArgs.get(idx)).name;
110     }
111
112     /**
113      * Get an argument value by index.
114      */

115     public String JavaDoc getArgValue(int idx) {
116         return ((Arg)fArgs.get(idx)).value;
117     }
118
119     /**
120      * Get an argument value by name.
121      */

122     public String JavaDoc getArgValue(String JavaDoc name) {
123         int len = fArgs.size();
124         for (int i = 0; i < len; i++) {
125             if (name.equals(getArgName(i))) {
126                 return getArgValue(i);
127             }
128         }
129         return null;
130     }
131
132     /**
133      * Validate the arguments names are in a list of valid arguments.
134      */

135     public void validateArgumentNames(String JavaDoc[] validNames) throws IOException JavaDoc {
136         int len = fArgs.size();
137         for (int i = 0; i < len; i++) {
138             String JavaDoc name = getArgName(i);
139             boolean valid = false;
140             for (int j = 0; j < validNames.length; j++) {
141                 if (validNames[j].equals(name)) {
142                     valid = true;
143                     break;
144                 }
145             }
146             if (!valid) {
147                 throw new IOException JavaDoc("invalid argument name for SSI "
148                                       + fCmd + " command \""
149                                       + name + "\": " + fSystemId);
150             }
151         }
152     }
153
154     /**
155      * Return object contents as a string for debugging.
156      */

157     public String JavaDoc toString() {
158         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(1024);
159         buf.append("cmd=`");
160         buf.append(fCmd);
161         buf.append("' systemId=");
162         buf.append(fSystemId);
163         buf.append(" args:");
164         int len = fArgs.size();
165         for (int i = 0; i < len; i++) {
166             buf.append(" ");
167             buf.append(getArgName(i));
168             buf.append("=`");
169             buf.append(getArgValue(i));
170             buf.append("'");
171         }
172         return buf.toString();
173     }
174 }
175
Popular Tags