Example usage for com.google.common.io Resources asByteSource

List of usage examples for com.google.common.io Resources asByteSource

Introduction

In this page you can find the example usage for com.google.common.io Resources asByteSource.

Prototype

public static ByteSource asByteSource(URL url) 

Source Link

Document

Returns a ByteSource that reads from the given URL.

Usage

From source file:org.openqa.selenium.safari.SafariExtensions.java

private static ByteSource getExtensionResource() {
    URL url = SafariExtensions.class.getResource(EXTENSION_RESOURCE_PATH);
    checkNotNull(url, "Unable to locate extension resource, %s", EXTENSION_RESOURCE_PATH);
    return Resources.asByteSource(url);
}

From source file:org.daisy.maven.xspec.XSpecRunner.java

@Activate
public void init() {
    try {//from  ww w .  j  av a  2 s . c om

        System.setProperty("xml.catalog.ignoreMissing", "true");
        if (processor == null)
            processor = new Processor(false);
        defaultResolver = processor.getUnderlyingConfiguration().getURIResolver();

        XsltCompiler xsltCompiler = processor.newXsltCompiler();
        xsltCompiler.setURIResolver(new XSpecResolver(xsltCompiler.getURIResolver()));
        // Initialize the XSpec compiler
        xspecCompilerLoader = xsltCompiler.compile(getXSpecSource("/xspec/compiler/generate-xspec-tests.xsl"));

        // Initialize the XSpec report formatter
        xspecHtmlFormatterLoader = xsltCompiler
                .compile(getXSpecSource("/xspec/reporter/format-xspec-report.xsl"));

        // Initialize the XSpec summary formatter
        xspecHtmlSummaryFormatterLoader = xsltCompiler
                .compile(getXSpecSource("/xspec-extra/format-xspec-summary.xsl"));

        // Initialize the JUnit report formatter
        xspecJUnitFormatterLoader = xsltCompiler
                .compile(getXSpecSource("/xspec-extra/format-junit-report.xsl"));

        // Configure the XPath compiler used to parse the XSpec report
        xpathCompiler = processor.newXPathCompiler();
        xpathCompiler.setCaching(true);
        xpathCompiler.declareNamespace("", XSPEC_NAMESPACE);

        // Input supplier for the report CSS
        cssSupplier = Resources.asByteSource(XSpecRunner.class.getResource("/xspec/reporter/test-report.css"));

    } catch (SaxonApiException e) {
        throw new IllegalStateException(e);
    }
}

From source file:com.google.gwt.resources.rg.GssResourceGenerator.java

private ExtendedCssTree parseResources(List<URL> resources, TreeLogger logger)
        throws UnableToCompleteException {
    List<SourceCode> sourceCodes = new ArrayList<SourceCode>(resources.size());

    // assert that we only support either gss or css on one resource.
    boolean css = ensureEitherCssOrGss(resources, logger);

    if (css && !allowLegacy) {
        // TODO(dankurka): add link explaining the situation in detail.
        logger.log(Type.ERROR,/*from  w ww .  jav a 2s .  c o  m*/
                "Your ClientBundle is referencing css files instead of gss. "
                        + "You will need to either convert these files to gss using the "
                        + "converter tool or turn on auto convertion in your gwt.xml file. "
                        + "Note: Autoconversion will be removed after 2.7, you will need to move to gss."
                        + "Add this line to your gwt.xml file to temporary avoid this:"
                        + "<set-configuration-property name=\"CssResource.legacy\" value=\"true\" />");
        throw new UnableToCompleteException();
    }

    if (css) {
        String concatenatedCss = concatCssFiles(resources, logger);
        String gss = convertToGss(concatenatedCss, logger);
        sourceCodes.add(new SourceCode("[auto-converted gss files]", gss));
    } else {
        for (URL stylesheet : resources) {
            TreeLogger branchLogger = logger.branch(TreeLogger.DEBUG,
                    "Parsing GSS stylesheet " + stylesheet.toExternalForm());
            try {
                // TODO : always use UTF-8 to read the file ?
                String fileContent = Resources.asByteSource(stylesheet).asCharSource(Charsets.UTF_8).read();
                sourceCodes.add(new SourceCode(stylesheet.getFile(), fileContent));
                continue;

            } catch (IOException e) {
                branchLogger.log(TreeLogger.ERROR, "Unable to parse CSS", e);
            }
            throw new UnableToCompleteException();

        }
    }

    CssTree tree;

    try {
        tree = new GssParser(sourceCodes).parse();
    } catch (GssParserException e) {
        logger.log(TreeLogger.ERROR, "Unable to parse CSS", e);
        throw new UnableToCompleteException();
    }

    List<String> permutationAxes = finalizeTree(tree);

    checkErrors();

    return new ExtendedCssTree(tree, permutationAxes);
}

From source file:com.google.gwt.resources.rg.GssResourceGenerator.java

private String concatCssFiles(List<URL> resources, TreeLogger logger) throws UnableToCompleteException {
    StringBuffer buffer = new StringBuffer();
    for (URL stylesheet : resources) {
        try {/*from ww  w  . ja va  2  s  . com*/
            String fileContent = Resources.asByteSource(stylesheet).asCharSource(Charsets.UTF_8).read();
            buffer.append(fileContent);
            buffer.append("\n");

        } catch (IOException e) {
            logger.log(TreeLogger.ERROR, "Unable to parse CSS", e);
            throw new UnableToCompleteException();
        }
    }
    return buffer.toString();
}