001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.bind.v2.util;
038:
039: import java.io.IOException;
040: import java.io.InputStream;
041: import java.io.InputStreamReader;
042: import java.io.Reader;
043:
044: import javax.activation.DataHandler;
045: import javax.activation.DataSource;
046: import javax.activation.MimeType;
047: import javax.activation.MimeTypeParseException;
048: import javax.xml.transform.Source;
049: import javax.xml.transform.stream.StreamSource;
050:
051: /**
052: * {@link Source} implementation backed by {@link DataHandler}.
053: *
054: * <p>
055: * This implementation allows the same {@link Source} to be used
056: * mutliple times.
057: *
058: * <p>
059: * {@link Source} isn't really pluggable. As a consequence,
060: * this implementation is clunky --- weak against unexpected
061: * usage of the class.
062: *
063: * @author Kohsuke Kawaguchi
064: */
065: public final class DataSourceSource extends StreamSource {
066: private final DataSource source;
067:
068: /**
069: * If null, default to the encoding declaration
070: */
071: private final String charset;
072:
073: // remember the value we returned so that the 2nd invocation
074: // will return the same object, which is what's expeted out of
075: // StreamSource
076: private Reader r;
077: private InputStream is;
078:
079: public DataSourceSource(DataHandler dh)
080: throws MimeTypeParseException {
081: this (dh.getDataSource());
082: }
083:
084: public DataSourceSource(DataSource source)
085: throws MimeTypeParseException {
086: this .source = source;
087:
088: String ct = source.getContentType();
089: if (ct == null) {
090: charset = null;
091: } else {
092: MimeType mimeType = new MimeType(ct);
093: this .charset = mimeType.getParameter("charset");
094: }
095: }
096:
097: @Override
098: public void setReader(Reader reader) {
099: throw new UnsupportedOperationException();
100: }
101:
102: @Override
103: public void setInputStream(InputStream inputStream) {
104: throw new UnsupportedOperationException();
105: }
106:
107: @Override
108: public Reader getReader() {
109: try {
110: if (charset == null)
111: return null;
112: if (r == null)
113: r = new InputStreamReader(source.getInputStream(),
114: charset);
115: return r;
116: } catch (IOException e) {
117: // argh
118: throw new RuntimeException(e);
119: }
120: }
121:
122: @Override
123: public InputStream getInputStream() {
124: try {
125: if (charset != null)
126: return null;
127: if (is == null)
128: is = source.getInputStream();
129: return is;
130: } catch (IOException e) {
131: // argh
132: throw new RuntimeException(e);
133: }
134: }
135:
136: public DataSource getDataSource() {
137: return source;
138: }
139: }
|