Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Actuate Corporation  - initial API and implementation
 *******************************************************************************/

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    private static final String reg1 = "Total." + "(count|ave|sum|max|min)" + "\\(", reg2 = "\\)", reg3 = "\\[",
            reg4 = "\\]";

    public static boolean isValidExp(String exp, String[] columnNames) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < columnNames.length; i++) {
            sb.append(columnNames[i] + "|");
        }
        String columnRegExp = "(" + sb.substring(0, sb.length() - 1) + ")";
        columnRegExp = columnRegExp.replaceAll(reg3, "Z");
        columnRegExp = columnRegExp.replaceAll(reg4, "Z");

        String aggregateRegExp = reg1 + columnRegExp + reg2;

        exp = exp.replaceAll(reg3, "Z");
        exp = exp.replaceAll(reg4, "Z");

        Pattern p = Pattern.compile(aggregateRegExp);
        Matcher m = p.matcher(exp);
        boolean agg = m.matches();

        p = Pattern.compile(columnRegExp);
        m = p.matcher(exp);
        return agg || m.matches();
    }

    public static String replaceAll(String str, String old, String news) {
        if (str == null) {
            return str;
        }

        int begin = 0;
        int idx = 0;
        int len = old.length();
        StringBuffer buf = new StringBuffer();

        while ((idx = str.indexOf(old, begin)) >= 0) {
            buf.append(str.substring(begin, idx));
            buf.append(news);
            begin = idx + len;
        }

        return new String(buf.append(str.substring(begin)));
    }
}