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.ArrayList; 017import java.util.List; 018 019/** 020 * An element path characterizes a traversal path in an XML document. 021 * 022 * @author Ceki Gulcu 023 * @since 1.1.0 024 */ 025public class ElementPath { 026 027 ArrayList<String> partList = new ArrayList<String>(); 028 029 public ElementPath() { 030 } 031 032 public ElementPath(List<String> list) { 033 partList.addAll(list); 034 } 035 036 /** 037 * Build an elementPath from a string. 038 * <p> 039 * Note that "/x" is considered equivalent to "x" and to "x/" 040 */ 041 public ElementPath(String pathStr) { 042 if (pathStr == null) { 043 return; 044 } 045 046 String[] partArray = pathStr.split("/"); 047 if (partArray == null) 048 return; 049 050 for (String part : partArray) { 051 if (part.length() > 0) { 052 partList.add(part); 053 } 054 } 055 } 056 057 public ElementPath duplicate() { 058 ElementPath p = new ElementPath(); 059 p.partList.addAll(this.partList); 060 return p; 061 } 062 063 // Joran error skipping relies on the equals method 064 @Override 065 public boolean equals(Object o) { 066 if ((o == null) || !(o instanceof ElementPath)) { 067 return false; 068 } 069 070 ElementPath r = (ElementPath) o; 071 072 if (r.size() != size()) { 073 return false; 074 } 075 076 int len = size(); 077 078 for (int i = 0; i < len; i++) { 079 if (!equalityCheck(get(i), r.get(i))) { 080 return false; 081 } 082 } 083 084 // if everything matches, then the two patterns are equal 085 return true; 086 } 087 088 private boolean equalityCheck(String x, String y) { 089 return x.equalsIgnoreCase(y); 090 } 091 092 public List<String> getCopyOfPartList() { 093 return new ArrayList<String>(partList); 094 } 095 096 public void push(String s) { 097 partList.add(s); 098 } 099 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}