1 package org.apache.archiva.web.test;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
23 * Utility class for creating xpath expressions
25 public class XPathExpressionUtil
27 public static final String CONTAINS = "contains";
29 public static final String AND = " and ";
31 public static final String CURRENT_NODE = "./";
33 public static final String PARENT_NODE = "../";
35 public static final String GRANDPARENT_NODE = "../../";
37 public static final String ELEMENT_ANY_LEVEL = "//";
39 public static final String TABLE_COLUMN = "td";
41 public static final String TABLE_ROW = "tr";
43 public static final String START_NODE_TEST = "[";
45 public static final String END_NODE_TEST = "]";
47 public static final String ANCHOR = "a";
49 public static final String IMG = "img";
51 public static final String LIST = "ul";
53 public static final String LINE = "li";
55 public static String getList( String[] values )
57 String xpathExpression = "";
59 if ( values.length > 0 )
61 xpathExpression += ELEMENT_ANY_LEVEL;
62 xpathExpression += LIST;
63 xpathExpression += START_NODE_TEST;
65 for ( int nIndex = 0; nIndex < values.length; nIndex++ )
67 xpathExpression += ( ( nIndex > 0 ) ? AND : "" );
68 xpathExpression += contains( LINE + position( nIndex + 1 ), values[nIndex] );
71 xpathExpression += END_NODE_TEST;
74 return xpathExpression;
78 * expression for acquiring an element in one of the table columns
80 * @param element the node element
81 * @param elementIndex column index of the element, used for skipping
82 * @param columnValues the values to be matched in each column, element column is included
85 public static String getColumnElement( String element, int elementIndex, String[] columnValues )
87 return getColumnElement( element, elementIndex, null, columnValues );
91 * expression for acquiring an element in one of the table columns
93 * @param element the node element
94 * @param elementIndex column index of the element, used for skipping
95 * @param elementValue the matched element value
96 * @param columnValues the values to be matched in each column, element column is included
99 public static String getColumnElement( String element, int elementIndex, String elementValue, String[] columnValues )
101 return getColumnElement( element, elementIndex, elementValue, "TEXT", columnValues );
105 * expression for acquiring an element in one of the table columns
107 * @param element the node element
108 * @param elementIndex column index of the element, used for skipping
109 * @param imageName the matched image name
110 * @param columnValues the values to be matched in each column, element column is included
113 public static String getImgColumnElement( String element, int elementIndex, String imageName, String[] columnValues )
115 return getColumnElement( element, elementIndex, imageName, IMG, columnValues );
119 * expression for acquiring an element in one of the table columns
121 * @param element the node element
122 * @param elementIndex column index of the element, used for skipping
123 * @param imageName the matched image name
124 * @param columnValues the values to be matched in each column, element column is included
127 private static String getColumnElement( String element, int elementIndex, String elementValue,
128 String elementValueType, String[] columnValues )
130 String xpathExpression = null;
132 if ( ( columnValues != null ) && ( columnValues.length > 0 ) )
134 xpathExpression = ELEMENT_ANY_LEVEL + element;
135 xpathExpression += START_NODE_TEST;
137 if ( elementValue != null )
139 if ( "TEXT".equals( elementValueType ) )
141 xpathExpression += contains( elementValue );
142 xpathExpression += ( columnValues.length > 0 ) ? AND : "";
146 // we are two levels below the table row element ( tr/td/<element> )
147 xpathExpression += matchColumns( GRANDPARENT_NODE, columnValues, elementIndex );
149 xpathExpression += END_NODE_TEST;
152 if ( IMG.equals( elementValueType ) )
154 xpathExpression += "/img[contains(@src, '" + elementValue + "')]";
157 return xpathExpression;
161 * expression for acquiring the table row that matches all column values with the same order as the list
163 * @param columnValues the matched list of columnValues
166 public static String getTableRow( String[] columnValues )
168 String xpathExpression = null;
170 if ( ( columnValues != null ) && ( columnValues.length > 0 ) )
172 xpathExpression = new String( ELEMENT_ANY_LEVEL + TABLE_ROW + START_NODE_TEST );
173 xpathExpression += matchColumns( columnValues );
174 xpathExpression += END_NODE_TEST;
177 return xpathExpression;
180 private static String matchColumns( String[] columnValues )
182 return matchColumns( columnValues, -1 );
185 private static String matchColumns( String[] columnValues, int skipIndex )
187 return matchColumns( null, columnValues, skipIndex );
190 private static String matchColumns( String parent, String[] columnValues, int skipIndex )
192 String xpathExpression = "";
194 for ( int nIndex = 0; nIndex < columnValues.length; nIndex++ )
196 if ( ( skipIndex != nIndex ) || ( skipIndex == -1 ) )
198 // prepend "and" if index > 0
199 xpathExpression += ( ( nIndex > 0 ) ? AND : "" );
200 xpathExpression += contains( parent, TABLE_COLUMN + position( nIndex + 1 ), columnValues[nIndex] );
204 return xpathExpression;
207 private static String position( int nIndex )
209 return new String( "[" + nIndex + "]" );
212 private static String contains( String parent, String element, String matchedString )
214 String finalElement = ( parent != null ) ? parent : "";
215 finalElement += element;
217 return contains( finalElement, matchedString );
220 private static String contains( String matchedString )
222 return contains( ".", matchedString );
225 private static String contains( String axis, String matchedString )
227 return new String( CONTAINS + "(" + axis + "," + "'" + matchedString + "')" );
230 private static String equals( String parent, String element, String matchedString )
232 String finalElement = ( parent != null ) ? parent : "";
233 finalElement += element;
235 return equals( finalElement, matchedString );
238 private static String equals( String axis, String matchedString )
240 return new String( axis + "==" + "'" + matchedString + "'" );