?? parser.java
字號:
/******************************************************************************* * Copyright (C) 2002, 2003 * ingenieurbuero fuer innovative informationstechnik (iiit) * Dipl.-Ing. Joerg Beckmann, Dortmund, Germany * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * version $Id: Parser.java,v 1.7 2003/04/13 21:09:56 joerg Exp $ ******************************************************************************/package de.iiit.access.server.plugins.parser;import de.iiit.access.common.api.*;import de.iiit.access.server.*;import de.iiit.access.server.api.*;import java.util.*;import java.util.regex.*;import org.apache.log4j.Logger;/** Provides a parser for operations on sets. * * <p>Every expression is evaluated from left to right. The operator '&' has * a higher priority then '+' and '-'.</p> * * Examples: * * <CODE> <PRE> * A1 = { a1, a2 } * A2 = { a3, a4 } * A3 = { a5, a6 } * A12 = A1 + A2 = { a1, a2, a3, a4 } * A13 = A1 + A3 = { a1, a2, a5, a6 } * A23 = A2 + A3 = { a3, a4, a3, a4 } * A = A1 + A2 + A3 = { a1, a2, a3, a4, a5, a6 } * N = { } * * A1 + A2 = A2 + A1 = { a1, a2, a3, a4 } * A1 + A1 = A1 * A + A1 = A + A2 = A + A3 = A * A1 - A1 = N * A1 - A2 = A1 * A1 - A = N * A - A1 - A2 = A - (A1 + A2) = A3 * * A - A1 = { a3, a4, a5, a6 } * A - A1 + A2 = (A - A1) + A2 = { a3, a4, a5, a6 } * A - A2 + A1 = (A - A2) + A1 = { a1, a2, a5, a6 } * A - (A1 + A2) = A - (A2 + A1) = A3 * A + A1 - A2 = (A + A1) - A2 = { a1, a2, a5, a6 } * A + A2 - A1 = (A + A2) - A1 = { a3, a4, a5, a6 } * A + A1 - A1 = (A + A1) - A1 = { a3, a4, a5, a6 } * A - A1 + A1 = (A - A1) + A1 = { a1, a2, a3, a4, a5, a6 } * A1 - A = N * A1 - A2 = A1 * * A1 & A2 = N * A & N = N * A & A1 & A2 = (A & A1) & A2 = N * A & (A1 & A2) = N * (A1 + A2) & (A2 + A3) = (A2 + A1) & (A3 + A2) = A2 * A1 + A2 & A2 + A3 = A1 + (A2 & A2) + A3 = A * A2 + A1 & A3 + A2 = A2 + (A1 & A3) + A2 = A2 * * A & A2 = A2 & A = A2 * A1 & A2 = A2 & A1 = N * </PRE> </CODE> * @version $Revision: 1.7 $ $Date: 2003/04/13 21:09:56 $ */public class Parser{ /** CVS Version Tag */ private static final String vcid = "$Id: Parser.java,v 1.7 2003/04/13 21:09:56 joerg Exp $"; private Logger logger = Logger.getLogger(this.getClass()); private static Pattern pOpenBrace = Pattern.compile("\\s*(\\()\\s*"); // ( private static Pattern pBrace = Pattern.compile("\\s*([\\(\\)])\\s*"); // ( or ) private static Pattern pArgument = Pattern.compile("\\s*([a-zA-Z0-9_]+)\\s*"); // Token private static Pattern pOperator = Pattern.compile("\\s*([&+\\-])\\s*"); // + or - or & private static Pattern pSpace = Pattern.compile("\\s*$"); // Nothing but whitespace private static Pattern pArgumentOnly = Pattern.compile("^\\s*([a-zA-Z0-9_]+)\\s*$"); // Token without calculations private boolean returnEmptySets = false; private ResolverPluginIf resolver = null; /** Creates a new instance of Parser. The AccessServer is asked for the * ResolverPlugin to use for the evaluations. */ public Parser() { resolver = AccessServer.getResolverPlugin(); } /** Creates a new instance of Parser. * @param resolver The ResolverPlugin to use for the evaluations. */ public Parser(ResolverPluginIf resolver) { this.resolver = resolver; } /** Sets whether the parser shall empty sets or null values when nothing was found. * @param flag true when empty sets shall be returned or false if null shall be returned */ public void setReturnEmptySets(boolean flag) { returnEmptySets = flag; } /** Retrieves whether the parser shall empty sets or null values when nothing was found. * @return true when empty sets shall be returned or false if null shall be returned */ public boolean getReturnEmptySets() { return returnEmptySets; } private Set evaluateArgument(ParserStackIf argStack, String name) throws ParserException { Set result = null; Class c = null; Object argument = argStack.peek(); try { c = Class.forName("java.util.Set"); } catch(Exception e) { logger.error("Exception catched", e); } if (argument.getClass().equals(vcid.getClass())) // Is it a String? { if (resolver != null) { if (name != null) result = resolver.resolve(argStack, name); else result = resolver.resolve(argStack); } if (result == null) { result = new HashSet(); if (! returnEmptySets) result.add(argument); } } else if (c.isAssignableFrom(argument.getClass())) // Is it a kind of Set { result = (Set) argument; } else throw new ParserException("Internal parser error, should not happen."); return result; } private int handleBraces(ParserStackIf argStack, String name, Vector formula, Matcher mOpenBrace, int position, int startPos) throws ParserException { int s = mOpenBrace.end(); String expression = (String) argStack.peek(); String subExpression = expression.substring(s); Matcher mBrace = pBrace.matcher(subExpression); boolean loop2 = true; int braceCount = 1; int e = s; while (loop2) { if (mBrace.find()) { if (mBrace.group(1).equals("(")) braceCount++; else if (--braceCount == 0) loop2 = false; } else throw new ParserException("Matching brace not found near position " + (position + startPos) + " in <" + expression + ">"); } subExpression = subExpression.substring(0, mBrace.start()); logger.debug("Sub-expression = <" + subExpression + ">"); argStack.pushArgument(subExpression); formula.add(evaluate(argStack, name, position + s)); argStack.pop(); return s + mBrace.end(); } private Set evaluate(ParserStackIf argStack, String name, int position) throws ParserException { HashSet result = null; Vector formula = new Vector(); int startPos = 0; String expression = (String) argStack.peek(); Matcher mArgumentOnly = pArgumentOnly.matcher(expression);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -