<commit_before>/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.graphity.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContext;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.graphity.RDFResource;
import org.xmlresolver.Resolver;

/**
 *
 * @author Pumba
 */
@Provider
@Produces({MediaType.TEXT_HTML, MediaType.APPLICATION_XML, "application/*+xml", MediaType.TEXT_XML})
public class ResourcelXSLTWriter implements MessageBodyWriter<RDFResource>
{
    public static final String XSLT_BASE = "/WEB-INF/xsl/";
 
    @Context ServletContext context;
    //@Context UriInfo uriInfo;
    private ByteArrayOutputStream bos = null;
    private SAXTransformerFactory stf = (SAXTransformerFactory)TransformerFactory.newInstance();


    @Override
    public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType)
    {
	return RDFResource.class.isAssignableFrom(type);
    }

    @Override
    public long getSize(RDFResource resource, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType)
    {
	/*
	if (bos == null)
	{
	    bos = new ByteArrayOutputStream();
	    resource.getModel().write(bos);
	}
	*/
	//return bos.size(); // is this the right value?
	//return Integer.valueOf(stream.toByteArray().length).longValue();
	return -1;
    }

    @Override
    public void writeTo(RDFResource resource, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException
    {
	//if (bos == null)
	{
	    bos = new ByteArrayOutputStream();
	    resource.getModel().write(bos);
	}
	// can we avoid buffering here? I guess not...
	try
	{
	    XSLTBuilder.fromStylesheet(context.getResource(XSLT_BASE + "group-triples.xsl").toURI().toString()).
		document(new ByteArrayInputStream(bos.toByteArray())).
		outputProperty(OutputKeys.INDENT, "yes").
		transform(getXSLTBuilder(resource).
		    result(new StreamResult(entityStream)));

	    //getXSLTBuilder(resource).transform(entityStream); // no preprocessing
	}

	catch (URISyntaxException ex)
	{
	    Logger.getLogger(ResourcelXSLTWriter.class.getName()).log(Level.SEVERE, null, ex);
	}	catch (TransformerException ex)
	{
	    Logger.getLogger(ResourcelXSLTWriter.class.getName()).log(Level.SEVERE, null, ex);
	}
    }
    
    public XSLTBuilder getXSLTBuilder(RDFResource resource) throws TransformerConfigurationException, MalformedURLException, MalformedURLException, MalformedURLException, MalformedURLException, URISyntaxException
    {
	XSLTBuilder builder = XSLTBuilder.fromStylesheet(getStylesheet()).
	    document(new ByteArrayInputStream(bos.toByteArray())).
	    parameter("uri", resource.getURI()).
	    parameter("base-uri", resource.getUriInfo().getBaseUri()).
	    resolver(new Resolver()).
	    outputProperty(OutputKeys.INDENT, "yes"); // is base uri necessary?
	    if (resource.getServiceURI() != null)
		builder.parameter("service-uri", resource.getServiceURI());
	    
	    if (resource.getUriInfo().getQueryParameters().getFirst("view") != null)
		builder.parameter("view", resource.getUriInfo().getQueryParameters().getFirst("view"));
	    
	return builder;
    }
    
    public Source getStylesheet() throws MalformedURLException, URISyntaxException
    {
	// using getResource() because getResourceAsStream() does not retain systemId
	return new StreamSource(context.getResource(XSLT_BASE + "ResourceReadView.xsl").toURI().toString());
    }
    <commit_msg>Reuse Resolver instance?<commit_after>/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.graphity.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContext;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.URIResolver;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.graphity.RDFResource;
import org.xmlresolver.Resolver;

/**
 *
 * @author Pumba
 */
@Provider
@Produces({MediaType.TEXT_HTML, MediaType.APPLICATION_XML, "application/*+xml", MediaType.TEXT_XML})
public class ResourcelXSLTWriter implements MessageBodyWriter<RDFResource>
{
    public static final String XSLT_BASE = "/WEB-INF/xsl/";
    private URIResolver resolver = new Resolver();
    
    @Context ServletContext context;
    //@Context UriInfo uriInfo;
    private ByteArrayOutputStream bos = null;
    private SAXTransformerFactory stf = (SAXTransformerFactory)TransformerFactory.newInstance();


    @Override
    public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType)
    {
	return RDFResource.class.isAssignableFrom(type);
    }

    @Override
    public long getSize(RDFResource resource, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType)
    {
	/*
	if (bos == null)
	{
	    bos = new ByteArrayOutputStream();
	    resource.getModel().write(bos);
	}
	*/
	//return bos.size(); // is this the right value?
	//return Integer.valueOf(stream.toByteArray().length).longValue();
	return -1;
    }

    @Override
    public void writeTo(RDFResource resource, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException
    {
	//if (bos == null)
	{
	    bos = new ByteArrayOutputStream();
	    resource.getModel().write(bos);
	}
	// can we avoid buffering here? I guess not...
	try
	{
	    XSLTBuilder.fromStylesheet(context.getResource(XSLT_BASE + "group-triples.xsl").toURI().toString()).
		document(new ByteArrayInputStream(bos.toByteArray())).
		outputProperty(OutputKeys.INDENT, "yes").
		transform(getXSLTBuilder(resource).
		    result(new StreamResult(entityStream)));

	    //getXSLTBuilder(resource).transform(entityStream); // no preprocessing
	}

	catch (URISyntaxException ex)
	{
	    Logger.getLogger(ResourcelXSLTWriter.class.getName()).log(Level.SEVERE, null, ex);
	}	catch (TransformerException ex)
	{
	    Logger.getLogger(ResourcelXSLTWriter.class.getName()).log(Level.SEVERE, null, ex);
	}
    }
    
    public XSLTBuilder getXSLTBuilder(RDFResource resource) throws TransformerConfigurationException, MalformedURLException, MalformedURLException, MalformedURLException, MalformedURLException, URISyntaxException
    {
	XSLTBuilder builder = XSLTBuilder.fromStylesheet(getStylesheet()).
	    document(new ByteArrayInputStream(bos.toByteArray())).
	    parameter("uri", resource.getURI()).
	    parameter("base-uri", resource.getUriInfo().getBaseUri()).
	    resolver(resolver).
	    outputProperty(OutputKeys.INDENT, "yes"); // is base uri necessary?
	    if (resource.getServiceURI() != null)
		builder.parameter("service-uri", resource.getServiceURI());
	    
	    if (resource.getUriInfo().getQueryParameters().getFirst("view") != null)
		builder.parameter("view", resource.getUriInfo().getQueryParameters().getFirst("view"));
	    
	return builder;
    }
    
    public Source getStylesheet() throws MalformedURLException, URISyntaxException
    {
	// using getResource() because getResourceAsStream() does not retain systemId
	return new StreamSource(context.getResource(XSLT_BASE + "ResourceReadView.xsl").toURI().toString());
    }
    
{'fraction_non_alphanumeric': 0.11680350301039956, 'fraction_numerical': 0.00021893814997263274, 'mean_word_length': 4.994750656167979, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 21}, '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': '5655861', 'n_tokens_mistral': 2754, 'n_tokens_neox': 2675, 'n_words': 544}