1 16 package org.outerj.daisy.books.publisher.impl.publicationprocess; 17 18 19 import org.xml.sax.*; 20 import org.xml.sax.helpers.NamespaceSupport ; 21 import org.apache.cocoon.xml.SaxBuffer; 22 import org.outerj.daisy.frontend.PreparedDocuments; 23 import org.outerj.daisy.repository.VariantKey; 24 25 import javax.xml.parsers.SAXParserFactory ; 26 import javax.xml.parsers.SAXParser ; 27 import java.io.InputStream ; 28 import java.util.Enumeration ; 29 30 public class PreparedDocumentsBuilder { 31 private static final String PUBLISHER_NAMESPACE = "http://outerx.org/daisy/1.0#publisher"; 32 33 public static PreparedDocuments build(InputStream is) throws Exception { 34 SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); 35 saxParserFactory.setNamespaceAware(true); 36 saxParserFactory.setValidating(false); 37 SAXParser saxParser = saxParserFactory.newSAXParser(); 38 39 PreparedDocumentsHandler preparedDocumentsHandler = new PreparedDocumentsHandler(); 40 XMLReader xmlReader = saxParser.getXMLReader(); 41 xmlReader.setContentHandler(preparedDocumentsHandler); 42 43 InputSource inputSource = new InputSource(is); 44 xmlReader.parse(inputSource); 45 46 PreparedDocuments preparedDocuments = preparedDocumentsHandler.getPreparedDocuments(); 47 if (preparedDocuments.getPreparedDocument(1) == null) { 48 throw new Exception ("Invalid preparedDocuments, there is no prepared document with ID 1."); 49 } 50 return preparedDocuments; 51 } 52 53 private static class PreparedDocumentsHandler implements ContentHandler { 54 private int elementNesting = 0; 55 private int currentPreparedDocumentId; 56 private VariantKey documentKey; 57 private SaxBuffer buffer; 58 private NamespaceSupport namespaceSupport = new NamespaceSupport (); 59 private PreparedDocuments preparedDocuments = new PreparedDocuments(); 60 61 public PreparedDocuments getPreparedDocuments() { 62 return preparedDocuments; 63 } 64 65 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { 66 if (elementNesting < 2) 67 namespaceSupport.pushContext(); 68 69 if (elementNesting == 0) { 70 if (!(namespaceURI.equals(PUBLISHER_NAMESPACE) && localName.equals("preparedDocuments"))) { 71 throw new SAXException("Expect as root element \"p:preparedDocuments\" but got \"" + qName + "\"."); 72 } 73 } else if (elementNesting == 1) { 74 if (namespaceURI.equals(PUBLISHER_NAMESPACE) && localName.equals("preparedDocument")) { 75 currentPreparedDocumentId = Integer.parseInt(atts.getValue("id")); 76 long documentId = Long.parseLong(atts.getValue("documentId")); 77 long branchId = Long.parseLong(atts.getValue("branchId")); 78 long languageId = Long.parseLong(atts.getValue("languageId")); 79 documentKey = new VariantKey(documentId, branchId, languageId); 80 buffer = new SaxBuffer(); 81 buffer.startDocument(); 82 Enumeration prefixEnum = namespaceSupport.getPrefixes(); 84 while (prefixEnum.hasMoreElements()) { 85 String prefix = (String )prefixEnum.nextElement(); 86 if (!prefix.equals("xml")) 87 buffer.startPrefixMapping(prefix, namespaceSupport.getURI(prefix)); 88 } 89 } else { 90 throw new SAXException("Encountered an unexpected element in preparedDocuments: \"" + qName + "\"."); 91 } 92 } else if (elementNesting >= 2) { 93 buffer.startElement(namespaceURI, localName, qName, atts); 94 } 95 elementNesting++; 96 } 97 98 public void endElement(String namespaceURI, String localName, String qName) throws SAXException { 99 elementNesting--; 100 if (elementNesting == 1) { 101 buffer.endDocument(); 102 preparedDocuments.putPreparedDocument(currentPreparedDocumentId, documentKey, buffer); 103 buffer = null; 104 documentKey = null; 105 } else if (elementNesting >= 2) { 106 buffer.endElement(namespaceURI, localName, qName); 107 } 108 } 109 110 public void endDocument() throws SAXException { 111 } 113 114 public void startDocument() throws SAXException { 115 } 117 118 public void characters(char ch[], int start, int length) throws SAXException { 119 if (elementNesting >= 2) 120 buffer.characters(ch, start, length); 121 } 122 123 public void ignorableWhitespace(char ch[], int start, int length) throws SAXException { 124 if (elementNesting >= 2) 125 buffer.ignorableWhitespace(ch, start, length); 126 } 127 128 public void endPrefixMapping(String prefix) throws SAXException { 129 if (elementNesting >= 2) 130 buffer.endPrefixMapping(prefix); 131 } 132 133 public void skippedEntity(String name) throws SAXException { 134 if (elementNesting >= 2) 135 buffer.skippedEntity(name); 136 } 137 138 public void setDocumentLocator(Locator locator) { 139 if (elementNesting >= 2) 140 buffer.setDocumentLocator(locator); 141 } 142 143 public void processingInstruction(String target, String data) throws SAXException { 144 if (elementNesting >= 2) 145 buffer.processingInstruction(target, data); 146 } 147 148 public void startPrefixMapping(String prefix, String uri) throws SAXException { 149 if (elementNesting >= 2) 150 buffer.startPrefixMapping(prefix, uri); 151 else 152 namespaceSupport.declarePrefix(prefix, uri); 153 } 154 } 155 } 156 | Popular Tags |