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

org.hibernate.tool.hbm2x.ArtifactCollector Maven / Gradle / Ivy

There is a newer version: 5.6.15.Final
Show newest version
package org.hibernate.tool.hbm2x;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Callback class that all exporters are given to allow better feedback and
 * processing of the output afterwards.
 * 
 * @author Max Rydahl Andersen
 *
 */
public class ArtifactCollector {

	final protected Map> files = new HashMap>();

	/**
	 * Called to inform that a file has been created by the exporter.
	 */
	public void addFile(File file, String type) {
		List existing = files.get(type);
		if (existing == null) {
			existing = new ArrayList();
			files.put(type, existing);
		}
		existing.add(file);
	}

	public int getFileCount(String type) {
		List existing = files.get(type);

		return (existing == null) ? 0 : existing.size();
	}

	public File[] getFiles(String type) {
		List existing = files.get(type);

		if (existing == null) {
			return new File[0];
		} else {
			return (File[]) existing.toArray(new File[existing.size()]);
		}
	}

	public Set getFileTypes() {
		return files.keySet();
	}

	public void formatFiles() {

		formatXml("xml");
		formatXml("hbm.xml");
		formatXml("cfg.xml");

	}

	private void formatXml(String type) throws ExporterException {
		List list = files.get(type);
		if (list != null && !list.isEmpty()) {
			for (Iterator iter = list.iterator(); iter.hasNext();) {
				File xmlFile = iter.next();
				try {
					XMLPrettyPrinter.prettyPrintFile(xmlFile);
				} catch (IOException e) {
					throw new ExporterException("Could not format XML file: " + xmlFile, e);
				}
			}
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy