001: /*
002: * Copyright (c) 2007, intarsys consulting GmbH
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * - Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * - Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * - Neither the name of intarsys nor the names of its contributors may be used
015: * to endorse or promote products derived from this software without specific
016: * prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
028: * POSSIBILITY OF SUCH DAMAGE.
029: */
030: package de.intarsys.tools.locator;
031:
032: import java.io.ByteArrayInputStream;
033: import java.io.IOException;
034: import java.io.InputStream;
035: import java.io.OutputStream;
036: import java.io.Reader;
037: import java.io.StringReader;
038: import java.io.Writer;
039: import java.net.URL;
040:
041: import de.intarsys.tools.file.FileTools;
042: import de.intarsys.tools.randomaccess.IRandomAccess;
043: import de.intarsys.tools.randomaccess.RandomAccessByteArray;
044:
045: /**
046: * A simple adapter from a {@link String} to {@link ILocator}.
047: *
048: */
049: public class StringLocator extends CommonLocator {
050: final private String content;
051:
052: private String type;
053:
054: private String localName;
055:
056: public StringLocator(String content, String name, String type) {
057: super ();
058: this .content = content;
059: this .localName = name;
060: this .type = type;
061: }
062:
063: /*
064: * (non-Javadoc)
065: *
066: * @see de.intarsys.tools.locator.ILocator#getFullName()
067: */
068: public String getFullName() {
069: if (getType() == null) {
070: return getLocalName();
071: } else {
072: return getLocalName() + "." + getType();
073: }
074: }
075:
076: /*
077: * (non-Javadoc)
078: *
079: * @see de.intarsys.tools.locator.ILocator#getInputStream()
080: */
081: public InputStream getInputStream() throws IOException {
082: return new ByteArrayInputStream(getContent().getBytes());
083: }
084:
085: /*
086: * (non-Javadoc)
087: *
088: * @see de.intarsys.tools.locator.ILocator#getLocalName()
089: */
090: public String getLocalName() {
091: return localName;
092: }
093:
094: /*
095: * (non-Javadoc)
096: *
097: * @see de.intarsys.tools.locator.ILocator#getTypedName()
098: */
099: public String getTypedName() {
100: return (type == null) ? localName : (localName + "." + type);
101: }
102:
103: /*
104: * (non-Javadoc)
105: *
106: * @see de.intarsys.tools.locator.ILocator#getReader()
107: */
108: public Reader getReader() throws IOException {
109: return new StringReader(getContent());
110: }
111:
112: /*
113: * (non-Javadoc)
114: *
115: * @see de.intarsys.tools.locator.ILocator#getReader(java.lang.String)
116: */
117: public Reader getReader(String encoding) throws IOException {
118: return new StringReader(getContent());
119: }
120:
121: /*
122: * (non-Javadoc)
123: *
124: * @see de.intarsys.tools.locator.ILocator#getType()
125: */
126: public String getType() {
127: return type;
128: }
129:
130: /*
131: * (non-Javadoc)
132: *
133: * @see de.intarsys.tools.locator.ILocator#listLocators(de.intarsys.tools.locator.ILocatorNameFilter)
134: */
135: public ILocator[] listLocators(ILocatorNameFilter filter)
136: throws IOException {
137: return new ILocator[0];
138: }
139:
140: /*
141: * (non-Javadoc)
142: *
143: * @see de.intarsys.tools.locator.ILocator#isDirectory()
144: */
145: public boolean isDirectory() {
146: return false;
147: }
148:
149: /*
150: * (non-Javadoc)
151: *
152: * @see de.intarsys.tools.locator.ILocator#exists()
153: */
154: public boolean exists() {
155: return getContent() != null;
156: }
157:
158: /*
159: * (non-Javadoc)
160: *
161: * @see de.intarsys.tools.locator.ILocator#getParent()
162: */
163: public ILocator getParent() {
164: return null;
165: }
166:
167: /*
168: * (non-Javadoc)
169: *
170: * @see de.intarsys.tools.locator.ILocator#getChild(java.lang.String)
171: */
172: public ILocator getChild(String child) {
173: return null;
174: }
175:
176: /*
177: * (non-Javadoc)
178: *
179: * @see de.intarsys.tools.component.ISynchronizable#isOutOfSynch()
180: */
181: public boolean isOutOfSynch() {
182: return false;
183: }
184:
185: /*
186: * (non-Javadoc)
187: *
188: * @see de.intarsys.tools.component.ISynchronizable#isSynchSynchronous()
189: */
190: public boolean isSynchSynchronous() {
191: return false;
192: }
193:
194: /*
195: * (non-Javadoc)
196: *
197: * @see de.intarsys.tools.component.ISynchronizable#synch()
198: */
199: public void synch() {
200: //
201: }
202:
203: protected String getContent() {
204: return content;
205: }
206:
207: /*
208: * (non-Javadoc)
209: *
210: * @see de.intarsys.tools.locator.ILocator#getOutputStream()
211: */
212: public OutputStream getOutputStream() throws IOException {
213: throw new UnsupportedOperationException();
214: }
215:
216: /*
217: * (non-Javadoc)
218: *
219: * @see de.intarsys.tools.locator.ILocator#getWriter()
220: */
221: public Writer getWriter() throws IOException {
222: throw new UnsupportedOperationException();
223: }
224:
225: /*
226: * (non-Javadoc)
227: *
228: * @see de.intarsys.tools.locator.ILocator#getWriter(java.lang.String)
229: */
230: public Writer getWriter(String encoding) throws IOException {
231: throw new UnsupportedOperationException();
232: }
233:
234: /*
235: * (non-Javadoc)
236: *
237: * @see de.intarsys.tools.locator.ILocator#toURL()
238: */
239: public URL toURL() {
240: return null;
241: }
242:
243: /*
244: * (non-Javadoc)
245: *
246: * @see de.intarsys.tools.locator.ILocator#getRandomAccessData()
247: */
248: public IRandomAccess getRandomAccess() throws IOException {
249: return new RandomAccessByteArray(getContent().getBytes());
250: }
251:
252: /*
253: * (non-Javadoc)
254: *
255: * @see de.intarsys.tools.locator.ILocator#isReadOnly()
256: */
257: public boolean isReadOnly() {
258: return true;
259: }
260:
261: public void rename(String newName) throws IOException {
262: localName = FileTools.getBaseName(newName);
263: type = FileTools.getExtension(newName, getType());
264: }
265:
266: public void delete() throws IOException {
267: // nothing to do...
268: }
269:
270: }
|