1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/dew/src/main/java/org/apidesign/bck2brwsr/dew/ClassLoaderJavaFileObject.java Wed Oct 02 21:00:24 2013 +0200
1.3 @@ -0,0 +1,81 @@
1.4 +/*
1.5 + * To change this license header, choose License Headers in Project Properties.
1.6 + * To change this template file, choose Tools | Templates
1.7 + * and open the template in the editor.
1.8 + */
1.9 +
1.10 +package org.apidesign.bck2brwsr.dew;
1.11 +
1.12 +import java.io.BufferedReader;
1.13 +import java.io.FileNotFoundException;
1.14 +import java.io.IOException;
1.15 +import java.io.InputStream;
1.16 +import java.io.InputStreamReader;
1.17 +import java.io.OutputStream;
1.18 +import java.io.OutputStreamWriter;
1.19 +import java.io.Reader;
1.20 +import java.io.Writer;
1.21 +
1.22 +/**
1.23 + *
1.24 + * @author Tomas Zezula
1.25 + */
1.26 +class ClassLoaderJavaFileObject extends BaseFileObject {
1.27 +
1.28 + ClassLoaderJavaFileObject(final String path) {
1.29 + super(path, getKind(path));
1.30 + }
1.31 +
1.32 + @Override
1.33 + public InputStream openInputStream() throws IOException {
1.34 + final InputStream in = getClass().getClassLoader().getResourceAsStream(path.substring(1));
1.35 + if (in == null) {
1.36 + getClass().getClassLoader().getResourceAsStream(path.substring(1));
1.37 + throw new FileNotFoundException(path);
1.38 +
1.39 + }
1.40 + return in;
1.41 + }
1.42 +
1.43 + @Override
1.44 + public OutputStream openOutputStream() throws IOException {
1.45 + throw new UnsupportedOperationException("Read Only FileObject"); //NOI18N
1.46 + }
1.47 +
1.48 + @Override
1.49 + public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
1.50 + return new InputStreamReader(openInputStream());
1.51 + }
1.52 +
1.53 + @Override
1.54 + public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
1.55 + final BufferedReader in = new BufferedReader(openReader(ignoreEncodingErrors));
1.56 + try {
1.57 + final StringBuilder sb = new StringBuilder();
1.58 + String line;
1.59 + while ((line = in.readLine()) != null) {
1.60 + sb.append(line);
1.61 + sb.append('\n'); //NOI18N
1.62 + }
1.63 + return sb.toString();
1.64 + } finally {
1.65 + in.close();
1.66 + }
1.67 + }
1.68 +
1.69 + @Override
1.70 + public Writer openWriter() throws IOException {
1.71 + return new OutputStreamWriter(openOutputStream());
1.72 + }
1.73 +
1.74 + @Override
1.75 + public long getLastModified() {
1.76 + return System.currentTimeMillis();
1.77 + }
1.78 +
1.79 + @Override
1.80 + public boolean delete() {
1.81 + return false;
1.82 + }
1.83 +
1.84 +}