JcrPropertyResource.java 1.86 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.jcr.Binary
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 */
package com.adobe.granite.ui.clientlibs.impl;

import com.adobe.granite.ui.clientlibs.script.ScriptResource;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;

public class JcrPropertyResource
implements ScriptResource {
    private final Property data;
    private final String name;

    public JcrPropertyResource(Property data, String name) {
        this.name = name;
        this.data = data;
    }

    @Override
    public String getName() {
        return this.name;
    }

    @Override
    public long getSize() {
        try {
            return this.data.getLength();
        }
        catch (RepositoryException e) {
            return -1;
        }
    }

    public Property getData() {
        return this.data;
    }

    public String getEncoding() {
        String encoding = "utf-8";
        try {
            if (this.data.getParent().hasProperty("{http://www.jcp.org/jcr/1.0}encoding")) {
                encoding = this.data.getParent().getProperty("{http://www.jcp.org/jcr/1.0}encoding").getString();
            }
        }
        catch (RepositoryException e) {
            // empty catch block
        }
        return encoding;
    }

    @Override
    public Reader getReader() throws IOException {
        try {
            return new InputStreamReader(this.data.getBinary().getStream(), this.getEncoding());
        }
        catch (RepositoryException e) {
            IOException io = new IOException("Unable to provide data stream.");
            io.initCause((Throwable)e);
            throw io;
        }
    }
}