001: /*
002: * Copyright 2005-2007 Noelios Consulting.
003: *
004: * The contents of this file are subject to the terms of the Common Development
005: * and Distribution License (the "License"). You may not use this file except in
006: * compliance with the License.
007: *
008: * You can obtain a copy of the license at
009: * http://www.opensource.org/licenses/cddl1.txt See the License for the specific
010: * language governing permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL HEADER in each file and
013: * include the License file at http://www.opensource.org/licenses/cddl1.txt If
014: * applicable, add the following below this CDDL HEADER, with the fields
015: * enclosed by brackets "[]" replaced with your own identifying information:
016: * Portions Copyright [yyyy] [name of copyright owner]
017: */
018:
019: package org.restlet.resource;
020:
021: import java.io.ByteArrayInputStream;
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.io.OutputStream;
025: import java.io.OutputStreamWriter;
026: import java.io.UnsupportedEncodingException;
027: import java.util.logging.Logger;
028:
029: import org.restlet.data.CharacterSet;
030: import org.restlet.data.Language;
031: import org.restlet.data.MediaType;
032:
033: /**
034: * Represents an Unicode string that can be converted to any character set
035: * supported by Java.
036: *
037: * @author Jerome Louvel (contact@noelios.com)
038: */
039: public class StringRepresentation extends StreamRepresentation {
040: private CharSequence text;
041:
042: /**
043: * Constructor. The following metadata are used by default: "text/plain"
044: * media type, no language and the ISO-8859-1 character set.
045: *
046: * @param text
047: * The string value.
048: */
049: public StringRepresentation(CharSequence text) {
050: this (text, MediaType.TEXT_PLAIN);
051: }
052:
053: /**
054: * Constructor. The following metadata are used by default: "text/plain"
055: * media type, no language and the ISO-8859-1 character set.
056: *
057: * @param text
058: * The string value.
059: * @param language
060: * The language.
061: */
062: public StringRepresentation(CharSequence text, Language language) {
063: this (text, MediaType.TEXT_PLAIN, language);
064: }
065:
066: /**
067: * Constructor. The following metadata are used by default: no language and
068: * the ISO-8859-1 character set.
069: *
070: * @param text
071: * The string value.
072: * @param mediaType
073: * The media type.
074: */
075: public StringRepresentation(CharSequence text, MediaType mediaType) {
076: this (text, mediaType, null);
077: }
078:
079: /**
080: * Constructor. The following metadata are used by default: ISO-8859-1
081: * character set.
082: *
083: * @param text
084: * The string value.
085: * @param mediaType
086: * The media type.
087: * @param language
088: * The language.
089: */
090: public StringRepresentation(CharSequence text, MediaType mediaType,
091: Language language) {
092: this (text, mediaType, language, CharacterSet.ISO_8859_1);
093: }
094:
095: /**
096: * Constructor.
097: *
098: * @param text
099: * The string value.
100: * @param mediaType
101: * The media type.
102: * @param language
103: * The language.
104: * @param characterSet
105: * The character set.
106: */
107: public StringRepresentation(CharSequence text, MediaType mediaType,
108: Language language, CharacterSet characterSet) {
109: super (mediaType);
110: this .text = text;
111: setMediaType(mediaType);
112: if (language != null) {
113: getLanguages().add(language);
114: }
115:
116: setCharacterSet(characterSet);
117: updateSize();
118: }
119:
120: /**
121: * Returns a stream with the representation's content. This method is
122: * ensured to return a fresh stream for each invocation unless it is a
123: * transient representation, in which case null is returned.
124: *
125: * @return A stream with the representation's content.
126: * @throws IOException
127: */
128: public InputStream getStream() throws IOException {
129: if (getText() != null) {
130: if (getCharacterSet() != null) {
131: return new ByteArrayInputStream(getText().getBytes(
132: getCharacterSet().getName()));
133: } else {
134: return new ByteArrayInputStream(getText().getBytes());
135: }
136: } else {
137: return null;
138: }
139: }
140:
141: /**
142: * Converts the representation to a string value. Be careful when using this
143: * method as the conversion of large content to a string fully stored in
144: * memory can result in OutOfMemoryErrors being thrown.
145: *
146: * @return The representation as a string value.
147: */
148: public String getText() {
149: return (this .text == null) ? null : this .text.toString();
150: }
151:
152: /**
153: * Sets the string value.
154: *
155: * @param text
156: * The string value.
157: */
158: public void setText(String text) {
159: this .text = text;
160: updateSize();
161: }
162:
163: /**
164: * Sets the character set or null if not applicable.
165: *
166: * @param characterSet
167: * The character set or null if not applicable.
168: */
169: public void setCharacterSet(CharacterSet characterSet) {
170: super .setCharacterSet(characterSet);
171: updateSize();
172: }
173:
174: /**
175: * Updates the expected size according to the current string value.
176: */
177: protected void updateSize() {
178: if (getText() != null) {
179: try {
180: if (getCharacterSet() != null) {
181: setSize(getText().getBytes(
182: getCharacterSet().getName()).length);
183: } else {
184: setSize(getText().getBytes().length);
185: }
186: } catch (UnsupportedEncodingException e) {
187: Logger.getLogger(StringRepresentation.class
188: .getCanonicalName());
189: setSize(UNKNOWN_SIZE);
190: }
191: } else {
192: setSize(UNKNOWN_SIZE);
193: }
194: }
195:
196: /**
197: * Writes the representation to a byte stream. This method is ensured to
198: * write the full content for each invocation unless it is a transient
199: * representation, in which case an exception is thrown.
200: *
201: * @param outputStream
202: * The output stream.
203: * @throws IOException
204: */
205: public void write(OutputStream outputStream) throws IOException {
206: if (getText() != null) {
207: OutputStreamWriter osw = null;
208:
209: if (getCharacterSet() != null) {
210: osw = new OutputStreamWriter(outputStream,
211: getCharacterSet().getName());
212: } else {
213: osw = new OutputStreamWriter(outputStream);
214: }
215:
216: osw.write(getText());
217: osw.flush();
218: }
219: }
220:
221: }
|