]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.lib.detached/src/main/java/org/argeo/slc/lib/detached/DetachedXmlConverterSpring.java
32987338fce8698578d8c15f12547e247c6dc965
[gpl/argeo-slc.git] / runtime / org.argeo.slc.lib.detached / src / main / java / org / argeo / slc / lib / detached / DetachedXmlConverterSpring.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
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.argeo.slc.lib.detached;
17
18 import javax.xml.transform.Result;
19 import javax.xml.transform.Source;
20
21 import org.argeo.slc.SlcException;
22 import org.argeo.slc.detached.DetachedAnswer;
23 import org.argeo.slc.detached.DetachedCommunication;
24 import org.argeo.slc.detached.DetachedException;
25 import org.argeo.slc.detached.DetachedRequest;
26 import org.argeo.slc.detached.DetachedXmlConverter;
27 import org.springframework.oxm.Marshaller;
28 import org.springframework.oxm.Unmarshaller;
29
30 public class DetachedXmlConverterSpring implements DetachedXmlConverter {
31 private Marshaller marshaller;
32 private Unmarshaller unmarshaller;
33
34 public void marshallCommunication(DetachedCommunication detCom,
35 Result result) {
36 if (detCom instanceof DetachedRequest) {
37 marshallRequest((DetachedRequest) detCom, result);
38 } else if (detCom instanceof DetachedAnswer) {
39 marshallAnswer((DetachedAnswer) detCom, result);
40 } else {
41 throw new DetachedException("Unkown communication type "
42 + detCom.getClass());
43 }
44 }
45
46 public DetachedCommunication unmarshallCommunication(Source source) {
47 try {
48 return (DetachedCommunication) unmarshaller.unmarshal(source);
49 } catch (Exception e) {
50 throw new SlcException("Could not unmarshall", e);
51 }
52 }
53
54 public void marshallRequest(DetachedRequest request, Result result) {
55 try {
56 marshaller.marshal(request, result);
57 } catch (Exception e) {
58 throw new SlcException("Could not marshall", e);
59 }
60 }
61
62 public void marshallAnswer(DetachedAnswer answer, Result result) {
63 try {
64 marshaller.marshal(answer, result);
65 } catch (Exception e) {
66 throw new SlcException("Could not marshall", e);
67 }
68 }
69
70 public void setMarshaller(Marshaller marshaller) {
71 this.marshaller = marshaller;
72 }
73
74 public void setUnmarshaller(Unmarshaller unmarshaller) {
75 this.unmarshaller = unmarshaller;
76 }
77
78 }