Skip to content

Package: text_xml

text_xml

nameinstructionbranchcomplexitylinemethod
getData(ActivationDataFlavor, DataSource)
M: 20 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getDataFlavors()
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
isXmlType(String)
M: 27 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 40 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
text_xml()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
writeTo(Object, String, OutputStream)
M: 96 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 20 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 1997, 2023 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 Public License v. 2.0, which is available at
6: * http://www.eclipse.org/legal/epl-2.0.
7: *
8: * This Source Code may also be made available under the following Secondary
9: * Licenses when the conditions for such availability set forth in the
10: * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11: * version 2 with the GNU Classpath Exception, which is available at
12: * https://www.gnu.org/software/classpath/license.html.
13: *
14: * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15: */
16:
17: package org.eclipse.angus.mail.handlers;
18:
19: import jakarta.activation.ActivationDataFlavor;
20: import jakarta.activation.DataSource;
21: import jakarta.mail.internet.ContentType;
22: import jakarta.mail.internet.ParseException;
23:
24: import javax.xml.transform.Source;
25: import javax.xml.transform.Transformer;
26: import javax.xml.transform.TransformerException;
27: import javax.xml.transform.TransformerFactory;
28: import javax.xml.transform.stream.StreamResult;
29: import javax.xml.transform.stream.StreamSource;
30: import java.io.IOException;
31: import java.io.OutputStream;
32:
33: /**
34: * DataContentHandler for text/xml.
35: *
36: * @author Anil Vijendran
37: * @author Bill Shannon
38: */
39: public class text_xml extends text_plain {
40:
41: private static final ActivationDataFlavor[] flavors = {
42: new ActivationDataFlavor(String.class, "text/xml", "XML String"),
43: new ActivationDataFlavor(String.class, "application/xml", "XML String"),
44: new ActivationDataFlavor(StreamSource.class, "text/xml", "XML"),
45: new ActivationDataFlavor(StreamSource.class, "application/xml", "XML")
46: };
47:
48: /**
49: * Creates a default {@code text_xml}.
50: */
51: public text_xml() {
52: }
53:
54: @Override
55: protected ActivationDataFlavor[] getDataFlavors() {
56: return flavors;
57: }
58:
59: @Override
60: protected Object getData(ActivationDataFlavor aFlavor, DataSource ds)
61: throws IOException {
62:• if (aFlavor.getRepresentationClass() == String.class)
63: return super.getContent(ds);
64:• else if (aFlavor.getRepresentationClass() == StreamSource.class)
65: return new StreamSource(ds.getInputStream());
66: else
67: return null; // XXX - should never happen
68: }
69:
70: /**
71: *
72: */
73: @Override
74: public void writeTo(Object obj, String mimeType, OutputStream os)
75: throws IOException {
76:• if (!isXmlType(mimeType))
77: throw new IOException(
78: "Invalid content type \"" + mimeType + "\" for text/xml DCH");
79:• if (obj instanceof String) {
80: super.writeTo(obj, mimeType, os);
81: return;
82: }
83:• if (!(obj instanceof DataSource || obj instanceof Source)) {
84: throw new IOException("Invalid Object type = " + obj.getClass() +
85: ". XmlDCH can only convert DataSource or Source to XML.");
86: }
87:
88: try {
89: Transformer transformer =
90: TransformerFactory.newInstance().newTransformer();
91: StreamResult result = new StreamResult(os);
92:• if (obj instanceof DataSource) {
93: // Streaming transform applies only to
94: // javax.xml.transform.StreamSource
95: transformer.transform(
96: new StreamSource(((DataSource) obj).getInputStream()),
97: result);
98: } else {
99: transformer.transform((Source) obj, result);
100: }
101: } catch (TransformerException | RuntimeException ex) {
102: IOException ioex = new IOException(
103: "Unable to run the JAXP transformer on a stream "
104: + ex.getMessage());
105: ioex.initCause(ex);
106: throw ioex;
107: }
108: }
109:
110: private boolean isXmlType(String type) {
111: try {
112: ContentType ct = new ContentType(type);
113:• return ct.getSubType().equals("xml") &&
114:• (ct.getPrimaryType().equals("text") ||
115:• ct.getPrimaryType().equals("application"));
116: } catch (ParseException | RuntimeException ex) {
117: return false;
118: }
119: }
120: }