001/*-------------------------------------------------------------------------+
002|                                                                          |
003| Copyright 2005-2011 The ConQAT Project                                   |
004|                                                                          |
005| Licensed under the Apache License, Version 2.0 (the "License");          |
006| you may not use this file except in compliance with the License.         |
007| You may obtain a copy of the License at                                  |
008|                                                                          |
009|    http://www.apache.org/licenses/LICENSE-2.0                            |
010|                                                                          |
011| Unless required by applicable law or agreed to in writing, software      |
012| distributed under the License is distributed on an "AS IS" BASIS,        |
013| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
014| See the License for the specific language governing permissions and      |
015| limitations under the License.                                           |
016+-------------------------------------------------------------------------*/
017package eu.cqse.check.framework.scanner;
018
019/**
020 * Base class for implementations of the token interface. This class is
021 * immutable.
022 */
023public abstract class Token implements IToken {
024
025        /** Version for serialization. */
026        private static final long serialVersionUID = 1L;
027
028        /** Token type. */
029        protected final ETokenType type;
030
031        /** Number of characters before token in the resource it originates from */
032        protected final int offset;
033
034        /** Line number token was found at. */
035        protected final int lineNumber;
036
037        /** Verbatim copy of the text found. */
038        protected final String text;
039
040        /** Identifier of the resource this token originates from */
041        protected final String originId;
042
043        /** Constructor. */
044        protected Token(ETokenType type, int offset, int lineNumber, String text, String originId) {
045                this.type = type;
046                this.offset = offset;
047                this.lineNumber = lineNumber;
048                this.text = text;
049                this.originId = originId;
050        }
051
052        /**
053         * Obtain string representation of this token. This is meant for debugging
054         * purposes.
055         */
056        @Override
057        public String toString() {
058                String result = "TOKEN (" + type + ") Text: >>" + text + "<<";
059
060                result += " Origin: '" + originId + "' line#: " + lineNumber;
061                return result;
062        }
063
064        /** {@inheritDoc} */
065        @Override
066        public String getText() {
067                return text;
068        }
069
070        /**
071         * {@inheritDoc}
072         */
073        @Override
074        public int getOffset() {
075                return offset;
076        }
077
078        /** {@inheritDoc} */
079        @Override
080        public int getEndOffset() {
081                return offset + text.length() - 1;
082        }
083
084        /** {@inheritDoc} */
085        @Override
086        public int getLineNumber() {
087                return lineNumber;
088        }
089
090        /** {@inheritDoc} */
091        @Override
092        public String getOriginId() {
093                return originId;
094        }
095
096        /** {@inheritDoc} */
097        @Override
098        public ETokenType getType() {
099                return type;
100        }
101
102}