Path: blob/master/src/jdk.internal.opt/share/classes/jdk/internal/joptsimple/util/PathConverter.java
40951 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package jdk.internal.joptsimple.util;2627import java.nio.file.Path;28import java.nio.file.Paths;29import java.text.MessageFormat;30import java.util.ResourceBundle;3132import jdk.internal.joptsimple.ValueConversionException;33import jdk.internal.joptsimple.ValueConverter;3435/**36* Converts command line options to {@link Path} objects and checks the status of the underlying file.37*/38public class PathConverter implements ValueConverter<Path> {39private final PathProperties[] pathProperties;4041public PathConverter( PathProperties... pathProperties ) {42this.pathProperties = pathProperties;43}4445@Override46public Path convert( String value ) {47Path path = Paths.get(value);4849if ( pathProperties != null ) {50for ( PathProperties each : pathProperties ) {51if ( !each.accept( path ) )52throw new ValueConversionException( message( each.getMessageKey(), path.toString() ) );53}54}5556return path;57}5859@Override60public Class<Path> valueType() {61return Path.class;62}6364@Override65public String valuePattern() {66return null;67}6869private String message( String errorKey, String value ) {70ResourceBundle bundle = ResourceBundle.getBundle( "jdk.internal.joptsimple.ExceptionMessages" );71Object[] arguments = new Object[] { value, valuePattern() };72String template = bundle.getString( PathConverter.class.getName() + "." + errorKey + ".message" );73return new MessageFormat( template ).format( arguments );74}75}767778