Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright (c) 2012. betterFORM Project - http://www.betterform.de
 * Licensed under the terms of BSD License
 */

import java.util.ArrayList;

public class Main {
    /**
     * Splits the specified step into parts.
     * <p/>
     * Examples:
     * <ul>
     * <li><code>"a"</code> returns <code>{"a"}</code>.
     * <li><code>"a[p]"</code> returns <code>{"a", "p"}</code>.
     * <li><code>"a[p][q]"</code> returns <code>{"a", "p", "q"}</code>.
     * <li><code>"child::a[p][q]"</code> returns <code>{"child::a", "p", "q"}</code>.
     * <li><code>"a[b[p]][c/d[q]]"</code> returns <code>{"a", "b[p]", "c/d[q]"}</code>.
     * </ul>
     *
     * @param step the step.
     * @return the specified step in parts.
     */
    public static String[] splitStep(String step) {
        if (step == null) {
            return null;
        }

        int offset = 0;
        int braces = 0;
        ArrayList list = new ArrayList();
        for (int index = 0; index < step.length(); index++) {
            switch (step.charAt(index)) {
            case '[':
                if (braces == 0) {
                    if (offset == 0) {
                        list.add(step.substring(offset, index));
                    }
                    offset = index + 1;
                }
                braces++;
                break;
            case ']':
                if (braces == 1) {
                    list.add(step.substring(offset, index));
                    offset = index + 1;
                }
                braces--;
                break;
            }
        }

        if (offset < step.length()) {
            list.add(step.substring(offset, step.length()));
        }

        return (String[]) list.toArray(new String[0]);
    }
}