// HttpBag.java
// $Id: HttpBag.java,v 1.1 1997/07/21 21:55:33 eric Exp $
// COPYRIGHT
package w3c.model.www.pep.altlib;
import java.util.*;
//import w3c.util.*;
/**
* Internal representation of protocol headers conforming to the bag spec.
* The <strong>bag</strong> specification is part of the Protocol Extension
* Protocol defined by w3c, it can be found
* <a href="http://www.w3.org/hypertext/WWW/TR/WD-http-pep.html>here</a>.
*/
public class HttpBag extends HttpRawBag
ArrayDictionary items = null;
protected void complete() {}
protected HttpRawBaglookupSubBag( Stringname) throws HttpParserException{
HttpBagbag = HttpBag(true, name);
items.put(bag.name, bag);
return bag;
}
public void addElement( Stringname) throws HttpParserException{
items.put(name, Boolean.TRUE);
}
protected final void updateByteValue() {
HttpBufferbuf = HttpBuffer();
if ( isToplevel )) {
// Dump this bag as a list of bags:
Enumeratione = items.elements();
booleans = true;
while ( e.hasMoreElements() )) {
HttpBagbag = (HttpBag) e.nextElement();
// Append separator if needed:
if ( s ))
s = false;
elsebuf.append((byte)',');
// Dump the bag now:
bag.appendValue(buf);
}
} else {
buf.append((byte) '{');
buf.append(name);
buf.append((byte) '');
// Append all items:
Enumeratione = items.keys();
booleans = true;
while (e.hasMoreElements())) {
// Append separator after first item only:
if ( s ))
s = false;
elsebuf.append((byte) '');
// Dump the item:
Stringname = (String) e.nextElement();
Objectvalue = items.get(name);
if ( valueinstanceofBoolean )) {
buf.append(name);
} elseif ( valueinstanceofHttpBag )) {
((HttpBag) value).appendValue(buf);
} else {
Stringmsg = "Invalid bag item value, key=""+name+"".";
throw HttpInvalidValueException(msg);
}
}
buf.append((byte) '}');
}
raw = buf.getByteCopy();
roff = 0;
rlen = raw.length;
}
/**
* Add a named bag into this bag.
* @param bag The bag to add (in case item is a bag).
*/
/* public void addBag (HttpBag subbag) {
validate();
items.put (subbag.getName(), subbag) ;
}
*/
/**
* Does this bag have a named sub-bag of the given name ?
* @param name The name of the sub-bag to be tested for.
* @return <strong>true</strong> if this sub-bag exists.
*/
public boolean hasBag ( Stringname) {
validate();
Objectitem = items.get (name) ;
if ( (item != null) && (iteminstanceofHttpBag) ))
return true ;
return false ;
}
/**
* Get a named sub-bag from this bag.
* @param name The name of the sub-bag to get.
* @return A bag instance, or <strong>null</strong> if none was found.
*/
public HttpBag getBag ( Stringname) {
validate();
if ( hasBag (name)))
return ( (HttpBag) items.get (name) ;
return null ;
}
/**
* Add an item into this bag.
* @param name The new item name.
*/
public void addItem ( Stringname) throws HttpParserException{
validate();
if (!verifyAtom(name)))
throw HttpParserException("atom ""+name+"" is illegal in this context.");
items.put (name, Boolean.TRUE);
}
/**
* Default atom verifyer allows all atoms.
* @param name The atom to verify.
*/
public boolean verifyAtom ( Stringname) {
return true;
}
/**
* Add a sub-bag to this bag.
* @param subbag The sub-bag to add.
*/
public void addItem( HttpBagsubbag) {
validate();
items.put(subbag.getName(), subbag);
}
/**
* Does this bag contains the given item, being a bag or a simple word.
* @param name The name of the item to test.
* @return <strong>true</strong> if the bag contains the given item,
* <strong>false</strong> otherwise.
*/
public boolean hasItem ( Stringname) {
validate();
return items.get (name) != null ;
}
/**
* Remove an item from that bag.
* @param name The name of the item to remove.
*/
public void removeItem( Stringname) {
validate();
items.remove(name);
}
/**
* Get all named items from this bag.
* This include both named sub-bags and items by their own.
*/
public Enumeration keys () {
validate();
return items.keys() ;
}
/**
* Is that a top level bag or not ?
* @return A boolean, <strong>true</strong> if the bag was a toplevel
* bag.
*/
public boolean isToplevelBag() {
return isToplevel;
}
public HttpBag() {
super();
this.items = ArrayDictionary(5, 5);
}
HttpBag( Stringname) {
super(name);
this.items = ArrayDictionary(5, 5);
}
HttpBag( booleanisValid, Stringname) {
super(isValid, name);
this.items = ArrayDictionary(5, 5);
}