001: /*
002: * Copyright 2007 Outerthought bvba and Schaubroeck nv
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.outerj.daisy.diff.tag;
017:
018: import org.xml.sax.ContentHandler;
019: import org.xml.sax.SAXException;
020: import org.xml.sax.helpers.AttributesImpl;
021:
022: /**
023: * Outputs the diff result as HTML elements to a SAX ContentHandler. The
024: * startDocument and endDocument events are not generated by this class. This
025: * version is used for tag based diff results.
026: */
027: public class TagSaxDiffOutput {
028:
029: private ContentHandler consumer;
030:
031: public TagSaxDiffOutput(ContentHandler consumer)
032: throws SAXException {
033: this .consumer = consumer;
034: }
035:
036: public void addClearPart(String text) throws Exception {
037: addBasicText(text);
038: }
039:
040: private boolean insideTag = false;
041:
042: private void addBasicText(String text) throws SAXException {
043: char[] c = text.toCharArray();
044:
045: AttributesImpl noattrs = new AttributesImpl();
046:
047: for (int i = 0; i < c.length; i++) {
048: switch (c[i]) {
049: case '\n':
050: consumer.startElement("", "br", "br", noattrs);
051: consumer.endElement("", "br", "br");
052: consumer.characters("\n".toCharArray(), 0, "\n"
053: .length());
054: break;
055: case '<':
056: if (insideTag == false) {
057: AttributesImpl attrs = new AttributesImpl();
058: attrs.addAttribute("", "class", "class", "CDATA",
059: "diff-tag-html");
060: consumer.startElement("", "span", "span", attrs);
061: insideTag = true;
062: } else {
063: consumer.endElement("", "span", "span");
064: insideTag = false;
065: }
066: consumer.characters("<".toCharArray(), 0, "<".length());
067: break;
068: case '>':
069: consumer.characters(">".toCharArray(), 0, ">".length());
070:
071: if (insideTag == true) {
072: consumer.endElement("", "span", "span");
073: insideTag = false;
074: }
075: break;
076: default:
077: consumer.characters(c, i, 1);
078: }
079: }
080: }
081:
082: private int removedID = 1;
083: private int addedID = 1;
084:
085: public void addRemovedPart(String text) throws Exception {
086: AttributesImpl attrs = new AttributesImpl();
087: attrs.addAttribute("", "class", "class", "CDATA",
088: "diff-tag-removed");
089: attrs.addAttribute("", "id", "id", "CDATA", "removed"
090: + removedID);
091: attrs.addAttribute("", "title", "title", "CDATA", "#removed"
092: + removedID);
093: removedID++;
094: consumer.startElement("", "span", "span", attrs);
095: addBasicText(text);
096: consumer.endElement("", "span", "span");
097: }
098:
099: public void addAddedPart(String text) throws Exception {
100: AttributesImpl attrs = new AttributesImpl();
101: attrs.addAttribute("", "class", "class", "CDATA",
102: "diff-tag-added");
103: attrs.addAttribute("", "id", "id", "CDATA", "added" + addedID);
104: attrs.addAttribute("", "title", "title", "CDATA", "#added"
105: + addedID);
106: addedID++;
107: consumer.startElement("", "span", "span", attrs);
108: addBasicText(text);
109: consumer.endElement("", "span", "span");
110: }
111: }
|