<commit_before>package controllers;

import java.io.ByteArrayOutputStream;
import java.io.CharArrayReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.jcr.RepositoryException;
import javax.jcr.Session;

import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.PNGTranscoder;
import org.apache.batik.util.XMLResourceDescriptor;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.message.BasicNameValuePair;
import org.codehaus.jackson.node.ArrayNode;
import org.codehaus.jackson.node.ObjectNode;
import org.jcrom.Jcrom;
import org.w3c.dom.Document;

import play.libs.F;
import play.libs.Json;
import play.mvc.Result;
import providers.CacheableUserProvider;
import service.JcrSessionFactory;
import service.filestore.FileStore;
import service.filestore.FileStoreImpl;
import au.edu.uq.aorra.charts.ChartRenderer;
import be.objectify.deadbolt.java.actions.SubjectPresent;

import com.google.common.collect.Lists;
import com.google.inject.Inject;

import ereefs.charts.ChartDescription;
import ereefs.charts.ChartFactory;
import ereefs.charts.ChartType;
import ereefs.spreadsheet.DataSource;
import ereefs.spreadsheet.XlsxDataSource;

public class Chart extends SessionAwareController {

    private final FileStore fileStore;

    @Inject
    public Chart(final JcrSessionFactory sessionFactory,
            final Jcrom jcrom,
            final CacheableUserProvider sessionHandler,
            final FileStore fileStore) {
      super(sessionFactory, jcrom, sessionHandler);
      this.fileStore = fileStore;
    }

    private String buildUrl(ereefs.charts.Chart chart, String format, String[] path) throws UnsupportedEncodingException {
        StringBuilder result = new StringBuilder("charts/");
        result.append(chart.getDescription().getType().toString().toLowerCase());
        result.append(".");
        if(StringUtils.isNotBlank(format)) {
            result.append(format);
        } else {
            result.append("png");
        }
        result.append("?");
        List<NameValuePair> qparams = new ArrayList<NameValuePair>();
        for(Map.Entry<String, String> me : chart.getDescription().getProperties().entrySet()) {
            qparams.add(new BasicNameValuePair(me.getKey(), me.getValue()));
        }
        for(String p : path) {
            qparams.add(new BasicNameValuePair("path", p));
        }
        result.append(URLEncodedUtils.format(qparams, "UTF-8"));
        return result.toString();
    }

    private String getParameter(String key) {
        String[] values = request().queryString().get(key);
        return ((values != null) && (values.length > 0))?values[0]:null;
    }

    private List<DataSource> getDatasources(Session session, String[] paths) throws Exception {
        List<DataSource> result = Lists.newArrayList();
        if(paths == null) {
            return null;
        }
        final FileStore.Manager fm = fileStore.getManager(session);
        for(String path : paths) {
            FileStore.FileOrFolder fof = fm.getFileOrFolder("/"+path);
            if (fof instanceof FileStoreImpl.File) {
                FileStoreImpl.File file = (FileStoreImpl.File) fof;
                // Check this is an OpenXML document (no chance otherwise)
                if (!file.getMimeType().equals(
                    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")) {
                } else {
                    result.add(new XlsxDataSource(file.getData()));
                }
            }
        }
        return result;
    }

    @SubjectPresent
    public Result charts(final String chart) {
        return inUserSession(new F.Function<Session, Result>() {
            @Override
            public final Result apply(Session session) throws RepositoryException {
                try {
                    List<DataSource> datasources = getDatasources(<commit_msg>Removing extra leading slash from chart path handling.<commit_after>package controllers;

import java.io.ByteArrayOutputStream;
import java.io.CharArrayReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.jcr.RepositoryException;
import javax.jcr.Session;

import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.PNGTranscoder;
import org.apache.batik.util.XMLResourceDescriptor;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.message.BasicNameValuePair;
import org.codehaus.jackson.node.ArrayNode;
import org.codehaus.jackson.node.ObjectNode;
import org.jcrom.Jcrom;
import org.w3c.dom.Document;

import play.libs.F;
import play.libs.Json;
import play.mvc.Result;
import providers.CacheableUserProvider;
import service.JcrSessionFactory;
import service.filestore.FileStore;
import service.filestore.FileStoreImpl;
import au.edu.uq.aorra.charts.ChartRenderer;
import be.objectify.deadbolt.java.actions.SubjectPresent;

import com.google.common.collect.Lists;
import com.google.inject.Inject;

import ereefs.charts.ChartDescription;
import ereefs.charts.ChartFactory;
import ereefs.charts.ChartType;
import ereefs.spreadsheet.DataSource;
import ereefs.spreadsheet.XlsxDataSource;

public class Chart extends SessionAwareController {

    private final FileStore fileStore;

    @Inject
    public Chart(final JcrSessionFactory sessionFactory,
            final Jcrom jcrom,
            final CacheableUserProvider sessionHandler,
            final FileStore fileStore) {
      super(sessionFactory, jcrom, sessionHandler);
      this.fileStore = fileStore;
    }

    private String buildUrl(ereefs.charts.Chart chart, String format, String[] path) throws UnsupportedEncodingException {
        StringBuilder result = new StringBuilder("charts/");
        result.append(chart.getDescription().getType().toString().toLowerCase());
        result.append(".");
        if(StringUtils.isNotBlank(format)) {
            result.append(format);
        } else {
            result.append("png");
        }
        result.append("?");
        List<NameValuePair> qparams = new ArrayList<NameValuePair>();
        for(Map.Entry<String, String> me : chart.getDescription().getProperties().entrySet()) {
            qparams.add(new BasicNameValuePair(me.getKey(), me.getValue()));
        }
        for(String p : path) {
            qparams.add(new BasicNameValuePair("path", p));
        }
        result.append(URLEncodedUtils.format(qparams, "UTF-8"));
        return result.toString();
    }

    private String getParameter(String key) {
        String[] values = request().queryString().get(key);
        return ((values != null) && (values.length > 0))?values[0]:null;
    }

    private List<DataSource> getDatasources(Session session, String[] paths) throws Exception {
        List<DataSource> result = Lists.newArrayList();
        if(paths == null) {
            return null;
        }
        final FileStore.Manager fm = fileStore.getManager(session);
        for(String path : paths) {
            FileStore.FileOrFolder fof = fm.getFileOrFolder(path);
            if (fof instanceof FileStoreImpl.File) {
                FileStoreImpl.File file = (FileStoreImpl.File) fof;
                // Check this is an OpenXML document (no chance otherwise)
                if (!file.getMimeType().equals(
                    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")) {
                } else {
                    result.add(new XlsxDataSource(file.getData()));
                }
            }
        }
        return result;
    }

    @SubjectPresent
    public Result charts(final String chart) {
        return inUserSession(new F.Function<Session, Result>() {
            @Override
            public final Result apply(Session session) throws RepositoryException {
                try {
                    List<DataSource> datasources = getDatasources(
{'fraction_non_alphanumeric': 0.10473727518514164, 'fraction_numerical': 0.0011755025273304338, 'mean_word_length': 3.166503428011753, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1749928', 'n_tokens_mistral': 2415, 'n_tokens_neox': 2355, 'n_words': 470}