Home | History | Annotate | Download | only in tokens
      1 /**
      2  * Copyright (c) 2008, http://www.snakeyaml.org
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package org.yaml.snakeyaml.tokens;
     17 
     18 import org.yaml.snakeyaml.error.Mark;
     19 import org.yaml.snakeyaml.error.YAMLException;
     20 
     21 public abstract class Token {
     22     public enum ID {
     23         Alias, Anchor, BlockEnd, BlockEntry, BlockMappingStart, BlockSequenceStart, Directive, DocumentEnd, DocumentStart, FlowEntry, FlowMappingEnd, FlowMappingStart, FlowSequenceEnd, FlowSequenceStart, Key, Scalar, StreamEnd, StreamStart, Tag, Value, Whitespace, Comment, Error
     24     }
     25 
     26     private final Mark startMark;
     27     private final Mark endMark;
     28 
     29     public Token(Mark startMark, Mark endMark) {
     30         if (startMark == null || endMark == null) {
     31             throw new YAMLException("Token requires marks.");
     32         }
     33         this.startMark = startMark;
     34         this.endMark = endMark;
     35     }
     36 
     37     public String toString() {
     38         return "<" + this.getClass().getName() + "(" + getArguments() + ")>";
     39     }
     40 
     41     public Mark getStartMark() {
     42         return startMark;
     43     }
     44 
     45     public Mark getEndMark() {
     46         return endMark;
     47     }
     48 
     49     /**
     50      * @see "__repr__ for Token in PyYAML"
     51      */
     52     protected String getArguments() {
     53         return "";
     54     }
     55 
     56     /**
     57      * For error reporting.
     58      *
     59      * @see "class variable 'id' in PyYAML"
     60      */
     61     public abstract Token.ID getTokenId();
     62 
     63     /*
     64      * for tests only
     65      */
     66     @Override
     67     public boolean equals(Object obj) {
     68         if (obj instanceof Token) {
     69             return toString().equals(obj.toString());
     70         } else {
     71             return false;
     72         }
     73     }
     74 
     75     /*
     76      * for tests only
     77      */
     78     @Override
     79     public int hashCode() {
     80         return toString().hashCode();
     81     }
     82 }
     83