1 /*
2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
39
40 /**
41 * JAF data handler for XML content
42 *
43 * @author Anil Vijendran
44 */
45 public class XmlDataContentHandler implements DataContentHandler {
46 public static final String STR_SRC = "javax.xml.transform.stream.StreamSource";
47 private static Class<?> streamSourceClass = null;
48
49 public XmlDataContentHandler() throws ClassNotFoundException {
50 if (streamSourceClass == null) {
51 streamSourceClass = Class.forName(STR_SRC);
52 }
53 }
54
55 /**
56 * return the DataFlavors for this <code>DataContentHandler</code>
57 * @return The DataFlavors.
58 */
59 public DataFlavor[] getTransferDataFlavors() { // throws Exception;
60 DataFlavor flavors[] = new DataFlavor[2];
61
62 flavors[0] =
63 new ActivationDataFlavor(streamSourceClass, "text/xml", "XML");
64 flavors[1] =
65 new ActivationDataFlavor(streamSourceClass, "application/xml", "XML");
66
67 return flavors;
68 }
69
70 /**
71 * return the Transfer Data of type DataFlavor from InputStream
72 * @param df The DataFlavor.
73 * @param ins The InputStream corresponding to the data.
74 * @return The constructed Object.
75 */
76 public Object getTransferData(DataFlavor flavor, DataSource dataSource)
77 throws IOException {
78 if (flavor.getMimeType().startsWith("text/xml") ||
79 flavor.getMimeType().startsWith("application/xml")) {
80 if (flavor.getRepresentationClass().getName().equals(STR_SRC)) {
81 return new StreamSource(dataSource.getInputStream());
82 }
83 }
84 return null;
85 }
86
87 /**
88 *
89 */
90 public Object getContent(DataSource dataSource) throws IOException {
91 return new StreamSource(dataSource.getInputStream());
92 }
93
94 /**
95 * construct an object from a byte stream
96 * (similar semantically to previous method, we are deciding
97 * which one to support)
98 */
99 public void writeTo(Object obj, String mimeType, OutputStream os)
100 throws IOException {
101 if (!mimeType.startsWith("text/xml") && !mimeType.startsWith("application/xml"))
102 throw new IOException(
103 "Invalid content type \"" + mimeType + "\" for XmlDCH");
104
105
106 try {
107 Transformer transformer = EfficientStreamingTransformer.newTransformer();
108 StreamResult result = new StreamResult(os);
109 if (obj instanceof DataSource) {
110 // Streaming transform applies only to javax.xml.transform.StreamSource
111 transformer.transform((Source) getContent((DataSource)obj), result);
112 } else {
113 Source src=null;
114 if (obj instanceof String) {
115 src= new StreamSource(new java.io.StringReader((String) obj));
116 } else {
117 src=(Source) obj;
118 }
|
1 /*
2 * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
39
40 /**
41 * JAF data handler for XML content
42 *
43 * @author Anil Vijendran
44 */
45 public class XmlDataContentHandler implements DataContentHandler {
46 public static final String STR_SRC = "javax.xml.transform.stream.StreamSource";
47 private static Class<?> streamSourceClass = null;
48
49 public XmlDataContentHandler() throws ClassNotFoundException {
50 if (streamSourceClass == null) {
51 streamSourceClass = Class.forName(STR_SRC);
52 }
53 }
54
55 /**
56 * return the DataFlavors for this <code>DataContentHandler</code>
57 * @return The DataFlavors.
58 */
59 @Override
60 public DataFlavor[] getTransferDataFlavors() { // throws Exception;
61 DataFlavor flavors[] = new DataFlavor[2];
62
63 flavors[0] =
64 new ActivationDataFlavor(streamSourceClass, "text/xml", "XML");
65 flavors[1] =
66 new ActivationDataFlavor(streamSourceClass, "application/xml", "XML");
67
68 return flavors;
69 }
70
71 /**
72 * return the Transfer Data of type DataFlavor from InputStream
73 * @param flavor The DataFlavor.
74 * @param dataSource The DataSource.
75 * @return The constructed Object.
76 */
77 @Override
78 public Object getTransferData(DataFlavor flavor, DataSource dataSource)
79 throws IOException {
80 if (flavor.getMimeType().startsWith("text/xml") ||
81 flavor.getMimeType().startsWith("application/xml")) {
82 if (flavor.getRepresentationClass().getName().equals(STR_SRC)) {
83 return new StreamSource(dataSource.getInputStream());
84 }
85 }
86 return null;
87 }
88
89 /**
90 *
91 */
92 @Override
93 public Object getContent(DataSource dataSource) throws IOException {
94 return new StreamSource(dataSource.getInputStream());
95 }
96
97 /**
98 * construct an object from a byte stream
99 * (similar semantically to previous method, we are deciding
100 * which one to support)
101 */
102 @Override
103 public void writeTo(Object obj, String mimeType, OutputStream os)
104 throws IOException {
105 if (!mimeType.startsWith("text/xml") && !mimeType.startsWith("application/xml"))
106 throw new IOException(
107 "Invalid content type \"" + mimeType + "\" for XmlDCH");
108
109
110 try {
111 Transformer transformer = EfficientStreamingTransformer.newTransformer();
112 StreamResult result = new StreamResult(os);
113 if (obj instanceof DataSource) {
114 // Streaming transform applies only to javax.xml.transform.StreamSource
115 transformer.transform((Source) getContent((DataSource)obj), result);
116 } else {
117 Source src=null;
118 if (obj instanceof String) {
119 src= new StreamSource(new java.io.StringReader((String) obj));
120 } else {
121 src=(Source) obj;
122 }
|