001: // DirectoryResource.java
002: // $Id: IfCommand.java,v 1.6 2000/08/16 21:37:47 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1996.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.jigsaw.ssi.commands;
007:
008: import java.util.Dictionary;
009: import java.util.Hashtable;
010:
011: import org.w3c.www.http.HTTP;
012:
013: import org.w3c.jigsaw.http.Reply;
014: import org.w3c.jigsaw.http.Request;
015:
016: import org.w3c.tools.resources.FramedResource;
017: import org.w3c.tools.resources.Resource;
018: import org.w3c.tools.resources.ResourceFrame;
019:
020: import org.w3c.util.ArrayDictionary;
021:
022: import org.w3c.jigsaw.ssi.SSIFrame;
023:
024: /**
025: * Implementation of the SSI <code>if</code> command.
026: * @author Benoit Mahe <bmahe@sophia.inria.fr>
027: */
028: public class IfCommand implements ControlCommand {
029: private final static String NAME = "if";
030: private final static boolean debug = true;
031:
032: private static final String keys[] = { "name", "command", "var",
033: "equals" };
034:
035: protected static Hashtable ifstore = null;
036:
037: static {
038: ifstore = new Hashtable(23);
039: }
040:
041: /**
042: * return true if reply can be cached.
043: * @return a boolean.
044: */
045: public boolean acceptCaching() {
046: return false;
047: }
048:
049: /**
050: * Returns the (String) value of the given variable.
051: * @return a String instance.
052: */
053: public String getValue(Dictionary variables, String var,
054: Request request) {
055: return null;
056: }
057:
058: protected static int getPosition(String name)
059: throws ControlCommandException {
060: Integer pos = (Integer) ifstore.get(name);
061: if (pos == null)
062: throw new ControlCommandException(NAME, "Position unknown.");
063: else
064: return pos.intValue();
065: }
066:
067: /**
068: * register the command position in the structure
069: * witch store the SSIFrame.
070: */
071: public void setPosition(SSIFrame ssiframe, Request request,
072: CommandRegistry registry, ArrayDictionary parameters,
073: Dictionary variables, int position) {
074: Object values[] = parameters.getMany(keys);
075: String name = (String) values[0];
076: if (name != null)
077: ifstore.put(ssiframe.getResource().getURLPath() + ":"
078: + name, new Integer(position));
079: }
080:
081: /**
082: * Executes this command. Might modify variables.
083: * Must <em>not</em> modify the parameters.
084: * <P> It may handle conditional requests, <em>except</em> that if
085: * it replies with a status of HTTP.NOT_MODIFIED, it <em>must</em>
086: * still reply with a content (the same content that it would have
087: * returned for an inconditional request). This is because
088: * further SSI commands down the line may decide thay they have
089: * been modified, and then a content must be emitted by SSIFrame.
090: *
091: * @param request the original HTTP request
092: * @param parameters The parameters for this command
093: * @param variables The global variables for the parse
094: * @return a Reply with the output from the command */
095: public Reply execute(SSIFrame ssiframe, Request request,
096: ArrayDictionary parameters, Dictionary variables) {
097: return ssiframe.createCommandReply(request, HTTP.OK);
098: }
099:
100: protected boolean check(CommandRegistry registry,
101: ArrayDictionary parameters, Dictionary variables,
102: Request request) {
103: Object values[] = parameters.getMany(keys);
104: String name = (String) values[0];
105: String command = (String) values[1];
106: String var = (String) values[2];
107: String equals = (String) values[3];
108:
109: if ((command == null) || (var == null) || (equals == null))
110: return false;
111: Command cmd = registry.lookupCommand(command);
112: String value = cmd.getValue(variables, var, request);
113: return value.equals(equals);
114: }
115:
116: /**
117: * Give the next position in the structure witch
118: * store the SSIFrame.
119: * @return An integer
120: * @exception ControlCommandException if action failed.
121: */
122: public int jumpTo(SSIFrame ssiframe, Request request,
123: CommandRegistry registry, ArrayDictionary parameters,
124: Dictionary variables) throws ControlCommandException {
125: Object values[] = parameters.getMany(keys);
126: String name = (String) values[0];
127: if (name != null) {
128: if (check(registry, parameters, variables, request))
129: return getPosition(ssiframe.getResource().getURLPath()
130: + ":" + name) + 1;
131: try {
132: return (ElseCommand.getPosition(ssiframe.getResource()
133: .getURLPath()
134: + ":" + name) + 1);
135: } catch (ControlCommandException ex) {
136: return (EndifCommand.getPosition(ssiframe.getResource()
137: .getURLPath()
138: + ":" + name) + 1);
139: }
140: }
141: throw new ControlCommandException(NAME, "name not initialized.");
142: }
143:
144: /**
145: * Returns the name of this command. <em>(Case sensitivity is up to
146: * the <code>lookupCommand</code> method in the command registry.)</em>
147: *
148: * @return the name of the command
149: * @see org.w3c.jigsaw.ssi.commands.CommandRegistry#lookupCommand
150: */
151: public String getName() {
152: return NAME;
153: }
154:
155: }
|