KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 JavaDoc;
28 import org.xml.sax.Attributes JavaDoc;
29 import org.xml.sax.SAXException JavaDoc;
30
31 import java.io.OutputStream JavaDoc;
32
33 public class AssembleBookTask implements PublicationProcessTask {
34     private static final String JavaDoc PUBLISHER_NAMESPACE = "http://outerx.org/daisy/1.0#publisher";
35     private final String JavaDoc output;
36
37     public AssembleBookTask(String JavaDoc output) {
38         this.output = output;
39     }
40
41     public void run(PublicationContext context) throws Exception JavaDoc {
42         context.getPublicationLog().info("Running assemble book task.");
43         String JavaDoc outputPath = BookInstanceLayout.getPublicationOutputPath(context.getPublicationOutputName()) + output;
44         OutputStream JavaDoc 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 JavaDoc {
62         Section[] sections = sectionContainer.getSections();
63         for (int i = 0; i < sections.length; i++) {
64             if (sections[i].getBookStorePath() != null) {
65                 String JavaDoc 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 JavaDoc 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 JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc atts) throws SAXException JavaDoc {
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 JavaDoc("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 JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName) throws SAXException JavaDoc {
107             if (namespaceURI.equals(PUBLISHER_NAMESPACE) && localName.equals("daisyPreparedInclude")) {
108                 // ignore
109
} else {
110                 consumer.endElement(namespaceURI, localName, qName);
111             }
112         }
113     }
114 }
115
Popular Tags