Coverage Report - org.apache.xmlrpc.parser.RecursiveTypeParserImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
RecursiveTypeParserImpl
59% 
70% 
2,923
 
 1  
 /*
 2  
  * Copyright 1999,2005 The Apache Software Foundation.
 3  
  * 
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  * 
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  * 
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.apache.xmlrpc.parser;
 17  
 
 18  
 import javax.xml.namespace.QName;
 19  
 
 20  
 import org.apache.ws.commons.util.NamespaceContextImpl;
 21  
 import org.apache.xmlrpc.XmlRpcException;
 22  
 import org.apache.xmlrpc.common.TypeFactory;
 23  
 import org.apache.xmlrpc.common.XmlRpcExtensionException;
 24  
 import org.apache.xmlrpc.common.XmlRpcStreamConfig;
 25  
 import org.apache.xmlrpc.serializer.XmlRpcWriter;
 26  
 import org.xml.sax.Attributes;
 27  
 import org.xml.sax.SAXException;
 28  
 import org.xml.sax.SAXParseException;
 29  
 
 30  
 
 31  
 /** Abstract base class of a parser, that invokes other type
 32  
  * parsers recursively.
 33  
  */
 34  
 public abstract class RecursiveTypeParserImpl extends TypeParserImpl {
 35  
         private final NamespaceContextImpl context;
 36  
         protected final XmlRpcStreamConfig cfg;
 37  
         private final TypeFactory factory;
 38  
         private boolean inValueTag;
 39  
         private TypeParser typeParser;
 40  
         private String text;
 41  
 
 42  
         /** Creates a new instance.
 43  
          * @param pContext The namespace context.
 44  
          * @param pConfig The request or response configuration.
 45  
          * @param pFactory The type factory.
 46  
          */
 47  616
         protected RecursiveTypeParserImpl(XmlRpcStreamConfig pConfig,
 48  
                                                                           NamespaceContextImpl pContext,
 49  
                                                                           TypeFactory pFactory) {
 50  616
                 cfg = pConfig;
 51  616
                 context = pContext;
 52  616
                 factory = pFactory;
 53  616
         }
 54  
 
 55  
         protected void startValueTag() throws SAXException {
 56  760
                 inValueTag = true;
 57  760
                 text = null;
 58  760
                 typeParser = null;
 59  760
         }
 60  
 
 61  
         protected abstract void addResult(Object pResult) throws SAXException;
 62  
 
 63  
         protected void endValueTag() throws SAXException {
 64  760
                 if (inValueTag) {
 65  760
                         if (typeParser == null) {
 66  112
                                 addResult(text == null ? "" : text);
 67  112
                                 text = null;
 68  
                         } else {
 69  648
                                 typeParser.endDocument();
 70  
                                 try {
 71  648
                                         addResult(typeParser.getResult());
 72  0
                                 } catch (XmlRpcException e) {
 73  0
                                         throw new SAXException(e);
 74  
                                 }
 75  648
                                 typeParser = null;
 76  
                         }
 77  
                 } else {
 78  0
                         throw new SAXParseException("Invalid state: Not inside value tag.",
 79  0
                                                                                 getDocumentLocator());
 80  
                 }
 81  760
         }
 82  
 
 83  
         public void startDocument() throws SAXException {
 84  616
                 inValueTag = false;
 85  616
                 text = null;
 86  616
                 typeParser = null;
 87  616
         }
 88  
 
 89  
         public void endElement(String pURI, String pLocalName, String pQName)
 90  
                         throws SAXException {
 91  1304
                 if (inValueTag) {
 92  1304
                         if (typeParser == null) {
 93  0
                                 throw new SAXParseException("Invalid state: No type parser configured.",
 94  0
                                                                                         getDocumentLocator());
 95  
                         } else {
 96  1304
                                 typeParser.endElement(pURI, pLocalName, pQName);
 97  
                         }
 98  
                 } else {
 99  0
                         throw new SAXParseException("Invalid state: Not inside value tag.",
 100  0
                                         getDocumentLocator());
 101  
                 }
 102  1304
         }
 103  
 
 104  
         public void startElement(String pURI, String pLocalName,
 105  
                                                          String pQName, Attributes pAttrs) throws SAXException {
 106  1304
                 if (inValueTag) {
 107  1304
                         if (typeParser == null) {
 108  648
                                 typeParser = factory.getParser(cfg, context, pURI, pLocalName);
 109  648
                                 if (typeParser == null) {
 110  0
                                         if (XmlRpcWriter.EXTENSIONS_URI.equals(pURI)  &&  !cfg.isEnabledForExtensions()) {
 111  0
                                                 String msg = "The tag " + new QName(pURI, pLocalName) + " is invalid, if isEnabledForExtensions() == false.";
 112  0
                                                 throw new SAXParseException(msg, getDocumentLocator(),
 113  0
                                                                                                         new XmlRpcExtensionException(msg));
 114  
                                         } else {
 115  0
                                                 throw new SAXParseException("Unknown type: " + new QName(pURI, pLocalName),
 116  0
                                                                                                         getDocumentLocator());
 117  
                                         }
 118  
                                 }
 119  648
                                 typeParser.setDocumentLocator(getDocumentLocator());
 120  648
                                 typeParser.startDocument();
 121  648
                                 if (text != null) {
 122  0
                                         typeParser.characters(text.toCharArray(), 0, text.length());
 123  0
                                         text = null;
 124  
                                 }
 125  
                         }
 126  1304
                         typeParser.startElement(pURI, pLocalName, pQName, pAttrs);
 127  
                 } else {
 128  0
                         throw new SAXParseException("Invalid state: Not inside value tag.",
 129  0
                                         getDocumentLocator());
 130  
                 }
 131  1304
         }
 132  
 
 133  
         public void characters(char[] pChars, int pOffset, int pLength) throws SAXException {
 134  1040
                 if (typeParser == null) {
 135  104
                         if (inValueTag) {
 136  104
                                 String s = new String(pChars, pOffset, pLength);
 137  104
                                 text = text == null ? s : text + s;
 138  
                         } else {
 139  0
                                 super.characters(pChars, pOffset, pLength);
 140  
                         }
 141  
                 } else {
 142  936
                         typeParser.characters(pChars, pOffset, pLength);
 143  
                 }
 144  1040
         }
 145  
 
 146  
         public void ignorableWhitespace(char[] pChars, int pOffset, int pLength) throws SAXException {
 147  0
                 if (typeParser == null) {
 148  0
                         if (inValueTag) {
 149  0
                                 String s = new String(pChars, pOffset, pLength);
 150  0
                                 text = text == null ? s : text + s;
 151  
                         } else {
 152  0
                                 super.ignorableWhitespace(pChars, pOffset, pLength);
 153  
                         }
 154  
                 } else {
 155  0
                         typeParser.ignorableWhitespace(pChars, pOffset, pLength);
 156  
                 }
 157  0
         }
 158  
 
 159  
         public void processingInstruction(String pTarget, String pData) throws SAXException {
 160  0
                 if (typeParser == null) {
 161  0
                         super.processingInstruction(pTarget, pData);
 162  
                 } else {
 163  0
                         typeParser.processingInstruction(pTarget, pData);
 164  
                 }
 165  0
         }
 166  
 
 167  
         public void skippedEntity(String pEntity) throws SAXException {
 168  0
                 if (typeParser == null) {
 169  0
                         super.skippedEntity(pEntity);
 170  
                 } else {
 171  0
                         typeParser.skippedEntity(pEntity);
 172  
                 }
 173  0
         }
 174  
 
 175  
         public void startPrefixMapping(String pPrefix, String pURI) throws SAXException {
 176  469
                 if (typeParser == null) {
 177  461
                         super.startPrefixMapping(pPrefix, pURI);
 178  
                 } else {
 179  8
                         context.startPrefixMapping(pPrefix, pURI);
 180  
                 }
 181  469
         }
 182  
 
 183  
         public void endPrefixMapping(String pPrefix) throws SAXException {
 184  469
                 if (typeParser == null) {
 185  461
                         super.endPrefixMapping(pPrefix);
 186  
                 } else {
 187  8
                         context.endPrefixMapping(pPrefix);
 188  
                 }
 189  469
         }
 190  
 }