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 ConfigLine forValue(final String newValue) {
77 final ConfigLine e = new ConfigLine();
78 e.prefix = prefix;
79 e.section = section;
80 e.subsection = subsection;
81 e.name = name;
82 e.value = newValue;
83 e.suffix = suffix;
84 return e;
85 }
86
87 boolean match(final String aSection, final String aSubsection,
88 final String aKey) {
89 return eqIgnoreCase(section, aSection)
90 && eqSameCase(subsection, aSubsection)
91 && eqIgnoreCase(name, aKey);
92 }
93
94 boolean match(final String aSection, final String aSubsection) {
95 return eqIgnoreCase(section, aSection)
96 && eqSameCase(subsection, aSubsection);
97 }
98
99 private static boolean eqIgnoreCase(final String a, final String b) {
100 if (a == null && b == null)
101 return true;
102 if (a == null || b == null)
103 return false;
104 return StringUtils.equalsIgnoreCase(a, b);
105 }
106
107 private static boolean eqSameCase(final String a, final String b) {
108 if (a == null && b == null)
109 return true;
110 if (a == null || b == null)
111 return false;
112 return a.equals(b);
113 }
114
115 @SuppressWarnings("nls")
116 @Override
117 public String toString() {
118 if (section == null)
119 return "<empty>";
120 StringBuilder b = new StringBuilder(section);
121 if (subsection != null)
122 b.append(".").append(subsection);
123 if (name != null)
124 b.append(".").append(name);
125 if (value != null)
126 b.append("=").append(value);
127 return b.toString();
128 }
129 }