KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > publisher > serverimpl > docpreparation > MergePartsHandler


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.publisher.serverimpl.docpreparation;
17
18 import org.xml.sax.*;
19 import org.xml.sax.helpers.AttributesImpl JavaDoc;
20 import org.outerj.daisy.repository.*;
21 import org.outerj.daisy.repository.schema.RepositorySchema;
22 import org.outerj.daisy.repository.schema.PartType;
23 import org.apache.avalon.framework.logger.Logger;
24 import org.outerj.daisy.xmlutil.SaxBuffer;
25 import org.outerj.daisy.xmlutil.LocalSAXParserFactory;
26 import org.outerj.daisy.xmlutil.XmlMimeTypeHelper;
27 import org.outerj.daisy.publisher.serverimpl.AbstractHandler;
28 import org.outerj.daisy.publisher.serverimpl.StripDocumentHandler;
29 import org.outerj.daisy.util.Constants;
30
31 import javax.xml.parsers.SAXParser JavaDoc;
32 import java.io.InputStream JavaDoc;
33 import java.io.InputStreamReader JavaDoc;
34 import java.io.BufferedInputStream JavaDoc;
35 import java.io.Reader JavaDoc;
36 import java.util.Locale JavaDoc;
37 import java.util.Set JavaDoc;
38
39 /**
40  * A ContentHandler that merges parts for which the daisyHtml flag
41  * on the PartType is set to true, and optionally for other part types.
42  * It reacts on the XML produced by the document object.
43  */

44 public class MergePartsHandler extends AbstractHandler implements ContentHandler {
45     private static final String JavaDoc PART_EL = "part";
46
47     private RepositorySchema repositorySchema;
48     private Locale JavaDoc locale;
49     private Logger logger;
50     private Set JavaDoc inlineParts;
51     private Version version;
52
53     public MergePartsHandler(Version version, Set JavaDoc inlineParts,
54             ContentHandler consumer, Repository repository, Locale JavaDoc locale, Logger logger) {
55         super(consumer);
56         this.version = version;
57         this.inlineParts = inlineParts;
58         this.repositorySchema = repository.getRepositorySchema();
59         this.locale = locale;
60         this.logger = logger;
61     }
62
63     public void startElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes atts) throws SAXException {
64         if (localName.equals(PART_EL) && namespaceURI.equals(Constants.DAISY_NAMESPACE)) {
65             long typeId = Long.parseLong(atts.getValue("", "typeId"));
66             PartType partType = null;
67             try {
68                 partType = repositorySchema.getPartTypeById(typeId, false);
69             } catch (RepositoryException e) {
70                 consumer.startElement(namespaceURI, localName, qName, atts);
71                 outputErrorAsDoc("Error retrieving part type info for part " + typeId + ": " + e.getMessage());
72             }
73
74             if (partType != null) {
75                 Part part = version.getPart(typeId);
76                 boolean isXmlMimeType = XmlMimeTypeHelper.isXmlMimeType(part.getMimeType());
77                 boolean inlinePart = partType.isDaisyHtml() ||
78                         (
79                                 (inlineParts.contains(partType.getName()) || inlineParts.contains(String.valueOf(typeId)))
80                                 && (isXmlMimeType || part.getMimeType().startsWith("text/"))
81                         );
82
83                 AttributesImpl JavaDoc newAtts = new AttributesImpl JavaDoc(atts);
84                 String JavaDoc label = partType.getLabel(locale);
85                 newAtts.addAttribute("", "label", "label", "CDATA", label);
86                 newAtts.addAttribute("", "name", "name", "CDATA", partType.getName());
87                 newAtts.addAttribute("", "daisyHtml", "daisyHtml", "CDATA", String.valueOf(partType.isDaisyHtml()));
88                 newAtts.addAttribute("", "inlined", "inlined", "CDATA", String.valueOf(inlinePart));
89                 consumer.startElement(namespaceURI, localName, qName, newAtts);
90
91                 if (inlinePart) {
92                     SaxBuffer saxBuffer = new SaxBuffer();
93                     try {
94                         InputStream JavaDoc is = null;
95                         try {
96                             is = new BufferedInputStream JavaDoc(version.getPart(typeId).getDataStream());
97                             if (partType.isDaisyHtml() || isXmlMimeType) {
98                                 SAXParser JavaDoc parser = LocalSAXParserFactory.getSAXParserFactory().newSAXParser();
99                                 XMLReader xmlReader = parser.getXMLReader();
100                                 xmlReader.setContentHandler(saxBuffer);
101                                 InputSource inputSource = new InputSource(is);
102                                 xmlReader.parse(inputSource);
103                             } else {
104                                 Reader JavaDoc reader = new InputStreamReader JavaDoc(is);
105                                 char[] buffer = new char[8192];
106                                 int read;
107                                 while ((read = reader.read(buffer)) != -1) {
108                                     saxBuffer.characters(buffer, 0, read);
109                                 }
110                             }
111                         } finally {
112                             if (is != null)
113                                 is.close();
114                         }
115                     } catch (Throwable JavaDoc e) {
116                         logger.error("Error including part content.", e);
117                         outputErrorAsDoc("Error including part content.");
118                     }
119                     saxBuffer.toSAX(new StripDocumentHandler(consumer));
120                 }
121             }
122         } else {
123             consumer.startElement(namespaceURI, localName, qName, atts);
124         }
125     }
126
127     public void endDocument() throws SAXException {
128         consumer.endDocument();
129     }
130
131     public void startDocument () throws SAXException {
132         consumer.startDocument();
133     }
134
135     public void characters (char ch[], int start, int length) throws SAXException {
136         consumer.characters(ch, start, length);
137     }
138
139     public void ignorableWhitespace (char ch[], int start, int length) throws SAXException {
140         consumer.ignorableWhitespace(ch, start, length);
141     }
142
143     public void endPrefixMapping (String JavaDoc prefix) throws SAXException {
144         consumer.endPrefixMapping(prefix);
145     }
146
147     public void skippedEntity (String JavaDoc name) throws SAXException {
148         consumer.skippedEntity(name);
149     }
150
151     public void setDocumentLocator (Locator locator) {
152         consumer.setDocumentLocator(locator);
153     }
154
155     public void processingInstruction (String JavaDoc target, String JavaDoc data) throws SAXException {
156         consumer.processingInstruction(target, data);
157     }
158
159     public void startPrefixMapping (String JavaDoc prefix, String JavaDoc uri) throws SAXException {
160         consumer.startPrefixMapping(prefix, uri);
161     }
162
163     public void endElement (String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName) throws SAXException {
164         consumer.endElement(namespaceURI, localName, qName);
165     }
166 }
167
Popular Tags