Path: blob/master/test/hotspot/jtreg/vmTestbase/vm/share/CommentedFileReader.java
41153 views
/*1* Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22package vm.share;2324import java.io.*;25import java.util.LinkedList;2627/**28* Utility class intended to read file line by line and skip comments.29*/30public class CommentedFileReader {3132/**33* Type of comments that should be removed from file.34*/35public static enum CommentStyle {36/**37* Comments started with <i>#</i>.38*/39BASH,40/**41* Comments started with <i>//</i>.42*/43JAVA44}4546/**47* Get lines from specified file and filter out comments.48* Only comments in BASH style will be filtered out.49*50* @param path to file that should be readed51* @return filtered lines from file52*/53public static String[] readFile(String path) throws IOException {54return readFile(new File(path), CommentStyle.BASH);55}5657/**58* Get lines from specified file and filter out comments.59* Only comments in BASH style will be filtered out.60*61* @param file that should be readed62* @return filtered lines from file63*/64public static String[] readFile(File file) throws IOException {65return readFile(file, CommentStyle.BASH);66}6768/**69* Get lines from specified file without comments.70*71* @param path to file that should be readed72* @param commentStyle describes what strings will be treated as comments73* @return filtered lines from file74*/75public static String[] readFile(String path, CommentStyle commentStyle) throws IOException {76return readFile(new File(path), commentStyle);77}7879/**80* Get lines from specified file without comments.81*82* @param file that should be readed83* @param commentStyle describes what strings will be treated as comments84* @return filtered lines from file85*/86public static String[] readFile(File file, CommentStyle commentStyle) throws IOException {87LinkedList<String> entries = new LinkedList<String>();88BufferedReader reader = new BufferedReader(new FileReader(file));89String commentBeginning;9091switch (commentStyle) {92case BASH:93commentBeginning = "#";94break;95case JAVA:96commentBeginning = "//";97break;98default:99throw new IllegalArgumentException("Unknown comment style");100}101102while (true) {103String entry = reader.readLine();104if (entry == null) {105break;106}107108entry = entry.replaceAll(commentBeginning + ".*", "").trim();109110if (entry.length() > 0) {111entries.add(entry);112}113}114115return entries.toArray(new String[entries.size()]);116}117118}119120121