Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 package org.apache.xmlrpc.parser;
00020
00021 import java.util.ArrayList;
00022 import java.util.List;
00023
00024 import javax.xml.namespace.QName;
00025
00026 import org.apache.xmlrpc.serializer.XmlRpcWriter;
00027 import org.xml.sax.Attributes;
00028 import org.xml.sax.ContentHandler;
00029 import org.xml.sax.Locator;
00030 import org.xml.sax.SAXException;
00031 import org.xml.sax.SAXParseException;
00032
00033
00037 public abstract class ExtParser implements TypeParser {
00038 private Locator locator;
00039 private ContentHandler handler;
00040 private int level = 0;
00041 private final List prefixes = new ArrayList();
00042
00049 protected abstract ContentHandler getExtHandler() throws SAXException;
00050
00053 protected abstract String getTagName();
00054
00055 public void endDocument() throws SAXException {
00056 }
00057
00058 public void startDocument() throws SAXException {
00059 }
00060
00061 public void characters(char[] pChars, int pOffset, int pLength)
00062 throws SAXException {
00063 if (handler == null) {
00064 if (!TypeParserImpl.isEmpty(pChars, pOffset, pLength)) {
00065 throw new SAXParseException("Unexpected non-whitespace content: " + new String(pChars, pOffset, pLength),
00066 locator);
00067 }
00068 } else {
00069 handler.characters(pChars, pOffset, pLength);
00070 }
00071 }
00072
00073 public void ignorableWhitespace(char[] pChars, int pOffset, int pLength)
00074 throws SAXException {
00075 if (handler != null) {
00076 ignorableWhitespace(pChars, pOffset, pLength);
00077 }
00078 }
00079
00080 public void endPrefixMapping(String pPrefix) throws SAXException {
00081 if (handler != null) {
00082 handler.endPrefixMapping(pPrefix);
00083 }
00084 }
00085
00086 public void skippedEntity(String pName) throws SAXException {
00087 if (handler == null) {
00088 throw new SAXParseException("Don't know how to handle entity " + pName,
00089 locator);
00090 } else {
00091 handler.skippedEntity(pName);
00092 }
00093 }
00094
00095 public void setDocumentLocator(Locator pLocator) {
00096 locator = pLocator;
00097 if (handler != null) {
00098 handler.setDocumentLocator(pLocator);
00099 }
00100 }
00101
00102 public void processingInstruction(String pTarget, String pData)
00103 throws SAXException {
00104 if (handler != null) {
00105 handler.processingInstruction(pTarget, pData);
00106 }
00107 }
00108
00109 public void startPrefixMapping(String pPrefix, String pURI)
00110 throws SAXException {
00111 if (handler == null) {
00112 prefixes.add(pPrefix);
00113 prefixes.add(pURI);
00114 } else {
00115 handler.startPrefixMapping(pPrefix, pURI);
00116 }
00117 }
00118
00119 public void startElement(String pURI, String pLocalName,
00120 String pQName, Attributes pAttrs) throws SAXException {
00121 switch (level++) {
00122 case 0:
00123 final String tag = getTagName();
00124 if (!XmlRpcWriter.EXTENSIONS_URI.equals(pURI) ||
00125 !tag.equals(pLocalName)) {
00126 throw new SAXParseException("Expected " +
00127 new QName(XmlRpcWriter.EXTENSIONS_URI, tag) +
00128 ", got " +
00129 new QName(pURI, pLocalName),
00130 locator);
00131 }
00132 handler = getExtHandler();
00133 handler.startDocument();
00134 for (int i = 0; i < prefixes.size(); i += 2) {
00135 handler.startPrefixMapping((String) prefixes.get(i),
00136 (String) prefixes.get(i+1));
00137 }
00138 break;
00139 default:
00140 handler.startElement(pURI, pLocalName, pQName, pAttrs);
00141 break;
00142 }
00143 }
00144
00145 public void endElement(String pURI, String pLocalName, String pQName)
00146 throws SAXException {
00147 switch (--level) {
00148 case 0:
00149 for (int i = 0; i < prefixes.size(); i += 2) {
00150 handler.endPrefixMapping((String) prefixes.get(i));
00151 }
00152 handler.endDocument();
00153 handler = null;
00154 break;
00155 default:
00156 handler.endElement(pURI, pLocalName, pQName);
00157 break;
00158 }
00159 }
00160 }