1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.xmlfield.core.internal;
17
18 import static org.apache.commons.lang.StringUtils.substringAfterLast;
19 import static org.apache.commons.lang.StringUtils.substringBefore;
20 import static org.apache.commons.lang.StringUtils.substringBetween;
21
22 import java.util.HashMap;
23 import java.util.Map;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
26
27 import org.apache.commons.lang.StringUtils;
28
29
30
31
32
33
34
35 public class XPathUtils {
36
37 public static final int TYPE_ATTRIBUTE = 3;
38
39 public static final int TYPE_TAG = 1;
40
41 public static final int TYPE_TAG_WITH_ATTRIBUTE = 2;
42
43
44
45
46
47
48
49
50
51
52
53 public static String getElementName(String xPath) {
54 String name = XPathUtils.getElementNameWithSelector(xPath);
55
56
57 name = substringBefore(name, "[");
58 return name;
59 }
60
61 public static String getElementNameWithSelector(String xPath) {
62 String name = xPath;
63 if (name.contains("/")) {
64 name = substringAfterLast(name, "/");
65 }
66 return name;
67 }
68
69
70
71
72
73
74
75
76 public static int getElementPosition(String xPath) {
77 String name = XPathUtils.getElementNameWithSelector(xPath);
78
79
80 name = substringBetween(name, "[", "]");
81 if (name == null) {
82 return 1;
83 }
84 return Integer.parseInt(name);
85 }
86
87
88
89
90
91
92
93
94 public static String getElementPrefix(String xPath) {
95 String name = XPathUtils.getElementNameWithSelector(xPath);
96
97 if (name.contains(":")) {
98
99 return substringBefore(name, ":");
100 }
101
102 return null;
103
104 }
105
106
107
108
109
110
111
112
113 public static Map<String, String> getElementSelectorAttributes(String xPath) {
114 String name = XPathUtils.getElementNameWithSelector(xPath);
115 HashMap<String, String> result = null;
116 if (name.contains("[@")) {
117 result = new HashMap<String, String>();
118 String[] attributes = StringUtils.split(name, "[@");
119 for (String a : attributes) {
120 if (a.contains("=")) {
121 String[] aSplitted = a.split("=");
122 int endIndex = 1;
123 switch (aSplitted[1].charAt(0)) {
124 case '\"':
125 case '\'':
126 endIndex++;
127
128 break;
129 default:
130 }
131 aSplitted[1] = aSplitted[1].substring(1, aSplitted[1].length() - endIndex);
132
133 result.put(aSplitted[0], aSplitted[1]);
134 }
135
136 }
137
138 }
139
140 return result;
141 }
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162 public static int getElementType(String xPath) {
163 String name = XPathUtils.getElementNameWithSelector(xPath);
164
165 if (name.startsWith("@")) {
166 return XPathUtils.TYPE_ATTRIBUTE;
167 }
168
169 if (name.contains("[@")) {
170 return XPathUtils.TYPE_TAG_WITH_ATTRIBUTE;
171 }
172
173 return XPathUtils.TYPE_TAG;
174 }
175
176
177
178
179
180
181
182
183 public static String getElementXPath(String fieldXPath) {
184 Pattern pattern = Pattern.compile("^(.+)\\/@(.+)$");
185 Matcher matcher = pattern.matcher(fieldXPath);
186 if (matcher.matches()) {
187 return matcher.group(1);
188 }
189 return null;
190 }
191
192
193
194
195
196
197
198
199 public static boolean isAttributeXPath(String fieldXPath) {
200 Pattern pattern = Pattern.compile("^(.*)@(.+)$");
201 Matcher matcher = pattern.matcher(fieldXPath);
202 if (matcher.matches()) {
203 return true;
204 }
205 return false;
206 }
207
208 }