Home | History | Annotate | Download | only in srcgen
      1 /*
      2  * Copyright (C) 2015 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.android.icu4j.srcgen;
     17 
     18 import com.google.currysrc.api.input.CompoundDirectoryInputFileGenerator;
     19 import com.google.currysrc.api.input.DirectoryInputFileGenerator;
     20 import com.google.currysrc.api.input.FilesInputFileGenerator;
     21 import com.google.currysrc.api.input.InputFileGenerator;
     22 import com.google.currysrc.api.match.SourceMatchers;
     23 import com.google.currysrc.api.output.BasicOutputSourceFileGenerator;
     24 import com.google.currysrc.api.process.DefaultRule;
     25 import com.google.currysrc.api.process.Processor;
     26 
     27 import java.io.File;
     28 import java.util.ArrayList;
     29 import java.util.List;
     30 
     31 /**
     32  * Useful chunks of {@link com.google.currysrc.api.Rules} code shared between various tools.
     33  */
     34 public class Icu4jTransformRules {
     35   private Icu4jTransformRules() {}
     36 
     37   public static CompoundDirectoryInputFileGenerator createInputFileGenerator(String[] dirNames) {
     38     List<InputFileGenerator> dirs = new ArrayList<>(dirNames.length);
     39     for (int i = 0; i < dirNames.length; i++) {
     40       File inputFile = new File(dirNames[i]);
     41       InputFileGenerator inputFileGenerator;
     42       if (isValidDir(inputFile)) {
     43         inputFileGenerator = new DirectoryInputFileGenerator(inputFile);
     44       } else if (isValidFile(inputFile)) {
     45         inputFileGenerator = new FilesInputFileGenerator(inputFile);
     46       } else {
     47         throw new IllegalArgumentException("Input arg [" + inputFile + "] does not exist.");
     48       }
     49       dirs.add(inputFileGenerator);
     50     }
     51     return new CompoundDirectoryInputFileGenerator(dirs);
     52   }
     53 
     54   public static BasicOutputSourceFileGenerator createOutputFileGenerator(String outputDirName) {
     55     File outputDir = new File(outputDirName);
     56     if (!isValidDir(outputDir)) {
     57       throw new IllegalArgumentException("Output dir [" + outputDir + "] does not exist.");
     58     }
     59     return new BasicOutputSourceFileGenerator(outputDir);
     60   }
     61 
     62   public static DefaultRule createMandatoryRule(Processor processor) {
     63     return new DefaultRule(processor, SourceMatchers.all(), true /* mustModify */);
     64   }
     65 
     66   public static DefaultRule createOptionalRule(Processor processor) {
     67     return new DefaultRule(processor, SourceMatchers.all(), false /* mustModify */);
     68   }
     69 
     70   private static boolean isValidDir(File dir) {
     71     return dir.exists() && dir.isDirectory();
     72   }
     73 
     74   private static boolean isValidFile(File file) {
     75     return file.exists() && file.isFile();
     76   }
     77 
     78 }
     79