blob: 963326211623ac7eb41ea458f492a150091a2956 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.tuscany.sca.core.databinding.transformers;
import static javax.xml.stream.XMLStreamConstants.START_ELEMENT;
import java.lang.reflect.Constructor;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import org.apache.tuscany.sca.core.context.CallableReferenceExt;
import org.apache.tuscany.sca.core.context.ServiceReferenceExt;
import org.apache.tuscany.sca.databinding.PullTransformer;
import org.apache.tuscany.sca.databinding.TransformationContext;
import org.apache.tuscany.sca.databinding.TransformationException;
import org.apache.tuscany.sca.databinding.impl.BaseTransformer;
import org.oasisopen.sca.CallableReference;
@SuppressWarnings("unchecked")
public class XMLStreamReader2CallableReference extends BaseTransformer<XMLStreamReader, CallableReference>
implements PullTransformer<XMLStreamReader, CallableReference> {
private static final String SCA11_NS = "http://docs.oasis-open.org/ns/opencsa/sca/200903";
private static final String COMPOSITE = "composite";
private static final QName COMPOSITE_QNAME = new QName(SCA11_NS, COMPOSITE);
public CallableReference transform(XMLStreamReader source, TransformationContext context) {
try {
if (source != null) {
skipTopLevelElement(source);
Class refType =
context == null ? CallableReferenceExt.class : context.getTargetDataType().getPhysical();
Class implType;
if (refType.isAssignableFrom(CallableReferenceExt.class)) {
implType = CallableReferenceExt.class;
} else if (refType.isAssignableFrom(ServiceReferenceExt.class)) {
implType = ServiceReferenceExt.class;
} else {
throw new TransformationException("Unrecognized transformation target type");
}
Constructor constructor = implType.getConstructor(new Class[] {XMLStreamReader.class});
return (CallableReference)constructor.newInstance(new Object[] {source});
} else {
return null;
}
} catch (Exception e) {
throw new TransformationException(e);
}
}
/*
* Step over top-level element added by CallableReference2XMLStreamReader
*/
private void skipTopLevelElement(XMLStreamReader source) throws XMLStreamException {
while (source.hasNext()) {
int event = source.getEventType();
if (event == START_ELEMENT) {
QName name = source.getName();
if (COMPOSITE_QNAME.equals(name)) {
return;
}
}
source.next();
}
throw new TransformationException("<composite> element not found");
}
@Override
protected Class<XMLStreamReader> getSourceType() {
return XMLStreamReader.class;
}
@Override
protected Class<CallableReference> getTargetType() {
return CallableReference.class;
}
@Override
public int getWeight() {
return 10;
}
}
|