You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AttributesNode.java 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. import static java.nio.charset.StandardCharsets.UTF_8;
  12. import java.io.BufferedReader;
  13. import java.io.IOException;
  14. import java.io.InputStream;
  15. import java.io.InputStreamReader;
  16. import java.util.ArrayList;
  17. import java.util.Collections;
  18. import java.util.List;
  19. /**
  20. * Represents a bundle of attributes inherited from a base directory.
  21. *
  22. * This class is not thread safe, it maintains state about the last match.
  23. *
  24. * @since 3.7
  25. */
  26. public class AttributesNode {
  27. /** The rules that have been parsed into this node. */
  28. private final List<AttributesRule> rules;
  29. /**
  30. * Create an empty ignore node with no rules.
  31. */
  32. public AttributesNode() {
  33. rules = new ArrayList<>();
  34. }
  35. /**
  36. * Create an ignore node with given rules.
  37. *
  38. * @param rules
  39. * list of rules.
  40. */
  41. public AttributesNode(List<AttributesRule> rules) {
  42. this.rules = rules;
  43. }
  44. /**
  45. * Parse files according to gitattribute standards.
  46. *
  47. * @param in
  48. * input stream holding the standard ignore format. The caller is
  49. * responsible for closing the stream.
  50. * @throws java.io.IOException
  51. * Error thrown when reading an ignore file.
  52. */
  53. public void parse(InputStream in) throws IOException {
  54. BufferedReader br = asReader(in);
  55. String txt;
  56. while ((txt = br.readLine()) != null) {
  57. txt = txt.trim();
  58. if (txt.length() > 0 && !txt.startsWith("#") /* Comments *///$NON-NLS-1$
  59. && !txt.startsWith("!") /* Negative pattern forbidden for attributes */) { //$NON-NLS-1$
  60. int patternEndSpace = txt.indexOf(' ');
  61. int patternEndTab = txt.indexOf('\t');
  62. final int patternEnd;
  63. if (patternEndSpace == -1)
  64. patternEnd = patternEndTab;
  65. else if (patternEndTab == -1)
  66. patternEnd = patternEndSpace;
  67. else
  68. patternEnd = Math.min(patternEndSpace, patternEndTab);
  69. if (patternEnd > -1)
  70. rules.add(new AttributesRule(txt.substring(0, patternEnd),
  71. txt.substring(patternEnd + 1).trim()));
  72. }
  73. }
  74. }
  75. private static BufferedReader asReader(InputStream in) {
  76. return new BufferedReader(new InputStreamReader(in, UTF_8));
  77. }
  78. /**
  79. * Getter for the field <code>rules</code>.
  80. *
  81. * @return list of all ignore rules held by this node
  82. */
  83. public List<AttributesRule> getRules() {
  84. return Collections.unmodifiableList(rules);
  85. }
  86. }