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>
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  
52  package org.eclipse.jgit.lib;
53  
54  import org.eclipse.jgit.util.StringUtils;
55  
56  /** A line in a Git {@link Config} file. */
57  class ConfigLine {
58  	/** The text content before entry. */
59  	String prefix;
60  
61  	/** The section name for the entry. */
62  	String section;
63  
64  	/** Subsection name. */
65  	String subsection;
66  
67  	/** The key name. */
68  	String name;
69  
70  	/** The value. */
71  	String value;
72  
73  	/** The text content after entry. */
74  	String suffix;
75  
76  	/** The source from which this line was included from. */
77  	String includedFrom;
78  
79  	ConfigLine forValue(String newValue) {
80  		final ConfigLineLine.html#ConfigLine">ConfigLine e = new ConfigLine();
81  		e.prefix = prefix;
82  		e.section = section;
83  		e.subsection = subsection;
84  		e.name = name;
85  		e.value = newValue;
86  		e.suffix = suffix;
87  		e.includedFrom = includedFrom;
88  		return e;
89  	}
90  
91  	boolean match(final String aSection, final String aSubsection,
92  			final String aKey) {
93  		return eqIgnoreCase(section, aSection)
94  				&& eqSameCase(subsection, aSubsection)
95  				&& eqIgnoreCase(name, aKey);
96  	}
97  
98  	boolean match(String aSection, String aSubsection) {
99  		return eqIgnoreCase(section, aSection)
100 				&& eqSameCase(subsection, aSubsection);
101 	}
102 
103 	private static boolean eqIgnoreCase(String a, String b) {
104 		if (a == null && b == null)
105 			return true;
106 		if (a == null || b == null)
107 			return false;
108 		return StringUtils.equalsIgnoreCase(a, b);
109 	}
110 
111 	private static boolean eqSameCase(String a, String b) {
112 		if (a == null && b == null)
113 			return true;
114 		if (a == null || b == null)
115 			return false;
116 		return a.equals(b);
117 	}
118 
119 	/** {@inheritDoc} */
120 	@SuppressWarnings("nls")
121 	@Override
122 	public String toString() {
123 		if (section == null)
124 			return "<empty>";
125 		StringBuilder b = new StringBuilder(section);
126 		if (subsection != null)
127 			b.append(".").append(subsection);
128 		if (name != null)
129 			b.append(".").append(name);
130 		if (value != null)
131 			b.append("=").append(value);
132 		return b.toString();
133 	}
134 }