| 1 | |
package org.webslinger.vfs; |
| 2 | |
|
| 3 | |
import java.io.ByteArrayInputStream; |
| 4 | |
import java.io.InputStream; |
| 5 | |
import java.io.IOException; |
| 6 | |
import java.io.OutputStream; |
| 7 | |
import java.util.Collection; |
| 8 | |
import java.util.Collections; |
| 9 | |
import java.util.Map; |
| 10 | |
|
| 11 | |
import org.webslinger.io.Charsets; |
| 12 | |
import org.webslinger.io.IOUtil; |
| 13 | |
|
| 14 | |
public class GeneratedStringVFSDelegate<T> extends VFSDelegate<GeneratedStringVFSDelegate.GeneratedString, GeneratedStringVFSDelegate.GeneratedString, T> { |
| 15 | |
protected final VFSDelegate<?, T, T> delegate; |
| 16 | |
|
| 17 | 26 | public GeneratedStringVFSDelegate(VFSDelegate<?, T, T> delegate) { |
| 18 | 26 | this.delegate = delegate; |
| 19 | 26 | } |
| 20 | |
|
| 21 | |
public VFSDelegate<?, T, T> getVFSDelegate() { |
| 22 | 0 | return delegate; |
| 23 | |
} |
| 24 | |
|
| 25 | |
public GeneratedString<T> getId(GeneratedString<T> s) { |
| 26 | 0 | return s; |
| 27 | |
} |
| 28 | |
|
| 29 | |
public Type getType(GeneratedString<T> s) { |
| 30 | 0 | return Type.FILE; |
| 31 | |
} |
| 32 | |
|
| 33 | |
public long getLastModifiedTime(GeneratedString<T> s) throws IOException { |
| 34 | 0 | return s.getLastModifiedTime(); |
| 35 | |
} |
| 36 | |
|
| 37 | |
public Collection<Permission> getPermissions(GeneratedString<T> s) throws IOException { |
| 38 | 0 | return Permission.READABLE_SET; |
| 39 | |
} |
| 40 | |
|
| 41 | |
public String absolutePath(GeneratedString<T> s) throws IOException { |
| 42 | 0 | return null; |
| 43 | |
} |
| 44 | |
|
| 45 | |
public String getBaseName(GeneratedString<T> s) throws IOException { |
| 46 | 0 | return null; |
| 47 | |
} |
| 48 | |
|
| 49 | |
public T getParent(GeneratedString<T> s) throws IOException { |
| 50 | 0 | return s.source; |
| 51 | |
} |
| 52 | |
|
| 53 | |
public T resolve(GeneratedString<T> s, String name) throws IOException { |
| 54 | 0 | return getVFSDelegate().resolve(s.source, name); |
| 55 | |
} |
| 56 | |
|
| 57 | |
public T[] getChildren(GeneratedString<T> s) throws IOException { |
| 58 | 0 | return getVFSDelegate().getChildren(s.source); |
| 59 | |
} |
| 60 | |
|
| 61 | |
public boolean attributeExists(GeneratedString<T> s, String name) throws IOException { |
| 62 | 0 | return s.attributeExists(name); |
| 63 | |
} |
| 64 | |
|
| 65 | |
public Object getAttribute(GeneratedString<T> s, String name) throws IOException { |
| 66 | 0 | return s.getAttribute(name); |
| 67 | |
} |
| 68 | |
|
| 69 | |
public String getContentType(GeneratedString<T> s) throws IOException { |
| 70 | 0 | return "text/plain"; |
| 71 | |
} |
| 72 | |
|
| 73 | |
public long getLength(GeneratedString<T> s) throws IOException { |
| 74 | 0 | return s.getResult().length(); |
| 75 | |
} |
| 76 | |
|
| 77 | |
public String getString(GeneratedString<T> s) throws IOException { |
| 78 | 0 | return s.getResult(); |
| 79 | |
} |
| 80 | |
|
| 81 | |
public InputStream openInput(GeneratedString<T> s) throws IOException { |
| 82 | |
try { |
| 83 | 0 | return new ByteArrayInputStream(IOUtil.getBytes(s.getResult(), Charsets.UTF8)); |
| 84 | 0 | } catch (RuntimeException e) { |
| 85 | 0 | throw e; |
| 86 | 0 | } catch (Error e) { |
| 87 | 0 | throw e; |
| 88 | 0 | } catch (Exception e) { |
| 89 | 0 | throw (IOException) new IOException(e.getMessage()).initCause(e); |
| 90 | |
} |
| 91 | |
} |
| 92 | |
|
| 93 | |
public abstract static class AbstractGeneratedString<T> { |
| 94 | |
public final T source; |
| 95 | |
|
| 96 | 0 | protected AbstractGeneratedString(T source) { |
| 97 | 0 | this.source = source; |
| 98 | 0 | } |
| 99 | |
|
| 100 | |
protected abstract String getResult() throws IOException; |
| 101 | |
protected abstract long getLastModifiedTime() throws IOException; |
| 102 | |
protected abstract boolean attributeExists(String name) throws IOException; |
| 103 | |
protected abstract Object getAttribute(String name) throws IOException; |
| 104 | |
} |
| 105 | |
|
| 106 | |
public static final class GeneratedString<T> extends AbstractGeneratedString<T> { |
| 107 | |
public final String result; |
| 108 | |
public final long lastModifiedTime; |
| 109 | |
public final Map<String, ? super Object> attributes; |
| 110 | |
|
| 111 | |
public GeneratedString(T source, String result, long lastModifiedTime) { |
| 112 | 0 | super(source); |
| 113 | 0 | this.result = result; |
| 114 | 0 | this.lastModifiedTime = lastModifiedTime; |
| 115 | 0 | this.attributes = Collections.emptyMap(); |
| 116 | 0 | } |
| 117 | |
|
| 118 | |
public GeneratedString(T source, String result, long lastModifiedTime, Map<String, ? super Object> attributes) { |
| 119 | 0 | super(source); |
| 120 | 0 | this.result = result; |
| 121 | 0 | this.lastModifiedTime = lastModifiedTime; |
| 122 | 0 | this.attributes = attributes; |
| 123 | 0 | } |
| 124 | |
|
| 125 | |
public boolean equals(Object o) { |
| 126 | 0 | if (!(o instanceof GeneratedString)) return false; |
| 127 | 0 | GeneratedString other = (GeneratedString) o; |
| 128 | 0 | return source.equals(other.source) && result.equals(other.result); |
| 129 | |
} |
| 130 | |
|
| 131 | |
public int hashCode() { |
| 132 | 0 | return source.hashCode() ^ result.hashCode(); |
| 133 | |
} |
| 134 | |
|
| 135 | |
public String getResult() { |
| 136 | 0 | return result; |
| 137 | |
} |
| 138 | |
|
| 139 | |
public long getLastModifiedTime() throws IOException { |
| 140 | 0 | return lastModifiedTime; |
| 141 | |
} |
| 142 | |
|
| 143 | |
public boolean attributeExists(String name) throws IOException { |
| 144 | 0 | return attributes.containsKey(name); |
| 145 | |
} |
| 146 | |
|
| 147 | |
public Object getAttribute(String name) throws IOException { |
| 148 | 0 | return attributes.get(name); |
| 149 | |
} |
| 150 | |
|
| 151 | |
public Map<String, ?> getAttributes() throws IOException { |
| 152 | 0 | return attributes; |
| 153 | |
} |
| 154 | |
} |
| 155 | |
} |