dew/src/main/java/org/apidesign/bck2brwsr/dew/ClassLoaderJavaFileObject.java
author tzezula
Wed, 02 Oct 2013 22:32:57 +0200
changeset 1325 f7f25ea1bbec
parent 1324 263482b074e9
child 1326 8ae6a6c42b5f
permissions -rw-r--r--
Fixed ClassLoader problems.
tzezula@1324
     1
/*
tzezula@1324
     2
 * To change this license header, choose License Headers in Project Properties.
tzezula@1324
     3
 * To change this template file, choose Tools | Templates
tzezula@1324
     4
 * and open the template in the editor.
tzezula@1324
     5
 */
tzezula@1324
     6
tzezula@1324
     7
package org.apidesign.bck2brwsr.dew;
tzezula@1324
     8
tzezula@1324
     9
import java.io.BufferedReader;
tzezula@1324
    10
import java.io.FileNotFoundException;
tzezula@1324
    11
import java.io.IOException;
tzezula@1324
    12
import java.io.InputStream;
tzezula@1324
    13
import java.io.InputStreamReader;
tzezula@1324
    14
import java.io.OutputStream;
tzezula@1324
    15
import java.io.OutputStreamWriter;
tzezula@1324
    16
import java.io.Reader;
tzezula@1324
    17
import java.io.Writer;
tzezula@1324
    18
tzezula@1324
    19
/**
tzezula@1324
    20
 *
tzezula@1324
    21
 * @author Tomas Zezula
tzezula@1324
    22
 */
tzezula@1324
    23
class ClassLoaderJavaFileObject extends BaseFileObject {
tzezula@1324
    24
tzezula@1324
    25
    ClassLoaderJavaFileObject(final String path) {
tzezula@1324
    26
        super(path, getKind(path));
tzezula@1324
    27
    }    
tzezula@1324
    28
tzezula@1324
    29
    @Override
tzezula@1324
    30
    public InputStream openInputStream() throws IOException {
tzezula@1324
    31
        final InputStream in = getClass().getClassLoader().getResourceAsStream(path.substring(1));
tzezula@1324
    32
        if (in == null) {
tzezula@1324
    33
            throw new FileNotFoundException(path);
tzezula@1324
    34
        }
tzezula@1324
    35
        return in;
tzezula@1324
    36
    }
tzezula@1324
    37
tzezula@1324
    38
    @Override
tzezula@1324
    39
    public OutputStream openOutputStream() throws IOException {
tzezula@1324
    40
        throw new UnsupportedOperationException("Read Only FileObject");    //NOI18N
tzezula@1324
    41
    }
tzezula@1324
    42
tzezula@1324
    43
    @Override
tzezula@1324
    44
    public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
tzezula@1324
    45
        return new InputStreamReader(openInputStream());
tzezula@1324
    46
    }
tzezula@1324
    47
tzezula@1324
    48
    @Override
tzezula@1324
    49
    public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
tzezula@1324
    50
        final BufferedReader in = new BufferedReader(openReader(ignoreEncodingErrors));
tzezula@1324
    51
        try {
tzezula@1324
    52
            final StringBuilder sb = new StringBuilder();
tzezula@1324
    53
            String line;
tzezula@1324
    54
            while ((line = in.readLine()) != null) {
tzezula@1324
    55
                sb.append(line);
tzezula@1324
    56
                sb.append('\n');    //NOI18N
tzezula@1324
    57
            }
tzezula@1324
    58
            return sb.toString();
tzezula@1324
    59
        } finally {
tzezula@1324
    60
            in.close();
tzezula@1324
    61
        }
tzezula@1324
    62
    }
tzezula@1324
    63
tzezula@1324
    64
    @Override
tzezula@1324
    65
    public Writer openWriter() throws IOException {
tzezula@1324
    66
        return new OutputStreamWriter(openOutputStream());
tzezula@1324
    67
    }
tzezula@1324
    68
tzezula@1324
    69
    @Override
tzezula@1324
    70
    public long getLastModified() {
tzezula@1324
    71
        return System.currentTimeMillis();
tzezula@1324
    72
    }
tzezula@1324
    73
tzezula@1324
    74
    @Override
tzezula@1324
    75
    public boolean delete() {
tzezula@1324
    76
        return false;
tzezula@1324
    77
    }
tzezula@1324
    78
tzezula@1324
    79
}