001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.joran.spi;
015
016import java.util.List;
017
018/**
019 * ElementSelector extends {@link ElementPath} with matching operations such as {@link #fullPathMatch(ElementPath)},
020 * {@link #getPrefixMatchLength(ElementPath)} and {@link #getTailMatchLength(ElementPath)}.
021 *
022 * <p>Parts of the path may contain '*' for wildcard matching.
023 *
024 * @author Ceki G&uuml;lc&uuml;
025 * @since 1.1.0
026 */
027public class ElementSelector extends ElementPath {
028
029    public ElementSelector() {
030        super();
031    }
032
033    public ElementSelector(List<String> list) {
034        super(list);
035    }
036
037    /**
038     * Build an elementPath from a string.
039     *
040     * Note that "/x" is considered equivalent to "x" and to "x/"
041     *
042     */
043    public ElementSelector(String p) {
044        super(p);
045    }
046
047    public boolean fullPathMatch(ElementPath path) {
048        if (path.size() != size()) {
049            return false;
050        }
051
052        int len = size();
053        for (int i = 0; i < len; i++) {
054            if (!equalityCheck(get(i), path.get(i))) {
055                return false;
056            }
057        }
058        // if everything matches, then the two patterns are equal
059        return true;
060    }
061
062    /**
063     * Returns the number of "tail" components that this pattern has in common
064     * with the pattern p passed as parameter. By "tail" components we mean the
065     * components at the end of the pattern.
066     */
067    public int getTailMatchLength(ElementPath p) {
068        if (p == null) {
069            return 0;
070        }
071
072        int lSize = this.partList.size();
073        int rSize = p.partList.size();
074
075        // no match possible for empty sets
076        if ((lSize == 0) || (rSize == 0)) {
077            return 0;
078        }
079
080        int minLen = (lSize <= rSize) ? lSize : rSize;
081        int match = 0;
082
083        // loop from the end to the front
084        for (int i = 1; i <= minLen; i++) {
085            String l = this.partList.get(lSize - i);
086            String r = p.partList.get(rSize - i);
087
088            if (equalityCheck(l, r)) {
089                match++;
090            } else {
091                break;
092            }
093        }
094        return match;
095    }
096
097    public boolean isContainedIn(ElementPath p) {
098        if (p == null) {
099            return false;
100        }
101        return p.toStableString().contains(toStableString());
102    }
103
104    /**
105     * Returns the number of "prefix" components that this pattern has in common
106     * with the pattern p passed as parameter. By "prefix" components we mean the
107     * components at the beginning of the pattern.
108     */
109    public int getPrefixMatchLength(ElementPath p) {
110        if (p == null) {
111            return 0;
112        }
113
114        int lSize = this.partList.size();
115        int rSize = p.partList.size();
116
117        // no match possible for empty sets
118        if ((lSize == 0) || (rSize == 0)) {
119            return 0;
120        }
121
122        int minLen = (lSize <= rSize) ? lSize : rSize;
123        int match = 0;
124
125        for (int i = 0; i < minLen; i++) {
126            String l = this.partList.get(i);
127            String r = p.partList.get(i);
128
129            if (equalityCheck(l, r)) {
130                match++;
131            } else {
132                break;
133            }
134        }
135
136        return match;
137    }
138
139    private boolean equalityCheck(String x, String y) {
140        return x.equalsIgnoreCase(y);
141    }
142
143    @Override
144    public boolean equals(Object o) {
145        if ((o == null) || !(o instanceof ElementSelector)) {
146            return false;
147        }
148
149        ElementSelector r = (ElementSelector) o;
150
151        if (r.size() != size()) {
152            return false;
153        }
154
155        int len = size();
156
157        for (int i = 0; i < len; i++) {
158            if (!equalityCheck(get(i), r.get(i))) {
159                return false;
160            }
161        }
162
163        // if everything matches, then the two patterns are equal
164        return true;
165    }
166
167    @Override
168    public int hashCode() {
169        int hc = 0;
170        int len = size();
171
172        for (int i = 0; i < len; i++) {
173            // make Pattern comparisons case insensitive
174            // http://jira.qos.ch/browse/LBCORE-76
175            hc ^= get(i).toLowerCase().hashCode();
176        }
177        return hc;
178    }
179
180}