Java String Split splitOgnl(String ognl)

Here you can find the source of splitOgnl(String ognl)

Description

Regular expression with repeating groups is a pain to get right and then nobody understands the reg exp afterwards.

License

Apache License

Declaration

public static List<String> splitOgnl(String ognl) 

Method Source Code

//package com.java2s;
/**/*w  w w.  j  a  v a 2s.co  m*/
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.ArrayList;
import java.util.List;

public class Main {
    /**
     * Regular expression with repeating groups is a pain to get right
     * and then nobody understands the reg exp afterwards.
     * So use a bit ugly/low-level java code to split the ognl into methods.
     */
    public static List<String> splitOgnl(String ognl) {
        // TODO: if possible use reg exp to split instead

        List<String> methods = new ArrayList<String>();

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < ognl.length(); i++) {
            char ch = ognl.charAt(i);
            // special for starting
            if (i == 0 || (i == 1 && ognl.charAt(0) == '?') || (ch != '.' && ch != '?')) {
                sb.append(ch);
            } else {
                if (ch == '.') {
                    String s = sb.toString();

                    // reset sb
                    sb.setLength(0);

                    // pass over ? to the new method
                    if (s.endsWith("?")) {
                        sb.append("?");
                        s = s.substring(0, s.length() - 1);
                    }

                    // add the method
                    methods.add(s);
                }
                // and dont lose the char
                sb.append(ch);
            }
        }
        // add remainder in buffer
        if (sb.length() > 0) {
            methods.add(sb.toString());
        }

        return methods;
    }
}

Related

  1. splitManagedBean(String el)
  2. splitMultiMessageString( String multiMessageString, String messageSplitter)
  3. splitName(String name)
  4. splitNames(final String string)
  5. splitObjectString(String str)
  6. splitOn(String toSplit, String splitter)
  7. splitOnNoWiki(String s)
  8. splitOnTokens(String text)
  9. splitOnTokens(String text)