001    /*
002     * 
003     * $Revision: 13085 $ $Date: 2008-02-06 18:27:24 +0100 (Mi, 06 Feb 2008) $
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.services.z3950;
025    
026    /**
027      Parse a prefix string
028          based on "Little Language pattern (Patterns in Java, Mark Grand, 1998)
029          and code from jzkit2 (http://developer.k-int.com/jzkit2/)
030     * @author Harald Richter
031     * @version $Revision: 13085 $ $Date: 2008-02-06 18:27:24 +0100 (Mi, 06 Feb 2008) $
032     */
033     
034    
035    import java.io.IOException;
036    import java.io.Reader;
037    import java.io.StreamTokenizer;
038    
039    public class MCRZ3950PrefixQueryLexer
040    {
041      StreamTokenizer input = null;
042    
043      static final int ERROR=-1;
044      static final int AND=1;
045      static final int OR=2;
046      static final int NOT=3;
047      static final int ATTR=4;
048      static final int ATTRSET=5;
049      static final int TERM=6;
050      static final int SPACE=7;
051      static final int EQUALS=8;
052      static final int EOL=9;
053      static final int EOF=10;
054      static final int NUMBER=11;
055      static final int ELEMENTNAME=12;
056      static final int PARAM=13;
057    
058      public MCRZ3950PrefixQueryLexer(Reader r)
059      {
060        input = new StreamTokenizer(r);
061        input.resetSyntax();
062        input.eolIsSignificant(true);
063        input.parseNumbers();
064        input.wordChars('\u0000', '\u00ff');
065        input.ordinaryChar('=');
066        input.quoteChar('"');
067        input.whitespaceChars(' ',' ');
068      }
069    
070      String getString()
071      {
072        return input.sval;
073      }
074    
075      int getInt()
076      {
077        return (int)input.nval;
078      }
079    
080      double getNumber()
081      {
082        return input.nval;
083      }
084    
085      int nextToken()
086      {
087        int token=0;
088        try
089        {
090          switch (input.nextToken())
091          {
092            case StreamTokenizer.TT_EOF:
093              token=EOF;
094              break;
095            case StreamTokenizer.TT_EOL:
096              token=EOL;
097              break;
098            case StreamTokenizer.TT_WORD:
099              if ( input.sval.equalsIgnoreCase("@AND") )
100                token=AND;
101              else if ( input.sval.equalsIgnoreCase("@OR") )
102                token = OR;
103              else if ( input.sval.equalsIgnoreCase("@NOT") )
104                token = NOT;
105              else if ( input.sval.equalsIgnoreCase("@ATTR") )
106                token = ATTR;
107              else if ( input.sval.equalsIgnoreCase("@ATTRSET") )
108                token = ATTRSET;
109              else if ( input.sval.equalsIgnoreCase("@ELEMENTNAME") )
110                token = ELEMENTNAME;
111              else if ( input.sval.equalsIgnoreCase("@PARAM") )
112                token = PARAM;
113              else 
114                token = TERM;
115              break;
116            case StreamTokenizer.TT_NUMBER:
117              token=NUMBER;
118              break;
119            case '=':
120              token = EQUALS;
121              break;
122            case '"':
123                token = TERM;
124              break;
125          }
126        }
127        catch(IOException e)
128        {
129           token=EOF;
130        }
131        return token;
132      }
133    }