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.util.ArrayList;
021import java.util.List;
022
023/**
024 * This class offers utility methods for scanners.
025 * 
026 * @author Florian Deissenboeck
027 * @author Elmar Juergens
028 */
029public class ScannerUtils {
030
031        /**
032         * Read tokens from a strict scanner and store them in a list. This expects the
033         * scanner to return a token with type {@link ETokenType#EOF} at the end of the
034         * file. This last token will not be added to the list of tokens.
035         * 
036         * @param scanner
037         *            the scanner to read from
038         * @param tokens
039         *            the list to store tokens
040         * @param exceptions
041         *            another list to store possible exceptions that occurred during
042         *            scanning.
043         * @throws IOException
044         *             thrown if scanner throws an IO exception
045         */
046        public static void readTokens(IScanner scanner, List<IToken> tokens, List<ScannerException> exceptions)
047                        throws IOException {
048
049                IToken token = null;
050
051                do {
052                        try {
053                                token = scanner.getNextToken();
054                                if (token.getType() != ETokenType.EOF) {
055                                        tokens.add(token);
056                                }
057                        } catch (ScannerException e) {
058                                exceptions.add(e);
059                                continue;
060                        }
061                } while (token != null && token.getType() != ETokenType.EOF);
062
063        }
064
065        /**
066         * Read tokens from a lenient scanner and store them in a list. This expects the
067         * scanner to return a token with type {@link ETokenType#EOF} at the end of the
068         * file. This last token will not be added to the list of tokens.
069         * 
070         * @param scanner
071         *            the scanner to read from
072         * 
073         * @throws IOException
074         *             thrown if scanner throws an IO exception
075         */
076        public static List<IToken> readTokens(ILenientScanner scanner) throws IOException {
077
078                List<IToken> tokens = new ArrayList<>();
079
080                IToken token = null;
081                while ((token = scanner.getNextToken()).getType() != ETokenType.EOF) {
082                        tokens.add(token);
083                }
084
085                return tokens;
086
087        }
088
089        /**
090         * Returns the results of scanning a string. The origin of the tokens is set to
091         * some arbitrary value.
092         */
093        public static List<IToken> getTokens(String content, ELanguage language) {
094                return getTokens(content, language, "defaultTestOrigin");
095        }
096
097        /**
098         * Returns the results of scanning a string. The origin of the tokens is set to
099         * the given origin.
100         */
101        public static List<IToken> getTokens(String content, ELanguage language, String origin) {
102                ILenientScanner scanner = ScannerFactory.newLenientScanner(language, content, origin);
103                try {
104                        return readTokens(scanner);
105                } catch (IOException e) {
106                        throw new AssertionError("can not happen as the scanner is lenient and we are working from memory");
107                }
108        }
109}