1
2
3
4
5
6
7
8
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
24
25
26
27
28
29 public class AttributesNode {
30
31 private final List<AttributesRule> rules;
32
33
34
35
36 public AttributesNode() {
37 rules = new ArrayList<>();
38 }
39
40
41
42
43
44
45
46 public AttributesNode(List<AttributesRule> rules) {
47 this.rules = rules;
48 }
49
50
51
52
53
54
55
56
57
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("#")
65 ) {
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
90
91
92
93 public List<AttributesRule> getRules() {
94 return Collections.unmodifiableList(rules);
95 }
96
97 }