001/* 002 * Copyright 2020-2024 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright 2020-2024 Ping Identity Corporation 007 * 008 * Licensed under the Apache License, Version 2.0 (the "License"); 009 * you may not use this file except in compliance with the License. 010 * You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, software 015 * distributed under the License is distributed on an "AS IS" BASIS, 016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 017 * See the License for the specific language governing permissions and 018 * limitations under the License. 019 */ 020/* 021 * Copyright (C) 2020-2024 Ping Identity Corporation 022 * 023 * This program is free software; you can redistribute it and/or modify 024 * it under the terms of the GNU General Public License (GPLv2 only) 025 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only) 026 * as published by the Free Software Foundation. 027 * 028 * This program is distributed in the hope that it will be useful, 029 * but WITHOUT ANY WARRANTY; without even the implied warranty of 030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 031 * GNU General Public License for more details. 032 * 033 * You should have received a copy of the GNU General Public License 034 * along with this program; if not, see <http://www.gnu.org/licenses>. 035 */ 036package com.unboundid.util.args; 037 038 039 040import java.io.Serializable; 041 042import com.unboundid.util.NotMutable; 043import com.unboundid.util.NotNull; 044import com.unboundid.util.ThreadSafety; 045import com.unboundid.util.ThreadSafetyLevel; 046 047import static com.unboundid.util.args.ArgsMessages.*; 048 049 050 051/** 052 * This class provides an implementation of an argument value validator that 053 * ensures that values can be parsed as valid IA5 strings (that is, strings 054 * containing only ASCII characters). 055 */ 056@NotMutable() 057@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 058public final class IA5StringArgumentValueValidator 059 extends ArgumentValueValidator 060 implements Serializable 061{ 062 /** 063 * The serial version UID for this serializable class. 064 */ 065 private static final long serialVersionUID = 7395996449791650693L; 066 067 068 069 // Indicates whether values are allowed to be empty strings. 070 private final boolean allowEmptyStrings; 071 072 073 074 /** 075 * Creates a new IA5 string argument value validator. Empty strings will not 076 * be considered valid. 077 */ 078 public IA5StringArgumentValueValidator() 079 { 080 this(false); 081 } 082 083 084 085 /** 086 * Creates a new IA5 string argument value validator. 087 * 088 * @param allowEmptyStrings Indicates whether empty strings will be 089 * considered valid. 090 */ 091 public IA5StringArgumentValueValidator(final boolean allowEmptyStrings) 092 { 093 this.allowEmptyStrings = allowEmptyStrings; 094 } 095 096 097 098 /** 099 * {@inheritDoc} 100 */ 101 @Override() 102 public void validateArgumentValue(@NotNull final Argument argument, 103 @NotNull final String valueString) 104 throws ArgumentException 105 { 106 final int length = valueString.length(); 107 if ((length == 0) && (! allowEmptyStrings)) 108 { 109 throw new ArgumentException(ERR_IA5_STRING_VALIDATOR_EMPTY_STRING.get( 110 argument.getIdentifierString())); 111 } 112 113 for (int i=0; i < length; i++) 114 { 115 final char c = valueString.charAt(i); 116 final int asciiByte = (c & 0x7F); 117 if (c != asciiByte) 118 { 119 throw new ArgumentException(ERR_IA5_STRING_VALIDATOR_ILLEGAL_CHAR.get( 120 valueString, argument.getIdentifierString(), 121 String.valueOf(c), 122 i)); 123 } 124 } 125 } 126 127 128 129 /** 130 * Indicates whether empty strings should be considered valid. 131 * 132 * @return {@code true} if empty strings should be considered valid, or 133 * {@code false} if not. 134 */ 135 public boolean allowEmptyStrings() 136 { 137 return allowEmptyStrings; 138 } 139 140 141 142 /** 143 * Retrieves a string representation of this argument value validator. 144 * 145 * @return A string representation of this argument value validator. 146 */ 147 @Override() 148 @NotNull() 149 public String toString() 150 { 151 final StringBuilder buffer = new StringBuilder(); 152 toString(buffer); 153 return buffer.toString(); 154 } 155 156 157 158 /** 159 * Appends a string representation of this argument value validator to the 160 * provided buffer. 161 * 162 * @param buffer The buffer to which the string representation should be 163 * appended. 164 */ 165 public void toString(@NotNull final StringBuilder buffer) 166 { 167 buffer.append("IA5StringArgumentValueValidator(allowEmptyStrings="); 168 buffer.append(allowEmptyStrings); 169 buffer.append(')'); 170 } 171}