001    /*
002     * 
003     * $Revision$ $Date$
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.access.mcrimpl;
025    
026    import java.text.DateFormat;
027    import java.text.SimpleDateFormat;
028    import java.util.Date;
029    
030    import org.jdom.Element;
031    import org.mycore.parsers.bool.MCRBooleanClauseParser;
032    import org.mycore.parsers.bool.MCRCondition;
033    import org.mycore.parsers.bool.MCRFalseCondition;
034    import org.mycore.parsers.bool.MCRParseException;
035    import org.mycore.parsers.bool.MCRTrueCondition;
036    
037    class MCRRuleParser extends MCRBooleanClauseParser {
038        MCRRuleParser() {
039        }
040    
041        private static DateFormat dateformat = new SimpleDateFormat("dd.MM.yyyy");
042    
043        private static Date parseDate(String s, boolean dayafter) throws MCRParseException {
044            try {
045                long time = dateformat.parse(s).getTime();
046    
047                if (dayafter) {
048                    time += (1000 * 60 * 60 * 24);
049                }
050    
051                return new Date(time);
052            } catch (java.text.ParseException e) {
053                throw new MCRParseException("unable to parse date " + s);
054            }
055        }
056    
057        protected MCRCondition parseSimpleCondition(Element e) throws MCRParseException {
058            String name = e.getName();
059            if (name.equals("boolean")) {
060                    return super.parseSimpleCondition(e);
061            } else if (name.equals("condition")) {
062                String field = e.getAttributeValue("field").toLowerCase().trim();
063                String operator = e.getAttributeValue("operator").trim();
064                String value = e.getAttributeValue("value").trim();
065                boolean not = "!=".equals(operator);
066    
067                if (field.equals("group")) {
068                    return new MCRGroupClause(value, not);
069                } else if (field.equals("user")) {
070                    return new MCRUserClause(value, not);
071                }else if (field.equals("ip")) {
072                    return new MCRIPClause(value);
073                }else if (field.equals("date")) {
074                    if(operator.equals("<")) {
075                            return new MCRDateBeforeClause(parseDate(value, false));
076                    }else if(operator.equals("<=")) {
077                            return new MCRDateBeforeClause(parseDate(value, true));
078                    }else if(operator.equals(">")) {
079                            return new MCRDateAfterClause(parseDate(value, true));
080                    }else if(operator.equals(">=")) {
081                            return new MCRDateAfterClause(parseDate(value, false));
082                    }
083                    else {
084                            throw new MCRParseException("Not a valid operator <" + operator + ">");
085                    }
086                }else {
087                    throw new MCRParseException("Not a valid condition field <" + field + ">");
088                }
089            }else{
090                throw new MCRParseException("Not a valid name <" + name + ">");
091            }       
092        }
093    
094        protected MCRCondition parseSimpleCondition(String s) throws MCRParseException {
095            /* handle specific rules */
096            if (s.equalsIgnoreCase("false")) {
097                return new MCRFalseCondition();
098            }
099    
100            if (s.equalsIgnoreCase("true")) {
101                return new MCRTrueCondition();
102            }
103    
104            if (s.startsWith("group")) {
105                s = s.substring(5).trim();
106                if (s.startsWith("!=")) {
107                    return new MCRGroupClause(s.substring(2).trim(), true);
108                } else if(s.startsWith("=")) {
109                    return new MCRGroupClause(s.substring(1).trim(), false);
110                } else throw new MCRParseException("syntax error: " + s);
111            }
112    
113            if (s.startsWith("user")) {
114                s = s.substring(4).trim();
115                if (s.startsWith("!=")) {
116                    return new MCRUserClause(s.substring(2).trim(), true);
117                } else if(s.startsWith("=")) {
118                    return new MCRUserClause(s.substring(1).trim(), false);
119                } else throw new MCRParseException("syntax error: " + s);
120            }
121    
122            if (s.startsWith("ip ")) {
123                return new MCRIPClause(s.substring(3).trim());
124            }
125    
126            if (s.startsWith("date ")) {
127                s = s.substring(5).trim();
128                if (s.startsWith(">=")) {
129                    return new MCRDateAfterClause(parseDate(s.substring(2).trim(), false));
130                } else if (s.startsWith("<=")) {
131                    return new MCRDateBeforeClause(parseDate(s.substring(2).trim(), true));
132                } else if (s.startsWith(">")) {
133                    return new MCRDateAfterClause(parseDate(s.substring(1).trim(), true));
134                } else if (s.startsWith("<")) {
135                    return new MCRDateBeforeClause(parseDate(s.substring(1).trim(), false));
136                } else throw new MCRParseException("syntax error: " + s);
137            }
138    
139            throw new MCRParseException("syntax error: " + s);
140        }
141    };