All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eclipse.jetty.start.TextFile Maven / Gradle / Ivy

There is a newer version: 12.1.0.alpha0
Show newest version
//
//  ========================================================================
//  Copyright (c) 1995-2021 Mort Bay Consulting Pty Ltd and others.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//

package org.eclipse.jetty.start;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.regex.Pattern;
import java.util.stream.Stream;

/**
 * Simple common abstraction for Text files, that consist of a series of lines.
 * 

* Ignoring lines that are empty, deemed to be comments, or are duplicates of prior lines. */ public class TextFile implements Iterable { private final Path file; private final List lines = new ArrayList<>(); private final List allLines = new ArrayList<>(); public TextFile(Path file) throws IOException { this.file = file; init(); if (!FS.canReadFile(file)) { StartLog.debug("Skipping read of missing file: %s", file.toAbsolutePath()); return; } try (BufferedReader buf = Files.newBufferedReader(file, StandardCharsets.UTF_8)) { String line; while ((line = buf.readLine()) != null) { if (line.length() == 0) { continue; } allLines.add(line); if (line.charAt(0) == '#') { continue; } // TODO - bad form calling derived method from base class constructor process(line.trim()); } } } public void addUniqueLine(String line) { if (lines.contains(line)) { // skip return; } lines.add(line); } public Path getFile() { return file; } public List getLineMatches(Pattern pattern) { List ret = new ArrayList<>(); for (String line : lines) { if (pattern.matcher(line).matches()) { ret.add(line); } } return ret; } public List getLines() { return lines; } public List getAllLines() { return allLines; } public void init() { } public Stream stream() { return lines.stream(); } @Override public Iterator iterator() { return lines.iterator(); } public ListIterator listIterator() { return lines.listIterator(); } public void process(String line) { addUniqueLine(line); } @Override public String toString() { return file.toString(); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy