Coverage Report - org.apache.xmlrpc.parser.ExtParser
 
Classes in this File Line Coverage Branch Coverage Complexity
ExtParser
47% 
50% 
2,231
 
 1  
 /*
 2  
  * Copyright 2003, 2004  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 java.util.ArrayList;
 19  
 import java.util.List;
 20  
 
 21  
 import javax.xml.namespace.QName;
 22  
 
 23  
 import org.apache.xmlrpc.serializer.XmlRpcWriter;
 24  
 import org.xml.sax.Attributes;
 25  
 import org.xml.sax.ContentHandler;
 26  
 import org.xml.sax.Locator;
 27  
 import org.xml.sax.SAXException;
 28  
 import org.xml.sax.SAXParseException;
 29  
 
 30  
 
 31  
 /** Base class for parsing external XML representations, like DOM,
 32  
  * or JAXB.
 33  
  */
 34  16
 public abstract class ExtParser implements TypeParser {
 35  
         private Locator locator;
 36  
         private ContentHandler handler;
 37  8
         private int level = 0;
 38  8
         private final List prefixes = new ArrayList();
 39  
 
 40  
         /** Returns a content handler for parsing the actual
 41  
          * contents.
 42  
          * @return A SAX handler for parsing the XML inside
 43  
          * the outer ex:foo element.
 44  
          * @throws SAXException Creating the handler failed.
 45  
          */
 46  
         protected abstract ContentHandler getExtHandler() throws SAXException;
 47  
 
 48  
         /** Returns the outer node name.
 49  
          */
 50  
         protected abstract String getTagName();
 51  
 
 52  
         public void endDocument() throws SAXException {
 53  8
         }
 54  
 
 55  
         public void startDocument() throws SAXException {
 56  8
         }
 57  
 
 58  
         public void characters(char[] pChars, int pOffset, int pLength)
 59  
                         throws SAXException {
 60  80
                 if (handler == null) {
 61  0
                         if (!TypeParserImpl.isEmpty(pChars, pOffset, pLength)) {
 62  0
                                 throw new SAXParseException("Unexpected non-whitespace content: " + new String(pChars, pOffset, pLength),
 63  0
                                                                                         locator);
 64  
                         }
 65  
                 } else {
 66  80
                         handler.characters(pChars, pOffset, pLength);
 67  
                 }
 68  80
         }
 69  
 
 70  
         public void ignorableWhitespace(char[] pChars, int pOffset, int pLength)
 71  
                         throws SAXException {
 72  0
                 if (handler != null) {
 73  0
                         ignorableWhitespace(pChars, pOffset, pLength);
 74  
                 }
 75  0
         }
 76  
 
 77  
         public void endPrefixMapping(String pPrefix) throws SAXException {
 78  0
                 if (handler != null) {
 79  0
                         handler.endPrefixMapping(pPrefix);
 80  
                 }
 81  0
         }
 82  
 
 83  
         public void skippedEntity(String pName) throws SAXException {
 84  0
                 if (handler == null) {
 85  0
                         throw new SAXParseException("Don't know how to handle entity " + pName,
 86  0
                                                                                 locator);
 87  
                 } else {
 88  0
                         handler.skippedEntity(pName);
 89  
                 }
 90  0
         }
 91  
 
 92  
         public void setDocumentLocator(Locator pLocator) {
 93  8
                 locator = pLocator;
 94  8
                 if (handler != null) {
 95  0
                         handler.setDocumentLocator(pLocator);
 96  
                 }
 97  8
         }
 98  
 
 99  
         public void processingInstruction(String pTarget, String pData)
 100  
                         throws SAXException {
 101  0
                 if (handler != null) {
 102  0
                         handler.processingInstruction(pTarget, pData);
 103  
                 }
 104  0
         }
 105  
 
 106  
         public void startPrefixMapping(String pPrefix, String pURI)
 107  
                         throws SAXException {
 108  0
                 if (handler == null) {
 109  0
                         prefixes.add(pPrefix);
 110  0
                         prefixes.add(pURI);
 111  
                 } else {
 112  0
                         handler.startPrefixMapping(pPrefix, pURI);
 113  
                 }
 114  0
         }
 115  
 
 116  
         public void startElement(String pURI, String pLocalName,
 117  
                                                          String pQName, Attributes pAttrs) throws SAXException {
 118  56
                 switch (level++) {
 119  
                         case 0:
 120  8
                                 final String tag = getTagName();
 121  8
                                 if (!XmlRpcWriter.EXTENSIONS_URI.equals(pURI)  ||
 122  8
                                         !tag.equals(pLocalName)) {
 123  0
                                         throw new SAXParseException("Expected " +
 124  0
                                                                                                 new QName(XmlRpcWriter.EXTENSIONS_URI, tag) +
 125  0
                                                                                                 ", got " +
 126  0
                                                                                                 new QName(pURI, pLocalName),
 127  0
                                                                                                 locator);
 128  
                                 }
 129  8
                                 handler = getExtHandler();
 130  8
                                 handler.startDocument();
 131  8
                                 for (int i = 0;  i < prefixes.size();  i += 2) {
 132  0
                                         handler.startPrefixMapping((String) prefixes.get(i),
 133  0
                                                                                            (String) prefixes.get(i+1));
 134  
                                 }
 135  8
                                 break;
 136  
                         default:
 137  48
                                 handler.startElement(pURI, pLocalName, pQName, pAttrs);
 138  
                                 break;
 139  
                 }
 140  56
         }
 141  
 
 142  
         public void endElement(String pURI, String pLocalName, String pQName)
 143  
                         throws SAXException {
 144  56
                 switch (--level) {
 145  
                         case 0:
 146  8
                                 for (int i = 0;  i < prefixes.size();  i += 2) {
 147  0
                                         handler.endPrefixMapping((String) prefixes.get(i));
 148  
                                 }
 149  8
                                 handler.endDocument();
 150  8
                                 handler = null;
 151  8
                                 break;
 152  
                         default:
 153  48
                                 handler.endElement(pURI, pLocalName, pQName);
 154  
                                 break;
 155  
                 }
 156  56
         }
 157  
 }