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 de.isas.mztab2.io.serialization;
017
018import com.fasterxml.jackson.databind.util.StdConverter;
019import de.isas.mztab2.model.Parameter;
020import java.util.ArrayList;
021import java.util.List;
022import static uk.ac.ebi.pride.jmztab2.model.MZTabConstants.NULL;
023
024/**
025 * <p>
026 * Parameter to String converter implementation for
027 * {@link de.isas.mztab2.model.Parameter}.</p>
028 *
029 * @author nilshoffmann
030 */
031public class ParameterConverter extends StdConverter<Parameter, String> {
032
033    @Override
034    public String convert(Parameter in) {
035//        Serializers.checkIndexedElement(in);
036        return toString(in);
037    }
038
039    /**
040     * In case, the name of the param contains commas, quotes MUST be added to
041     * avoid problems with the parsing: [label, accession, "first part of the
042     * param name , second part of the name", value].
043     *
044     * For example: [MOD, MOD:00648, "N,O-diacetylated L-serine",]
045     */
046    private static void printReserveString(String name, StringBuilder sb) {
047        List<String> charList = new ArrayList<String>();
048
049        charList.add(",");
050
051        boolean containReserveChar = false;
052        for (String c : charList) {
053            if (name.contains(c)) {
054                containReserveChar = true;
055                break;
056            }
057        }
058
059        if (containReserveChar) {
060            sb.append("\"").
061                    append(name).
062                    append("\"");
063        } else {
064            sb.append(name);
065        }
066    }
067
068    /**
069     * If there exists reserved characters in value, remove them all.
070     */
071    private String removeReservedChars(String value) {
072        if (value != null) {
073            value = value.trim();
074
075            // define a reserved character list.
076            List<String> reserveCharList = new ArrayList<>();
077
078            reserveCharList.add(",");
079
080            for (String c : reserveCharList) {
081                value = value.replaceAll(c, "");
082            }
083        }
084
085        return value;
086    }
087
088    /**
089     * In case, the name of the param contains commas, quotes MUST be added to
090     * avoid problems with the parsing: [label, accession, "first part of the
091     * param name , second part of the name", value].
092     *
093     * For example: [MOD, MOD:00648, "N,O-diacetylated L-serine",]
094     *
095     * @param param a {@link de.isas.mztab2.model.Parameter} object.
096     * @return a {@link java.lang.String} object.
097     */
098    private String toString(Parameter param) {
099        if (param == null) {
100            return NULL;
101        }
102        StringBuilder sb = new StringBuilder();
103
104        sb.append("[");
105
106        if (param.getCvLabel() != null) {
107            sb.append(param.getCvLabel());
108        }
109        sb.append(", ");
110
111        if (param.getCvAccession() != null) {
112            sb.append(param.getCvAccession());
113        }
114        sb.append(", ");
115
116        printReserveString(param.getName(), sb);
117        sb.append(", ");
118
119        if (param.getValue() != null) {
120            printReserveString(param.getValue(), sb);
121        }
122
123        sb.append("]");
124
125        return sb.toString();
126    }
127}