Package br.com.objectos.way.etc

Source Code of br.com.objectos.way.etc.ResourceSet

/*
* ResourceDir.java criado em 15/12/2013
*
* Propriedade de Objectos Fábrica de Software LTDA.
* Reprodução parcial ou total proibida.
*/
package br.com.objectos.way.etc;

import static com.google.common.collect.Sets.newLinkedHashSet;

import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.List;
import java.util.Set;

import br.com.objectos.way.base.io.Directory;

import com.github.mustachejava.DefaultMustacheFactory;
import com.github.mustachejava.Mustache;
import com.github.mustachejava.MustacheFactory;
import com.google.common.base.Charsets;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Strings;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.io.Resources;

/**
* @author marcio.endo@objectos.com.br (Marcio Endo)
*/
class ResourceSet {

  private final Class<?> contextClass;

  private final String dir;

  private final MustacheFactory mustaches;

  private final Set<Resource> resources = newLinkedHashSet();

  private final Set<Resource> evalNames = newLinkedHashSet();

  private Object context;

  private ResourceSet(Class<?> contextClass, String dir) {
    this.contextClass = contextClass;
    this.dir = dir;
    this.mustaches = new DefaultMustacheFactory(dir);
  }

  public static ResourceSet at(String dir) {
    return new ResourceSet(ResourceSet.class, dir);
  }
  public static ResourceSet at(Class<?> contextClass, String dir) {
    return new ResourceSet(contextClass, dir);
  }

  public void add(String resourceName) {
    Resource resource = Resource.add(dir, resourceName);
    resources.add(resource);
  }

  public void addFromListAt(String listName) {
    try {
      URL listUrl;
      listUrl = Resources.getResource(contextClass, listName);

      List<String> lines;
      lines = Resources.readLines(listUrl, Charsets.UTF_8);

      Iterable<String> valids;
      valids = Iterables.filter(lines, ValidLine.INSTANCE);

      Iterable<Resource> _resources;
      _resources = Iterables.transform(valids, new ToResource());

      List<Resource> resources;
      resources = ImmutableList.copyOf(_resources);

      this.resources.addAll(resources);

    } catch (IOException e) {
      throw Throwables.propagate(e);

    }
  }

  public void map(String source, String dest) {
    Resource resource = Resource.map(dir, source, dest);
    resources.add(resource);
  }

  public void evalWith(Object context) {
    this.context = context;
  }

  public void only(String resourceName) {
    Resource resource = Resource.add(dir, resourceName);
    this.evalNames.add(resource);
  }

  public void copyTo(Directory targetDir) {
    CopyTo copyTo = new CopyTo(this, targetDir);

    if (context != null) {
      if (evalNames.isEmpty()) {
        copyTo = new CopyToEval(this, targetDir);
      } else {
        copyTo = new CopyToEvalSelection(this, targetDir);
      }
    }

    copyTo.execute();
  }

  Mustache compileMustache(InputStreamReader reader, String resourceName) {
    return mustaches.compile(reader, resourceName);
  }

  Class<?> getContextClass() {
    return contextClass;
  }

  String getDir() {
    return dir;
  }

  Set<Resource> getEvalNames() {
    return evalNames;
  }

  Set<Resource> getResources() {
    return resources;
  }

  Object getContext() {
    return context;
  }

  private enum ValidLine implements Predicate<String> {
    INSTANCE;
    @Override
    public boolean apply(String input) {
      return !Strings.isNullOrEmpty(input);
    }
  }

  private class ToResource implements Function<String, Resource> {
    @Override
    public Resource apply(String input) {
      return Resource.add(dir, input);
    }
  }

}
TOP

Related Classes of br.com.objectos.way.etc.ResourceSet

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.