Java tutorial
//package com.java2s; //License from project: Apache License import java.util.Arrays; import java.util.List; public class Main { private static final int MAX_SEARCH_LOOPS = 10000; /** * @param dom * The dom string. * @param pos * Position where to start searching. * @param element * The element. * @return the position where the close element is */ public static int getCloseElementLocation(String dom, int pos, String element) { String[] elements = { "LINK", "META", "INPUT", "BR" }; List<String> singleElements = Arrays.asList(elements); if (singleElements.contains(element.toUpperCase())) { return dom.indexOf('>', pos) + 1; } // make sure not before the node int openElements = 1; int i = 0; int position = pos; String dom_lower = dom.toLowerCase(); String element_lower = element.toLowerCase(); String openElement = "<" + element_lower; String closeElement = "</" + element_lower; while (i < MAX_SEARCH_LOOPS) { if (dom_lower.indexOf(openElement, position) == -1 && dom_lower.indexOf(closeElement, position) == -1) { return -1; } if (dom_lower.indexOf(openElement, position) < dom_lower.indexOf(closeElement, position) && dom_lower.indexOf(openElement, position) != -1) { openElements++; position = dom_lower.indexOf(openElement, position) + 1; } else { openElements--; position = dom_lower.indexOf(closeElement, position) + 1; } if (openElements == 0) { break; } i++; } return position - 1; } /** * @param dom * The dom. * @param xpath * The xpath expression. * @return the position where the close element is */ public static int getCloseElementLocation(String dom, String xpath) { return getCloseElementLocation(dom, getXPathLocation(dom, xpath) + 1, getLastElementXPath(xpath)); } /** * returns position of xpath element which match the expression xpath in the String dom. * * @param dom * the Document to search in * @param xpath * the xpath query * @return position of xpath element, if fails returns -1 **/ public static int getXPathLocation(String dom, String xpath) { String dom_lower = dom.toLowerCase(); String xpath_lower = xpath.toLowerCase(); String[] elements = xpath_lower.split("/"); int pos = 0; int temp; int number; for (String element : elements) { if (!element.isEmpty() && !element.startsWith("@") && !element.contains("()")) { if (element.contains("[")) { try { number = Integer .parseInt(element.substring(element.indexOf("[") + 1, element.indexOf("]"))); } catch (NumberFormatException e) { return -1; } } else { number = 1; } for (int i = 0; i < number; i++) { // find new open element temp = dom_lower.indexOf("<" + stripEndSquareBrackets(element), pos); if (temp > -1) { pos = temp + 1; // if depth>1 then goto end of current element if (number > 1 && i < number - 1) { pos = getCloseElementLocation(dom_lower, pos, stripEndSquareBrackets(element)); } } } } } return pos - 1; } /** * @param xpath * The xpath expression to find the last element of. * @return returns the last element in the xpath expression */ public static String getLastElementXPath(String xpath) { String[] elements = xpath.split("/"); for (int i = elements.length - 1; i >= 0; i--) { if (!elements[i].equals("") && elements[i].indexOf("()") == -1 && !elements[i].startsWith("@")) { return stripEndSquareBrackets(elements[i]); } } return ""; } /** * @param string * @return string without the before [ */ private static String stripEndSquareBrackets(String string) { if (string.contains("[")) { return string.substring(0, string.indexOf('[')); } else { return string; } } }