View Javadoc
1   /*
2    * Copyright (C) 2010, Red Hat Inc. and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  package org.eclipse.jgit.attributes;
11  
12  import static java.nio.charset.StandardCharsets.UTF_8;
13  
14  import java.io.BufferedReader;
15  import java.io.IOException;
16  import java.io.InputStream;
17  import java.io.InputStreamReader;
18  import java.util.ArrayList;
19  import java.util.Collections;
20  import java.util.List;
21  
22  /**
23   * Represents a bundle of attributes inherited from a base directory.
24   *
25   * This class is not thread safe, it maintains state about the last match.
26   *
27   * @since 3.7
28   */
29  public class AttributesNode {
30  	/** The rules that have been parsed into this node. */
31  	private final List<AttributesRule> rules;
32  
33  	/**
34  	 * Create an empty ignore node with no rules.
35  	 */
36  	public AttributesNode() {
37  		rules = new ArrayList<>();
38  	}
39  
40  	/**
41  	 * Create an ignore node with given rules.
42  	 *
43  	 * @param rules
44  	 *            list of rules.
45  	 */
46  	public AttributesNode(List<AttributesRule> rules) {
47  		this.rules = rules;
48  	}
49  
50  	/**
51  	 * Parse files according to gitattribute standards.
52  	 *
53  	 * @param in
54  	 *            input stream holding the standard ignore format. The caller is
55  	 *            responsible for closing the stream.
56  	 * @throws java.io.IOException
57  	 *             Error thrown when reading an ignore file.
58  	 */
59  	public void parse(InputStream in) throws IOException {
60  		BufferedReader br = asReader(in);
61  		String txt;
62  		while ((txt = br.readLine()) != null) {
63  			txt = txt.trim();
64  			if (txt.length() > 0 && !txt.startsWith("#") /* Comments *///$NON-NLS-1$
65  					&& !txt.startsWith("!") /* Negative pattern forbidden for attributes */) { //$NON-NLS-1$
66  				int patternEndSpace = txt.indexOf(' ');
67  				int patternEndTab = txt.indexOf('\t');
68  
69  				final int patternEnd;
70  				if (patternEndSpace == -1)
71  					patternEnd = patternEndTab;
72  				else if (patternEndTab == -1)
73  					patternEnd = patternEndSpace;
74  				else
75  					patternEnd = Math.min(patternEndSpace, patternEndTab);
76  
77  				if (patternEnd > -1)
78  					rules.add(new AttributesRule(txt.substring(0, patternEnd),
79  							txt.substring(patternEnd + 1).trim()));
80  			}
81  		}
82  	}
83  
84  	private static BufferedReader asReader(InputStream in) {
85  		return new BufferedReader(new InputStreamReader(in, UTF_8));
86  	}
87  
88  	/**
89  	 * Getter for the field <code>rules</code>.
90  	 *
91  	 * @return list of all ignore rules held by this node
92  	 */
93  	public List<AttributesRule> getRules() {
94  		return Collections.unmodifiableList(rules);
95  	}
96  
97  }