View Javadoc

    1   /**
    2    * Copyright (c) 2008-2011, 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
    17  package org.yaml.snakeyaml.tokens;
    18
    19  import org.yaml.snakeyaml.error.Mark;
    20  import org.yaml.snakeyaml.error.YAMLException;
    21
    22  public abstract class Token {
    23      public enum ID {
    24          Alias, Anchor, BlockEnd, BlockEntry, BlockMappingStart, BlockSequenceStart, Directive, DocumentEnd, DocumentStart, FlowEntry, FlowMappingEnd, FlowMappingStart, FlowSequenceEnd, FlowSequenceStart, Key, Scalar, StreamEnd, StreamStart, Tag, Value
    25      }
    26
    27      private final Mark startMark;
    28      private final Mark endMark;
    29
    30      public Token(Mark startMark, Mark endMark) {
    31          if (startMark == null || endMark == null) {
    32              throw new YAMLException("Token requires marks.");
    33          }
    34          this.startMark = startMark;
    35          this.endMark = endMark;
    36      }
    37
    38      public String toString() {
    39          return "<" + this.getClass().getName() + "(" + getArguments() + ")>";
    40      }
    41
    42      public Mark getStartMark() {
    43          return startMark;
    44      }
    45
    46      public Mark getEndMark() {
    47          return endMark;
    48      }
    49
    50      /**
    51       * @see __repr__ for Token in PyYAML
    52       */
    53      protected String getArguments() {
    54          return "";
    55      }
    56
    57      /**
    58       * For error reporting.
    59       *
    60       * @see class variable 'id' in PyYAML
    61       */
    62      public abstract Token.ID getTokenId();
    63
    64      /*
    65       * for tests only
    66       */
    67      @Override
    68      public boolean equals(Object obj) {
    69          if (obj instanceof Token) {
    70              return toString().equals(obj.toString());
    71          } else {
    72              return false;
    73          }
    74      }
    75  }