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 019import java.io.IOException; 020import java.io.Reader; 021 022import eu.cqse.check.framework.scanner.ETokenType.ETokenClass; 023 024/** 025 * Wrapps an {@link ILenientScanner} and converts tokens of 026 * {@link ETokenClass#ERROR} into {@link ScannerException}s. 027 */ 028public class StrictScanner implements IScanner { 029 030 /** Underlying lenient scanner */ 031 private final ILenientScanner scanner; 032 033 /** 034 * Constructor 035 * 036 * @param scanner 037 * wrapped {@link ILenientScanner} 038 */ 039 public StrictScanner(ILenientScanner scanner) { 040 this.scanner = scanner; 041 } 042 043 /** {@inheritDoc} */ 044 @Override 045 public IToken getNextToken() throws IOException, ScannerException { 046 IToken token = scanner.getNextToken(); 047 048 if (token != null && token.getType().isError()) { 049 throw new ScannerException(token.getType(), token.getText(), token.getLineNumber()); 050 } 051 052 return token; 053 } 054 055 /** {@inheritDoc} */ 056 @Override 057 public void close() throws IOException { 058 scanner.close(); 059 } 060 061 /** {@inheritDoc} */ 062 @Override 063 public void reset(Reader reader, String originId) { 064 scanner.reset(reader, originId); 065 } 066 067}