001/* 
002 * Copyright 2018 Leibniz-Institut für Analytische Wissenschaften – ISAS – e.V..
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package uk.ac.ebi.pride.jmztab2.utils.parser;
017
018import static uk.ac.ebi.pride.jmztab2.model.MZTabConstants.TAB;
019import uk.ac.ebi.pride.jmztab2.model.Section;
020import uk.ac.ebi.pride.jmztab2.utils.errors.FormatErrorType;
021import uk.ac.ebi.pride.jmztab2.utils.errors.MZTabError;
022import uk.ac.ebi.pride.jmztab2.utils.errors.MZTabErrorList;
023import uk.ac.ebi.pride.jmztab2.utils.errors.MZTabException;
024
025/**
026 * Common tab split line parser. If there exists format or logical errors during the parse process,
027 * system will add them into {@link uk.ac.ebi.pride.jmztab2.utils.errors.MZTabErrorList}, or break validate and throw {@link uk.ac.ebi.pride.jmztab2.utils.errors.MZTabException}
028 * directly.
029 *
030 * @see MZTabHeaderLineParser
031 * @see MZTabDataLineParser
032 * @see MTDLineParser
033 * @author qingwei
034 * @since 10/02/13
035 * 
036 */
037public class MZTabLineParser {
038
039    protected int lineNumber;
040    protected Section section;
041    protected String line;
042
043    /**
044     * based on TAB char to split raw line into String array.
045     */
046    protected String[] items;
047
048    protected final MZTabParserContext context;
049    protected MZTabErrorList errorList;
050    
051    /**
052     * <p>Constructor for MZTabLineParser.</p>
053     *
054     * @param context a {@link uk.ac.ebi.pride.jmztab2.utils.parser.MZTabParserContext} object.
055     */
056    protected MZTabLineParser(MZTabParserContext context) {
057        if (context == null) {
058            throw new NullPointerException("Parser context should be created first!");
059        }
060        this.context = context;
061    }
062
063    /**
064     * We assume that user before call this method, have parse the raw line
065     * is not empty line and start with section prefix.
066     *
067     * @param lineNumber a int.
068     * @param line a {@link java.lang.String} object.
069     * @param errorList a {@link uk.ac.ebi.pride.jmztab2.utils.errors.MZTabErrorList} object.
070     * @throws uk.ac.ebi.pride.jmztab2.utils.errors.MZTabException if any.
071     */
072    protected void parse(int lineNumber, String line, MZTabErrorList errorList) throws MZTabException {
073        this.lineNumber = lineNumber;
074        this.line = line;
075        this.errorList = errorList == null ? new MZTabErrorList() : errorList;
076
077        this.items = line.split("\\s*" + TAB + "\\s*");
078        items[0] = items[0].trim();
079        items[items.length - 1] = items[items.length - 1].trim();
080
081        section = Section.findSection(items[0]);
082
083        if (section == null) {
084            MZTabError error = new MZTabError(FormatErrorType.LinePrefix, lineNumber, items[0]);
085            this.errorList.add(error);
086        }
087    }
088}