Coverage Report - org.apache.xmlrpc.jaxb.JaxbSerializer
 
Classes in this File Line Coverage Branch Coverage Complexity
JaxbSerializer
0% 
0% 
1,286
 
 1  0
 /*
 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.jaxb;
 17  
 
 18  
 import javax.xml.bind.JAXBContext;
 19  
 import javax.xml.bind.JAXBException;
 20  
 
 21  
 import org.apache.xmlrpc.serializer.ExtSerializer;
 22  
 import org.xml.sax.Attributes;
 23  
 import org.xml.sax.ContentHandler;
 24  
 import org.xml.sax.Locator;
 25  
 import org.xml.sax.SAXException;
 26  
 
 27  
 
 28  
 /** A serializer for JAXB objects.
 29  
  */
 30  
 public class JaxbSerializer extends ExtSerializer {
 31  
         private final JAXBContext context;
 32  
 
 33  
         /** The tag name for serializing JAXB objects.
 34  
          */
 35  
         public static final String JAXB_TAG = "jaxb";
 36  
 
 37  
         /** Creates a new instance with the given context.
 38  
          * @param pContext The context being used for creating marshallers.
 39  
          */
 40  0
         public JaxbSerializer(JAXBContext pContext) {
 41  0
                 context = pContext;
 42  0
         }
 43  
 
 44  0
         protected String getTagName() { return JAXB_TAG; }
 45  
 
 46  
         protected void serialize(final ContentHandler pHandler, Object pObject) throws SAXException {
 47  
                 /* We must ensure, that startDocument() and endDocument() events
 48  
                  * are suppressed. So we replace the content handler with the following:
 49  
                  */
 50  0
                 ContentHandler h = new ContentHandler() {
 51  0
                         public void endDocument() throws SAXException {}
 52  0
                         public void startDocument() throws SAXException {}
 53  
                         public void characters(char[] pChars, int pOffset, int pLength) throws SAXException {
 54  0
                                 pHandler.characters(pChars, pOffset, pLength);
 55  0
                         }
 56  
                         public void ignorableWhitespace(char[] pChars, int pOffset, int pLength) throws SAXException {
 57  0
                                 pHandler.ignorableWhitespace(pChars, pOffset, pLength);
 58  0
                         }
 59  
                         public void endPrefixMapping(String pPrefix) throws SAXException {
 60  0
                                 pHandler.endPrefixMapping(pPrefix);
 61  0
                         }
 62  
                         public void skippedEntity(String pName) throws SAXException {
 63  0
                                 pHandler.endPrefixMapping(pName);
 64  0
                         }
 65  
                         public void setDocumentLocator(Locator pLocator) {
 66  0
                                 pHandler.setDocumentLocator(pLocator);
 67  0
                         }
 68  
                         public void processingInstruction(String pTarget, String pData) throws SAXException {
 69  0
                                 pHandler.processingInstruction(pTarget, pData);
 70  0
                         }
 71  
                         public void startPrefixMapping(String pPrefix, String pURI) throws SAXException {
 72  0
                                 pHandler.startPrefixMapping(pPrefix, pURI);
 73  0
                         }
 74  
                         public void endElement(String pURI, String pLocalName, String pQName) throws SAXException {
 75  0
                                 pHandler.endElement(pURI, pLocalName, pQName);
 76  0
                         }
 77  
                         public void startElement(String pURI, String pLocalName, String pQName, Attributes pAttrs) throws SAXException {
 78  0
                                 pHandler.startElement(pURI, pLocalName, pQName, pAttrs);
 79  0
                         }
 80  
                 };
 81  
                 try {
 82  0
                         context.createMarshaller().marshal(pObject, h);
 83  0
                 } catch (JAXBException e) {
 84  0
                         Throwable t = e.getLinkedException();
 85  0
                         if (t != null  &&  t instanceof SAXException) {
 86  0
                                 throw (SAXException) t;
 87  
                         } else {
 88  0
                                 throw new SAXException(e);
 89  
                         }
 90  
                 }
 91  0
         }
 92  
 }