KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > books > publisher > impl > publicationprocess > ShiftHeadersTask


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.io.OutputStream JavaDoc;
30 import java.util.Stack JavaDoc;
31 import java.util.regex.Pattern JavaDoc;
32 import java.util.regex.Matcher JavaDoc;
33
34 public class ShiftHeadersTask implements PublicationProcessTask {
35
36     public void run(PublicationContext context) throws Exception JavaDoc {
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 JavaDoc {
42         SAXParser JavaDoc 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                 // Shift the headers, catch result in a SaxBuffer
47
String JavaDoc 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 JavaDoc 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                 // Store the result
60
OutputStream JavaDoc 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 JavaDoc headerStack = new Stack JavaDoc();
74         private final int shiftAmount;
75         private static final Pattern JavaDoc 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 JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes atts) throws SAXException {
83             Matcher JavaDoc 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 JavaDoc(newLevel));
89             } else {
90                 consumer.startElement(namespaceURI, localName, qName, atts);
91             }
92         }
93
94         public void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName) throws SAXException {
95             Matcher JavaDoc matcher = headerPattern.matcher(localName);
96             if (matcher.matches() && namespaceURI.equals("")) {
97                 int level = ((Integer JavaDoc)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