View Javadoc
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> and others
11   *
12   * This program and the accompanying materials are made available under the
13   * terms of the Eclipse Distribution License v. 1.0 which is available at
14   * https://www.eclipse.org/org/documents/edl-v10.php.
15   *
16   * SPDX-License-Identifier: BSD-3-Clause
17   */
18  
19  package org.eclipse.jgit.lib;
20  
21  import org.eclipse.jgit.util.StringUtils;
22  
23  /** A line in a Git {@link Config} file. */
24  class ConfigLine {
25  	/** The text content before entry. */
26  	String prefix;
27  
28  	/** The section name for the entry. */
29  	String section;
30  
31  	/** Subsection name. */
32  	String subsection;
33  
34  	/** The key name. */
35  	String name;
36  
37  	/** The value. */
38  	String value;
39  
40  	/** The text content after entry. */
41  	String suffix;
42  
43  	/** The source from which this line was included from. */
44  	String includedFrom;
45  
46  	ConfigLine forValue(String newValue) {
47  		final ConfigLineLine.html#ConfigLine">ConfigLine e = new ConfigLine();
48  		e.prefix = prefix;
49  		e.section = section;
50  		e.subsection = subsection;
51  		e.name = name;
52  		e.value = newValue;
53  		e.suffix = suffix;
54  		e.includedFrom = includedFrom;
55  		return e;
56  	}
57  
58  	boolean match(final String aSection, final String aSubsection,
59  			final String aKey) {
60  		return eqIgnoreCase(section, aSection)
61  				&& eqSameCase(subsection, aSubsection)
62  				&& eqIgnoreCase(name, aKey);
63  	}
64  
65  	boolean match(String aSection, String aSubsection) {
66  		return eqIgnoreCase(section, aSection)
67  				&& eqSameCase(subsection, aSubsection);
68  	}
69  
70  	private static boolean eqIgnoreCase(String a, String b) {
71  		if (a == null && b == null)
72  			return true;
73  		if (a == null || b == null)
74  			return false;
75  		return StringUtils.equalsIgnoreCase(a, b);
76  	}
77  
78  	private static boolean eqSameCase(String a, String b) {
79  		if (a == null && b == null)
80  			return true;
81  		if (a == null || b == null)
82  			return false;
83  		return a.equals(b);
84  	}
85  
86  	/** {@inheritDoc} */
87  	@SuppressWarnings("nls")
88  	@Override
89  	public String toString() {
90  		if (section == null)
91  			return "<empty>";
92  		StringBuilder b = new StringBuilder(section);
93  		if (subsection != null)
94  			b.append(".").append(subsection);
95  		if (name != null)
96  			b.append(".").append(name);
97  		if (value != null)
98  			b.append("=").append(value);
99  		return b.toString();
100 	}
101 }