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

org.kuali.common.util.MetaInfUtils Maven / Gradle / Ivy

There is a newer version: 4.4.17
Show newest version
/**
 * Copyright 2010-2013 The Kuali Foundation
 *
 * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
 *
 * 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 org.kuali.common.util;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Properties;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.kuali.common.util.metainf.MetaInfContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MetaInfUtils {

	private static final Logger logger = LoggerFactory.getLogger(MetaInfUtils.class);

	public static void scanAndCreateFiles(List contexts) throws IOException {
		for (MetaInfContext context : contexts) {
			List files = getFiles(context);
			List resources = getResources(context, files);
			doLocations(context, resources);
			if (context.isAddPropertiesFile()) {
				doProperties(context, resources);
			}
		}
	}

	public static List getFiles(MetaInfContext context) throws IOException {
		Assert.notNull(context.getBaseDir(), "baseDir is null");
		Assert.notNull(context.getOutputFile(), "outputFile is null");
		logger.debug("Examining " + LocationUtils.getCanonicalPath(context.getBaseDir()));
		logger.debug("Patterns - {}", getPatternLogMessage(context));
		List includes = context.getIncludes();
		List excludes = context.getExcludes();
		SimpleScanner scanner = new SimpleScanner(context.getBaseDir(), includes, excludes);
        return scanner.getFiles();
	}

	protected static String getPatternLogMessage(MetaInfContext context) {
		StringBuilder sb = new StringBuilder();
		String incl = CollectionUtils.getSpaceSeparatedString(context.getIncludes());
		String excl = CollectionUtils.getSpaceSeparatedString(context.getExcludes());
		sb.append("[");
		if (!StringUtils.isBlank(incl)) {
			sb.append("include: ").append(incl);
		}
		if (!StringUtils.isBlank(excl)) {
			sb.append(", exclude:").append(excl);
		}
		boolean includeEverything = StringUtils.isBlank(incl) && StringUtils.isBlank(excl);
		if (includeEverything) {
			sb.append("include: *");
		}
		sb.append("]");
		return sb.toString();
	}

	public static void doLocations(MetaInfContext context, List resources) throws IOException {
		List locations = getLocations(resources);
		if (context.isSort()) {
			Collections.sort(locations);
		}
		String path1 = LocationUtils.getCanonicalPath(context.getBaseDir());
		String path2 = LocationUtils.getCanonicalPath(context.getOutputFile());
		String path = StringUtils.remove(path2, path1);
		logger.info("Creating [" + path + "] - {} resources", locations.size());
		FileUtils.writeLines(context.getOutputFile(), locations);
	}

	public static void doProperties(MetaInfContext context, List resources) {
		logger.debug("doProperties()");
		Properties properties = getProperties(context, resources);
		File propertiesFile = new File(LocationUtils.getCanonicalPath(context.getOutputFile()) + ".properties");
		PropertyUtils.store(properties, propertiesFile, "UTF-8");
	}

	public static Properties getProperties(MetaInfContext context, List resources) {
		Properties properties = new Properties();
		for (MetaInfResource resource : resources) {
			String sizeKey = resource.getKey() + ".size";
			properties.setProperty(sizeKey, resource.getSize() + "");
			if (context.isAddLineCount()) {
				String linesKey = resource.getKey() + ".lines";
				properties.setProperty(linesKey, resource.getLines() + "");
			}
		}
		return properties;
	}

	public static String getPropertyKey(String location) {
		String key = StringUtils.replace(location, ":", ".");
		return StringUtils.replace(key, "/", ".");
	}

	public static void scanAndCreateFile(MetaInfContext context) throws IOException {
		scanAndCreateFiles(Arrays.asList(context));
	}

	public static List getResources(MetaInfContext context, List files) throws IOException {
		List resources = new ArrayList();
        for (File file : files) {
            MetaInfResource resource = getResource(context, file);
            resources.add(resource);
        }
		return resources;
	}

	public static List getLocations(List resources) {
		List locations = new ArrayList();
		for (MetaInfResource resource : resources) {
			locations.add(resource.getLocation());
		}
		return locations;
	}

	public static List getLocations(File baseDir, List files, String prefix) throws IOException {
		List locations = new ArrayList();
        for (File file : files) {
            String location = getLocation(baseDir, file, prefix);
            locations.add(location);
        }
		return locations;
	}

	public static MetaInfResource getResource(MetaInfContext context, File file) throws IOException {
		String location = getLocation(context.getBaseDir(), file, context.getPrefix());
		long size = file.length();
		long lines = -1;
		if (context.isAddLineCount()) {
			lines = LocationUtils.getLineCount(file);
		}
		String key = getPropertyKey(location);

		MetaInfResource resource = new MetaInfResource();
		resource.setLocation(location);
		resource.setSize(size);
		resource.setKey(key);
		resource.setLines(lines);
		return resource;
	}

	public static String getLocation(File baseDir, File file, String prefix) throws IOException {
		String dir = baseDir.getCanonicalPath();
		String path = file.getCanonicalPath();
		int pos = dir.length() + 1;
		return prefix + StringUtils.substring(path, pos);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy