Example usage for org.apache.commons.io IOUtils toCharArray

List of usage examples for org.apache.commons.io IOUtils toCharArray

Introduction

In this page you can find the example usage for org.apache.commons.io IOUtils toCharArray.

Prototype

public static char[] toCharArray(Reader input) throws IOException 

Source Link

Document

Get the contents of a Reader as a character array.

Usage

From source file:org.apache.hadoop.gateway.filter.rewrite.impl.xml.XmlFilterReaderTest.java

@Test
public void testMappedAttribute() throws IOException, ParserConfigurationException, XMLStreamException {
    Map<String, String> map = new HashMap<String, String>();
    map.put("input-text", "output-text");
    String inputXml = "<root attribute='input-text'/>";
    StringReader inputReader = new StringReader(inputXml);
    XmlFilterReader filterReader = new MapXmlFilterReader(inputReader, map);
    String outputHtml = new String(IOUtils.toCharArray(filterReader));
    //System.out.println( outputHtml );
    assertThat(the(outputHtml), hasXPath("/root/@attribute", equalTo("output-text")));
}

From source file:org.apache.hadoop.gateway.filter.rewrite.impl.xml.XmlFilterReaderTest.java

@Test
public void testCombined() throws IOException, ParserConfigurationException, XMLStreamException {
    Map<String, String> map = new HashMap<String, String>();
    map.put("attr1-input", "attr1-output");
    map.put("attr2-input", "attr2-output");
    map.put("attr3-input", "attr3-output");
    map.put("attr4-input", "attr4-output");
    map.put("attr5-input", "attr5-output");
    map.put("attr6-input", "attr6-output");
    map.put("attr7-input", "attr7-output");
    map.put("root-input1", "root-output1");
    map.put("root-input2", "root-output2");
    map.put("root-input3", "root-output3");
    map.put("child1-input", "child1-output");
    map.put("child2-input", "child2-output");

    String inputXml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" + "<!-- Comment -->\n"
            + "<ns1:root xmlns:ns1='http://hortonworks.com/xml/ns1' attr1='attr1-input' ns1:attr2='attr2-input'>\n"
            + "  root-input1\n" + "  <child1 attr3='attr3-input' ns1:attr4='attr4-input'>\n"
            + "    child1-input\n" + "  </child1>\n" + "  root-input2\n"
            + "  <ns2:child2 xmlns:ns2='http://hortonworks.com/xml/ns2' attr5='attr5-input' ns1:attr6='attr6-input' ns2:attr7='attr7-input'>\n"
            + "    child2-input\n" + "  </ns2:child2>\n" + "  root-input3\n" + "</ns1:root>";
    //System.out.println( inputXml );

    StringReader inputReader = new StringReader(inputXml);
    XmlFilterReader filterReader = new MapXmlFilterReader(inputReader, map);
    String outputXml = new String(IOUtils.toCharArray(filterReader));
    //System.out.println( outputXml );
    //System.out.flush();

    SimpleNamespaceContext ns = new SimpleNamespaceContext();
    ns.bind("n1", "http://hortonworks.com/xml/ns1");
    ns.bind("n2", "http://hortonworks.com/xml/ns2");

    assertThat(the(outputXml), hasXPath("/n1:root", ns));
    assertThat(the(outputXml), hasXPath("/n1:root/@attr1", ns, equalTo("attr1-output")));
    assertThat(the(outputXml), hasXPath("/n1:root/@n1:attr2", ns, equalTo("attr2-output")));
    assertThat(the(outputXml), hasXPath("/n1:root/text()[1]", ns, equalTo("root-output1")));
    assertThat(the(outputXml), hasXPath("/n1:root/text()[2]", ns, equalTo("root-output2")));
    assertThat(the(outputXml), hasXPath("/n1:root/text()[3]", ns, equalTo("root-output3")));
    assertThat(the(outputXml), hasXPath("/n1:root/child1", ns));
    assertThat(the(outputXml), hasXPath("/n1:root/child1/@attr3", ns, equalTo("attr3-output")));
    assertThat(the(outputXml), hasXPath("/n1:root/child1/@n1:attr4", ns, equalTo("attr4-output")));
    assertThat(the(outputXml), hasXPath("/n1:root/child1/text()", ns, equalTo("child1-output")));
    assertThat(the(outputXml), hasXPath("/n1:root/n2:child2", ns));
    assertThat(the(outputXml), hasXPath("/n1:root/n2:child2/@attr5", ns, equalTo("attr5-output")));
    assertThat(the(outputXml), hasXPath("/n1:root/n2:child2/@n1:attr6", ns, equalTo("attr6-output")));
    assertThat(the(outputXml), hasXPath("/n1:root/n2:child2/@n2:attr7", ns, equalTo("attr7-output")));
    assertThat(the(outputXml), hasXPath("/n1:root/n2:child2/text()", ns, equalTo("child2-output")));
}

From source file:org.apache.hadoop.gateway.filter.rewrite.impl.xml.XmlFilterReaderTest.java

@Test
public void testTagNameLetterCase() throws Exception {
    String inputXml = "<Root/>";
    StringReader inputReader = new StringReader(inputXml);

    XmlFilterReader filterReader = new NoopXmlFilterReader(inputReader, null);
    String outputXml = new String(IOUtils.toCharArray(filterReader));
    assertThat(the(outputXml), hasXPath("/Root"));
}

From source file:org.apache.hadoop.gateway.filter.rewrite.impl.xml.XmlFilterReaderTest.java

@Test
public void testXmlWithHtmlTagNames() throws Exception {
    String inputXml = "<root><br><table name=\"table1\"/><table name=\"table2\"/></br></root>";
    StringReader inputReader = new StringReader(inputXml);

    XmlFilterReader filterReader = new NoopXmlFilterReader(inputReader, null);
    String outputXml = new String(IOUtils.toCharArray(filterReader));
    assertThat(the(outputXml), hasXPath("/root/br/table[1]/@name", equalTo("table1")));
    assertThat(the(outputXml), hasXPath("/root/br/table[2]/@name", equalTo("table2")));
}

From source file:org.apache.lucene.analysis.jate.OpenNLPTokenizer.java

void fillBuffer() throws IOException {
    fullText = IOUtils.toCharArray(input);
    /*int offset = 0;/*from  www . j a  v  a2s.  c  o m*/
    int size = 10000;
    fullText = new char[size];
    int length = input.read(fullText);
    while(length == size) {
    //    fullText = IOUtils.toCharArray(input);
    fullText = Arrays.copyOf(fullText, offset + size);
    offset += size;
    length = input.read(fullText, offset, size);
    }
    fullText = Arrays.copyOf(fullText, offset + length);*/
}

From source file:org.apache.solr.update.processor.LangDetectLanguageIdentifierUpdateProcessorFactory.java

public static synchronized void loadData() throws IOException, LangDetectException {
    if (loaded) {
        return;//from  w  ww.  j a  va 2  s.c  o m
    }
    loaded = true;
    List<String> profileData = new ArrayList<>();
    for (String language : languages) {
        InputStream stream = LangDetectLanguageIdentifierUpdateProcessor.class
                .getResourceAsStream("langdetect-profiles/" + language);
        BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8));
        profileData.add(new String(IOUtils.toCharArray(reader)));
        reader.close();
    }
    DetectorFactory.loadProfile(profileData);
    DetectorFactory.setSeed(0);
}

From source file:org.hippoecm.frontend.plugins.console.menu.patch.ApplyPatchDialog.java

private boolean uploadPatch() {
    final FileUpload upload = fileUploadField.getFileUpload();
    if (upload == null) {
        error("No file selected");
        return false;
    }//from  w w  w. jav a2s .  c  o m

    boolean result = false;
    InputStream uis = null, fis = null;
    OutputStream fos = null;
    final StringBuilder sb = new StringBuilder();
    try {
        uis = upload.getInputStream();
        tempFile = File.createTempFile("patch-" + Time.now(), ".xml");
        fos = new FileOutputStream(tempFile);
        IOUtils.copy(uis, fos);
        fis = new FileInputStream(tempFile);
        final Patch patch = parsePatch(fis);
        final String target = patch.getTarget();
        if (target != null && !target.equals(getModelObject().getPath())) {
            sb.append(
                    "The patch seems to be targeted at a different node than the one to which you are about to apply it.");
            sb.append(" Patch is targeted at ").append(target).append(".");
            sb.append(" About to apply patch to ").append(getModelObject().getPath()).append(".");
            sb.append(" Continue?");
        }
        if (patch.getOperations().isEmpty()) {
            sb.append("The patch is empty.");
        }
        result = true;
    } catch (IOException e) {
        final String message = "An unexpected error occurred: " + e.getMessage();
        error(message);
        log.error(message, e);
    } catch (RepositoryException e) {
        final String message = "An unexpected error occurred: " + e.getMessage();
        error(message);
        log.error(message, e);
    } finally {
        IOUtils.closeQuietly(uis);
        IOUtils.closeQuietly(fos);
        IOUtils.closeQuietly(fis);
        final String message = sb.toString();
        if (!message.isEmpty()) {
            info(message);
        }
        if (result) {
            final AjaxRequestTarget target = RequestCycle.get().find(AjaxRequestTarget.class);
            try {
                fis = new FileInputStream(tempFile);
                textArea.setDefaultModelObject(new String(IOUtils.toCharArray(fis)));
                target.add(textArea);
            } catch (IOException e) {
                log.error(e.getClass().getName() + ": " + e.getMessage(), e);
            } finally {
                IOUtils.closeQuietly(fis);
            }
            fileUploadField.setEnabled(false);
            target.add(fileUploadField);
        }
    }

    return result;
}

From source file:org.intermine.bio.web.logic.LiftOverService.java

/**
 * Send a HTTP POST request to liftOver service.
 *
 * @param grsc the Genomic Region Search constraint
 * @param org human or mouse/*from  w  w  w  .  j a  v a2s . c  om*/
 * @param genomeVersionSource older genome version
 * @param genomeVersionTarget intermine genome version
 * @param liftOverServerURL url
 * @return a list of GenomicRegion
 */
public String doLiftOver(GenomicRegionSearchConstraint grsc, String org, String genomeVersionSource,
        String genomeVersionTarget, String liftOverServerURL) {

    List<GenomicRegion> genomicRegionList = grsc.getGenomicRegionList();
    String liftOverResponse;

    String coords = converToBED(genomicRegionList);
    String organism = ORGANISM_COMMON_NAME_MAP.get(org);

    try {
        // Construct data
        String data = URLEncoder.encode("coords", "UTF-8") + "=" + URLEncoder.encode(coords, "UTF-8");
        data += "&" + URLEncoder.encode("source", "UTF-8") + "="
                + URLEncoder.encode(genomeVersionSource, "UTF-8");
        data += "&" + URLEncoder.encode("target", "UTF-8") + "="
                + URLEncoder.encode(genomeVersionTarget, "UTF-8");

        // Send data
        URL url;
        // liftOverServerURL ends with "/"
        if (!liftOverServerURL.endsWith("/")) {
            url = new URL(liftOverServerURL + "/" + organism);
        } else {
            url = new URL(liftOverServerURL + organism);
        }
        URLConnection conn = url.openConnection();
        conn.setDoOutput(true);
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(data);
        wr.flush();
        wr.close();

        liftOverResponse = new String(IOUtils.toCharArray(conn.getInputStream()));

        LOG.info("LiftOver response message: \n" + liftOverResponse);

    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

    return liftOverResponse;
}

From source file:org.intermine.bio.web.struts.GenomicRegionSearchOptionsController.java

private String fetchGalaxyData(String galaxyDataUrl) {

    String galaxyInput;//  w ww. j av  a 2  s  . co  m

    try {
        URL url = new URL(galaxyDataUrl);
        URLConnection conn = url.openConnection();
        galaxyInput = new String(IOUtils.toCharArray(conn.getInputStream())).trim();
    } catch (Exception e) {
        e.printStackTrace();
        return GALAXY_SERVER_CONNECTION_ERROR;
    }

    return galaxyInput;
}

From source file:org.jspringbot.keyword.selenium.SeleniumHelper.java

public Object executeJavascript(String code) throws IOException {
    HighlightRobotLogger.HtmlAppender appender = LOG.createAppender().appendBold("Execute Javascript:");

    if (StringUtils.startsWith(code, "file:") || StringUtils.startsWith(code, "classpath:")) {
        appender.appendProperty("Resource", code);
        ResourceEditor editor = new ResourceEditor();
        editor.setAsText(code);/*from w  w w  .  ja v a 2  s.com*/
        Resource resource = (Resource) editor.getValue();

        code = new String(IOUtils.toCharArray(resource.getInputStream()));
    }

    appender.appendJavascript(code);
    appender.log();

    return executor.executeScript(code);
}