author | mkos |
Sun, 30 Dec 2012 00:00:00 +0100 | |
changeset 22678 | ac1ea46be942 |
parent 12009 | 4abb694f273a |
permissions | -rw-r--r-- |
12009 | 1 |
/* |
22678
ac1ea46be942
8029237: Update copyright year to match last edit in jaxws repository for 2012
mkos
parents:
12009
diff
changeset
|
2 |
* Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
12009 | 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 |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package com.sun.xml.internal.bind.v2.util; |
|
27 |
||
28 |
import java.io.IOException; |
|
29 |
import java.io.InputStream; |
|
30 |
import java.io.InputStreamReader; |
|
31 |
import java.io.Reader; |
|
32 |
||
33 |
import javax.activation.DataHandler; |
|
34 |
import javax.activation.DataSource; |
|
35 |
import javax.activation.MimeType; |
|
36 |
import javax.activation.MimeTypeParseException; |
|
37 |
import javax.xml.transform.Source; |
|
38 |
import javax.xml.transform.stream.StreamSource; |
|
39 |
||
40 |
/** |
|
41 |
* {@link Source} implementation backed by {@link DataHandler}. |
|
42 |
* |
|
43 |
* <p> |
|
44 |
* This implementation allows the same {@link Source} to be used |
|
45 |
* mutliple times. |
|
46 |
* |
|
47 |
* <p> |
|
48 |
* {@link Source} isn't really pluggable. As a consequence, |
|
49 |
* this implementation is clunky --- weak against unexpected |
|
50 |
* usage of the class. |
|
51 |
* |
|
52 |
* @author Kohsuke Kawaguchi |
|
53 |
*/ |
|
54 |
public final class DataSourceSource extends StreamSource { |
|
55 |
private final DataSource source; |
|
56 |
||
57 |
/** |
|
58 |
* If null, default to the encoding declaration |
|
59 |
*/ |
|
60 |
private final String charset; |
|
61 |
||
62 |
// remember the value we returned so that the 2nd invocation |
|
63 |
// will return the same object, which is what's expeted out of |
|
64 |
// StreamSource |
|
65 |
private Reader r; |
|
66 |
private InputStream is; |
|
67 |
||
68 |
public DataSourceSource(DataHandler dh) throws MimeTypeParseException { |
|
69 |
this(dh.getDataSource()); |
|
70 |
} |
|
71 |
||
72 |
public DataSourceSource(DataSource source) throws MimeTypeParseException { |
|
73 |
this.source = source; |
|
74 |
||
75 |
String ct = source.getContentType(); |
|
76 |
if(ct==null) { |
|
77 |
charset = null; |
|
78 |
} else { |
|
79 |
MimeType mimeType = new MimeType(ct); |
|
80 |
this.charset = mimeType.getParameter("charset"); |
|
81 |
} |
|
82 |
} |
|
83 |
||
84 |
@Override |
|
85 |
public void setReader(Reader reader) { |
|
86 |
throw new UnsupportedOperationException(); |
|
87 |
} |
|
88 |
||
89 |
@Override |
|
90 |
public void setInputStream(InputStream inputStream) { |
|
91 |
throw new UnsupportedOperationException(); |
|
92 |
} |
|
93 |
||
94 |
@Override |
|
95 |
public Reader getReader() { |
|
96 |
try { |
|
97 |
if(charset==null) return null; |
|
98 |
if(r==null) |
|
99 |
r = new InputStreamReader(source.getInputStream(),charset); |
|
100 |
return r; |
|
101 |
} catch (IOException e) { |
|
102 |
// argh |
|
103 |
throw new RuntimeException(e); |
|
104 |
} |
|
105 |
} |
|
106 |
||
107 |
@Override |
|
108 |
public InputStream getInputStream() { |
|
109 |
try { |
|
110 |
if(charset!=null) return null; |
|
111 |
if(is==null) |
|
112 |
is = source.getInputStream(); |
|
113 |
return is; |
|
114 |
} catch (IOException e) { |
|
115 |
// argh |
|
116 |
throw new RuntimeException(e); |
|
117 |
} |
|
118 |
} |
|
119 |
||
120 |
public DataSource getDataSource() { |
|
121 |
return source; |
|
122 |
} |
|
123 |
} |