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: 30 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 8 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: 88 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 25 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 java.io.IOException;
25: import java.io.OutputStream;
26:
27: import javax.xml.transform.Source;
28: import javax.xml.transform.Transformer;
29: import javax.xml.transform.TransformerException;
30: import javax.xml.transform.TransformerFactory;
31: import javax.xml.transform.stream.StreamResult;
32: import javax.xml.transform.stream.StreamSource;
33:
34: /**
35: * DataContentHandler for text/xml.
36: *
37: * @author Anil Vijendran
38: * @author Bill Shannon
39: */
40: public class text_xml extends text_plain {
41:
42: private static final ActivationDataFlavor[] flavors = {
43:         new ActivationDataFlavor(String.class, "text/xml", "XML String"),
44:         new ActivationDataFlavor(String.class, "application/xml", "XML String"),
45:         new ActivationDataFlavor(StreamSource.class, "text/xml", "XML"),
46:         new ActivationDataFlavor(StreamSource.class, "application/xml", "XML")
47: };
48:
49: /**
50: * Creates a default {@code text_xml}.
51: */
52: public text_xml() {
53: }
54:
55: @Override
56: protected ActivationDataFlavor[] getDataFlavors() {
57:         return flavors;
58: }
59:
60: @Override
61: protected Object getData(ActivationDataFlavor aFlavor, DataSource ds)
62:                                 throws IOException {
63:•        if (aFlavor.getRepresentationClass() == String.class)
64:          return super.getContent(ds);
65:•        else if (aFlavor.getRepresentationClass() == StreamSource.class)
66:          return new StreamSource(ds.getInputStream());
67:         else
68:          return null; // XXX - should never happen
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 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:         } catch (RuntimeException ex) {
108:          IOException ioex = new IOException(
109:                 "Unable to run the JAXP transformer on a stream "
110:                  + ex.getMessage());
111:          ioex.initCause(ex);
112:          throw ioex;
113:         }
114: }
115:
116: private boolean isXmlType(String type) {
117:         try {
118:          ContentType ct = new ContentType(type);
119:•         return ct.getSubType().equals("xml") &&
120:•                 (ct.getPrimaryType().equals("text") ||
121:•                 ct.getPrimaryType().equals("application"));
122:         } catch (ParseException ex) {
123:          return false;
124:         } catch (RuntimeException ex) {
125:          return false;
126:         }
127: }
128: }