Home | History | Annotate | Download | only in android
      1 // Copyright 2015 The Bazel Authors. All rights reserved.
      2 //
      3 // Licensed under the Apache License, Version 2.0 (the "License");
      4 // you may not use this file except in compliance with the License.
      5 // You may obtain a copy of the License at
      6 //
      7 //    http://www.apache.org/licenses/LICENSE-2.0
      8 //
      9 // Unless required by applicable law or agreed to in writing, software
     10 // distributed under the License is distributed on an "AS IS" BASIS,
     11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 // See the License for the specific language governing permissions and
     13 // limitations under the License.
     14 package com.google.devtools.build.android;
     15 
     16 import com.google.devtools.common.options.Converter;
     17 import com.google.devtools.common.options.OptionsParsingException;
     18 import java.nio.file.FileSystems;
     19 import java.nio.file.Files;
     20 import java.nio.file.InvalidPathException;
     21 import java.nio.file.Path;
     22 
     23 /**
     24  * Some convenient converters used by android actions. Note: These are specific to android actions.
     25  */
     26 public final class Converters {
     27   private static final Converter<String> IDENTITY_CONVERTER =
     28       new Converter<String>() {
     29         @Override
     30         public String convert(String input) {
     31           return input;
     32         }
     33 
     34         @Override
     35         public String getTypeDescription() {
     36           return "a string";
     37         }
     38       };
     39 
     40   /** Validating converter for Paths. A Path is considered valid if it resolves to a file. */
     41   public static class PathConverter implements Converter<Path> {
     42 
     43     private final boolean mustExist;
     44 
     45     public PathConverter() {
     46       this.mustExist = false;
     47     }
     48 
     49     protected PathConverter(boolean mustExist) {
     50       this.mustExist = mustExist;
     51     }
     52 
     53     @Override
     54     public Path convert(String input) throws OptionsParsingException {
     55       try {
     56         Path path = FileSystems.getDefault().getPath(input);
     57         if (mustExist && !Files.exists(path)) {
     58           throw new OptionsParsingException(
     59               String.format("%s is not a valid path: it does not exist.", input));
     60         }
     61         return path;
     62       } catch (InvalidPathException e) {
     63         throw new OptionsParsingException(
     64             String.format("%s is not a valid path: %s.", input, e.getMessage()), e);
     65       }
     66     }
     67 
     68     @Override
     69     public String getTypeDescription() {
     70       return "a valid filesystem path";
     71     }
     72   }
     73 
     74   /**
     75    * Validating converter for Paths. A Path is considered valid if it resolves to a file and exists.
     76    */
     77   public static class ExistingPathConverter extends PathConverter {
     78     public ExistingPathConverter() {
     79       super(true);
     80     }
     81   }
     82 }
     83