001/* 002 * ============================================================================ 003 * Copyright © 2002-2021 by Thomas Thrien. 004 * All Rights Reserved. 005 * ============================================================================ 006 * Licensed to the public under the agreements of the GNU Lesser General Public 007 * License, version 3.0 (the "License"). You may obtain a copy of the License at 008 * 009 * http://www.gnu.org/licenses/lgpl.html 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 013 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 014 * License for the specific language governing permissions and limitations 015 * under the License. 016 */ 017 018package org.tquadrat.foundation.config.cli; 019 020import static org.apiguardian.api.API.Status.INTERNAL; 021 022import java.util.Collection; 023import java.util.List; 024import java.util.function.BiConsumer; 025import java.util.regex.Pattern; 026 027import org.apiguardian.api.API; 028import org.tquadrat.foundation.annotation.ClassVersion; 029import org.tquadrat.foundation.config.CmdLineException; 030import org.tquadrat.foundation.config.spi.CLIDefinition; 031import org.tquadrat.foundation.config.spi.Parameters; 032import org.tquadrat.foundation.i18n.Message; 033import org.tquadrat.foundation.i18n.Translation; 034 035/** 036 * <p>{@summary An implementation of 037 * {@link CmdLineValueHandler} 038 * for 039 * {@link String} 040 * values.}</p> 041 * <p>The value can be validated against a regular expression that is 042 * provided via 043 * {@link org.tquadrat.foundation.config.spi.CLIDefinition#format()}.</p> 044 * 045 * @see Pattern 046 * @see org.tquadrat.foundation.config.Option#format() 047 * @see org.tquadrat.foundation.config.Argument#format() 048 * 049 * @extauthor Kohsuke Kawaguchi - kk@kohsuke.org 050 * @modified Thomas Thrien - thomas.thrien@tquadrat.org 051 * @version $Id: StringValueHandler.java 896 2021-04-05 20:25:33Z tquadrat $ 052 * @since 0.0.1 053 * 054 * @UMLGraph.link 055 */ 056@ClassVersion( sourceVersion = "$Id: StringValueHandler.java 896 2021-04-05 20:25:33Z tquadrat $" ) 057@API( status = INTERNAL, since = "0.0.1" ) 058public final class StringValueHandler extends CmdLineValueHandler<String> 059{ 060 /*-----------*\ 061 ====** Constants **======================================================== 062 \*-----------*/ 063 /** 064 * The error message that indicates a failed validation: {@value}. 065 */ 066 public static final String MSG_ValidationFailed = "Validation failed for '%1$s'"; 067 068 /** 069 * The resource bundle key for the message that indicates that the 070 * validation for the command line value had failed. 071 */ 072 @Message 073 ( 074 description = "The error message that indicates that the validation for the command line value had failed.", 075 translations = 076 { 077 @Translation( language = "en", text = MSG_ValidationFailed ), 078 @Translation( language = "de", text = "Validierung für '%1$s' ist fehlgeschlagen" ) 079 } 080 ) 081 public static final int MSGKEY_ValidationFailed = 29; 082 083 /*--------------*\ 084 ====** Constructors **===================================================== 085 \*--------------*/ 086 /** 087 * Creates a new {@code StringValueHandler} instance. 088 * 089 * @param context The CLI definition that provides the context for this 090 * value handler. 091 * @param valueSetter The function that places the translated value to 092 * the property. 093 */ 094 public StringValueHandler( final CLIDefinition context, final BiConsumer<String,String> valueSetter ) 095 { 096 //---* Daddy will do the null check *---------------------------------- 097 super( context, valueSetter ); 098 } // StringValueHandler() 099 100 /** 101 * Creates a new {@code StringValueHandler} instance. 102 * 103 * @param valueSetter The function that places the translated value to 104 * the property. 105 */ 106 public StringValueHandler( final BiConsumer<String,String> valueSetter ) 107 { 108 //---* Daddy will do the null check *---------------------------------- 109 super( valueSetter ); 110 } // StringValueHandler() 111 112 /*---------*\ 113 ====** Methods **========================================================== 114 \*---------*/ 115 /** 116 * {@inheritDoc} 117 */ 118 @Override 119 protected Collection<String> translate( final Parameters params ) throws CmdLineException 120 { 121 Collection<String> retValue = List.of(); 122 final var value = params.getParameter( 0 ); 123 final var definition = getCLIDefinition(); 124 definition.flatMap( CLIDefinition::format ) 125 .map( Pattern::compile ) 126 .ifPresent( p -> 127 { 128 if( !p.matcher( value ).matches() ) 129 { 130 throw new CmdLineException( MSG_ValidationFailed, MSGKEY_ValidationFailed, value ); 131 } 132 } ); 133 retValue = List.of( value ); 134 135 //---* Done *---------------------------------------------------------- 136 return retValue; 137 } // translate() 138} 139// class StringValueHandler 140 141/* 142 * End of File 143 */