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.xml.sax.*; 26 27 import javax.xml.parsers.SAXParser ; 28 import java.io.InputStream ; 29 import java.io.OutputStream ; 30 import java.util.Stack ; 31 import java.util.regex.Pattern ; 32 import java.util.regex.Matcher ; 33 34 public class ShiftHeadersTask implements PublicationProcessTask { 35 36 public void run(PublicationContext context) throws Exception { 37 context.getPublicationLog().info("Running shift headers task."); 38 processSections(context.getBook(), context, 0); 39 } 40 41 private void processSections(SectionContainer sectionContainer, PublicationContext context, int shiftLevel) throws Exception { 42 SAXParser parser = LocalSAXParserFactory.getSAXParserFactory().newSAXParser(); 43 Section[] sections = sectionContainer.getSections(); 44 for (int i = 0; i < sections.length; i++) { 45 if (sections[i].getBookStorePath() != null) { 46 String location = BookInstanceLayout.getDocumentInPublicationStorePath(sections[i].getBookStorePath(), context.getPublicationOutputName()); 48 SaxBuffer buffer = new SaxBuffer(); 49 HeaderShifterHandler headerShifterHandler = new HeaderShifterHandler(buffer, shiftLevel); 50 parser.getXMLReader().setContentHandler(headerShifterHandler); 51 InputStream is = context.getBookInstance().getResource(location); 52 try { 53 InputSource inputSource = new InputSource(is); 54 parser.getXMLReader().parse(inputSource); 55 } finally { 56 is.close(); 57 } 58 59 OutputStream os = context.getBookInstance().getResourceOutputStream(location); 61 try { 62 XmlSerializer xmlSerializer = new XmlSerializer(os); 63 buffer.toSAX(xmlSerializer); 64 } finally { 65 os.close(); 66 } 67 } 68 processSections(sections[i], context, shiftLevel + 1); 69 } 70 } 71 72 static class HeaderShifterHandler extends AbstractContentHandler { 73 private Stack headerStack = new Stack (); 74 private final int shiftAmount; 75 private static final Pattern headerPattern = Pattern.compile("h([0-9]+)"); 76 77 public HeaderShifterHandler(ContentHandler consumer, int shiftAmount) { 78 super(consumer); 79 this.shiftAmount = shiftAmount; 80 } 81 82 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { 83 Matcher matcher = headerPattern.matcher(localName); 84 if (matcher.matches() && namespaceURI.equals("")) { 85 int currentLevel = Integer.parseInt(matcher.group(1)); 86 int newLevel = currentLevel + shiftAmount + 1; 87 consumer.startElement(namespaceURI, "h" + newLevel, "h" + newLevel, atts); 88 headerStack.push(new Integer (newLevel)); 89 } else { 90 consumer.startElement(namespaceURI, localName, qName, atts); 91 } 92 } 93 94 public void endElement(String namespaceURI, String localName, String qName) throws SAXException { 95 Matcher matcher = headerPattern.matcher(localName); 96 if (matcher.matches() && namespaceURI.equals("")) { 97 int level = ((Integer )headerStack.pop()).intValue(); 98 consumer.endElement(namespaceURI, "h" + level, "h" + level); 99 } else { 100 consumer.endElement(namespaceURI, localName, qName); 101 } 102 } 103 } 104 } 105 | Popular Tags |