KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > frontend > PreparedDocuments


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;
17
18 import org.apache.cocoon.xml.SaxBuffer;
19 import org.apache.cocoon.xml.IncludeXMLConsumer;
20 import org.xml.sax.ContentHandler JavaDoc;
21 import org.xml.sax.SAXException JavaDoc;
22 import org.xml.sax.helpers.AttributesImpl JavaDoc;
23 import org.outerj.daisy.repository.VariantKey;
24
25 import java.util.Map JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28
29 /**
30  * Holds a SaxBuffer for each of the prepared documents.
31  *
32  */

33 public class PreparedDocuments {
34     private Map JavaDoc preparedDocumentsById = new HashMap JavaDoc();
35     private static final String JavaDoc PUBLISHER_NAMESPACE = "http://outerx.org/daisy/1.0#publisher";
36
37     public void putPreparedDocument(int id, VariantKey documentKey, SaxBuffer content) {
38         preparedDocumentsById.put(new Integer JavaDoc(id), new PreparedDocument(content, id, documentKey));
39     }
40
41     /**
42      * The buffer includes startDocument and endDocument events.
43      */

44     public PreparedDocument getPreparedDocument(int id) {
45         return (PreparedDocument)preparedDocumentsById.get(new Integer JavaDoc(id));
46     }
47
48     public int[] getPreparedDocumentIds() {
49         int[] ids = new int[preparedDocumentsById.size()];
50         Iterator JavaDoc it = preparedDocumentsById.keySet().iterator();
51         int i = 0;
52         while (it.hasNext()) {
53             ids[i++] = ((Integer JavaDoc)it.next()).intValue();
54         }
55         return ids;
56     }
57
58     public static class PreparedDocument {
59         private VariantKey documentKey;
60         private SaxBuffer saxBuffer;
61         private int id;
62
63         public PreparedDocument(SaxBuffer saxBuffer, int id, VariantKey documentKey) {
64             this.saxBuffer = saxBuffer;
65             this.id = id;
66             this.documentKey = documentKey;
67         }
68
69         public SaxBuffer getSaxBuffer() {
70             return saxBuffer;
71         }
72
73         public int getId() {
74             return id;
75         }
76
77         public VariantKey getDocumentKey() {
78             return documentKey;
79         }
80     }
81
82     public void generateSax(ContentHandler JavaDoc contentHandler) throws SAXException JavaDoc {
83         AttributesImpl JavaDoc rootAttrs = new AttributesImpl JavaDoc();
84         contentHandler.startElement(PUBLISHER_NAMESPACE, "preparedDocuments", "p:preparedDocuments", rootAttrs);
85
86         Iterator JavaDoc documentsIt = preparedDocumentsById.values().iterator();
87         while (documentsIt.hasNext()) {
88             PreparedDocument preparedDocument = (PreparedDocument)documentsIt.next();
89             AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
90             attrs.addAttribute("", "id", "id", "CDATA", String.valueOf(preparedDocument.getId()));
91             VariantKey documentKey = preparedDocument.getDocumentKey();
92             attrs.addAttribute("", "documentId", "documentId", "CDATA", String.valueOf(documentKey.getDocumentId()));
93             attrs.addAttribute("", "branchId", "branchId", "CDATA", String.valueOf(documentKey.getBranchId()));
94             attrs.addAttribute("", "languageId", "languageId", "CDATA", String.valueOf(documentKey.getLanguageId()));
95
96             contentHandler.startElement(PUBLISHER_NAMESPACE, "preparedDocument", "p:preparedDocument", attrs);
97             preparedDocument.getSaxBuffer().toSAX(new IncludeXMLConsumer(contentHandler));
98             contentHandler.endElement(PUBLISHER_NAMESPACE, "preparedDocument", "p:preparedDocument");
99         }
100
101         contentHandler.endElement(PUBLISHER_NAMESPACE, "preparedDocuments", "p:preparedDocuments");
102     }
103
104 }
105
Popular Tags