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.LocalSAXParserFactory; 24 import org.apache.cocoon.xml.SaxBuffer; 25 import org.apache.cocoon.xml.AttributesImpl; 26 import org.xml.sax.InputSource ; 27 import org.xml.sax.ContentHandler ; 28 import org.xml.sax.Attributes ; 29 import org.xml.sax.SAXException ; 30 31 import javax.xml.parsers.SAXParser ; 32 import java.io.InputStream ; 33 import java.io.OutputStream ; 34 35 public class AddSectionTypesTask implements PublicationProcessTask { 36 private static final String PUBLISHER_NAMESPACE = "http://outerx.org/daisy/1.0#publisher"; 37 38 public void run(PublicationContext context) throws Exception { 39 context.getPublicationLog().info("Running add section type task."); 40 processSections(context.getBook(), context, 0); 41 } 42 43 private void processSections(SectionContainer sectionContainer, PublicationContext context, int shiftLevel) throws Exception { 44 SAXParser parser = LocalSAXParserFactory.getSAXParserFactory().newSAXParser(); 45 Section[] sections = sectionContainer.getSections(); 46 for (int i = 0; i < sections.length; i++) { 47 if (sections[i].getType() != null && sections[i].getBookStorePath() != null) { 48 String location = BookInstanceLayout.getDocumentInPublicationStorePath(sections[i].getBookStorePath(), context.getPublicationOutputName()); 50 SaxBuffer buffer = new SaxBuffer(); 51 AddSectionTypeHandler addSectionTypeHandler = new AddSectionTypeHandler(buffer, sections[i].getType()); 52 parser.getXMLReader().setContentHandler(addSectionTypeHandler); 53 InputStream is = context.getBookInstance().getResource(location); 54 try { 55 InputSource inputSource = new InputSource (is); 56 parser.getXMLReader().parse(inputSource); 57 } finally { 58 is.close(); 59 } 60 61 OutputStream os = context.getBookInstance().getResourceOutputStream(location); 63 try { 64 XmlSerializer xmlSerializer = new XmlSerializer(os); 65 buffer.toSAX(xmlSerializer); 66 } finally { 67 os.close(); 68 } 69 } 70 processSections(sections[i], context, shiftLevel + 1); 71 } 72 } 73 74 static class AddSectionTypeHandler extends AbstractContentHandler { 75 private final String sectionType; 76 private boolean inPreparedDocument = false; 77 78 public AddSectionTypeHandler(ContentHandler consumer, String sectionType) { 79 super(consumer); 80 this.sectionType = sectionType; 81 } 82 83 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { 84 if (!inPreparedDocument && namespaceURI.equals(PUBLISHER_NAMESPACE) && localName.equals("preparedDocument") 85 && atts.getValue("id").equals("1")) { 86 inPreparedDocument = true; 87 } else if (namespaceURI.equals("") && localName.equals("h0")) { 88 AttributesImpl newAttrs = new AttributesImpl(atts); 89 newAttrs.addCDATAAttribute("daisySectionType", sectionType); 90 atts = newAttrs; 91 } 92 super.startElement(namespaceURI, localName, qName, atts); 93 } 94 95 public void endElement(String namespaceURI, String localName, String qName) throws SAXException { 96 if (inPreparedDocument && namespaceURI.equals(PUBLISHER_NAMESPACE) && localName.equals("preparedDocument")) 97 inPreparedDocument = false; 98 99 super.endElement(namespaceURI, localName, qName); 100 } 101 102 } 103 } 104 | Popular Tags |