KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > frontend > components > docbasket > DocumentBasket


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.frontend.components.docbasket;
17
18 import org.apache.excalibur.xml.sax.XMLizable;
19 import org.apache.cocoon.xml.AttributesImpl;
20 import org.apache.commons.collections.set.ListOrderedSet;
21 import org.xml.sax.ContentHandler JavaDoc;
22 import org.xml.sax.SAXException JavaDoc;
23
24 import java.util.*;
25
26 public class DocumentBasket implements XMLizable {
27     private ListOrderedSet entries = new ListOrderedSet();
28     private long updateCount = 0;
29     private static final int MAX_SIZE = 500;
30
31     public synchronized List getEntries() {
32         return new ArrayList(entries);
33     }
34
35     public synchronized void appendEntry(DocumentBasketEntry entry) {
36         if (entries.size() >= MAX_SIZE)
37             throw new RuntimeException JavaDoc("Your document basket has reached the maximal allowed size, " + MAX_SIZE + ". Please remove some documents before adding more.");
38
39         entries.add(entry);
40
41         updateCount++;
42     }
43
44     public synchronized void appendEntries(DocumentBasketEntry[] newEntries) {
45         for (int i = 0; i < newEntries.length; i++) {
46             appendEntry(newEntries[i]);
47         }
48     }
49
50     public synchronized void clear() {
51         entries.clear();
52         updateCount++;
53     }
54
55     public synchronized int size() {
56         return entries.size();
57     }
58
59     public synchronized long getUpdateCount() {
60         return updateCount;
61     }
62
63     public synchronized void setEntries(List entries) {
64         if (entries.size() > MAX_SIZE)
65             throw new RuntimeException JavaDoc("The document basket cannot contain more then " + MAX_SIZE + " documents.");
66         this.entries.clear();
67         this.entries.addAll(entries);
68         updateCount++;
69     }
70
71     public synchronized void toSAX(ContentHandler JavaDoc contentHandler) throws SAXException JavaDoc {
72         AttributesImpl attrs = new AttributesImpl();
73         attrs.addCDATAAttribute("size", String.valueOf(entries.size()));
74         attrs.addCDATAAttribute("updateCount", String.valueOf(updateCount));
75
76         contentHandler.startElement("", "documentBasket", "documentBasket", attrs);
77
78         Iterator entriesIt = entries.iterator();
79         while (entriesIt.hasNext()) {
80             DocumentBasketEntry entry = (DocumentBasketEntry)entriesIt.next();
81             entry.toSAX(contentHandler);
82         }
83
84         contentHandler.endElement("", "documentBasket", "documentBasket");
85     }
86 }
87
Popular Tags