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.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

public class Main {
    /**
     * Parses the XPath expression in a string array containing the XPath elements. The expressiong must be a simple /a/b/c for
     * and it cannot contain any wild-cards or attributes.
     * 
     * @param sPath Path to be parsed.
     * @return The XPath expression in a string array.
     * @throws IllegalArgumentException Thrown if the parsing failed.
     */
    public static String[] parsePath(String sPath) throws IllegalArgumentException {
        if (sPath.indexOf("//") >= 0) {
            throw new IllegalArgumentException("Destination path cannot have wild-cards.");
        }

        List<String> lElemList = new LinkedList<String>(Arrays.asList(sPath.split("/")));

        for (ListIterator<String> iIter = lElemList.listIterator(); iIter.hasNext();) {
            String sElem = iIter.next();

            if (sElem.equals(".")) {
                throw new IllegalArgumentException("Destination path cannot have wild-cards.");
            }

            int iIndex = sElem.indexOf("@");

            if (iIndex >= 0) {
                sElem = ((iIndex > 0) ? sElem.substring(0, iIndex) : "");
            }

            if (sElem.length() == 0) {
                iIter.remove();
            }
        }

        return lElemList.toArray(new String[lElemList.size()]);
    }
}