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: */
017: package org.apache.wicket.util.resource;
018:
019: import java.io.IOException;
020: import java.io.InputStreamReader;
021: import java.io.Reader;
022: import java.nio.charset.Charset;
023: import java.util.Locale;
024:
025: import org.apache.wicket.WicketRuntimeException;
026: import org.apache.wicket.util.io.IOUtils;
027: import org.apache.wicket.util.io.Streams;
028: import org.apache.wicket.util.time.Time;
029:
030: /**
031: * @see org.apache.wicket.util.resource.IResourceStream
032: *
033: * @author Jonathan Locke
034: */
035: public abstract class AbstractResourceStream implements
036: IStringResourceStream {
037: /** Charset for resource */
038: private Charset charset;
039: private Locale locale;
040:
041: /**
042: * Sets the character set used for reading this resource.
043: *
044: * @param charset
045: * Charset for component
046: */
047: public void setCharset(final Charset charset) {
048: this .charset = charset;
049: }
050:
051: /**
052: * @return This resource as a String.
053: */
054: public String asString() {
055: Reader reader = null;
056: try {
057: if (charset == null) {
058: reader = new InputStreamReader(getInputStream());
059: } else {
060: reader = new InputStreamReader(getInputStream(),
061: charset);
062: }
063: return Streams.readString(reader);
064: } catch (IOException e) {
065: throw new WicketRuntimeException(
066: "Unable to read resource as String", e);
067: } catch (ResourceStreamNotFoundException e) {
068: throw new WicketRuntimeException(
069: "Unable to read resource as String", e);
070: } finally {
071: IOUtils.closeQuietly(reader);
072: try {
073: close();
074: } catch (IOException e) {
075: // ignore
076: }
077: }
078: }
079:
080: /**
081: * @return Charset for resource
082: */
083: protected Charset getCharset() {
084: return charset;
085: }
086:
087: /**
088: * @see org.apache.wicket.util.resource.IResourceStream#getLocale()
089: */
090: public Locale getLocale() {
091: return locale;
092: }
093:
094: /**
095: * @see org.apache.wicket.util.resource.IResourceStream#setLocale(java.util.Locale)
096: */
097: public void setLocale(Locale locale) {
098: this .locale = locale;
099: }
100:
101: /**
102: * @see org.apache.wicket.util.resource.IResourceStream#length()
103: */
104: public long length() {
105: return -1;
106: }
107:
108: /**
109: * @see org.apache.wicket.util.resource.IResourceStream#getContentType()
110: */
111: public String getContentType() {
112: return null;
113: }
114:
115: /**
116: * @see org.apache.wicket.util.watch.IModifiable#lastModifiedTime()
117: */
118: public Time lastModifiedTime() {
119: return null;
120: }
121: }
|