Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ConfigLine.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (C) 2010, Mathias Kinzler <mathias.kinzler@sap.com>
  3. * Copyright (C) 2009, Constantine Plotnikov <constantine.plotnikov@gmail.com>
  4. * Copyright (C) 2007, Dave Watson <dwatson@mimvista.com>
  5. * Copyright (C) 2008-2010, Google Inc.
  6. * Copyright (C) 2009, Google, Inc.
  7. * Copyright (C) 2009, JetBrains s.r.o.
  8. * Copyright (C) 2007-2008, Robin Rosenberg <robin.rosenberg@dewire.com>
  9. * Copyright (C) 2006-2008, Shawn O. Pearce <spearce@spearce.org>
  10. * Copyright (C) 2008, Thad Hughes <thadh@thad.corp.google.com>
  11. * and other copyright owners as documented in the project's IP log.
  12. *
  13. * This program and the accompanying materials are made available
  14. * under the terms of the Eclipse Distribution License v1.0 which
  15. * accompanies this distribution, is reproduced below, and is
  16. * available at http://www.eclipse.org/org/documents/edl-v10.php
  17. *
  18. * All rights reserved.
  19. *
  20. * Redistribution and use in source and binary forms, with or
  21. * without modification, are permitted provided that the following
  22. * conditions are met:
  23. *
  24. * - Redistributions of source code must retain the above copyright
  25. * notice, this list of conditions and the following disclaimer.
  26. *
  27. * - Redistributions in binary form must reproduce the above
  28. * copyright notice, this list of conditions and the following
  29. * disclaimer in the documentation and/or other materials provided
  30. * with the distribution.
  31. *
  32. * - Neither the name of the Eclipse Foundation, Inc. nor the
  33. * names of its contributors may be used to endorse or promote
  34. * products derived from this software without specific prior
  35. * written permission.
  36. *
  37. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  38. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  39. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  40. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  41. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  42. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  43. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  44. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  45. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  46. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  47. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  48. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  49. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  50. */
  51. package org.eclipse.jgit.lib;
  52. import org.eclipse.jgit.util.StringUtils;
  53. /** A line in a Git {@link Config} file. */
  54. class ConfigLine {
  55. /** The text content before entry. */
  56. String prefix;
  57. /** The section name for the entry. */
  58. String section;
  59. /** Subsection name. */
  60. String subsection;
  61. /** The key name. */
  62. String name;
  63. /** The value. */
  64. String value;
  65. /** The text content after entry. */
  66. String suffix;
  67. ConfigLine forValue(final String newValue) {
  68. final ConfigLine e = new ConfigLine();
  69. e.prefix = prefix;
  70. e.section = section;
  71. e.subsection = subsection;
  72. e.name = name;
  73. e.value = newValue;
  74. e.suffix = suffix;
  75. return e;
  76. }
  77. boolean match(final String aSection, final String aSubsection,
  78. final String aKey) {
  79. return eqIgnoreCase(section, aSection)
  80. && eqSameCase(subsection, aSubsection)
  81. && eqIgnoreCase(name, aKey);
  82. }
  83. boolean match(final String aSection, final String aSubsection) {
  84. return eqIgnoreCase(section, aSection)
  85. && eqSameCase(subsection, aSubsection);
  86. }
  87. private static boolean eqIgnoreCase(final String a, final String b) {
  88. if (a == null && b == null)
  89. return true;
  90. if (a == null || b == null)
  91. return false;
  92. return StringUtils.equalsIgnoreCase(a, b);
  93. }
  94. private static boolean eqSameCase(final String a, final String b) {
  95. if (a == null && b == null)
  96. return true;
  97. if (a == null || b == null)
  98. return false;
  99. return a.equals(b);
  100. }
  101. @SuppressWarnings("nls")
  102. @Override
  103. public String toString() {
  104. if (section == null)
  105. return "<empty>";
  106. StringBuilder b = new StringBuilder(section);
  107. if (subsection != null)
  108. b.append(".").append(subsection);
  109. if (name != null)
  110. b.append(".").append(name);
  111. if (value != null)
  112. b.append("=").append(value);
  113. return b.toString();
  114. }
  115. }