001    /*
002     * Copyright (C) 2010 eXo Platform SAS.
003     *
004     * This is free software; you can redistribute it and/or modify it
005     * under the terms of the GNU Lesser General Public License as
006     * published by the Free Software Foundation; either version 2.1 of
007     * the License, or (at your option) any later version.
008     *
009     * This software is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012     * Lesser General Public License for more details.
013     *
014     * You should have received a copy of the GNU Lesser General Public
015     * License along with this software; if not, write to the Free
016     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018     */
019    
020    package org.crsh.cmdline.matcher.tokenizer;
021    
022    /**
023     * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
024     * @version $Revision$
025     */
026    public abstract class Token {
027    
028    
029      public final static class Whitespace extends Token {
030    
031        Whitespace(int index, String raw) {
032          super(index, raw);
033        }
034    
035        @Override
036        public boolean equals(Object obj) {
037          if (obj == this) {
038            return true;
039          }
040          if (obj instanceof Whitespace) {
041            Whitespace that = (Whitespace)obj;
042            return super.equals(obj) && index == that.index;
043          }
044          return false;
045        }
046    
047        @Override
048        public String toString() {
049          return "Token.Whitespace[index=" + index + ",raw=" + raw + "]";
050        }
051      }
052    
053      public abstract static class Literal extends Token {
054    
055        public abstract static class Option extends Literal {
056    
057          /** . */
058          private final String name;
059    
060          public final String getName() {
061            return name;
062          }
063    
064          Option(int index, String raw, String value, String name) {
065            super(index, raw, value);
066            this.name = name;
067          }
068    
069          public final static class Short extends Option {
070            Short(int index, String raw, String value) {
071              super(index, raw, value, value.substring(1));
072            }
073          }
074    
075          public final static class Long extends Option {
076            Long(int index, String raw, String value) {
077              super(index, raw, value, value.substring(2));
078            }
079          }
080        }
081    
082        public final static class Word extends Literal {
083          Word(int index, String raw, String value) {
084            super(index, raw, value);
085          }
086    
087          Word(int index, String value) {
088            super(index, value);
089          }
090        }
091    
092        /** . */
093        final String value;
094    
095        Literal(int index, String value) {
096          this(index, value, value);
097        }
098    
099        Literal(int index, String raw, String value) {
100          super(index, raw);
101    
102          if (value == null) {
103            throw new NullPointerException();
104          }
105    
106          //
107          this.value = value;
108        }
109    
110        public String getValue() {
111          return value;
112        }
113    
114        @Override
115        public boolean equals(Object obj) {
116          if (obj == this) {
117            return true;
118          }
119          if (obj.getClass().equals(getClass())) {
120            Literal that = (Literal)obj;
121            return super.equals(obj) && index == that.index && value.equals(that.value);
122          }
123          return false;
124        }
125    
126        @Override
127        public String toString() {
128          return getClass().getSimpleName() + "[index=" + index + ",raw=" + raw + ",value=" + value + "]";
129        }
130      }
131    
132      /** The index in the containing sequence. */
133      final int index;
134    
135      /** . */
136      final String raw;
137    
138      Token(int index, String raw) {
139    
140        if (index < 0) {
141          throw new IllegalArgumentException();
142        }
143        if (raw == null) {
144          throw new NullPointerException();
145        }
146    
147        //
148        this.index = index;
149        this.raw = raw;
150      }
151    
152      /**
153       * Returns the raw text.
154       *
155       * @return the raw text
156       */
157      public String getRaw() {
158        return raw;
159      }
160    
161      /**
162       * Returns the from index is the containing string.
163       *
164       * @return the from index
165       */
166      public int getFrom() {
167        return index;
168      }
169    
170      /**
171       * Returns the to index in the containing string.
172       *
173       * @return the to index
174       */
175      public int getTo() {
176        return index + raw.length();
177      }
178    
179      @Override
180      public boolean equals(Object obj) {
181        if (obj == this) {
182          return true;
183        }
184        if (obj instanceof Token) {
185          Token that = (Token)obj;
186          return index == that.index && raw.equals(that.raw);
187        }
188        return false;
189      }
190    }