001/**
002 *  Copyright (C) 2016 Gary Gregory. All rights reserved.
003 *
004 *  See the NOTICE.txt file distributed with this work for additional
005 *  information regarding copyright ownership.
006 *  
007 *  Licensed under the Apache License, Version 2.0 (the "License");
008 *  you may not use this file except in compliance with the License.
009 *  You may obtain a copy of the License at
010 *  
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 *  
013 *  Unless required by applicable law or agreed to in writing, software
014 *  distributed under the License is distributed on an "AS IS" BASIS,
015 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 *  See the License for the specific language governing permissions and
017 *  limitations under the License.
018 */
019
020package com.garygregory.jcommander.converters.security;
021
022import java.security.AlgorithmParameterGenerator;
023import java.security.NoSuchAlgorithmException;
024import java.security.NoSuchProviderException;
025
026import com.garygregory.jcommander.converters.AbstractBaseConverter;
027
028/**
029 * Converts a {@link String} into a {@link AlgorithmParameterGenerator}.
030 * <p>
031 * For a description of the format, see {@link AlgorithmParameterGenerator#getInstance(String)}.
032 * </p>
033 * 
034 * <p>
035 * Example:
036 * </p>
037 * 
038 * <pre class="prettyprint">
039 * <code class=
040 * "language-java">&#64;Parameter(names = { "--algorithmParameterGenerator" }, converter = AlgorithmParameterGeneratorConverter.class)
041 * private AlgorithmParameterGenerator algorithmParameterGenerator;</code>
042 * </pre>
043 * <p>
044 * 
045 * @see AlgorithmParameterGenerator
046 * @see AlgorithmParameterGenerator#getInstance(String)
047 * 
048 * @since 1.0.0
049 * @author <a href="mailto:ggregory@garygregory.com">Gary Gregory</a>
050 */
051public class AlgorithmParameterGeneratorConverter extends AbstractBaseConverter<AlgorithmParameterGenerator> {
052
053    /**
054     * Constructs a converter.
055     * 
056     * @param optionName
057     *            The option name, may be null.
058     */
059    public AlgorithmParameterGeneratorConverter(final String optionName) {
060        super(optionName, AlgorithmParameterGenerator.class);
061    }
062
063    @Override
064    protected AlgorithmParameterGenerator convertImpl(final String value) throws NoSuchAlgorithmException, NoSuchProviderException {
065        final String[] split = split(value);
066        final String algorithm = split[0];
067        return isSingle(split) ? AlgorithmParameterGenerator.getInstance(value)
068                : AlgorithmParameterGenerator.getInstance(algorithm, split[1]);
069    }
070
071}