1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/bind/v2/util/DataSourceSource.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,123 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.xml.internal.bind.v2.util; 1.30 + 1.31 +import java.io.IOException; 1.32 +import java.io.InputStream; 1.33 +import java.io.InputStreamReader; 1.34 +import java.io.Reader; 1.35 + 1.36 +import javax.activation.DataHandler; 1.37 +import javax.activation.DataSource; 1.38 +import javax.activation.MimeType; 1.39 +import javax.activation.MimeTypeParseException; 1.40 +import javax.xml.transform.Source; 1.41 +import javax.xml.transform.stream.StreamSource; 1.42 + 1.43 +/** 1.44 + * {@link Source} implementation backed by {@link DataHandler}. 1.45 + * 1.46 + * <p> 1.47 + * This implementation allows the same {@link Source} to be used 1.48 + * mutliple times. 1.49 + * 1.50 + * <p> 1.51 + * {@link Source} isn't really pluggable. As a consequence, 1.52 + * this implementation is clunky --- weak against unexpected 1.53 + * usage of the class. 1.54 + * 1.55 + * @author Kohsuke Kawaguchi 1.56 + */ 1.57 +public final class DataSourceSource extends StreamSource { 1.58 + private final DataSource source; 1.59 + 1.60 + /** 1.61 + * If null, default to the encoding declaration 1.62 + */ 1.63 + private final String charset; 1.64 + 1.65 + // remember the value we returned so that the 2nd invocation 1.66 + // will return the same object, which is what's expeted out of 1.67 + // StreamSource 1.68 + private Reader r; 1.69 + private InputStream is; 1.70 + 1.71 + public DataSourceSource(DataHandler dh) throws MimeTypeParseException { 1.72 + this(dh.getDataSource()); 1.73 + } 1.74 + 1.75 + public DataSourceSource(DataSource source) throws MimeTypeParseException { 1.76 + this.source = source; 1.77 + 1.78 + String ct = source.getContentType(); 1.79 + if(ct==null) { 1.80 + charset = null; 1.81 + } else { 1.82 + MimeType mimeType = new MimeType(ct); 1.83 + this.charset = mimeType.getParameter("charset"); 1.84 + } 1.85 + } 1.86 + 1.87 + @Override 1.88 + public void setReader(Reader reader) { 1.89 + throw new UnsupportedOperationException(); 1.90 + } 1.91 + 1.92 + @Override 1.93 + public void setInputStream(InputStream inputStream) { 1.94 + throw new UnsupportedOperationException(); 1.95 + } 1.96 + 1.97 + @Override 1.98 + public Reader getReader() { 1.99 + try { 1.100 + if(charset==null) return null; 1.101 + if(r==null) 1.102 + r = new InputStreamReader(source.getInputStream(),charset); 1.103 + return r; 1.104 + } catch (IOException e) { 1.105 + // argh 1.106 + throw new RuntimeException(e); 1.107 + } 1.108 + } 1.109 + 1.110 + @Override 1.111 + public InputStream getInputStream() { 1.112 + try { 1.113 + if(charset!=null) return null; 1.114 + if(is==null) 1.115 + is = source.getInputStream(); 1.116 + return is; 1.117 + } catch (IOException e) { 1.118 + // argh 1.119 + throw new RuntimeException(e); 1.120 + } 1.121 + } 1.122 + 1.123 + public DataSource getDataSource() { 1.124 + return source; 1.125 + } 1.126 +}