001 /*
002 *
003 * $Revision: 14981 $ $Date: 2009-03-20 15:19:48 +0100 (Fri, 20 Mar 2009) $
004 *
005 * This file is part of *** M y C o R e ***
006 * See http://www.mycore.de/ for details.
007 *
008 * This program is free software; you can use it, redistribute it
009 * and / or modify it under the terms of the GNU General Public License
010 * (GPL) as published by the Free Software Foundation; either version 2
011 * of the License or (at your option) any later version.
012 *
013 * This program is distributed in the hope that it will be useful, but
014 * WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016 * GNU General Public License for more details.
017 *
018 * You should have received a copy of the GNU General Public License
019 * along with this program, in a file called gpl.txt or license.txt.
020 * If not, write to the Free Software Foundation Inc.,
021 * 59 Temple Place - Suite 330, Boston, MA 02111-1307 USA
022 */
023
024 package org.mycore.parsers.bool;
025
026 import java.util.Collection;
027 import java.util.LinkedList;
028 import java.util.List;
029
030 import org.jdom.Attribute;
031 import org.jdom.Element;
032
033 /**
034 * @author Frank Lützenkirchen
035 */
036 public abstract class MCRSetCondition implements MCRCondition {
037
038 public final static String AND = "and";
039
040 public final static String OR = "or";
041
042 protected String operator;
043
044 protected List<MCRCondition> children = new LinkedList<MCRCondition>();
045
046 protected MCRSetCondition(String operator) {
047 this.operator = operator;
048 }
049
050 public String getOperator() {
051 return operator;
052 }
053
054 public MCRSetCondition addChild(MCRCondition condition) {
055 children.add(condition);
056 return this;
057 }
058
059 public MCRSetCondition addAll(Collection<MCRCondition> conditions) {
060 children.addAll(conditions);
061 return this;
062 }
063
064 public List<MCRCondition> getChildren() {
065 return children;
066 }
067
068 public String toString() {
069 StringBuffer sb = new StringBuffer();
070 for (int i = 0; i < children.size(); i++) {
071 sb.append("(").append(children.get(i)).append(")");
072 if (i < (children.size() - 1))
073 sb.append(' ').append(operator.toUpperCase()).append(' ');
074 }
075 return sb.toString();
076 }
077
078 public Element toXML() {
079 Element cond = new Element("boolean").setAttribute("operator", operator);
080 for (MCRCondition child : children)
081 cond.addContent(child.toXML());
082 return cond;
083 }
084
085 public Element info() {
086 Element el = new Element("info");
087 el.setAttribute(new Attribute("type", operator));
088 el.setAttribute(new Attribute("children", "" + children.size()));
089 return el;
090 }
091
092 public void accept(MCRConditionVisitor visitor) {
093 visitor.visitType(info());
094 for (MCRCondition child : children)
095 child.accept(visitor);
096 }
097
098 public abstract boolean evaluate(Object o);
099 }