1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
package com.jclark.xsl.sax;
import com.jclark.xsl.om.*;
import org.xml.sax.*;
import com.jclark.xsl.tr.OutputMethod;
import java.util.Vector;
import java.util.Enumeration;
import java.io.IOException;
class OutputMethodDefaulter implements DocumentHandler, CommentHandler, RawCharactersHandler {
private final ResultBase result;
private final OutputMethod outputMethod;
private final Vector savedEvents = new Vector();
private Locator locator;
static abstract class Event {
abstract void emit(DocumentHandler handler) throws SAXException;
}
static class ProcessingInstructionEvent extends Event {
private String target;
private String content;
ProcessingInstructionEvent(String target, String content) {
this.target = target;
this.content = content;
}
void emit(DocumentHandler handler) throws SAXException {
handler.processingInstruction(target, content);
}
}
static class CommentEvent extends Event {
private String content;
CommentEvent(String content) {
this.content = content;
}
void emit(DocumentHandler handler) throws SAXException {
if (handler instanceof CommentHandler)
((CommentHandler)handler).comment(content);
}
}
static class RawCharactersEvent extends Event {
private String chars;
RawCharactersEvent(String chars) {
this.chars = chars;
}
void emit(DocumentHandler handler) throws SAXException {
if (handler instanceof RawCharactersHandler)
((RawCharactersHandler)handler).rawCharacters(chars);
else {
char[] buf = chars.toCharArray();
handler.characters(buf, 0, buf.length);
}
}
}
static class CharactersEvent extends Event {
private char[] buf;
CharactersEvent(char[] b, int off, int len) {
buf = new char[len];
System.arraycopy(b, off, buf, 0, len);
}
void emit(DocumentHandler handler) throws SAXException {
handler.characters(buf, 0, buf.length);
}
}
static class IgnorableWhitespaceEvent extends Event {
private char[] buf;
IgnorableWhitespaceEvent(char[] b, int off, int len) {
buf = new char[len];
System.arraycopy(b, off, buf, 0, len);
}
void emit(DocumentHandler handler) throws SAXException {
handler.ignorableWhitespace(buf, 0, buf.length);
}
}
public void startDocument() { }
public void endDocument() throws SAXException {
getDocumentHandler(false).endDocument();
}
public void startElement(String name, AttributeList atts) throws SAXException {
getDocumentHandler(name.equalsIgnoreCase("html")
&& atts.getValue("xmlns") == null)
.startElement(name, atts);
}
public void endElement(String name) {
throw new Error("unbalanced call to endElement");
}
public void characters(char[] buf, int off, int len) throws SAXException {
for (int i = 0; i < len; i++) {
switch (buf[off + i]) {
case ' ':
case '\t':
case '\r':
case '\n':
break;
default:
getDocumentHandler(false).characters(buf, off, len);
return;
}
}
savedEvents.addElement(new CharactersEvent(buf, off, len));
}
public void ignorableWhitespace(char[] buf, int off, int len) throws SAXException {
savedEvents.addElement(new IgnorableWhitespaceEvent(buf, off, len));
}
public void comment(String content) {
savedEvents.addElement(new CommentEvent(content));
}
public void processingInstruction(String target, String content) {
savedEvents.addElement(new ProcessingInstructionEvent(target, content));
}
public void rawCharacters(String chars) {
savedEvents.addElement(new RawCharactersEvent(chars));
}
public void setDocumentLocator(Locator loc) {
this.locator = loc;
}
OutputMethodDefaulter(ResultBase result, OutputMethod outputMethod) {
this.result = result;
this.outputMethod = outputMethod;
}
private DocumentHandler getDocumentHandler(boolean isHtml)
throws SAXException {
Name name = outputMethod.getNameTable().createName(isHtml ? "html" : "xml");
try {
DocumentHandler handler = result.setOutputMethod(name, outputMethod);
if (locator != null)
handler.setDocumentLocator(locator);
handler.startDocument();
for (Enumeration iter = savedEvents.elements(); iter.hasMoreElements();)
((Event)iter.nextElement()).emit(handler);
return handler;
}
catch (IOException e) {
throw new SAXException(e);
}
}
}
|