1 16 package org.outerj.daisy.books.publisher.impl.publicationprocess; 17 18 import org.outerj.daisy.books.publisher.impl.bookmodel.SectionContainer; 19 import org.outerj.daisy.books.publisher.impl.bookmodel.Section; 20 import org.outerj.daisy.books.publisher.impl.BookInstanceLayout; 21 import org.outerj.daisy.books.publisher.impl.util.AbstractContentHandler; 22 import org.outerj.daisy.xmlutil.XmlSerializer; 23 import org.outerj.daisy.xmlutil.HtmlBodyRemovalHandler; 24 import org.outerj.daisy.frontend.PreparedDocuments; 25 import org.apache.cocoon.xml.AttributesImpl; 26 import org.apache.cocoon.xml.SaxBuffer; 27 import org.xml.sax.ContentHandler ; 28 import org.xml.sax.Attributes ; 29 import org.xml.sax.SAXException ; 30 31 import java.io.OutputStream ; 32 33 public class AssembleBookTask implements PublicationProcessTask { 34 private static final String PUBLISHER_NAMESPACE = "http://outerx.org/daisy/1.0#publisher"; 35 private final String output; 36 37 public AssembleBookTask(String output) { 38 this.output = output; 39 } 40 41 public void run(PublicationContext context) throws Exception { 42 context.getPublicationLog().info("Running assemble book task."); 43 String outputPath = BookInstanceLayout.getPublicationOutputPath(context.getPublicationOutputName()) + output; 44 OutputStream os = context.getBookInstance().getResourceOutputStream(outputPath); 45 try { 46 XmlSerializer xmlSerializer = new XmlSerializer(os); 47 xmlSerializer.startDocument(); 48 xmlSerializer.startElement("", "html", "html", new AttributesImpl()); 49 xmlSerializer.startElement("", "body", "body", new AttributesImpl()); 50 51 processSections(context.getBook(), context, xmlSerializer, 1); 52 53 xmlSerializer.endElement("", "body", "body"); 54 xmlSerializer.endElement("", "html", "html"); 55 xmlSerializer.endDocument(); 56 } finally { 57 os.close(); 58 } 59 } 60 61 private void processSections(SectionContainer sectionContainer, PublicationContext context, ContentHandler consumer, int level) throws Exception { 62 Section[] sections = sectionContainer.getSections(); 63 for (int i = 0; i < sections.length; i++) { 64 if (sections[i].getBookStorePath() != null) { 65 String location = BookInstanceLayout.getDocumentInPublicationStorePath(sections[i].getBookStorePath(), context.getPublicationOutputName()); 66 PreparedDocuments preparedDocuments = PreparedDocumentsBuilder.build(context.getBookInstance().getResource(location)); 67 PreparedIncludeMerger preparedIncludeMerger = new PreparedIncludeMerger(consumer, preparedDocuments); 68 SaxBuffer buffer = preparedDocuments.getPreparedDocument(1).getSaxBuffer(); 69 buffer.toSAX(new HtmlBodyRemovalHandler(preparedIncludeMerger)); 70 } else if (sections[i].getTitle() != null) { 71 String headerTag = "h" + level; 72 AttributesImpl attrs = new AttributesImpl(); 73 if (sections[i].getType() != null) { 74 attrs.addCDATAAttribute("daisySectionType", sections[i].getType()); 75 } 76 consumer.startElement("", headerTag, headerTag, attrs); 77 char[] title = sections[i].getTitle().toCharArray(); 78 consumer.characters(title, 0, title.length); 79 consumer.endElement("", headerTag, headerTag); 80 } 81 processSections(sections[i], context, consumer, level + 1); 82 } 83 } 84 85 static class PreparedIncludeMerger extends AbstractContentHandler { 86 private final PreparedDocuments preparedDocuments; 87 88 public PreparedIncludeMerger(ContentHandler consumer, PreparedDocuments preparedDocuments) { 89 super(consumer); 90 this.consumer = consumer; 91 this.preparedDocuments = preparedDocuments; 92 } 93 94 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { 95 if (namespaceURI.equals(PUBLISHER_NAMESPACE) && localName.equals("daisyPreparedInclude")) { 96 int id = Integer.parseInt(atts.getValue("id")); 97 PreparedDocuments.PreparedDocument preparedDocument = preparedDocuments.getPreparedDocument(id); 98 if (preparedDocument == null) 99 throw new SAXException ("Missing preparedDocument: " + id); 100 preparedDocument.getSaxBuffer().toSAX(new HtmlBodyRemovalHandler(this)); 101 } else { 102 consumer.startElement(namespaceURI, localName, qName, atts); 103 } 104 } 105 106 public void endElement(String namespaceURI, String localName, String qName) throws SAXException { 107 if (namespaceURI.equals(PUBLISHER_NAMESPACE) && localName.equals("daisyPreparedInclude")) { 108 } else { 110 consumer.endElement(namespaceURI, localName, qName); 111 } 112 } 113 } 114 } 115 | Popular Tags |