Java tutorial
//package com.java2s; //License from project: Common Public License import java.util.ArrayList; import java.util.List; public class Main { private static final List<String> BASE_PROPS_ATTS = new ArrayList<String>(); /** * @param domainsAtt * @return */ private static String[] getPropsAtts(String domainsAtt) { List<String> propsAtts = new ArrayList<String>(); String[] result = new String[propsAtts.size()]; propsAtts.addAll(BASE_PROPS_ATTS); if (domainsAtt == null) return propsAtts.toArray(result); char c; int p = 0; while (p < domainsAtt.length()) { c = domainsAtt.charAt(p); switch (c) { case ' ': case '\t': p++; break; case '(': p = parseParens(domainsAtt, ++p); break; case 'a': p = parsePropsDomain(domainsAtt, ++p, propsAtts); break; default: throw new RuntimeException("Unexpected character \"" + c + "\" at position " + p + 1 + " in domains attribute value \"" + domainsAtt + "\"" + ". Expected space, '(', or 'a'."); } } result = new String[propsAtts.size()]; return propsAtts.toArray(result); } /** * Scan to ")", return position following the ")". * @param domainsAtt * @param p * @return position of character following the ")". */ private static int parseParens(String domainsAtt, int p) { char c = domainsAtt.charAt(p); while (p < domainsAtt.length()) { switch (c) { case ')': p++; break; default: p++; } } return p; } /** * @param domainsAtt * @param i * @param propsAtts * @return */ private static int parsePropsDomain(String domainsAtt, int p, List<String> propsAtts) { if (p >= domainsAtt.length()) { throw new RuntimeException( "Unexpected end of string, expected '(' in domains attribute value \"" + domainsAtt + "\""); } char c = domainsAtt.charAt(p++); if (c != '(') { throw new RuntimeException("Expected '(' following 'a', found '" + c + "' at position " + p + " in domains attribute value \"" + domainsAtt + "\""); } StringBuilder buf = new StringBuilder(); boolean done = false; while (!done && p < domainsAtt.length()) { c = domainsAtt.charAt(p); switch (c) { case ')': p++; done = true; break; default: buf.append(c); p++; } } if (!done) { throw new RuntimeException( "Unexpected end of string, expected ')' in domains attribute value \"" + domainsAtt + "\""); } String[] tokens = buf.toString().trim().split(" "); if (tokens.length < 2) { throw new RuntimeException("Expected two tokens in attribute domain declaration, found " + tokens.length + " in domains attribute value \"" + domainsAtt + "\""); } if ("props".equals(tokens[0])) { propsAtts.add(tokens[tokens.length - 1]); } return p; } }