1 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 ; 21 import org.xml.sax.SAXException ; 22 import org.xml.sax.helpers.AttributesImpl ; 23 import org.outerj.daisy.repository.VariantKey; 24 25 import java.util.Map ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 29 33 public class PreparedDocuments { 34 private Map preparedDocumentsById = new HashMap (); 35 private static final String 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 (id), new PreparedDocument(content, id, documentKey)); 39 } 40 41 44 public PreparedDocument getPreparedDocument(int id) { 45 return (PreparedDocument)preparedDocumentsById.get(new Integer (id)); 46 } 47 48 public int[] getPreparedDocumentIds() { 49 int[] ids = new int[preparedDocumentsById.size()]; 50 Iterator it = preparedDocumentsById.keySet().iterator(); 51 int i = 0; 52 while (it.hasNext()) { 53 ids[i++] = ((Integer )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 contentHandler) throws SAXException { 83 AttributesImpl rootAttrs = new AttributesImpl (); 84 contentHandler.startElement(PUBLISHER_NAMESPACE, "preparedDocuments", "p:preparedDocuments", rootAttrs); 85 86 Iterator documentsIt = preparedDocumentsById.values().iterator(); 87 while (documentsIt.hasNext()) { 88 PreparedDocument preparedDocument = (PreparedDocument)documentsIt.next(); 89 AttributesImpl attrs = new AttributesImpl (); 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 |