JaxbSerializer.java
Go to the documentation of this file.
00001 /*
00002  * Licensed to the Apache Software Foundation (ASF) under one
00003  * or more contributor license agreements.  See the NOTICE file
00004  * distributed with this work for additional information
00005  * regarding copyright ownership.  The ASF licenses this file
00006  * to you under the Apache License, Version 2.0 (the
00007  * "License"); you may not use this file except in compliance
00008  * with the License.  You may obtain a copy of the License at
00009  *
00010  *   http://www.apache.org/licenses/LICENSE-2.0
00011  *
00012  * Unless required by applicable law or agreed to in writing,
00013  * software distributed under the License is distributed on an
00014  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
00015  * KIND, either express or implied.  See the License for the
00016  * specific language governing permissions and limitations
00017  * under the License.    
00018  */
00019 package org.apache.xmlrpc.jaxb;
00020 
00021 import javax.xml.bind.JAXBContext;
00022 import javax.xml.bind.JAXBException;
00023 
00024 import org.apache.xmlrpc.serializer.ExtSerializer;
00025 import org.xml.sax.Attributes;
00026 import org.xml.sax.ContentHandler;
00027 import org.xml.sax.Locator;
00028 import org.xml.sax.SAXException;
00029 
00030 
00033 public class JaxbSerializer extends ExtSerializer {
00034         private final JAXBContext context;
00035 
00038         public static final String JAXB_TAG = "jaxb";
00039 
00043         public JaxbSerializer(JAXBContext pContext) {
00044                 context = pContext;
00045         }
00046 
00047         protected String getTagName() { return JAXB_TAG; }
00048 
00049         protected void serialize(final ContentHandler pHandler, Object pObject) throws SAXException {
00050                 /* We must ensure, that startDocument() and endDocument() events
00051                  * are suppressed. So we replace the content handler with the following:
00052                  */
00053                 ContentHandler h = new ContentHandler() {
00054                         public void endDocument() throws SAXException {}
00055                         public void startDocument() throws SAXException {}
00056                         public void characters(char[] pChars, int pOffset, int pLength) throws SAXException {
00057                                 pHandler.characters(pChars, pOffset, pLength);
00058                         }
00059                         public void ignorableWhitespace(char[] pChars, int pOffset, int pLength) throws SAXException {
00060                                 pHandler.ignorableWhitespace(pChars, pOffset, pLength);
00061                         }
00062                         public void endPrefixMapping(String pPrefix) throws SAXException {
00063                                 pHandler.endPrefixMapping(pPrefix);
00064                         }
00065                         public void skippedEntity(String pName) throws SAXException {
00066                                 pHandler.endPrefixMapping(pName);
00067                         }
00068                         public void setDocumentLocator(Locator pLocator) {
00069                                 pHandler.setDocumentLocator(pLocator);
00070                         }
00071                         public void processingInstruction(String pTarget, String pData) throws SAXException {
00072                                 pHandler.processingInstruction(pTarget, pData);
00073                         }
00074                         public void startPrefixMapping(String pPrefix, String pURI) throws SAXException {
00075                                 pHandler.startPrefixMapping(pPrefix, pURI);
00076                         }
00077                         public void endElement(String pURI, String pLocalName, String pQName) throws SAXException {
00078                                 pHandler.endElement(pURI, pLocalName, pQName);
00079                         }
00080                         public void startElement(String pURI, String pLocalName, String pQName, Attributes pAttrs) throws SAXException {
00081                                 pHandler.startElement(pURI, pLocalName, pQName, pAttrs);
00082                         }
00083                 };
00084                 try {
00085                         context.createMarshaller().marshal(pObject, h);
00086                 } catch (JAXBException e) {
00087                         Throwable t = e.getLinkedException();
00088                         if (t != null  &&  t instanceof SAXException) {
00089                                 throw (SAXException) t;
00090                         } else {
00091                                 throw new SAXException(e);
00092                         }
00093                 }
00094         }
00095 }


rosjava_core
Author(s):
autogenerated on Wed Aug 26 2015 16:06:49