com.eincs.decanter.render.soy.SoyAcre.java Source code

Java tutorial

Introduction

Here is the source code for com.eincs.decanter.render.soy.SoyAcre.java

Source

/*
 * Copyright 2012 The Decanter Project
 *
 * The Decanter Project licenses this file to you 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.eincs.decanter.render.soy;

import java.io.File;
import java.util.Set;
import java.util.regex.Pattern;

import org.apache.commons.io.FileUtils;
import org.reflections.Reflections;
import org.reflections.scanners.ResourcesScanner;
import org.reflections.util.ClasspathHelper;
import org.reflections.util.ConfigurationBuilder;

import com.google.common.collect.Sets;
import com.google.template.soy.SoyFileSet;

/**
 * @author Jung-Haeng Lee
 */
public abstract class SoyAcre {

    public static String SOY_FILE_EXTENTION = "soy";
    public static String SOY_FILE_NAME_PATTERN = ".*\\.soy";

    /**
     * 
     * @return
     */
    public static SoyAcre forClasspath() {
        final Reflections reflections = new Reflections(new ConfigurationBuilder()
                .addUrls(ClasspathHelper.forJavaClassPath()).setScanners(new ResourcesScanner()));
        final Set<String> soyFiles = reflections.getResources(Pattern.compile(SOY_FILE_NAME_PATTERN));
        final ClassLoader clazzLoader = ClassLoader.getSystemClassLoader();

        return new SoyAcre() {
            @Override
            public void addToFileSet(SoyFileSet.Builder fileSetBuilder) {
                for (String soyFile : soyFiles) {
                    fileSetBuilder.add(clazzLoader.getResource(soyFile));
                }
            }
        };
    }

    /**
     * 
     * @param packageName
     * @return
     */
    public static SoyAcre forPakckage(String packageName) {
        final Reflections reflections = new Reflections(new ConfigurationBuilder()
                .addUrls(ClasspathHelper.forPackage(packageName, ClassLoader.getSystemClassLoader()))
                .setScanners(new ResourcesScanner()));
        final Set<String> soyFiles = reflections.getResources(Pattern.compile(SOY_FILE_NAME_PATTERN));
        final ClassLoader clazzLoader = ClassLoader.getSystemClassLoader();

        return new SoyAcre() {
            @Override
            public void addToFileSet(SoyFileSet.Builder fileSetBuilder) {
                for (String soyFile : soyFiles) {
                    fileSetBuilder.add(clazzLoader.getResource(soyFile));
                }
            }
        };
    }

    /**
     * 
     * @param directoryPath
     * @return
     */
    public static SoyAcre forDirectory(final String directoryPath) {
        File directory = new File(directoryPath);
        if (!directory.exists()) {
            directory.mkdirs();
        }

        if (!directory.isDirectory()) {
            throw new IllegalArgumentException(String.format("%s is not a directory.", directoryPath));
        }
        return forDirectory(directory);
    }

    /**
     * 
     * @param directory
     * @return
     */
    public static SoyAcre forDirectory(final File directory) {
        final String[] EXTENTIONS = new String[] { SOY_FILE_EXTENTION };
        return new SoyAcre() {
            @Override
            public void addToFileSet(SoyFileSet.Builder fileSetBuilder) {
                Set<File> soyFiles = Sets.newHashSet(FileUtils.iterateFiles(directory, EXTENTIONS, true));
                for (File soyFile : soyFiles) {
                    fileSetBuilder.add(soyFile);
                }
            }
        };
    }

    /**
     * 
     * @param fileSetBuilder
     */
    public abstract void addToFileSet(SoyFileSet.Builder fileSetBuilder);
}