Home | History | Annotate | Download | only in writer
      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 android.databinding.tool.writer;
     17 
     18 import org.apache.commons.io.IOUtils;
     19 
     20 import android.databinding.tool.util.L;
     21 
     22 import java.io.IOException;
     23 import java.io.Writer;
     24 
     25 import javax.annotation.processing.ProcessingEnvironment;
     26 import javax.tools.JavaFileObject;
     27 
     28 public class AnnotationJavaFileWriter extends JavaFileWriter {
     29     private final ProcessingEnvironment mProcessingEnvironment;
     30 
     31     public AnnotationJavaFileWriter(ProcessingEnvironment processingEnvironment) {
     32         mProcessingEnvironment = processingEnvironment;
     33     }
     34 
     35     @Override
     36     public void writeToFile(String canonicalName, String contents) {
     37         Writer writer = null;
     38         try {
     39             L.d("writing file %s", canonicalName);
     40             JavaFileObject javaFileObject =
     41                     mProcessingEnvironment.getFiler().createSourceFile(canonicalName);
     42             writer = javaFileObject.openWriter();
     43             writer.write(contents);
     44         } catch (IOException e) {
     45             L.e(e, "Could not write to %s", canonicalName);
     46         } finally {
     47             if (writer != null) {
     48                 IOUtils.closeQuietly(writer);
     49             }
     50         }
     51     }
     52 }
     53