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

br.com.objectos.way.etc.ResourceSet Maven / Gradle / Ivy

The newest version!
/*
 * 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.Reader;
import java.net.URL;
import java.util.List;
import java.util.Set;

import br.com.objectos.way.core.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.Predicates;
import com.google.common.base.Splitter;
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 [email protected] (Marcio Endo)
 */
class ResourceSet {

  private final Class contextClass;

  private final String dir;

  private final MustacheFactory mustaches;

  private final Set resources = newLinkedHashSet();

  private final Set 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 lines;
      lines = Resources.readLines(listUrl, Charsets.UTF_8);

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

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

      List 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 mapFromListAt(String listName) {
    try {
      URL listUrl;
      listUrl = Resources.getResource(contextClass, listName);

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

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

      Iterable pairs;
      pairs = Iterables.transform(valids, ToMapPair.INSTANCE);

      Iterable validPairs;
      validPairs = Iterables.filter(pairs, Predicates.notNull());

      Iterable _resources;
      _resources = Iterables.transform(validPairs, new MapPairToResource());

      List resources;
      resources = ImmutableList.copyOf(_resources);

      this.resources.addAll(resources);

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

    }
  }

  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(Reader reader, String resourceName) {
    return mustaches.compile(reader, resourceName);
  }

  Class getContextClass() {
    return contextClass;
  }

  String getDir() {
    return dir;
  }

  Set getEvalNames() {
    return evalNames;
  }

  Set getResources() {
    return resources;
  }

  Object getContext() {
    return context;
  }

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

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

  private enum ToMapPair implements Function {
    INSTANCE;
    @Override
    public MapPair apply(String line) {
      return MapPair.of(line);
    }
  }

  private class MapPairToResource implements Function {
    @Override
    public Resource apply(MapPair input) {
      return input.toResource(dir);
    }
  }

  private static class MapPair {
    final String source;
    final String dest;

    private MapPair(String source, String dest) {
      this.source = Strings.nullToEmpty(source).trim();
      this.dest = Strings.nullToEmpty(dest).trim();
    }

    public static MapPair of(String line) {
      MapPair res = null;

      List parts = Splitter.on("=>").splitToList(line);
      if (parts.size() == 2) {
        String source = parts.get(0);
        String dest = parts.get(1);
        res = new MapPair(source, dest);
      }

      return res;
    }

    public Resource toResource(String dir) {
      return Resource.map(dir, source, dest);
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy