vgritsenko 2005/03/31 18:38:08
Modified: java/src/org/apache/xindice/tools/command AddDocument.java
StringSerializer.java
Log:
string serializer sax methods should throw sax exception.
Revision Changes Path
1.15 +4 -4
xml-xindice/java/src/org/apache/xindice/tools/command/AddDocument.java
Index: AddDocument.java
===================================================================
RCS file:
/home/cvs/xml-xindice/java/src/org/apache/xindice/tools/command/AddDocument.java,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -r1.14 -r1.15
--- AddDocument.java 20 Jul 2004 20:31:37 -0000 1.14
+++ AddDocument.java 1 Apr 2005 02:38:08 -0000 1.15
@@ -1,5 +1,5 @@
/*
- * Copyright 1999-2004 The Apache Software Foundation.
+ * Copyright 1999-2005 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -75,7 +75,7 @@
spf.setNamespaceAware(true);
XMLReader reader = spf.newSAXParser().getXMLReader();
- StringSerializer ser = new StringSerializer(null);
+ StringSerializer ser = new StringSerializer();
reader.setContentHandler(ser);
reader.setProperty("http://xml.org/sax/properties/lexical-handler", ser);
reader.parse(new InputSource(fis));
1.10 +65 -86
xml-xindice/java/src/org/apache/xindice/tools/command/StringSerializer.java
Index: StringSerializer.java
===================================================================
RCS file:
/home/cvs/xml-xindice/java/src/org/apache/xindice/tools/command/StringSerializer.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- StringSerializer.java 8 Feb 2004 02:57:35 -0000 1.9
+++ StringSerializer.java 1 Apr 2005 02:38:08 -0000 1.10
@@ -1,5 +1,5 @@
/*
- * Copyright 1999-2004 The Apache Software Foundation.
+ * Copyright 1999-2005 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
+import org.xml.sax.SAXException;
import org.xml.sax.ext.LexicalHandler;
import java.util.HashMap;
@@ -28,35 +29,44 @@
/**
* SAX content handler that produces a string from the SAX events it
receives.
- * After calling endDocument(), the string becomes available by calling
- * <code>toString()</code>
+ * After calling <code>endDocument()</code>, the string becomes available by
+ * calling <code>toString()</code>
*
* @author <a href="mailto:[EMAIL PROTECTED]">James Bates</a>
* @version CVS $Revision$, $Date$
*/
public class StringSerializer implements ContentHandler, LexicalHandler {
- /* map that contains a stack for each prefix used in XML document */
+ /**
+ * The encoding that should be written into the XML declaration. May be
+ * <code>null</code> indicating no encoding should be written.
+ */
+ private String encoding;
+
+ /**
+ * Map containing stack of prefixes used in the XML document
+ */
private Map namespaceMap;
- /* namespace-declarations that should be written when the next element
- opens */
+ /**
+ * Namespace declarations that should be written out when the
+ * next element opens
+ */
private StringBuffer namespaceDecls;
- /* Are we currently inside a CDATA section? */
+ /**
+ * Are we currently inside a CDATA section?
+ */
private boolean inCDATA;
- /* the encoding name that should be written into the XML declaration.
May be
- * <code>null</code> indicating no encoding should be written.
+ /**
+ * The document so far
*/
- private String outputEncoding;
-
- /* the document so far */
private StringBuffer outputXml;
/**
- * Creates new <code>StringSerializer</code>. The output encoding is not
specified therefor
- * will be treated as "UTF-8".
+ * Creates new <code>StringSerializer</code>. The output encoding is not
+ * specified therefore will be treated as "UTF-8".
*/
public StringSerializer() {
this(null);
@@ -65,40 +75,36 @@
/**
* Creates new <code>StringSerializer</code>.
*
- * @param outputEncoding the encoding that should be written into the
- * XML declaration for the document. This encoding is not used in any
other
- * way: when the resultant string is written to a file, the writing
application
- * must still take care to actually write using the correct encoding.
- *
- * If <code>outputEncoding</code> is <code>null</code>, the encoding will
- * be omitted from the XML declaration.
+ * @param encoding The encoding that should be written into the
+ * XML declaration for the document. This encoding
+ * is not used in any other way: when the resultant
+ * string is written to a file, the writing application
+ * must still take care to actually write using the
+ * correct encoding. If <code>encoding</code> is
+ * <code>null</code>, the encoding will be omitted
+ * from the XML declaration.
*/
- public StringSerializer(String outputEncoding) {
-
- this.outputEncoding = outputEncoding;
- outputXml = new StringBuffer(1024); // allocate a 1k block to start
with
- namespaceMap = new HashMap();
- namespaceDecls = new StringBuffer();
- inCDATA = false;
+ public StringSerializer(String encoding) {
+ this.encoding = encoding;
+ this.outputXml = new StringBuffer(1024); // allocate a 1k block to
start with
+ this.namespaceMap = new HashMap();
+ this.namespaceDecls = new StringBuffer();
}
- public void startDocument() throws org.xml.sax.SAXException {
-
+ public void startDocument() throws SAXException {
outputXml.append("<?xml version=\"1.0\"");
- if (outputEncoding != null) {
-
+ if (encoding != null) {
outputXml.append(" encoding=\"");
- outputXml.append(outputEncoding);
+ outputXml.append(encoding);
outputXml.append('\"');
}
outputXml.append("?>");
}
- public void endDocument() throws org.xml.sax.SAXException {
+ public void endDocument() throws SAXException {
}
- public void processingInstruction(String target, String data) {
-
+ public void processingInstruction(String target, String data) throws
SAXException {
outputXml.append("<?");
outputXml.append(target);
outputXml.append(' ');
@@ -106,36 +112,30 @@
outputXml.append("?>");
}
- public void comment(char[] data, int start, int len) {
-
+ public void comment(char[] data, int start, int len) throws SAXException
{
outputXml.append("<!--");
outputXml.append(data, start, len);
outputXml.append("-->");
}
- private void xmlIze(StringBuffer sb, char[] data, int start, int len,
+ private void xmlIze(StringBuffer sb,
+ char[] data, int start, int len,
boolean isAttValue) {
for (int i = start; i < start + len; i++) {
-
if (data[i] == '<') {
-
sb.append("<");
} else if (data[i] == '&') {
-
sb.append("&");
} else if ((data[i] == '"') && (isAttValue)) {
-
sb.append(""");
} else {
-
sb.append(data[i]);
}
}
}
private void writeAttribute(StringBuffer sb, String attName, String
attValue) {
-
sb.append(' ');
sb.append(attName);
sb.append("=\"");
@@ -143,11 +143,9 @@
sb.append('\"');
}
- public void startPrefixMapping(String prefix, String uri) {
-
+ public void startPrefixMapping(String prefix, String uri) throws
SAXException {
Stack uriStack = (Stack) namespaceMap.get(prefix);
if (uriStack == null) {
-
uriStack = new Stack();
namespaceMap.put(prefix, uriStack);
}
@@ -155,47 +153,39 @@
uriStack.push(uri);
if (!prefix.equals("")) {
-
writeAttribute(namespaceDecls, "xmlns:" + prefix, uri);
} else {
-
writeAttribute(namespaceDecls, "xmlns", uri);
}
}
- public void endPrefixMapping(String prefix) {
-
+ public void endPrefixMapping(String prefix) throws SAXException {
Stack uriStack = (Stack) namespaceMap.get(prefix);
uriStack.pop();
}
-
- public void ignorableWhitespace(char[] data, int start, int len) {
-
+ public void ignorableWhitespace(char[] data, int start, int len) throws
SAXException {
outputXml.append(data, start, len);
}
private String getPrefix(String qName) {
-
if (qName.indexOf(':') != -1) {
-
return qName.substring(0, qName.indexOf(':'));
} else {
-
return "";
}
}
- public void startElement(String namespaceUri, String localName, String
qName,
- Attributes att) {
+ public void startElement(String ns, String localName, String qName,
Attributes att)
+ throws SAXException {
/* First: check element name qualification */
String prefix = getPrefix(qName);
if ((!(prefix.equals("") || prefix.equals("xml")))
- && (namespaceMap.get(prefix) == null)) {
+ && namespaceMap.get(prefix) == null) {
- writeAttribute(namespaceDecls, "xmlns:" + prefix, namespaceUri);
+ writeAttribute(namespaceDecls, "xmlns:" + prefix, ns);
}
outputXml.append('<');
@@ -213,7 +203,6 @@
if (!(attPrefix.equals("") || attPrefix.equals("xml"))) {
if (namespaceMap.get(attPrefix) == null) {
-
writeAttribute(outputXml, "xmlns:" + attPrefix,
attNamespaceUri);
}
}
@@ -224,34 +213,28 @@
outputXml.append('>');
}
- public void endElement(String namespaceUri, String localName, String
qName) {
-
+ public void endElement(String ns, String localName, String qName)
+ throws SAXException {
outputXml.append("</");
outputXml.append(qName);
outputXml.append('>');
}
- public void skippedEntity(String entity) {
-
+ public void skippedEntity(String entity) throws SAXException {
if (entity.startsWith("%")) {
-
outputXml.append(entity);
outputXml.append(';');
} else {
-
outputXml.append('&');
outputXml.append(entity);
outputXml.append(';');
}
}
- public void characters(char[] data, int start, int len) {
-
+ public void characters(char[] data, int start, int len) throws
SAXException {
if (inCDATA) {
-
outputXml.append(data, start, len);
} else {
-
xmlIze(outputXml, data, start, len, false);
}
}
@@ -259,33 +242,29 @@
public void setDocumentLocator(org.xml.sax.Locator locator) {
}
- public void startCDATA() {
-
+ public void startCDATA() throws SAXException {
outputXml.append("<![CDATA[");
inCDATA = true;
}
- public void endCDATA() throws org.xml.sax.SAXException {
-
+ public void endCDATA() throws SAXException {
inCDATA = false;
outputXml.append("]]>");
}
-
- public void startEntity(String entity) {
+ public void startEntity(String entity) throws SAXException {
}
- public void endEntity(String entity) {
+ public void endEntity(String entity) throws SAXException {
}
- public void startDTD(String docType, String systemID, String publicID) {
+ public void startDTD(String docType, String systemID, String publicID)
throws SAXException {
}
- public void endDTD() {
+ public void endDTD() throws SAXException {
}
public String toString() {
-
- return new String(outputXml);
+ return outputXml.toString();
}
}