Example usage for com.google.common.io CharSource read

List of usage examples for com.google.common.io CharSource read

Introduction

In this page you can find the example usage for com.google.common.io CharSource read.

Prototype

public String read() throws IOException 

Source Link

Document

Reads the contents of this source as a string.

Usage

From source file:com.totango.discoveryagent.ResourceLoader.java

public static String load(String resourceName) {
    try {//from  ww w .  j av a 2s .  com
        URL healthJsonURI = Resources.getResource(resourceName);
        CharSource healthJsonSource = Resources.asCharSource(healthJsonURI, Charsets.UTF_8);
        return healthJsonSource.read();
    } catch (Throwable t) {
        throw new RuntimeException(t);
    }
}

From source file:org.glowroot.local.ui.AggregateMerging.java

public static ProfileNode getProfile(List<CharSource> profiles) throws IOException {
    ProfileNode syntheticRootNode = ProfileNode.createSyntheticRoot();
    for (CharSource profile : profiles) {
        String profileContent = profile.read();
        if (profileContent.equals(AggregateDao.OVERWRITTEN)) {
            continue;
        }/*from   www  . j a v a  2 s.c  o  m*/
        ProfileNode toBeMergedRootNode = ObjectMappers.readRequiredValue(mapper, profileContent,
                ProfileNode.class);
        syntheticRootNode.mergeMatchedNode(toBeMergedRootNode);
    }
    return syntheticRootNode;
}

From source file:org.isisaddons.module.fakedata.dom.IsisClobs.java

private static Clob asClob(final String fileName) {
    final URL resource = Resources.getResource(IsisBlobs.class, "clobs/" + fileName);
    final CharSource charSource = Resources.asCharSource(resource, Charsets.US_ASCII);
    final String chars;
    try {// w  w w. j a  va2 s. co m
        chars = charSource.read();
        return new Clob(fileName, mimeTypeFor(fileName), chars);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.google.javascript.jscomp.deps.ClosureBundler.java

private void append(Appendable out, Mode mode, CharSource cs) throws IOException {
    append(out, mode, cs.read());
}

From source file:com.google.javascript.jscomp.deps.ClosureBundler.java

private void appendTraditional(Appendable out, CharSource contents) throws IOException {
    if (useEval) {
        out.append("(0,eval(\"");
        append(out, Mode.ESCAPED, contents);
        appendSourceUrl(out, Mode.ESCAPED);
        out.append("\"));");
    } else {//from   ww  w .  j a v a2 s  .c  om
        out.append(contents.read());
        appendSourceUrl(out, Mode.NORMAL);
    }
}

From source file:net.freifunk.autodeploy.printing.LabelPrintingServiceImpl.java

private final String createFilledInSVG(final Firmware firmware, final DetailedDevice device,
        final FirmwareConfiguration configuration, final String updateToken, final URI updateUri) {
    if (configuration instanceof FreifunkNordFirmwareConfiguration) {
        final FreifunkNordFirmwareConfiguration freifunkNordConfiguration = (FreifunkNordFirmwareConfiguration) configuration;
        final CharSource svgSource = Resources.asCharSource(Resources.getResource(getSVGFilename(firmware)),
                UTF_8);//w  w  w . ja v a  2  s  .c  o m
        final String svgString;
        try {
            svgString = svgSource.read();
        } catch (final IOException e) {
            throw new IllegalStateException("Could not read SVG.", e);
        }

        final String vpnKey = freifunkNordConfiguration.getVpnKey();
        final int vpnKeyLength = vpnKey.length();
        final int vpnKeyMiddle = vpnKeyLength / 2;
        final String vpnKey1 = vpnKey.substring(0, vpnKeyMiddle);
        final String vpnKey2 = vpnKey.substring(vpnKeyMiddle, vpnKeyLength);

        return svgString.replace("{nodename}", freifunkNordConfiguration.getNodename())
                .replace("{password}", freifunkNordConfiguration.getPassword())
                .replace("{device}", device.getDevice().getModel() + " " + device.getDevice().getVersion())
                .replace("{mac}", device.getMac()).replace("{vpnKey1}", vpnKey1).replace("{vpnKey2}", vpnKey2)
                .replace("{token}", updateToken == null ? "---" : updateToken)
                .replace("{updateUri}", updateUri.toString());
    } else {
        throw new IllegalArgumentException("Unsupported configuration: " + configuration.getClass());
    }
}

From source file:com.google.googlejavaformat.java.Formatter.java

/**
 * Format the given input (a Java compilation unit) into the output stream.
 *
 * @throws FormatterException if the input cannot be parsed
 *//*from   www.j  a  v  a 2  s.  co m*/
public void formatSource(CharSource input, CharSink output) throws FormatterException, IOException {
    // TODO(cushon): proper support for streaming input/output. Input may
    // not be feasible (parsing) but output should be easier.
    output.write(formatSource(input.read()));
}

From source file:org.onlab.util.XmlString.java

private String prettyPrintXml(CharSource inputXml) {
    try {//from w  w  w  . jav a 2s.  c  o m
        Document document;
        boolean wasFragment = false;

        DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        // do not print error to stderr
        docBuilder.setErrorHandler(new DefaultHandler());

        try {
            document = docBuilder.parse(new InputSource(inputXml.openStream()));
        } catch (SAXException e) {
            log.debug("will retry assuming input is XML fragment", e);
            // attempt to parse XML fragments, adding virtual root
            try {
                document = docBuilder.parse(new InputSource(
                        CharSource.concat(CharSource.wrap("<vroot>"), inputXml, CharSource.wrap("</vroot>"))
                                .openStream()));
                wasFragment = true;
            } catch (SAXException e1) {
                log.debug("SAXException after retry", e1);
                // Probably wasn't fragment issue, throwing original
                throw e;
            }
        }

        document.normalize();

        XPath xPath = XPathFactory.newInstance().newXPath();
        NodeList nodeList = (NodeList) xPath.evaluate("//text()[normalize-space()='']", document,
                XPathConstants.NODESET);

        for (int i = 0; i < nodeList.getLength(); ++i) {
            Node node = nodeList.item(i);
            node.getParentNode().removeChild(node);
        }

        // Setup pretty print options
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        t.setOutputProperty(OutputKeys.INDENT, "yes");
        t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

        // Return pretty print xml string
        StringWriter strWriter = new StringWriter();
        if (wasFragment) {
            // print everything but virtual root node added
            NodeList children = document.getDocumentElement().getChildNodes();
            for (int i = 0; i < children.getLength(); ++i) {
                t.transform(new DOMSource(children.item(i)), new StreamResult(strWriter));
            }
        } else {
            t.transform(new DOMSource(document), new StreamResult(strWriter));
        }
        return strWriter.toString();
    } catch (Exception e) {
        log.warn("Pretty printing failed", e);
        try {
            String rawInput = inputXml.read();
            log.debug("  failed input: \n{}", rawInput);
            return rawInput;
        } catch (IOException e1) {
            log.error("Failed to read from input", e1);
            return inputXml.toString();
        }
    }
}