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.output.BasicOutputSourceFileGenerator; 23 24 import java.io.File; 25 import java.util.ArrayList; 26 import java.util.List; 27 28 /** 29 * Useful chunks of {@link com.google.currysrc.api.RuleSet} code shared between various tools. 30 */ 31 public class Icu4jTransformRules { Icu4jTransformRules()32 private Icu4jTransformRules() {} 33 createInputFileGenerator(String[] dirNames)34 public static CompoundDirectoryInputFileGenerator createInputFileGenerator(String[] dirNames) { 35 List<InputFileGenerator> dirs = new ArrayList<>(dirNames.length); 36 for (int i = 0; i < dirNames.length; i++) { 37 File inputFile = new File(dirNames[i]); 38 InputFileGenerator inputFileGenerator; 39 if (isValidDir(inputFile)) { 40 inputFileGenerator = new DirectoryInputFileGenerator(inputFile); 41 } else if (isValidFile(inputFile)) { 42 inputFileGenerator = new FilesInputFileGenerator(inputFile); 43 } else { 44 throw new IllegalArgumentException("Input arg [" + inputFile + "] does not exist."); 45 } 46 dirs.add(inputFileGenerator); 47 } 48 return new CompoundDirectoryInputFileGenerator(dirs); 49 } 50 createOutputFileGenerator(String outputDirName)51 public static BasicOutputSourceFileGenerator createOutputFileGenerator(String outputDirName) { 52 File outputDir = new File(outputDirName); 53 if (!isValidDir(outputDir)) { 54 throw new IllegalArgumentException("Output dir [" + outputDir + "] does not exist."); 55 } 56 return new BasicOutputSourceFileGenerator(outputDir); 57 } 58 isValidDir(File dir)59 private static boolean isValidDir(File dir) { 60 return dir.exists() && dir.isDirectory(); 61 } 62 isValidFile(File file)63 private static boolean isValidFile(File file) { 64 return file.exists() && file.isFile(); 65 } 66 67 } 68