Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2011 The Android Open Source Project
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package com.android.builder.internal;
 
 
 import java.io.File;
 import java.util.Map;
 
 import static com.google.common.base.Preconditions.checkNotNull;

Processes a template to generate a file somewhere.
 
 
     private final InputStream mTemplateStream;
     private final Map<StringStringmPlaceHolderMap;

    
Creates a processor

Parameters:
templateStream the stream to read the template file from
placeHolderMap
 
     public TemplateProcessor(@NonNull InputStream templateStream,
                              @NonNull Map<StringStringplaceHolderMap) {
          = checkNotNull(templateStream);
          = checkNotNull(placeHolderMap);
     }

    
Generates the file from the template.

Parameters:
outputFile the file to create
 
     public void generate(File outputFilethrows IOException {
         String template = readEmbeddedTextFile();
 
         String content = replaceParameters(template);
 
         writeFile(outputFilecontent);
     }

    
Reads and returns the content of a text file embedded in the jar file.

Parameters:
templateStream the stream to read the template file from
Returns:
null if the file could not be read
Throws:
java.io.IOException
 
     private String readEmbeddedTextFile(InputStream templateStreamthrows IOException {
         InputStreamReader reader = new InputStreamReader(templateStream.);
 
         try {
             return CharStreams.toString(reader);
         } finally {
             reader.close();
         }
     }
 
     private void writeFile(File fileString contentthrows IOException {
         Files.write(contentfile.);
     }

    
Replaces placeholders found in a string with values.

Parameters:
str the string to search for placeholders.
parameters a map of <placeholder, Value> to search for in the string
Returns:
A new String object with the placeholder replaced by the values.
 
     private String replaceParameters(String strMap<StringStringparameters) {
 
         for (Entry<StringStringentry : parameters.entrySet()) {
             String value = entry.getValue();
             if (value != null) {
                 str = str.replaceAll(entry.getKey(), value);
             }
         }
 
        return str;
    }
New to GrepCode? Check out our FAQ X