Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    private static final Pattern TAG_PATTERN = Pattern.compile("(?<=[/|::])[a-zA-z]+(?=([/|\\[]|$))");
    private static final Pattern ID_PATTERN = Pattern.compile("(@[a-zA-Z]+)");

    /**
     * @param xpath
     *            The xpath to format.
     * @return formatted xpath with tag names in uppercase and attributes in lowercase
     */
    public static String formatXPath(String xpath) {
        String formatted = capitalizeTagNames(xpath);
        formatted = lowerCaseAttributes(formatted);
        return formatted;
    }

    private static String capitalizeTagNames(String xpath) {
        Matcher m = TAG_PATTERN.matcher(xpath);
        StringBuffer sb = new StringBuffer();
        while (m.find()) {
            String text = m.group();
            m.appendReplacement(sb, Matcher.quoteReplacement(text.toUpperCase()));
        }
        m.appendTail(sb);
        return sb.toString();
    }

    private static String lowerCaseAttributes(String formatted) {
        Matcher m = ID_PATTERN.matcher(formatted);
        StringBuffer sb = new StringBuffer();
        while (m.find()) {
            String text = m.group();
            m.appendReplacement(sb, Matcher.quoteReplacement(text.toLowerCase()));
        }
        m.appendTail(sb);
        return sb.toString();
    }
}