KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > publisher > serverimpl > InsertBreaksInCommentsHandler


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;
17
18 import org.xml.sax.ContentHandler JavaDoc;
19 import org.xml.sax.SAXException JavaDoc;
20 import org.xml.sax.Locator JavaDoc;
21 import org.xml.sax.Attributes JavaDoc;
22 import org.xml.sax.helpers.AttributesImpl JavaDoc;
23
24 public class InsertBreaksInCommentsHandler implements ContentHandler JavaDoc {
25     private static final String JavaDoc DAISY_NS = "http://outerx.org/daisy/1.0";
26     private boolean inComment = false;
27     private ContentHandler JavaDoc consumer;
28
29     public InsertBreaksInCommentsHandler(ContentHandler JavaDoc consumer) {
30         this.consumer = consumer;
31     }
32
33     public void startElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc atts) throws SAXException JavaDoc {
34         if (namespaceURI.equals(DAISY_NS) && localName.equals("comment")) {
35             inComment = true;
36         }
37         consumer.startElement(namespaceURI, localName, qName, atts);
38     }
39
40     public void characters(char ch[], int start, int length) throws SAXException JavaDoc {
41         if (inComment) {
42             int prevBreakPos = start - 1;
43             int end = start + length;
44             int i;
45             for (i = start; i < end; i++) {
46                 if (ch[i] == '\n') {
47                     if (prevBreakPos != i - 1)
48                         consumer.characters(ch, prevBreakPos + 1, i - prevBreakPos - 1);
49                     consumer.startElement("", "br", "br", new AttributesImpl JavaDoc());
50                     consumer.endElement("", "br", "br");
51                     prevBreakPos = i;
52                 }
53             }
54             if (prevBreakPos != i) {
55                 consumer.characters(ch, prevBreakPos + 1, i - prevBreakPos - 1);
56             }
57         } else {
58             consumer.characters(ch, start, length);
59         }
60     }
61
62     public void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName) throws SAXException JavaDoc {
63         if (namespaceURI.equals(DAISY_NS) && localName.equals("comment")) {
64             inComment = false;
65         }
66         consumer.endElement(namespaceURI, localName, qName);
67     }
68
69     public void endDocument() throws SAXException JavaDoc {
70         consumer.endDocument();
71     }
72
73     public void startDocument() throws SAXException JavaDoc {
74         consumer.startDocument();
75     }
76
77     public void ignorableWhitespace(char ch[], int start, int length) throws SAXException JavaDoc {
78         consumer.ignorableWhitespace(ch, start, length);
79     }
80
81     public void endPrefixMapping(String JavaDoc prefix) throws SAXException JavaDoc {
82         consumer.endPrefixMapping(prefix);
83     }
84
85     public void skippedEntity(String JavaDoc name) throws SAXException JavaDoc {
86         consumer.skippedEntity(name);
87     }
88
89     public void setDocumentLocator(Locator JavaDoc locator) {
90         consumer.setDocumentLocator(locator);
91     }
92
93     public void processingInstruction(String JavaDoc target, String JavaDoc data) throws SAXException JavaDoc {
94         consumer.processingInstruction(target, data);
95     }
96
97     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri) throws SAXException JavaDoc {
98         consumer.startPrefixMapping(prefix, uri);
99     }
100 }
101
Popular Tags