Coverage Report - org.apache.xmlrpc.serializer.XmlRpcWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
XmlRpcWriter
73% 
67% 
2,8
 
 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.serializer;
 17  
 
 18  
 import java.util.HashMap;
 19  
 import java.util.Map;
 20  
 
 21  
 import org.apache.xmlrpc.XmlRpcRequest;
 22  
 import org.apache.xmlrpc.XmlRpcRequestConfig;
 23  
 import org.apache.xmlrpc.common.TypeFactory;
 24  
 import org.apache.xmlrpc.common.XmlRpcStreamConfig;
 25  
 import org.xml.sax.Attributes;
 26  
 import org.xml.sax.ContentHandler;
 27  
 import org.xml.sax.SAXException;
 28  
 import org.xml.sax.helpers.AttributesImpl;
 29  
 
 30  
 
 31  
 /** This class is responsible for writing an XmlRpc request or an
 32  
  * XmlRpc response to an output stream.
 33  
  */
 34  1
 public class XmlRpcWriter {
 35  
         /** The namespace URI for proprietary XML-RPC extensions.
 36  
          */
 37  
         public static final String EXTENSIONS_URI = "http://ws.apache.org/xmlrpc/namespaces/extensions";
 38  1
         private static final Attributes ZERO_ATTRIBUTES = new AttributesImpl();
 39  
         private final XmlRpcStreamConfig config;
 40  
         private final TypeFactory typeFactory;
 41  
         private final ContentHandler handler;
 42  
 
 43  
         /** Creates a new instance.
 44  
          * @param pConfig The clients configuration.
 45  
          * @param pHandler The target SAX handler.
 46  
          * @param pTypeFactory The type factory being used to create serializers.
 47  
          */
 48  668
         public XmlRpcWriter(XmlRpcStreamConfig pConfig, ContentHandler pHandler,
 49  
                                             TypeFactory pTypeFactory) {
 50  668
                 config = pConfig;
 51  668
                 handler = pHandler;
 52  668
                 typeFactory = pTypeFactory;
 53  668
         }
 54  
 
 55  
         /** Writes a clients request to the output stream.
 56  
          * @param pRequest The request being written.
 57  
          * @throws SAXException Writing the request failed.
 58  
          */
 59  
         public void write(XmlRpcRequest pRequest) throws SAXException {
 60  388
                 handler.startDocument();
 61  388
                 boolean extensions = pRequest.getConfig().isEnabledForExtensions();
 62  388
                 if (extensions) {
 63  193
                         handler.startPrefixMapping("ex", XmlRpcWriter.EXTENSIONS_URI);
 64  
                 }
 65  388
                 handler.startElement("", "methodCall", "methodCall", ZERO_ATTRIBUTES);
 66  388
                 handler.startElement("", "methodName", "methodName", ZERO_ATTRIBUTES);
 67  388
                 String s = pRequest.getMethodName();
 68  388
                 handler.characters(s.toCharArray(), 0, s.length());
 69  388
                 handler.endElement("", "methodName", "methodName");
 70  388
                 handler.startElement("", "params", "params", ZERO_ATTRIBUTES);
 71  388
                 int num = pRequest.getParameterCount();
 72  672
                 for (int i = 0;  i < num;  i++) {
 73  388
                         handler.startElement("", "param", "param", ZERO_ATTRIBUTES);
 74  388
                         writeValue(pRequest.getParameter(i));
 75  284
                         handler.endElement("", "param", "param");
 76  
                 }
 77  284
                 handler.endElement("", "params", "params");
 78  284
         handler.endElement("", "methodCall", "methodCall");
 79  284
                 if (extensions) {
 80  193
                         handler.endPrefixMapping("ex");
 81  
                 }
 82  284
                 handler.endDocument();
 83  284
         }
 84  
 
 85  
         /** Writes a servers response to the output stream.
 86  
          * @param pConfig The request configuration.
 87  
          * @param pResult The result object.
 88  
          * @throws SAXException Writing the response failed.
 89  
          */
 90  
         public void write(XmlRpcRequestConfig pConfig, Object pResult) throws SAXException {
 91  280
                 handler.startDocument();
 92  280
                 boolean extensions = pConfig.isEnabledForExtensions();
 93  280
                 if (extensions) {
 94  269
                         handler.startPrefixMapping("ex", XmlRpcWriter.EXTENSIONS_URI);
 95  
                 }
 96  280
                 handler.startElement("", "methodResponse", "methodResponse", ZERO_ATTRIBUTES);
 97  280
                 handler.startElement("", "params", "params", ZERO_ATTRIBUTES);
 98  280
                 handler.startElement("", "param", "param", ZERO_ATTRIBUTES);
 99  280
                 writeValue(pResult);
 100  280
                 handler.endElement("", "param", "param");
 101  280
                 handler.endElement("", "params", "params");
 102  280
                 handler.endElement("", "methodResponse", "methodResponse");
 103  280
                 if (extensions) {
 104  269
                         handler.endPrefixMapping("ex");
 105  
                 }
 106  280
                 handler.endDocument();
 107  280
         }
 108  
 
 109  
         /** Writes a servers error message to the output stream.
 110  
          * @param pConfig The request configuration.
 111  
          * @param pCode The error code
 112  
          * @param pMessage The error message
 113  
          * @throws SAXException Writing the error message failed.
 114  
          */
 115  
         public void write(XmlRpcRequestConfig pConfig, int pCode, String pMessage) throws SAXException {
 116  0
                 handler.startDocument();
 117  0
                 boolean extensions = pConfig.isEnabledForExtensions();
 118  0
                 if (extensions) {
 119  0
                         handler.startPrefixMapping("ex", XmlRpcWriter.EXTENSIONS_URI);
 120  
                 }
 121  0
                 handler.startElement("", "methodResponse", "methodResponse", ZERO_ATTRIBUTES);
 122  0
                 handler.startElement("", "fault", "fault", ZERO_ATTRIBUTES);
 123  0
                 Map map = new HashMap();
 124  0
         map.put("faultCode", new Integer(pCode));
 125  0
         map.put("faultString", pMessage == null ? "" : pMessage);
 126  0
                 writeValue(map);
 127  0
                 handler.endElement("", "fault", "fault");
 128  0
                 handler.endElement("", "methodResponse", "methodResponse");
 129  0
                 if (extensions) {
 130  0
                         handler.endPrefixMapping("ex");
 131  
                 }
 132  0
                 handler.endDocument();
 133  0
         }
 134  
 
 135  
         /** Writes the XML representation of a Java object.
 136  
          * @param pObject The object being written.
 137  
          * @throws SAXException Writing the object failed.
 138  
          */
 139  
         protected void writeValue(Object pObject) throws SAXException {
 140  668
                 TypeSerializer serializer = typeFactory.getSerializer(config, pObject);
 141  572
                 if (serializer == null) {
 142  0
                         throw new SAXException("Unsupported Java type: " + pObject.getClass().getName());
 143  
                 }
 144  572
                 serializer.write(handler, pObject);
 145  564
         }
 146  
 }