001/*
002 * #%L
003 * Netarchivesuite - harvester
004 * %%
005 * Copyright (C) 2005 - 2014 The Royal Danish Library, the Danish State and University Library,
006 *             the National Library of France and the Austrian National Library.
007 * %%
008 * This program is free software: you can redistribute it and/or modify
009 * it under the terms of the GNU Lesser General Public License as
010 * published by the Free Software Foundation, either version 2.1 of the
011 * License, or (at your option) any later version.
012 * 
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016 * GNU General Lesser Public License for more details.
017 * 
018 * You should have received a copy of the GNU General Lesser Public
019 * License along with this program.  If not, see
020 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
021 * #L%
022 */
023
024package dk.netarkivet.harvester.datamodel.extendedfield;
025
026import java.util.LinkedHashMap;
027import java.util.Map;
028import java.util.NoSuchElementException;
029import java.util.StringTokenizer;
030
031/**
032 * Class to represent options for Extended Fields.
033 */
034public class ExtendedFieldOptions {
035
036    /** Key-Value separator. */
037    public static final String KEYVALUESEPARATOR = "=";
038    /** Line separator. */
039    public static final String NEWLINE = System.getProperty("line.separator");
040    /** The option lines. */
041    private String lines;
042    /** The validity state of the list of extended field options. */
043    private boolean valid = false;
044
045    /** Key-Value map containing the options. */
046    private Map<String, String> options = new LinkedHashMap<String, String>();
047
048    /**
049     * Constructor.
050     *
051     * @param aLines Options separated by newlines (Null argument allowed)
052     */
053    public ExtendedFieldOptions(String aLines) {
054        lines = aLines;
055        parsing();
056    }
057
058    /**
059     * Method that parses the data given to the constructor.
060     */
061    private void parsing() {
062        if (lines == null) {
063            return;
064        }
065
066        StringTokenizer st = new StringTokenizer(lines.trim(), System.getProperty("line.separator"));
067
068        String key = null;
069        String value = null;
070
071        while (st.hasMoreElements()) {
072            String line = st.nextToken();
073            StringTokenizer st2 = new StringTokenizer(line, KEYVALUESEPARATOR);
074
075            try {
076                key = st2.nextToken();
077                value = st2.nextToken();
078                if (key.length() == 0 || value.length() == 0) {
079                    continue;
080                }
081                options.put(key, value);
082            } catch (NoSuchElementException e) {
083                // invalid line, ignoring
084                continue;
085            }
086        }
087
088        if (!options.isEmpty()) {
089            valid = true;
090        }
091    }
092
093    /**
094     * Is these ExtendedField options valid.
095     *
096     * @return true, if the options are valid; otherwise false
097     */
098    public boolean isValid() {
099        return valid;
100    }
101
102    /**
103     * @return the options as a map.
104     */
105    public Map<String, String> getOptions() {
106        return options;
107    }
108
109    /**
110     * @return the options as lines separated by newlines.
111     */
112    public String getOptionsString() {
113        String str = "";
114
115        if (isValid()) {
116            for (String key : options.keySet()) {
117                str += key + KEYVALUESEPARATOR + options.get(key) + NEWLINE;
118            }
119        }
120
121        return str;
122    }
123
124    /**
125     * Check, if the given key is a valid option.
126     *
127     * @param aKey a given option key.
128     * @return true, if the list of options is valid, and there is an option in the options map with the given key.
129     */
130    public boolean isKeyValid(String aKey) {
131        if (isValid()) {
132            if (options.keySet().contains(aKey)) {
133                return true;
134            }
135        }
136        return false;
137    }
138
139}