Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2013 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.tradefed.util;
     17 
     18 import java.io.BufferedReader;
     19 import java.io.BufferedWriter;
     20 import java.io.File;
     21 import java.io.FileReader;
     22 import java.io.FileWriter;
     23 import java.io.IOException;
     24 import java.util.HashMap;
     25 import java.util.Map;
     26 import java.util.Map.Entry;
     27 
     28 /**
     29  * A utility class for changing (or adding) items in an Android property file
     30  *
     31  */
     32 public abstract class PropertyChanger {
     33 
     34     /**
     35      * A utility function to change (or add) items in an Android property file
     36      *
     37      * Properties that are in the original property file will have its value modified. New ones
     38      * will be appended. Caller is responsible for properly recycling temporary files, both original
     39      * and the modified.
     40      *
     41      * @param original the original property file
     42      * @param properties the properties to change or add
     43      * @return path to the new property file
     44      */
     45     public static File changeProperties(File original, Map<String, String> properties)
     46             throws IOException {
     47         Map<String, String> propsToAdd = new HashMap<String, String>(properties);
     48         File ret = FileUtil.createTempFile("chg_prop_", ".prop");
     49         BufferedReader br = null;
     50         BufferedWriter bw = null;
     51         try {
     52             br = new BufferedReader(new FileReader(original));
     53             bw = new BufferedWriter(new FileWriter(ret));
     54             String line = null;
     55             while ((line = br.readLine()) != null) {
     56                 int pos = line.indexOf('=');
     57                 if (pos != -1) {
     58                     String name = line.substring(0, pos);
     59                     if (propsToAdd.containsKey(name)) {
     60                         // modify the line if property needs to be changed
     61                         line = String.format("%s=%s", name, propsToAdd.get(name));
     62                         propsToAdd.remove(name);
     63                     }
     64                 }
     65                 // write line into new property file
     66                 bw.write(line + '\n');
     67             }
     68             // write remaining properties (new ones)
     69             for (Entry<String, String> entry : propsToAdd.entrySet()) {
     70                 bw.write(String.format("%s=%s\n", entry.getKey(), entry.getValue()));
     71             }
     72         } finally {
     73             StreamUtil.close(br);
     74             StreamUtil.close(bw);
     75         }
     76         return ret;
     77     }
     78 }
     79