Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 *    GeoTools - The Open Source Java GIS Toolkit
 *    http://geotools.org
 *
 *    (C) 2009-2011, Open Source Geospatial Foundation (OSGeo)
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation;
 *    version 2.1 of the License.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 */

import java.util.Scanner;

public class Main {
    /**
     * Remove indexes from an xpath string.
     * @param xpath xpath string
     * @return unindexed xpath string
     */
    public static String removeIndexes(String xpath) {
        final String[] partialSteps = xpath.split("[/]");
        if (partialSteps.length == 0) {
            return xpath;
        }

        int startIndex = 0;
        StringBuffer buf = new StringBuffer();

        for (int i = startIndex; i < partialSteps.length; i++) {
            String step = partialSteps[i];
            int start = step.indexOf('[');

            if (start > -1) {
                int end = step.indexOf(']');
                Scanner scanner = new Scanner(step.substring(start + 1, end));
                if (scanner.hasNextInt()) {
                    // remove index and the brackets
                    step = step.substring(0, start);
                }
            }
            buf.append(step);
            if (i < partialSteps.length - 1) {
                buf.append("/");
            }
        }
        return buf.toString();
    }
}