KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > diff > HtmlSaxDiffOutput


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.diff;
17
18 import org.xml.sax.ContentHandler JavaDoc;
19 import org.xml.sax.helpers.AttributesImpl JavaDoc;
20
21 /**
22  * Outputs the diff result as HTML elements to a SAX ContentHandler.
23  * The startDocument and endDocument events are not generated by this class.
24  */

25 public class HtmlSaxDiffOutput implements DiffOutput {
26     private ContentHandler JavaDoc consumer;
27     private DiffLineType currentLineType;
28
29     public HtmlSaxDiffOutput(ContentHandler JavaDoc consumer) {
30         this.consumer = consumer;
31     }
32
33     public void startLine(DiffLineType type) throws Exception JavaDoc {
34         this.currentLineType = type;
35         AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
36         attrs.addAttribute("", "class", "class", "CDATA", "diff-" + type.toString());
37         consumer.startElement("", "div", "div", attrs);
38     }
39
40     public void addUnchangedText(String JavaDoc text) throws Exception JavaDoc {
41         consumer.characters(text.toCharArray(), 0, text.length());
42     }
43
44     public void addChangedText(String JavaDoc text) throws Exception JavaDoc {
45         AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
46         attrs.addAttribute("", "class", "class", "CDATA", "diff-" + currentLineType.toString());
47         consumer.startElement("", "span", "span", attrs);
48         consumer.characters(text.toCharArray(), 0, text.length());
49         consumer.endElement("", "span", "span");
50     }
51
52     public void endLine() throws Exception JavaDoc {
53         consumer.endElement("", "div", "div");
54     }
55
56     public void skippedLines(int linesSkipped) throws Exception JavaDoc {
57         AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
58         attrs.addAttribute("", "class", "class", "CDATA", "diff-skipped");
59         consumer.startElement("", "div", "div", attrs);
60         String JavaDoc message = "(" + linesSkipped + " empty lines skipped)";
61         consumer.characters(message.toCharArray(), 0, message.length());
62         consumer.endElement("", "div", "div");
63     }
64 }
65
Popular Tags