1 |
0 |
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
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 |
|
|
29 |
|
|
30 |
|
public class JaxbSerializer extends ExtSerializer { |
31 |
|
private final JAXBContext context; |
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
public static final String JAXB_TAG = "jaxb"; |
36 |
|
|
37 |
|
|
38 |
|
|
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 |
|
|
48 |
|
|
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 |
|
} |