1 /**
2  * Logback: the reliable, generic, fast and flexible logging framework.
3  * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4  *
5  * This program and the accompanying materials are dual-licensed under
6  * either the terms of the Eclipse Public License v1.0 as published by
7  * the Eclipse Foundation
8  *
9  *   or (per the licensee's choosing)
10  *
11  * under the terms of the GNU Lesser General Public License version 2.1
12  * as published by the Free Software Foundation.
13  */

14 package ch.qos.logback.core.joran.spi;
15
16 import java.util.ArrayList;
17 import java.util.List;
18
19 /**
20  * A element path characterizes a traversal path in an XML document.
21  *
22  * @author Ceki Gulcu
23  * @since 1.1.0
24  */

25 public class ElementPath {
26     // contains String instances
27     ArrayList<String> partList = new ArrayList<String>();
28
29     public ElementPath() {
30     }
31
32     public ElementPath(List<String> list) {
33         partList.addAll(list);
34     }
35
36     /**
37      * Build an elementPath from a string.
38      * <p/>
39      * Note that "/x" is considered equivalent to "x" and to "x/"
40      */

41     public ElementPath(String pathStr) {
42         if (pathStr == null) {
43             return;
44         }
45
46         String[] partArray = pathStr.split("/");
47         if (partArray == null)
48             return;
49
50         for (String part : partArray) {
51             if (part.length() > 0) {
52                 partList.add(part);
53             }
54         }
55     }
56
57     public ElementPath duplicate() {
58         ElementPath p = new ElementPath();
59         p.partList.addAll(this.partList);
60         return p;
61     }
62
63     // Joran error skipping relies on the equals method
64     @Override
65     public boolean equals(Object o) {
66         if ((o == null) || !(o instanceof ElementPath)) {
67             return false;
68         }
69
70         ElementPath r = (ElementPath) o;
71
72         if (r.size() != size()) {
73             return false;
74         }
75
76         int len = size();
77
78         for (int i = 0; i < len; i++) {
79             if (!equalityCheck(get(i), r.get(i))) {
80                 return false;
81             }
82         }
83
84         // if everything matches, then the two patterns are equal
85         return true;
86     }
87
88     private boolean equalityCheck(String x, String y) {
89         return x.equalsIgnoreCase(y);
90     }
91
92     public List<String> getCopyOfPartList() {
93         return new ArrayList<String>(partList);
94     }
95
96     public void push(String s) {
97         partList.add(s);
98     }
99
100     public String get(int i) {
101         return (String) partList.get(i);
102     }
103
104     public void pop() {
105         if (!partList.isEmpty()) {
106             partList.remove(partList.size() - 1);
107         }
108     }
109
110     public String peekLast() {
111         if (!partList.isEmpty()) {
112             int size = partList.size();
113             return (String) partList.get(size - 1);
114         } else {
115             return null;
116         }
117     }
118
119     public int size() {
120         return partList.size();
121     }
122
123     protected String toStableString() {
124         StringBuilder result = new StringBuilder();
125         for (String current : partList) {
126             result.append("[").append(current).append("]");
127         }
128         return result.toString();
129     }
130
131     @Override
132     public String toString() {
133         return toStableString();
134     }
135 }
136