001: /*
002: * Time-stamp: <01/07/16 16:11:29 smulloni>
003: * $Id: IfHeader.java,v 1.1 2001/07/17 04:41:32 smulloni Exp $
004: *
005: * Copyright (c) 2001, Jacob Smullyan.
006: *
007: * This is part of SkunkDAV, a WebDAV client. See http://skunkdav.sourceforge.net/
008: * for the latest version.
009: *
010: * SkunkDAV is free software; you can redistribute it and/or
011: * modify it under the terms of the GNU General Public License as published
012: * by the Free Software Foundation; either version 2, or (at your option)
013: * any later version.
014: *
015: * SkunkDAV is distributed in the hope that it will be useful,
016: * but WITHOUT ANY WARRANTY; without even the implied warranty of
017: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
018: * General Public License for more details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with SkunkDAV; see the file COPYING. If not, write to the Free
022: * Software Foundation, 59 Temple Place - Suite 330, Boston, MA
023: * 02111-1307, USA.
024: */
025:
026: package org.skunk.dav.client;
027:
028: import java.util.ArrayList;
029: import java.util.HashMap;
030: import java.util.Iterator;
031: import java.util.Map;
032:
033: /**
034: * represents the RFC2518 If HTTP header.
035: * You need this if you want to perform operations
036: * on locked resources.
037: */
038: public class IfHeader {
039: private HashMap resourceTokenMap = null;
040: private ArrayList stateTokenList = null;
041: private boolean tagged;
042:
043: public IfHeader(boolean tagged) {
044: this .tagged = tagged;
045: }
046:
047: public IfHeader(Map resourceTokenMap) {
048: this (true);
049: this .resourceTokenMap = new HashMap();
050: this .resourceTokenMap.putAll(resourceTokenMap);
051: }
052:
053: public IfHeader(String[] stateTokens) {
054: this (false);
055: this .stateTokenList = new ArrayList();
056: for (int i = 0; i < stateTokens.length; i++)
057: this .stateTokenList.add(stateTokens[i]);
058: }
059:
060: public void addStateToken(String token) {
061: if (this .tagged) {
062: throw new UnsupportedOperationException(
063: "method not allowed with a tagged list If header");
064: }
065: if (stateTokenList == null)
066: stateTokenList = new ArrayList();
067: stateTokenList.add(token);
068: }
069:
070: public void addStateToken(String resourceURL, String token) {
071: if (!this .tagged) {
072: throw new UnsupportedOperationException(
073: "method not allowed with non-tagged list If header");
074: }
075: if (resourceTokenMap == null)
076: resourceTokenMap = new HashMap();
077: if (!resourceTokenMap.containsKey(resourceURL))
078: resourceTokenMap.put(resourceURL, token);
079: else {
080: Object val = resourceTokenMap.get(resourceURL);
081: if (val instanceof ArrayList)
082: ((ArrayList) val).add(token);
083: else {
084: ArrayList newVal = new ArrayList();
085: newVal.add(val);
086: newVal.add(token);
087: resourceTokenMap.put(resourceURL, newVal);
088: }
089:
090: }
091: }
092:
093: public boolean isTagged() {
094: return this .tagged;
095: }
096:
097: public boolean isEmpty() {
098: if (this .tagged) {
099: return this .resourceTokenMap == null
100: || this .resourceTokenMap.isEmpty();
101: }
102: return this .stateTokenList == null
103: || stateTokenList.size() == 0;
104: }
105:
106: public String toString() {
107: if (this .isEmpty())
108: return "";
109: StringBuffer sb = new StringBuffer();
110: if (this .tagged) {
111: for (Iterator it = this .resourceTokenMap.keySet()
112: .iterator(); it.hasNext();) {
113: Object key = it.next();
114: Object val = resourceTokenMap.get(key);
115: sb.append('<');
116: sb.append(key);
117: sb.append("> ");
118: if (val instanceof ArrayList) {
119: ArrayList vallist = (ArrayList) val;
120: for (Iterator it2 = vallist.iterator(); it2
121: .hasNext();) {
122: sb.append("(<");
123: sb.append(it2.next());
124: sb.append(">) ");
125: }
126: } else {
127: sb.append("(<");
128: sb.append(val);
129: sb.append(">) ");
130: }
131: }
132: } else {
133: for (Iterator it = this .stateTokenList.iterator(); it
134: .hasNext();) {
135: sb.append("(<");
136: sb.append(it.next());
137: sb.append(">) ");
138: }
139: }
140: return sb.toString();
141: }
142: }
143:
144: /* $Log: IfHeader.java,v $
145: /* Revision 1.1 2001/07/17 04:41:32 smulloni
146: /* integrated IfHeader more closely into method package; added lock test.
147: /* */
|