001/*
002 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 * $Id$
018 *
019 */
020
021package org.nuxeo.ecm.platform.commandline.executor.api;
022
023import java.io.File;
024import java.io.Serializable;
025import java.util.HashMap;
026import java.util.Map;
027
028/**
029 * Wraps command parameters (String or File). Quoted by default. Use {@link #addNamedParameter(String, String, boolean)}
030 * to avoid quotes.
031 *
032 * @author tiry
033 * @author Vincent Dutat
034 */
035public class CmdParameters implements Serializable {
036
037    private static final long serialVersionUID = 1L;
038
039    protected final Map<String, String> params = new HashMap<>();
040
041    private final HashMap<String, CmdParameter> cmdParameters = new HashMap<>();
042
043    /**
044     * It is recommended to use the CmdParameters instance returned by
045     * {@link CommandLineExecutorService#getDefaultCmdParameters()} which is initialized with a few common parameters.
046     *
047     * @see CommandLineExecutorService#getDefaultCmdParameters()
048     */
049    public CmdParameters() {
050    }
051
052    public void addNamedParameter(String name, String value) {
053        params.put(name, value);
054        // Quote by default
055        CmdParameter cmdParameter = new CmdParameter(value, true);
056        cmdParameters.put(name, cmdParameter);
057    }
058
059    public void addNamedParameter(String name, File file) {
060        addNamedParameter(name, file.getAbsolutePath());
061    }
062
063    public Map<String, String> getParameters() {
064        return params;
065    }
066
067    /**
068     * @since 7.1
069     */
070    public void addNamedParameter(String name, String value, boolean quote) {
071        params.put(name, value);
072        CmdParameter cmdParameter = new CmdParameter(value, quote);
073        cmdParameters.put(name, cmdParameter);
074    }
075
076    /**
077     * @since 7.1
078     */
079    public void addNamedParameter(String name, File file, boolean quote) {
080        addNamedParameter(name, file.getAbsolutePath(), quote);
081    }
082
083    /**
084     * @since 7.1
085     */
086    public HashMap<String, CmdParameter> getCmdParameters() {
087        return cmdParameters;
088    }
089
090    /**
091     * @since 7.1
092     */
093    public class CmdParameter {
094
095        private final String value;
096
097        private final boolean quote;
098
099        public CmdParameter(String value, boolean quote) {
100            this.value = value;
101            this.quote = quote;
102        }
103
104        public String getValue() {
105            return value;
106        }
107
108        public boolean isQuote() {
109            return quote;
110        }
111    }
112}