KickJava   Java API By Example, From Geeks To Geeks.

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


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.apache.cocoon.xml.AttributesImpl;
26 import org.xml.sax.InputSource JavaDoc;
27 import org.xml.sax.ContentHandler JavaDoc;
28 import org.xml.sax.Attributes JavaDoc;
29 import org.xml.sax.SAXException JavaDoc;
30
31 import javax.xml.parsers.SAXParser JavaDoc;
32 import java.io.InputStream JavaDoc;
33 import java.io.OutputStream JavaDoc;
34
35 public class AddSectionTypesTask implements PublicationProcessTask {
36     private static final String JavaDoc PUBLISHER_NAMESPACE = "http://outerx.org/daisy/1.0#publisher";
37
38     public void run(PublicationContext context) throws Exception JavaDoc {
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 JavaDoc {
44         SAXParser JavaDoc 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                 // Add the section type, catch result in a SaxBuffer
49
String JavaDoc 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 JavaDoc is = context.getBookInstance().getResource(location);
54                 try {
55                     InputSource JavaDoc inputSource = new InputSource JavaDoc(is);
56                     parser.getXMLReader().parse(inputSource);
57                 } finally {
58                     is.close();
59                 }
60
61                 // Store the result
62
OutputStream JavaDoc 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 JavaDoc sectionType;
76         private boolean inPreparedDocument = false;
77
78         public AddSectionTypeHandler(ContentHandler consumer, String JavaDoc sectionType) {
79             super(consumer);
80             this.sectionType = sectionType;
81         }
82
83         public void startElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc atts) throws SAXException JavaDoc {
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 JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName) throws SAXException JavaDoc {
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