1 19 20 package org.netbeans.modules.settings; 21 22 import java.io.IOException ; 23 import java.io.Reader ; 24 import java.io.Writer ; 25 import org.openide.filesystems.*; 26 import org.openide.util.Lookup; 27 import org.openide.util.lookup.Lookups; 28 29 33 public final class ContextProvider { 34 35 36 private ContextProvider() { 37 } 38 39 42 public static Writer createWriterContextProvider(Writer w, FileObject src) { 43 return new WriterProvider(w, src); 44 } 45 46 49 public static Reader createReaderContextProvider(Reader r, FileObject src) { 50 return new ReaderProvider(r, src); 51 } 52 53 private static final class WriterProvider extends Writer implements Lookup.Provider { 54 private final Writer orig; 55 private final FileObject src; 56 private Lookup lookup; 57 58 public WriterProvider(Writer w, FileObject src) { 59 this.orig = w; 60 this.src = src; 61 } 62 63 public void close() throws IOException { 64 orig.close(); 65 } 66 67 public void flush() throws IOException { 68 orig.flush(); 69 } 70 71 public void write(char[] cbuf, int off, int len) throws IOException { 72 orig.write(cbuf, off, len); 73 } 74 75 public Lookup getLookup() { 76 if (lookup == null) { 77 lookup = Lookups.singleton(new FileObjectContext(src)); 78 } 79 return lookup; 80 } 81 82 } 83 84 private static final class ReaderProvider extends Reader implements Lookup.Provider { 85 private final Reader orig; 86 private final FileObject src; 87 private Lookup lookup; 88 89 public ReaderProvider(Reader r, FileObject src) { 90 this.orig = r; 91 this.src = src; 92 } 93 94 public void close() throws IOException { 95 orig.close(); 96 } 97 98 public int read(char[] cbuf, int off, int len) throws IOException { 99 return orig.read(cbuf, off, len); 100 } 101 102 public Lookup getLookup() { 103 if (lookup == null) { 104 lookup = Lookups.singleton(new FileObjectContext(src)); 105 } 106 return lookup; 107 } 108 109 } 110 111 115 private static final class FileObjectContext extends FileObject { 116 private static final String UNSUPPORTED = "The Restricted FileObject" + " implementation allowing to get just read-only informations about" + " name and location. It should prevent any manipulation with file" + " or its content."; private final FileObject fo; 121 122 public FileObjectContext(FileObject fo) { 123 this.fo = fo; 124 } 125 126 public void addFileChangeListener(FileChangeListener fcl) { 127 throw new UnsupportedOperationException (UNSUPPORTED); 128 } 129 130 public FileObject createData(String name, String ext) throws IOException { 131 throw new UnsupportedOperationException (UNSUPPORTED); 132 } 133 134 public FileObject createFolder(String name) throws IOException { 135 throw new UnsupportedOperationException (UNSUPPORTED); 136 } 137 138 public void delete(FileLock lock) throws IOException { 139 throw new UnsupportedOperationException (UNSUPPORTED); 140 } 141 142 public Object getAttribute(String attrName) { 143 return fo.getAttribute(attrName); 144 } 145 146 public java.util.Enumeration <String > getAttributes() { 147 return fo.getAttributes(); 148 } 149 150 public FileObject[] getChildren() { 151 return new FileObject[0]; 152 } 153 154 public String getExt() { 155 return fo.getExt(); } 157 158 public FileObject getFileObject(String name, String ext) { 159 return null; 160 } 161 162 public FileSystem getFileSystem() throws FileStateInvalidException { 163 return fo.getFileSystem(); 164 } 165 166 public java.io.InputStream getInputStream() throws java.io.FileNotFoundException { 167 throw new UnsupportedOperationException (UNSUPPORTED); 168 } 169 170 public String getName() { 171 return fo.getName(); 172 } 173 174 public java.io.OutputStream getOutputStream(FileLock lock) throws java.io.IOException { 175 throw new UnsupportedOperationException (UNSUPPORTED); 176 } 177 178 public FileObject getParent() { 179 return fo.getParent(); 180 } 181 182 public long getSize() { 183 return fo.getSize(); 184 } 185 186 public boolean isData() { 187 return true; 188 } 189 190 public boolean isFolder() { 191 return false; 192 } 193 194 public boolean isReadOnly() { 195 return fo.isReadOnly(); 196 } 197 198 public boolean isRoot() { 199 return false; 200 } 201 202 public boolean isValid() { 203 return fo.isValid(); 204 } 205 206 public java.util.Date lastModified() { 207 return fo.lastModified(); 208 } 209 210 public FileLock lock() throws IOException { 211 throw new UnsupportedOperationException (UNSUPPORTED); 212 } 213 214 public void removeFileChangeListener(FileChangeListener fcl) { 215 throw new UnsupportedOperationException (UNSUPPORTED); 216 } 217 218 public void rename(FileLock lock, String name, String ext) throws IOException { 219 throw new UnsupportedOperationException (UNSUPPORTED); 220 } 221 222 public void setAttribute(String attrName, Object value) throws IOException { 223 throw new UnsupportedOperationException (UNSUPPORTED); 224 } 225 226 public void setImportant(boolean b) { 227 throw new UnsupportedOperationException (UNSUPPORTED); 228 } 229 230 } 231 232 } 233 | Popular Tags |