All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.objectos.code.ResourceArtifactBuilder Maven / Gradle / Ivy

/*
 * Copyright 2014-2015 Objectos, Fábrica de Software LTDA.
 *
 * 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 br.com.objectos.code;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.annotation.processing.Filer;
import javax.annotation.processing.ProcessingEnvironment;
import javax.tools.FileObject;
import javax.tools.StandardLocation;

/**
 * @author [email protected] (Marcio Endo)
 */
public class ResourceArtifactBuilder {

  private final String name;
  private final List lineList = new ArrayList<>();
  private boolean sort = false;

  ResourceArtifactBuilder(String name) {
    this.name = name;
  }

  public ResourceArtifactBuilder add(String line) {
    lineList.add(line);
    return this;
  }

  public ResourceArtifactBuilder add(String format, Object... args) {
    lineList.add(String.format(format, args));
    return this;
  }

  public ResourceArtifactBuilder addAll(Iterable lines) {
    lines.forEach(lineList::add);
    return this;
  }

  public ResourceArtifactBuilder addAll(Stream lines) {
    lines.forEach(lineList::add);
    return this;
  }

  public Artifact build() {
    return new Artifact() {
      @Override
      public String toString() {
        return lineList.stream().collect(Collectors.joining("\n"));
      }

      @Override
      public void writeTo(Path path) {
        throw new UnsupportedOperationException();
      }

      @Override
      protected void execute(ProcessingEnvironment processingEnv) {
        try {
          tryToWrite(processingEnv);
        } catch (IOException e) {
          log(processingEnv, e);
        }
      }
    };
  }

  public ResourceArtifactBuilder sort() {
    sort = true;
    return this;
  }

  private void tryToWrite(ProcessingEnvironment processingEnv) throws IOException {
    if (sort) {
      Collections.sort(lineList);
    }

    Filer filer = processingEnv.getFiler();
    FileObject file = filer.createResource(StandardLocation.CLASS_OUTPUT, "", name);
    try (OutputStream out = file.openOutputStream()) {
      BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));
      for (String line : lineList) {
        writer.write(line);
        writer.newLine();
      }
      writer.flush();
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy