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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
package com.jclark.xsl.sax;
import com.jclark.xsl.tr.*;
import com.jclark.xsl.om.*;
import org.xml.sax.*;
import java.io.IOException;
import java.net.URL;
public abstract class ResultBase implements Result, AttributeList {
private DocumentHandler documentHandler;
private CommentHandler commentHandler;
ErrorHandler errorHandler;
private RawCharactersHandler rawCharactersHandler;
static private final int INITIAL_BUF_SIZE = 8192;
private char[] buf = new char[INITIAL_BUF_SIZE];
private int bufUsed = 0;
private Name[] attributeNames = new Name[10];
private String[] attributeValues = new String[10];
private int nAttributes;
private Name pendingElementType;
private NamespacePrefixMap pendingNamespacePrefixMap;
OutputMethodHandler outputMethodHandler;
ResultBase(OutputMethodHandler outputMethodHandler, ErrorHandler errorHandler) {
this.outputMethodHandler = outputMethodHandler;
this.documentHandler = null;
this.errorHandler = errorHandler;
}
ResultBase(DocumentHandler documentHandler, ErrorHandler errorHandler) {
this.outputMethodHandler = null;
this.errorHandler = errorHandler;
setDocumentHandler(documentHandler);
}
private void setDocumentHandler(DocumentHandler handler) {
documentHandler = handler;
if (handler instanceof CommentHandler)
commentHandler = (CommentHandler)handler;
else
commentHandler = null;
if (handler instanceof RawCharactersHandler)
rawCharactersHandler = (RawCharactersHandler)handler;
else
rawCharactersHandler = null;
}
public void flush() throws XSLException {
if (pendingElementType != null) {
startElementContent(pendingElementType, pendingNamespacePrefixMap);
pendingElementType = null;
}
else if (bufUsed > 0) {
try {
documentHandler.characters(buf, 0, bufUsed);
bufUsed = 0;
}
catch (SAXException e) {
throwXSLException(e);
}
}
}
public void rawCharacters(String str) throws XSLException {
if (rawCharactersHandler == null)
characters(str);
else {
flush();
try {
rawCharactersHandler.rawCharacters(str);
}
catch (SAXException e) {
throwXSLException(e);
}
}
}
public void characters(String str) throws XSLException {
if (pendingElementType != null)
flush();
int strLength = str.length();
if (bufUsed + strLength > buf.length) {
char[] oldBuf = buf;
int newLen = oldBuf.length * 2;
while (newLen < bufUsed + strLength)
newLen *= 2;
buf = new char[newLen];
if (bufUsed > 0)
System.arraycopy(oldBuf, 0, buf, 0, bufUsed);
}
str.getChars(0, strLength, buf, bufUsed);
bufUsed += strLength;
}
public void comment(String str) throws XSLException {
if (commentHandler != null) {
flush();
try {
commentHandler.comment(fixComment(str));
}
catch (SAXException e) {
throwXSLException(e);
}
}
}
private static final String fixComment(String str) {
int i = str.indexOf('-');
while (i++ >= 0) {
int len = str.length();
if (i == len)
return str + " ";
if (str.charAt(i) == '-')
str = str.substring(0, i) + " " + str.substring(i);
i = str.indexOf('-', i);
}
return str;
}
public void processingInstruction(String target, String data)
throws XSLException {
try {
flush();
documentHandler.processingInstruction(target,
fixProcessingInstruction(data));
}
catch (SAXException e) {
throwXSLException(e);
}
}
private static final String fixProcessingInstruction(String str) {
int i = str.indexOf('?');
while (i++ >= 0) {
int len = str.length();
if (i == len)
break;
if (str.charAt(i) == '>')
str = str.substring(0, i) + " " + str.substring(i);
i = str.indexOf('?', i);
}
return str;
}
public void startElement(Name elementType, NamespacePrefixMap nsMap)
throws XSLException {
flush();
pendingElementType = elementType;
pendingNamespacePrefixMap = nsMap;
nAttributes = 0;
}
public void endElement(Name elementType) throws XSLException {
flush();
endElementContent(elementType);
}
protected final DocumentHandler getDocumentHandler() {
return documentHandler;
}
public int getLength() {
return nAttributes;
}
protected final Name getAttributeName(int i) {
return attributeNames[i];
}
public String getValue(int i) {
return attributeValues[i];
}
public String getType(int i) {
return "CDATA";
}
public String getType(String name) {
return "CDATA";
}
public String getValue(String name) {
int len = getLength();
for (int i = 0; i < len; i++) {
if (name.equals(getName(i)))
return getValue(i);
}
return null;
}
protected abstract void startElementContent(Name elementType,
NamespacePrefixMap nsMap)
throws XSLException;
protected abstract void endElementContent(Name elementType)
throws XSLException;
public void attribute(Name name, String value) throws XSLException {
if (pendingElementType == null)
return;
for (int i = 0; i < nAttributes; i++)
if (attributeNames[i].equals(name)) {
attributeValues[i] = value;
return;
}
if (nAttributes == attributeNames.length) {
attributeNames = grow(attributeNames);
attributeValues = grow(attributeValues);
}
attributeNames[nAttributes] = name;
attributeValues[nAttributes] = value;
nAttributes++;
}
static String[] grow(String[] v) {
String[] old = v;
v = new String[old.length * 2];
System.arraycopy(old, 0, v, 0, old.length);
return v;
}
static Name[] grow(Name[] v) {
Name[] old = v;
v = new Name[old.length * 2];
System.arraycopy(old, 0, v, 0, old.length);
return v;
}
public void start(OutputMethod outputMethod) throws XSLException {
try {
if (documentHandler == null) {
Name name = outputMethod.getName();
if (name == null)
setDocumentHandler(new OutputMethodDefaulter(this, outputMethod));
else
setOutputMethod(name, outputMethod);
}
documentHandler.startDocument();
}
catch (IOException e) {
throw new XSLException(e);
}
catch (SAXException e) {
throwXSLException(e);
}
}
DocumentHandler setOutputMethod(Name name, OutputMethod method) throws IOException, SAXException {
String nameString;
if (name.getNamespace() != null)
nameString = (name.getNamespace()
+ OutputMethodHandler.namespaceSeparator
+ name.getLocalPart());
else
nameString = name.getLocalPart();
setDocumentHandler(outputMethodHandler
.createDocumentHandler(nameString,
new OutputMethodAttributeList(method)));
return documentHandler;
}
public void end() throws XSLException {
try {
flush();
documentHandler.endDocument();
}
catch (SAXException e) {
throwXSLException(e);
}
}
protected void throwXSLException(SAXException e) throws XSLException {
Exception wrapped = e.getException();
if (wrapped != null)
throw new XSLException(wrapped);
else
throw new XSLException(e.getMessage());
}
public abstract void resultTreeFragment(ResultTreeFragment frag) throws XSLException;
public void message(Node node, String str) throws XSLException {
if (errorHandler != null) {
String systemId = null;
int lineNumber = -1;
if (node != null) {
URL url = node.getURL();
if (url != null)
systemId = url.toString();
lineNumber = node.getLineNumber();
}
try {
errorHandler.warning(new SAXParseException(str, null, systemId, lineNumber, -1, null));
}
catch (SAXException e) {
throwXSLException(e);
}
}
}
}
|