001: // ResourceFilter.java
002: // $Id: ResourceFilter.java,v 1.10 2002/06/26 17:27:43 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.tools.resources;
007:
008: import java.io.OutputStream;
009: import java.util.Hashtable;
010:
011: public class ResourceFilter extends ResourceFrame implements
012: FilterInterface {
013:
014: /**
015: * Get our target resource.
016: */
017: public Resource getTargetResource() {
018: Resource target = (Resource) getResource();
019: while (target instanceof ResourceFrame) {
020: target = ((ResourceFrame) target).getResource();
021: }
022: return target;
023: }
024:
025: /**
026: * Lookup time filtering.
027: * This method is called while Jigsaw performs resource lookup. Each time
028: * a lookup end up on the target resource of that filter, this method
029: * will be called.
030: * @param ls The current lookup state.
031: * @param lr The current lookup result.
032: * @return A boolean, <strong>true</strong> if this filter has performed
033: * the whole lookup, and side-effect the lookup result appropriatelly,
034: * <strong>false</strong> otherwise.
035: * @exception ProtocolException If processing should be interrupted,
036: * because an abnormal situation occured.
037: */
038:
039: public boolean lookup(LookupState ls, LookupResult lr)
040: throws ProtocolException {
041: return false;
042: }
043:
044: /**
045: * Simplified ingoingFilter API.
046: * This is a default, simplified API to the ingoing filter method.
047: * @param request The request to filter.
048: * @return A Reply instance, or <strong>null</strong> if processing
049: * should continue normally.
050: * @exception ProtocolException If processing should be interrupted,
051: * because an abnormal situation occured.
052: */
053:
054: public ReplyInterface ingoingFilter(RequestInterface request)
055: throws ProtocolException {
056: return null;
057: }
058:
059: /**
060: * The ingoingFilter method.
061: * This is the method that really gets called by Jigsaw core. By default
062: * it will invoke the simpler <code>ingoingFilter</code> method,
063: * taking only the request has a parameter.
064: * @param request The request that is about to be handled.
065: * @param filters The whole filter list to be applied to the resource.
066: * @param i A pointer into the above array, indicating which filters
067: * have already been triggered (the one whose index are lower than
068: * <strong>i</strong>), and what filters have to be triggered (the one
069: * whose index are greater or equal to <strong>i+1</strong>).
070: * @return A Reply instance, if the filter did know how to answer
071: * the request without further processing, <strong>null</strong>
072: * otherwise.
073: * @exception ProtocolException If processing should be interrupted,
074: * because an abnormal situation occured.
075: */
076:
077: public ReplyInterface ingoingFilter(RequestInterface request,
078: FilterInterface filters[], int i) throws ProtocolException {
079: return ingoingFilter(request);
080: }
081:
082: /**
083: * Simplified API to the outgoing filter metho.
084: * This is a simplified API to the ougoing filter method, you can either
085: * overide this method, or the more powerfull one.
086: * @param request The original request.
087: * @param reply It's original reply.
088: * @return A Reply instance, or <strong>null</strong> if processing
089: * should continue normally.
090: * @exception ProtocolException If processing should be interrupted,
091: * because an abnormal situation occured.
092: */
093:
094: public ReplyInterface outgoingFilter(RequestInterface request,
095: ReplyInterface reply) throws ProtocolException {
096: return null;
097: }
098:
099: public ReplyInterface exceptionFilter(RequestInterface request,
100: ProtocolException ex, FilterInterface filters[], int i) {
101: return null;
102: }
103:
104: /**
105: * The outgoingFilter method.
106: * This method is the one that gets called by Jigsaw core. By default it
107: * will call the simpler <code>outgoingFilter</code> method that takes
108: * only the request and the reply as parameters.
109: * @param request The request that has been processed.
110: * @param reply The original reply as emitted by the resource.
111: * @param filters The whole filter that applies to the resource.
112: * @param i The current index of filters. The <em>i</em> filter is ourself,
113: * filters with lower indexes have already been applied, and filters with
114: * greater indexes are still to be applied.
115: * @return A Reply instance, if that filter know how to complete the
116: * request processing, or <strong>null</strong> if reminaing filters
117: * are to be called by Jigsaw engine.
118: * @exception ProtocolException If processing should be interrupted,
119: * because an abnormal situation occured.
120: */
121:
122: public ReplyInterface outgoingFilter(RequestInterface request,
123: ReplyInterface reply, FilterInterface filters[], int fidx)
124: throws ProtocolException {
125: ReplyInterface fr = outgoingFilter(request, reply);
126: return (fr != reply) ? fr : null;
127: }
128:
129: public OutputStream outputFilter(RequestInterface request,
130: ReplyInterface reply, OutputStream output) {
131: return output;
132: }
133:
134: /**
135: * Set the values. (MUST be called before initialize).
136: * @param defs The Hashtable containing the values.
137: */
138: public void pickleValues(Hashtable defs) {
139: Object nvalues[] = new Object[attributes.length];
140: for (int i = 0; i < nvalues.length; i++) {
141: String attrname = attributes[i].getName();
142: Object def = defs.get(attrname);
143: // note that protocol frames have usually names in the
144: // frame-XX serie, so it is valid to save (big) on this.
145: if ((def != null)
146: && ((i == ATTR_HELP_URL) || (i == ATTR_IDENTIFIER))
147: && (def instanceof String)) {
148: nvalues[i] = ((String) def).intern();
149: } else {
150: nvalues[i] = def;
151: }
152: }
153: this .values = nvalues;
154: }
155:
156: /**
157: * Initialization method for attribute holders.
158: * This method allows to initialize an attribute holder by providing
159: * its attributes values through a Hashtable mapping attribute names
160: * to attribute values.
161: * @param defs The Hashtable containing the default values.
162: */
163:
164: public void initialize(Hashtable defs) {
165: Object values[] = ((this .values == null) ? new Object[attributes.length]
166: : this .values);
167: for (int i = 0; i < values.length; i++) {
168: String attrname = attributes[i].getName();
169: Object def = defs.get(attrname);
170: if (values[i] == null) {
171: // for help_url, we can save lots of space by using
172: // String.intern()
173: if ((def != null)
174: && ((i == ATTR_HELP_URL) || (i == ATTR_IDENTIFIER))
175: && (def instanceof String)) {
176: values[i] = ((String) def).intern();
177: } else {
178: values[i] = def;
179: }
180: }
181: }
182: initialize(values);
183: }
184: }
|