001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.solr.util;
017:
018: import java.io.ByteArrayInputStream;
019: import java.io.File;
020: import java.io.FileInputStream;
021: import java.io.FileReader;
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.io.InputStreamReader;
025: import java.io.Reader;
026: import java.io.StringReader;
027: import java.net.URL;
028: import java.net.URLConnection;
029:
030: /**
031: * Three concrete implementations for ContentStream - one for File/URL/String
032: *
033: * @author ryan
034: * @version $Id$
035: * @since solr 1.2
036: */
037: public abstract class ContentStreamBase implements ContentStream {
038: public static final String DEFAULT_CHARSET = "utf-8";
039:
040: protected String name;
041: protected String sourceInfo;
042: protected String contentType;
043: protected Long size;
044:
045: //---------------------------------------------------------------------
046: //---------------------------------------------------------------------
047:
048: public static String getCharsetFromContentType(String contentType) {
049: if (contentType != null) {
050: int idx = contentType.toLowerCase().indexOf("charset=");
051: if (idx > 0) {
052: return contentType.substring(idx + "charset=".length())
053: .trim();
054: }
055: }
056: return null;
057: }
058:
059: //------------------------------------------------------------------------
060: //------------------------------------------------------------------------
061:
062: /**
063: * Construct a <code>ContentStream</code> from a <code>URL</code>
064: *
065: * This uses a <code>URLConnection</code> to get the content stream
066: * @see URLConnection
067: */
068: public static class URLStream extends ContentStreamBase {
069: private final URL url;
070: final URLConnection conn;
071:
072: public URLStream(URL url) throws IOException {
073: this .url = url;
074: this .conn = this .url.openConnection();
075:
076: contentType = conn.getContentType();
077: name = url.toExternalForm();
078: size = new Long(conn.getContentLength());
079: sourceInfo = "url";
080: }
081:
082: public InputStream getStream() throws IOException {
083: return conn.getInputStream();
084: }
085: }
086:
087: /**
088: * Construct a <code>ContentStream</code> from a <code>File</code>
089: */
090: public static class FileStream extends ContentStreamBase {
091: private final File file;
092:
093: public FileStream(File f) throws IOException {
094: file = f;
095:
096: contentType = null; // ??
097: name = file.getName();
098: size = file.length();
099: sourceInfo = file.toURI().toString();
100: }
101:
102: public InputStream getStream() throws IOException {
103: return new FileInputStream(file);
104: }
105:
106: /**
107: * If an charset is defined (by the contentType) ues that, otherwise
108: * use a file reader
109: */
110: public Reader getReader() throws IOException {
111: String charset = getCharsetFromContentType(contentType);
112: return charset == null ? new FileReader(file)
113: : new InputStreamReader(getStream(), charset);
114: }
115: }
116:
117: /**
118: * Construct a <code>ContentStream</code> from a <code>File</code>
119: */
120: public static class StringStream extends ContentStreamBase {
121: private final String str;
122:
123: public StringStream(String str) {
124: this .str = str;
125:
126: contentType = null;
127: name = null;
128: size = new Long(str.length());
129: sourceInfo = "string";
130: }
131:
132: public InputStream getStream() throws IOException {
133: return new ByteArrayInputStream(str.getBytes());
134: }
135:
136: /**
137: * If an charset is defined (by the contentType) ues that, otherwise
138: * use a StringReader
139: */
140: public Reader getReader() throws IOException {
141: String charset = getCharsetFromContentType(contentType);
142: return charset == null ? new StringReader(str)
143: : new InputStreamReader(getStream(), charset);
144: }
145: }
146:
147: /**
148: * Base reader implementation. If the contentType declares a
149: * charset use it, otherwise use "utf-8".
150: */
151: public Reader getReader() throws IOException {
152: String charset = getCharsetFromContentType(getContentType());
153: return charset == null ? new InputStreamReader(getStream(),
154: DEFAULT_CHARSET) : new InputStreamReader(getStream(),
155: charset);
156: }
157:
158: //------------------------------------------------------------------
159: // Getters / Setters for overrideable attributes
160: //------------------------------------------------------------------
161:
162: public String getContentType() {
163: return contentType;
164: }
165:
166: public void setContentType(String contentType) {
167: this .contentType = contentType;
168: }
169:
170: public String getName() {
171: return name;
172: }
173:
174: public void setName(String name) {
175: this .name = name;
176: }
177:
178: public Long getSize() {
179: return size;
180: }
181:
182: public void setSize(Long size) {
183: this .size = size;
184: }
185:
186: public String getSourceInfo() {
187: return sourceInfo;
188: }
189:
190: public void setSourceInfo(String sourceInfo) {
191: this.sourceInfo = sourceInfo;
192: }
193: }
|