Skip to content

Package: XmlDataContentHandler

XmlDataContentHandler

nameinstructionbranchcomplexitylinemethod
XmlDataContentHandler()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
getContent(DataSource)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getTransferData(ActivationDataFlavor, DataSource)
M: 25 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getTransferDataFlavors()
M: 23 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
writeTo(Object, String, OutputStream)
M: 20 C: 45
69%
M: 4 C: 4
50%
M: 3 C: 2
40%
M: 2 C: 14
88%
M: 0 C: 1
100%

Coverage

1: /*
2: * Copyright (c) 1997, 2022 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Distribution License v. 1.0, which is available at
6: * http://www.eclipse.org/org/documents/edl-v10.php.
7: *
8: * SPDX-License-Identifier: BSD-3-Clause
9: */
10:
11: package com.sun.xml.messaging.saaj.soap;
12:
13: import java.io.IOException;
14: import java.io.OutputStream;
15:
16: import jakarta.activation.ActivationDataFlavor;
17: import jakarta.activation.DataContentHandler;
18: import jakarta.activation.DataSource;
19: import javax.xml.transform.Source;
20: import javax.xml.transform.Transformer;
21: import javax.xml.transform.stream.StreamResult;
22: import javax.xml.transform.stream.StreamSource;
23:
24: import com.sun.xml.messaging.saaj.util.transform.EfficientStreamingTransformer;
25:
26: /**
27: * JAF data handler for XML content
28: *
29: * @author Anil Vijendran
30: */
31: public class XmlDataContentHandler implements DataContentHandler {
32:
33: public XmlDataContentHandler() {
34: }
35:
36: /**
37: * return the DataFlavors for this <code>DataContentHandler</code>
38: * @return The DataFlavors.
39: */
40: @Override
41: public ActivationDataFlavor[] getTransferDataFlavors() {
42: ActivationDataFlavor[] flavors = new ActivationDataFlavor[2];
43:
44: flavors[0] =
45: new ActivationDataFlavor(StreamSource.class, "text/xml", "XML");
46: flavors[1] =
47: new ActivationDataFlavor(StreamSource.class, "application/xml", "XML");
48:
49: return flavors;
50: }
51:
52: /**
53: * return the Transfer Data of type DataFlavor from InputStream
54: * @param flavor The DataFlavor.
55: * @param dataSource The DataSource.
56: * @return The constructed Object.
57: */
58: @Override
59: public Object getTransferData(ActivationDataFlavor flavor, DataSource dataSource)
60: throws IOException {
61:• if (flavor.getMimeType().startsWith("text/xml") ||
62:• flavor.getMimeType().startsWith("application/xml")) {
63:• if (StreamSource.class.getName().equals(flavor.getRepresentationClass().getName())) {
64: return new StreamSource(dataSource.getInputStream());
65: }
66: }
67: return null;
68: }
69:
70: /**
71: *
72: */
73: @Override
74: public Object getContent(DataSource dataSource) throws IOException {
75: return new StreamSource(dataSource.getInputStream());
76: }
77:
78: /**
79: * construct an object from a byte stream
80: * (similar semantically to previous method, we are deciding
81: * which one to support)
82: */
83: @Override
84: public void writeTo(Object obj, String mimeType, OutputStream os)
85: throws IOException {
86:• if (!mimeType.startsWith("text/xml") && !mimeType.startsWith("application/xml"))
87: throw new IOException(
88: "Invalid content type \"" + mimeType + "\" for XmlDCH");
89:
90:
91: try {
92: Transformer transformer = EfficientStreamingTransformer.newTransformer();
93: StreamResult result = new StreamResult(os);
94:• if (obj instanceof DataSource) {
95: // Streaming transform applies only to javax.xml.transform.StreamSource
96: transformer.transform((Source) getContent((DataSource)obj), result);
97: } else {
98: Source src=null;
99:• if (obj instanceof String) {
100: src= new StreamSource(new java.io.StringReader((String) obj));
101: } else {
102: src=(Source) obj;
103: }
104: transformer.transform(src, result);
105: }
106: } catch (Exception ex) {
107: throw new IOException(
108: "Unable to run the JAXP transformer on a stream "
109: + ex.getMessage());
110: }
111: }
112: }