Java Code Examples for com.google.common.io.Resources#readLines()

The following examples show how to use com.google.common.io.Resources#readLines() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: VinSampler.java    From log-synth with Apache License 2.0 6 votes vote down vote up
private static SetMultimap<String, String> multiMapResource(String name) throws IOException {
    final Splitter onTab = Splitter.on("\t");

    //noinspection UnstableApiUsage
    return Resources.readLines(Resources.getResource(name), Charsets.UTF_8, new LineProcessor<SetMultimap<String, String>>() {
        final SetMultimap<String, String> r = HashMultimap.create();

        @Override
        public boolean processLine(String line) {
            Iterator<String> pieces = onTab.split(line).iterator();
            String key = pieces.next();
            r.put(key, pieces.next());
            return true;
        }

        @Override
        public SetMultimap<String, String> getResult() {
            return r;
        }
    });
}
 
Example 2
Source File: LicenseAcceptance.java    From testcontainers-java with MIT License 6 votes vote down vote up
public static void assertLicenseAccepted(final String imageName) {
    try {
        final URL url = Resources.getResource(ACCEPTANCE_FILE_NAME);
        final List<String> acceptedLicences = Resources.readLines(url, Charsets.UTF_8);

        if (acceptedLicences.stream().map(String::trim).anyMatch(imageName::equals)) {
            return;
        }
    } catch (Exception ignored) {
        // suppressed
    }

    throw new IllegalStateException("The image " + imageName + " requires you to accept a license agreement. " +
                    "Please place a file at the root of the classpath named " + ACCEPTANCE_FILE_NAME + ", e.g. at " +
                    "src/test/resources/" + ACCEPTANCE_FILE_NAME + ". This file should contain the line:\n  " +
                    imageName);

}
 
Example 3
Source File: Util.java    From log-synth with Apache License 2.0 6 votes vote down vote up
public static void readData(String resource, Function<String, Void> callback) throws IOException {
    //noinspection UnstableApiUsage
    Resources.readLines(Resources.getResource(resource), Charsets.UTF_8, new LineProcessor<Void>() {
        @Override
        public boolean processLine(String line) {
            if (!line.startsWith("# ")) {
                callback.apply(line);
            }
            return true;
        }

        @Override
        public Void getResult() {
            return null;
        }
    });
}
 
Example 4
Source File: ChartWriterTest.java    From qpid-broker-j with Apache License 2.0 6 votes vote down vote up
@Test
public void testWriteHtmlSummaryToFileSystemOverwritingExistingFile() throws Exception
{
    ChartingDefinition chartDef1 = mock(ChartingDefinition.class);
    when(chartDef1.getChartStemName()).thenReturn("chart1");
    when(chartDef1.getChartDescription()).thenReturn("chart description1");

    ChartingDefinition chartDef2 = mock(ChartingDefinition.class);
    when(chartDef2.getChartStemName()).thenReturn("chart2");

    File summaryFile = new File(_chartDir, ChartWriter.SUMMARY_FILE_NAME);

    writeDummyContentToSummaryFileToEnsureItGetsOverwritten(summaryFile);

    _writer.writeChartToFileSystem(_chart2, chartDef2);
    _writer.writeChartToFileSystem(_chart1, chartDef1);

    _writer.writeHtmlSummaryToFileSystem("Performance Charts");

    List<String> expected = Resources.readLines(Resources.getResource(getClass(), "expected-chart-summary.html"),
                                               StandardCharsets.UTF_8);
    List<String> actual = Files.readAllLines(summaryFile.toPath(), StandardCharsets.UTF_8);
    assertEquals("HTML summary file has unexpected content", expected, actual);
}
 
Example 5
Source File: RuntimeEnv.java    From Quicksql with MIT License 5 votes vote down vote up
public static void init() throws IOException {
    System.out.println("Elasticsearch Embedded Server is starting up, waiting....");
    final Map<String, String> mapping = ImmutableMap.of("stu_id", "keyword", "type", "keyword",
        "city", "keyword", "digest", "long", "province", "keyword");
    NODE.createIndex("student", mapping);

    // load records from file
    final List<ObjectNode> bulk = new ArrayList<>();
    Resources.readLines(CsvJoinWithEsExample.class.getResource("/student.json"),
        StandardCharsets.UTF_8, new LineProcessor<Void>() {
            @Override public boolean processLine(String line) throws IOException {
                line = line.replaceAll("_id", "id");
                bulk.add((ObjectNode) NODE.mapper().readTree(line));
                return true;
            }

            @Override public Void getResult() {
                return null;
            }
        });

    if (bulk.isEmpty()) {
        throw new IllegalStateException("No records to index. Empty file ?");
    }

    NODE.insertBulk("student", bulk);
    System.out.println("Elasticsearch Embedded Server has started!! Your query is running...");

}
 
Example 6
Source File: SqlLogicalPlanViewTest.java    From Quicksql with MIT License 5 votes vote down vote up
/**
 * test.
 *
 * @throws IOException io exception
 */

@BeforeClass
public static void setupInstance() throws IOException {
    final Map<String, String> mapping = ImmutableMap.of("stu_id", "long", "type", "long",
        "city", "keyword", "digest", "long", "province", "keyword");
    NODE.createIndex("student", mapping);

    // load records from file
    final List<ObjectNode> bulk = new ArrayList<>();
    Resources.readLines(QueryProcedureTest.class.getResource("/student.json"),
        StandardCharsets.UTF_8, new LineProcessor<Void>() {
            @Override
            public boolean processLine(String line) throws IOException {
                line = line.replaceAll("_id", "id");
                bulk.add((ObjectNode) NODE.mapper().readTree(line));
                return true;
            }

            @Override
            public Void getResult() {
                return null;
            }
        });

    if (bulk.isEmpty()) {
        throw new IllegalStateException("No records to index. Empty file ?");
    }

    NODE.insertBulk("student", bulk);

    producer = new QueryProcedureProducer(
        "inline: " + MetadataPostman.getCalciteModelSchema(tableNames), SqlRunner.builder());
}
 
Example 7
Source File: QueryProcedureTest.java    From Quicksql with MIT License 5 votes vote down vote up
/**
 * test.
 *
 * @throws IOException io exception
 */

@BeforeClass
public static void setupInstance() throws IOException {
    final Map<String, String> mapping = ImmutableMap.of("stu_id", "long", "type", "long",
        "city", "keyword", "digest", "long", "province", "keyword");
    NODE.createIndex("student", mapping);

    // load records from file
    final List<ObjectNode> bulk = new ArrayList<>();
    Resources.readLines(QueryProcedureTest.class.getResource("/student.json"),
        StandardCharsets.UTF_8, new LineProcessor<Void>() {
            @Override
            public boolean processLine(String line) throws IOException {
                line = line.replaceAll("_id", "id");
                bulk.add((ObjectNode) NODE.mapper().readTree(line));
                return true;
            }

            @Override
            public Void getResult() {
                return null;
            }
        });

    if (bulk.isEmpty()) {
        throw new IllegalStateException("No records to index. Empty file ?");
    }

    NODE.insertBulk("student", bulk);

    producer = new QueryProcedureProducer(
        "inline: " + MetadataPostman.getCalciteModelSchema(tableNames), SqlRunner.builder());
}
 
Example 8
Source File: CollectdParser.java    From datacollector with Apache License 2.0 5 votes vote down vote up
private void loadTypesDb(String typesDbLocation) {
  typesDb = new HashMap<>();
  try {
    List<String> lines;
    if (typesDbLocation == null || typesDbLocation.isEmpty()) {
      lines = Resources.readLines(Resources.getResource("types.db"), Charset.defaultCharset());
    } else {
      Path typesDbFile = FileSystems.getDefault().getPath(typesDbLocation); // NOSONAR
      lines = Files.readAllLines(typesDbFile, charset);
    }

    for (String line : lines) {
      String trimmed = line.trim();
      if (trimmed.isEmpty() || trimmed.startsWith("#")) {
        continue;
      }
      String[] parts = trimmed.split("\\s+", 2);
      String dataSetName = parts[0];
      String[] typeSpecs = parts[1].split(",");

      List<String> typeParams = new ArrayList<>();
      for (String typeSpec : typeSpecs) {
        typeParams.add(typeSpec.trim().split(":")[0]);
      }
      typesDb.put(dataSetName, typeParams);
    }
  } catch (IOException e) {
    LOG.error("Failed to parse type db.", e);
  }


}
 
Example 9
Source File: GooGoo.java    From react-java-goos with Apache License 2.0 5 votes vote down vote up
/**
 * 读取模版文件并做变量替换
 */
private static List<String> renderTemplate(String templateFileName, final Map<String, String> params) throws MalformedURLException, IOException {
    final Pattern p = Pattern.compile("\\{(.*?)\\}");
    // 定义每行的处理逻辑
    LineProcessor<List<String>> processor = new LineProcessor<List<String>>() {

        private List<String> result = Lists.newArrayList();

        @Override
        public boolean processLine(String line) throws IOException {
            String tmp = line;
            Matcher m = p.matcher(line);
            while (m.find()) {
                String key = m.group(1);
                if (params.containsKey(key)) {
                    tmp = tmp.replaceAll("\\{" + key + "\\}", params.get(key));
                }
            }

            result.add(tmp);
            return true;
        }

        @Override
        public List<String> getResult() {
            return result;
        }
    };

    return Resources.readLines(Resources.getResource(templateFileName), Charsets.UTF_8, processor);
}
 
Example 10
Source File: TestDatabaseSetupPostgres.java    From OpenCue with Apache License 2.0 5 votes vote down vote up
private void populateTestData() throws Exception {
    Connection conn = postgres.getPostgresDatabase().getConnection();

    URL url = Resources.getResource("conf/ddl/postgres/test_data.sql");
    List<String> testDataStatements = Resources.readLines(url, Charsets.UTF_8);
    for (String testDataStatement : testDataStatements) {
        Statement st = conn.createStatement();
        st.execute(testDataStatement);
        st.close();
    }

    conn.close();
}
 
Example 11
Source File: ElasticSearchAdapterTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Used to create {@code zips} index and insert zip data in bulk.
 * @throws Exception when instance setup failed
 */
@BeforeAll
public static void setupInstance() throws Exception {
  final Map<String, String> mapping = ImmutableMap.of("city", "keyword", "state",
      "keyword", "pop", "long");

  NODE.createIndex(ZIPS, mapping);

  // load records from file
  final List<ObjectNode> bulk = new ArrayList<>();
  Resources.readLines(ElasticSearchAdapterTest.class.getResource("/zips-mini.json"),
      StandardCharsets.UTF_8, new LineProcessor<Void>() {
        @Override public boolean processLine(String line) throws IOException {
          line = line.replace("_id", "id"); // _id is a reserved attribute in ES
          bulk.add((ObjectNode) NODE.mapper().readTree(line));
          return true;
        }

        @Override public Void getResult() {
          return null;
        }
      });

  if (bulk.isEmpty()) {
    throw new IllegalStateException("No records to index. Empty file ?");
  }

  NODE.insertBulk(ZIPS, bulk);
}
 
Example 12
Source File: GooGoo.java    From react-java-goos with Apache License 2.0 5 votes vote down vote up
/**
 * 从classpath中读取某个文件,原样写入outputDir
 */
private static void copyFileFromClasspath(String inputFile, String outputDir, String outputName) throws IOException {
    File target = new File(outputDir, outputName);
    if (target.exists()) {
        System.out.println("INFO: delete exist file " + target.getAbsolutePath());
        target.delete();
    }
    System.out.println("INFO: writing file " + target.getAbsolutePath());
    BufferedWriter bw = Files.newBufferedWriter(target.toPath());
    for (String line : Resources.readLines(Resources.getResource(inputFile), Charsets.UTF_8)) {
        bw.write(line);
        bw.newLine();
    }
    bw.close();
}
 
Example 13
Source File: ParserUtils.java    From QVisual with Apache License 2.0 5 votes vote down vote up
public static List<String> readLines(String path) {
    try {
        return Resources.readLines(Resources.getResource(path), Charset.forName("UTF-8"));
    } catch (IOException e) {
        logger.error("[reading file] by path: " + path, e);
    }

    return new ArrayList<>();
}
 
Example 14
Source File: LayoutClassesParser.java    From NBANDROID-V2 with Apache License 2.0 5 votes vote down vote up
@Override
public WidgetData get() {
    try {
        WidgetData clzDescriptors
                = Resources.readLines(url, Charsets.ISO_8859_1,
                        new LineProcessorImpl());
        return clzDescriptors;
    } catch (IOException ex) {
        LOG.log(Level.INFO, null, ex);
        return new WidgetData(Collections.<LayoutElementType, Collection<UIClassDescriptor>>emptyMap(),
                Collections.<UIClassDescriptor>emptySet());
    }
}
 
Example 15
Source File: HmfGenePanelBuilder.java    From hmftools with GNU General Public License v3.0 4 votes vote down vote up
@NotNull
private static String readEnsemblQuery() throws IOException {
    final List<String> lines = Resources.readLines(Resources.getResource("ensembl_query.sql"), Charset.defaultCharset());
    return StringUtils.join(lines.toArray(), "\n");
}
 
Example 16
Source File: ResourceUtil.java    From vjtools with Apache License 2.0 4 votes vote down vote up
/**
 * 读取文件的每一行,读取规则见本类注释.
 */
public static List<String> toLines(Class<?> contextClass, String resourceName) throws IOException {
	return Resources.readLines(Resources.getResource(contextClass, resourceName), Charsets.UTF_8);
}
 
Example 17
Source File: ResourceUtil.java    From vjtools with Apache License 2.0 4 votes vote down vote up
/**
 * 读取文件的每一行,读取规则见本类注释.
 */
public static List<String> toLines(String resourceName) throws IOException {
	return Resources.readLines(Resources.getResource(resourceName), Charsets.UTF_8);
}
 
Example 18
Source File: Utils.java    From green_android with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Reads and joins together with LF char (\n) all the lines from given file. It's assumed that file is in UTF-8.
 */
public static String getResourceAsString(URL url) throws IOException {
    List<String> lines = Resources.readLines(url, Charsets.UTF_8);
    return Joiner.on('\n').join(lines);
}
 
Example 19
Source File: BootstrappingNodesFactory.java    From PeerWasp with MIT License 4 votes vote down vote up
private void loadNodesFromUrl(final URL url) throws IOException {
	List<String> lines = Resources.readLines(url, Charsets.UTF_8);
	loadNodesFromList(lines);
}
 
Example 20
Source File: Spotify100Test.java    From heroic with Apache License 2.0 4 votes vote down vote up
@Test
public void testCases() throws Exception {
    final ObjectMapper expectedMapper = expectedObjectMapper();
    final ObjectMapper mapper = Spotify100.objectMapper();

    final List<String> lines = Resources.readLines(
        Resources.getResource(Spotify100Test.class, "spotify-100-tests.txt"),
        StandardCharsets.UTF_8);

    int i = 0;

    for (final String test : lines) {
        if (test.trim().isEmpty()) {
            continue;
        }

        if (test.trim().startsWith("#")) {
            continue;
        }

        final String[] parts = test.split("\\|");

        final int line = i++;

        final JsonMetric value;

        try {
            value = mapper.readValue(parts[0].trim(), JsonMetric.class);
        } catch (final Exception e) {
            throw new RuntimeException(line + ": " + e.getMessage(), e);
        }

        final Expected expected = expectedMapper.readValue(parts[1].trim(), Expected.class);

        expected.key().ifPresent(key -> {
            assertEquals(line + ": expected key", key, value.getKey());
        });

        expected.host().ifPresent(host -> {
            assertEquals(line + ": expected host", host, value.getHost());
        });

        expected.time().ifPresent(time -> {
            assertEquals(line + ": expected time", time, value.getTime());
        });

        expected.attributes().ifPresent(attributes -> {
            assertEquals(line + ": expected attributes", attributes, value.getAttributes());
        });

        expected.resource().ifPresent(resource -> {
            assertEquals(line + ": expected resource", resource, value.getResource());
        });

        expected.value().ifPresent(v -> {
            assertEquals(line + ": expected value", v, value.getValue());
        });
    }
}