KickJava   Java API By Example, From Geeks To Geeks.

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


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.BookInstanceLayout;
19 import org.outerj.daisy.xmlutil.XmlSerializer;
20 import org.outerj.daisy.xmlutil.LocalSAXParserFactory;
21 import org.xml.sax.*;
22
23 import javax.xml.parsers.SAXParser JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.util.Set JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.Iterator JavaDoc;
30
31 public class WriteChunksTask implements PublicationProcessTask {
32     private final String JavaDoc chunkFileExtension;
33     private final String JavaDoc outputPrefix;
34     private final String JavaDoc input;
35     private final String JavaDoc applyPipeline;
36     private final String JavaDoc pipelineOutputPrefix;
37     private final String JavaDoc chunkAfterPipelineFileExtension;
38
39     public WriteChunksTask(String JavaDoc input, String JavaDoc outputPrefix, String JavaDoc chunkFileExtension, String JavaDoc applyPipeline,
40                            String JavaDoc pipelineOutputPrefix, String JavaDoc chunkAfterPipelineFileExtension) {
41         this.input = input;
42         this.outputPrefix = outputPrefix;
43         this.chunkFileExtension = chunkFileExtension;
44         this.applyPipeline = applyPipeline;
45         this.pipelineOutputPrefix = pipelineOutputPrefix;
46         this.chunkAfterPipelineFileExtension = chunkAfterPipelineFileExtension != null ? chunkAfterPipelineFileExtension : "";
47     }
48
49     public void run(PublicationContext context) throws Exception JavaDoc {
50         context.getPublicationLog().info("Running write chunks task.");
51
52         String JavaDoc publicationOutputPath = BookInstanceLayout.getPublicationOutputPath(context.getPublicationOutputName());
53         String JavaDoc inputXml = publicationOutputPath + input;
54
55         InputStream JavaDoc is = null;
56         ChunkWriterHandler chunkWriter = null;
57         try {
58             SAXParser JavaDoc parser = LocalSAXParserFactory.getSAXParserFactory().newSAXParser();
59             chunkWriter = new ChunkWriterHandler(context);
60             parser.getXMLReader().setContentHandler(chunkWriter);
61             is = context.getBookInstance().getResource(inputXml);
62             parser.getXMLReader().parse(new InputSource(is));
63         } finally {
64             if (is != null)
65                 is.close();
66             if (chunkWriter != null)
67                 chunkWriter.dispose();
68         }
69
70         // Optionally apply a pipeline to each of the written chunks and write results to new files
71
if (applyPipeline != null) {
72             Iterator JavaDoc writtenChunksIt = chunkWriter.getWrittenChunks().iterator();
73             while (writtenChunksIt.hasNext()) {
74                 String JavaDoc chunkName = (String JavaDoc)writtenChunksIt.next();
75                 String JavaDoc input = publicationOutputPath + outputPrefix + chunkName + chunkFileExtension;
76                 String JavaDoc output = publicationOutputPath + pipelineOutputPrefix + chunkName + chunkAfterPipelineFileExtension;
77                 ApplyPipelineTask.applyPipeline(context, applyPipeline, input, output);
78             }
79         }
80     }
81
82     class ChunkWriterHandler implements ContentHandler {
83         private final PublicationContext context;
84         private OutputStream JavaDoc os;
85         private XmlSerializer consumer;
86         private int nesting = 0;
87         private Set JavaDoc writtenChunks = new HashSet JavaDoc();
88
89         public ChunkWriterHandler(PublicationContext context) {
90             this.context = context;
91         }
92
93         public void dispose() throws IOException JavaDoc {
94             if (os != null)
95                 os.close();
96         }
97
98         public Set JavaDoc getWrittenChunks() {
99             return writtenChunks;
100         }
101
102         public void startElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes atts) throws SAXException {
103             nesting++;
104             if (nesting == 2 && namespaceURI.equals("") && localName.equals("chunk")) {
105                 if (os != null || consumer != null)
106                     throw new SAXException("Chunk writer assertion error: new chunk and previous one is not yet finished.");
107
108                 String JavaDoc chunkName = atts.getValue("name");
109                 String JavaDoc publicationOutputPath = BookInstanceLayout.getPublicationOutputPath(context.getPublicationOutputName());
110                 String JavaDoc outputBasePath = publicationOutputPath + outputPrefix + chunkName;
111                 writtenChunks.add(chunkName);
112                 String JavaDoc outputPath = outputBasePath + chunkFileExtension;
113                 try {
114                     os = context.getBookInstance().getResourceOutputStream(outputPath);
115                     consumer = new XmlSerializer(os);
116                 } catch (Exception JavaDoc e) {
117                     throw new SAXException("Error initialising chunk output.", e);
118                 }
119                 consumer.startDocument();
120             } else if (nesting == 1 && !(namespaceURI.equals("") && localName.equals("chunks"))) {
121                 throw new SAXException("Invalid input: expected 'chunks' root element but got: " + qName);
122             } else if (consumer != null) {
123                 consumer.startElement(namespaceURI, localName, qName, atts);
124             }
125         }
126
127         public void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName) throws SAXException {
128             if (nesting == 2 && namespaceURI.equals("") && localName.equals("chunk")) {
129                 consumer.endDocument();
130                 consumer = null;
131                 try {
132                     os.close();
133                     os = null;
134                 } catch (IOException JavaDoc e) {
135                     throw new SAXException("Error closing chunk output stream.", e);
136                 }
137             } else if (consumer != null) {
138                 consumer.endElement(namespaceURI, localName, qName);
139             }
140             nesting--;
141         }
142
143         public void characters(char ch[], int start, int length) throws SAXException {
144             if (consumer != null)
145                 consumer.characters(ch, start, length);
146         }
147
148         public void ignorableWhitespace(char ch[], int start, int length) throws SAXException {
149             if (consumer != null)
150                 consumer.ignorableWhitespace(ch, start, length);
151         }
152
153         public void endPrefixMapping(String JavaDoc prefix) throws SAXException {
154             if (consumer != null)
155                 consumer.endPrefixMapping(prefix);
156         }
157
158         public void skippedEntity(String JavaDoc name) throws SAXException {
159             if (consumer != null)
160                 consumer.skippedEntity(name);
161         }
162
163         public void setDocumentLocator(Locator locator) {
164             // ignore
165
}
166
167         public void processingInstruction(String JavaDoc target, String JavaDoc data) throws SAXException {
168             if (consumer != null)
169                 consumer.processingInstruction(target, data);
170         }
171
172         public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri) throws SAXException {
173             if (consumer != null)
174                 consumer.startPrefixMapping(prefix, uri);
175         }
176
177         public void startDocument() throws SAXException {
178             // ignore
179
}
180
181         public void endDocument() throws SAXException {
182             // ignore
183
}
184     }
185 }
186
Popular Tags