1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * See LICENSE.txt included in this distribution for the specific 9 * language governing permissions and limitations under the License. 10 * 11 * When distributing Covered Code, include this CDDL HEADER in each 12 * file and include the License file at LICENSE.txt. 13 * If applicable, add the following below this CDDL HEADER, with the 14 * fields enclosed by brackets "[]" replaced with your own identifying 15 * information: Portions Copyright [yyyy] [name of copyright owner] 16 * 17 * CDDL HEADER END 18 */ 19 20 /* 21 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. 22 * Portions Copyright (c) 2019, 2020, Chris Fraire <cfraire@me.com>. 23 */ 24 package org.opengrok.indexer.configuration; 25 26 import java.io.File; 27 import java.io.Serializable; 28 import java.util.ArrayList; 29 import java.util.List; 30 31 /** 32 * wrapper class for IgnoredFiles and IgnoredDirs. 33 * 34 * @author Vladimir Kotal 35 */ 36 public class IgnoredNames implements Serializable { 37 private static final long serialVersionUID = 1L; 38 private static final String FILE_PREFIX = "f:"; 39 private static final String DIR_PREFIX = "d:"; 40 41 private IgnoredFiles ignoredFiles; 42 private IgnoredDirs ignoredDirs; 43 IgnoredNames()44 public IgnoredNames() { 45 ignoredFiles = new IgnoredFiles(); 46 ignoredDirs = new IgnoredDirs(); 47 } 48 getItems()49 public List<String> getItems() { 50 List<String> twoLists = new ArrayList<>(); 51 twoLists.addAll(ignoredFiles.getItems()); 52 twoLists.addAll(ignoredDirs.getItems()); 53 return twoLists; 54 } 55 setItems(List<String> item)56 public void setItems(List<String> item) { 57 clear(); 58 for (String s : item) { 59 add(s); 60 } 61 } 62 63 /** 64 * Adds the specified {@code pattern} if it follows the expected naming 65 * convention (or else ignored). 66 * @param pattern defined pattern starting either with {@code "f:"} for 67 * ignore-file or with {@code "d:"} for an ignore-directory 68 */ add(String pattern)69 public void add(String pattern) { 70 if (pattern.startsWith(FILE_PREFIX)) { 71 ignoredFiles.add(pattern.substring(FILE_PREFIX.length())); 72 } else if (pattern.startsWith(DIR_PREFIX)) { 73 ignoredDirs.add(pattern.substring(DIR_PREFIX.length())); 74 } else { 75 // backward compatibility 76 ignoredFiles.add(pattern); 77 } 78 } 79 80 /** 81 * Should the file be ignored or not? 82 * 83 * @param file the file to check 84 * @return true if this file should be ignored, false otherwise 85 */ ignore(File file)86 public boolean ignore(File file) { 87 if (file.isFile()) { 88 return ignoredFiles.match(file); 89 } else { 90 return ignoredDirs.match(file); 91 } 92 } 93 94 /** 95 * Should the file name be ignored or not ? 96 * 97 * @param name the name of the file to check 98 * @return true if this pathname should be ignored, false otherwise 99 */ ignore(String name)100 public boolean ignore(String name) { 101 return ignoredFiles.match(name) || ignoredDirs.match(name); 102 } 103 clear()104 public void clear() { 105 ignoredFiles.clear(); 106 ignoredDirs.clear(); 107 } 108 getIgnoredDirs()109 public IgnoredDirs getIgnoredDirs() { 110 return ignoredDirs; 111 } 112 getIgnoredFiles()113 public IgnoredFiles getIgnoredFiles() { 114 return ignoredFiles; 115 } 116 setIgnoredDirs(IgnoredDirs i)117 public void setIgnoredDirs(IgnoredDirs i) { 118 ignoredDirs = i; 119 } 120 setIgnoredFiles(IgnoredFiles i)121 public void setIgnoredFiles(IgnoredFiles i) { 122 ignoredFiles = i; 123 } 124 } 125