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 javax.xml.namespace.QName;
00022
00023 import org.apache.ws.commons.util.NamespaceContextImpl;
00024 import org.apache.xmlrpc.XmlRpcException;
00025 import org.apache.xmlrpc.common.TypeFactory;
00026 import org.apache.xmlrpc.common.XmlRpcExtensionException;
00027 import org.apache.xmlrpc.common.XmlRpcStreamConfig;
00028 import org.apache.xmlrpc.serializer.XmlRpcWriter;
00029 import org.xml.sax.Attributes;
00030 import org.xml.sax.SAXException;
00031 import org.xml.sax.SAXParseException;
00032
00033
00037 public abstract class RecursiveTypeParserImpl extends TypeParserImpl {
00038 private final NamespaceContextImpl context;
00039 protected final XmlRpcStreamConfig cfg;
00040 private final TypeFactory factory;
00041 private boolean inValueTag;
00042 private TypeParser typeParser;
00043 private StringBuffer text = new StringBuffer();
00044
00050 protected RecursiveTypeParserImpl(XmlRpcStreamConfig pConfig,
00051 NamespaceContextImpl pContext,
00052 TypeFactory pFactory) {
00053 cfg = pConfig;
00054 context = pContext;
00055 factory = pFactory;
00056 }
00057
00062 protected void startValueTag() throws SAXException {
00063 inValueTag = true;
00064 text.setLength(0);
00065 typeParser = null;
00066 }
00067
00068 protected abstract void addResult(Object pResult) throws SAXException;
00069
00070 protected void endValueTag() throws SAXException {
00071 if (inValueTag) {
00072 if (typeParser == null) {
00073 addResult(text.toString());
00074 text.setLength(0);
00075 } else {
00076 typeParser.endDocument();
00077 try {
00078 addResult(typeParser.getResult());
00079 } catch (XmlRpcException e) {
00080 throw new SAXException(e);
00081 }
00082 typeParser = null;
00083 }
00084 } else {
00085 throw new SAXParseException("Invalid state: Not inside value tag.",
00086 getDocumentLocator());
00087 }
00088 }
00089
00090 public void startDocument() throws SAXException {
00091 inValueTag = false;
00092 text.setLength(0);
00093 typeParser = null;
00094 }
00095
00096 public void endElement(String pURI, String pLocalName, String pQName)
00097 throws SAXException {
00098 if (inValueTag) {
00099 if (typeParser == null) {
00100 throw new SAXParseException("Invalid state: No type parser configured.",
00101 getDocumentLocator());
00102 } else {
00103 typeParser.endElement(pURI, pLocalName, pQName);
00104 }
00105 } else {
00106 throw new SAXParseException("Invalid state: Not inside value tag.",
00107 getDocumentLocator());
00108 }
00109 }
00110
00111 public void startElement(String pURI, String pLocalName,
00112 String pQName, Attributes pAttrs) throws SAXException {
00113 if (inValueTag) {
00114 if (typeParser == null) {
00115 typeParser = factory.getParser(cfg, context, pURI, pLocalName);
00116 if (typeParser == null) {
00117 if (XmlRpcWriter.EXTENSIONS_URI.equals(pURI) && !cfg.isEnabledForExtensions()) {
00118 String msg = "The tag " + new QName(pURI, pLocalName) + " is invalid, if isEnabledForExtensions() == false.";
00119 throw new SAXParseException(msg, getDocumentLocator(),
00120 new XmlRpcExtensionException(msg));
00121 } else {
00122 throw new SAXParseException("Unknown type: " + new QName(pURI, pLocalName),
00123 getDocumentLocator());
00124 }
00125 }
00126 typeParser.setDocumentLocator(getDocumentLocator());
00127 typeParser.startDocument();
00128 if (text.length() > 0) {
00129 typeParser.characters(text.toString().toCharArray(), 0, text.length());
00130 text.setLength(0);
00131 }
00132 }
00133 typeParser.startElement(pURI, pLocalName, pQName, pAttrs);
00134 } else {
00135 throw new SAXParseException("Invalid state: Not inside value tag.",
00136 getDocumentLocator());
00137 }
00138 }
00139
00140 public void characters(char[] pChars, int pOffset, int pLength) throws SAXException {
00141 if (typeParser == null) {
00142 if (inValueTag) {
00143 text.append(pChars, pOffset, pLength);
00144 } else {
00145 super.characters(pChars, pOffset, pLength);
00146 }
00147 } else {
00148 typeParser.characters(pChars, pOffset, pLength);
00149 }
00150 }
00151
00152 public void ignorableWhitespace(char[] pChars, int pOffset, int pLength) throws SAXException {
00153 if (typeParser == null) {
00154 if (inValueTag) {
00155 text.append(pChars, pOffset, pLength);
00156 } else {
00157 super.ignorableWhitespace(pChars, pOffset, pLength);
00158 }
00159 } else {
00160 typeParser.ignorableWhitespace(pChars, pOffset, pLength);
00161 }
00162 }
00163
00164 public void processingInstruction(String pTarget, String pData) throws SAXException {
00165 if (typeParser == null) {
00166 super.processingInstruction(pTarget, pData);
00167 } else {
00168 typeParser.processingInstruction(pTarget, pData);
00169 }
00170 }
00171
00172 public void skippedEntity(String pEntity) throws SAXException {
00173 if (typeParser == null) {
00174 super.skippedEntity(pEntity);
00175 } else {
00176 typeParser.skippedEntity(pEntity);
00177 }
00178 }
00179
00180 public void startPrefixMapping(String pPrefix, String pURI) throws SAXException {
00181 if (typeParser == null) {
00182 super.startPrefixMapping(pPrefix, pURI);
00183 } else {
00184 context.startPrefixMapping(pPrefix, pURI);
00185 }
00186 }
00187
00188 public void endPrefixMapping(String pPrefix) throws SAXException {
00189 if (typeParser == null) {
00190 super.endPrefixMapping(pPrefix);
00191 } else {
00192 context.endPrefixMapping(pPrefix);
00193 }
00194 }
00195 }