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

org.apache.torque.mojo.ConvertCloverETLMojo Maven / Gradle / Ivy

/**
 * Copyright 2004-2012 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.apache.torque.mojo;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.torque.util.CloverETLColumn;
import org.apache.torque.util.CloverETLTable;

/**
 * This project converts Clover ETL data generated by Kuali's Ant based tooling into a format that is consumable by the Maven Impex plugin
 *
 * @goal convertcloveretl
 */
public class ConvertCloverETLMojo extends BaseMojo {

	/**
	 * The source directory where Clover ETL data was generated
	 *
	 * @parameter expression="${impex.sourceDirectory}" default-value="${project.basedir}/src/main/resources"
	 * @required
	 */
	File sourceDir;

	/**
	 * The output directory where maven-impex-plugin data will get generated
	 *
	 * @parameter expression="${impex.outputDir}" default-value="${project.build.directory}/impex"
	 * @required
	 */
	File outputDir;

	/**
	 * The delimiter used by the Clover ETL data
	 *
	 * @parameter expression="${impex.delimiter}" default-value="|"
	 * @required
	 */
	String delimiter;

	/**
	 * The schema.xml file for Clover ETL
	 *
	 * @parameter expression="${impex.schemaFilename}" default-value="schema.xml"
	 * @required
	 */
	String schemaFilename;

	/**
	 * The name of the file containing the DTD for database schemas
	 *
	 * @parameter expression="${impex.databaseDTDFilename}" default-value="database.dtd"
	 * @required
	 */
	String databaseDTDFilename;

	/**
	 * The name of the file containing the DTD for this database schema
	 *
	 * @parameter expression="${impex.dataDTDFilename}" default-value="data.dtd"
	 * @required
	 */
	String dataDTDFilename;

	@Override
	protected void executeMojo() throws MojoExecutionException, MojoFailureException {
		getLog().info("Examining " + sourceDir.getAbsolutePath());
		handleSchema();
		handleData();
	}

	protected void handleSchema() {
		try {
			handleDataDTD();
			File newSchemaFile = new File(outputDir + "/" + schemaFilename);
			File oldSchemaFile = new File(sourceDir + "/" + schemaFilename);
			getLog().info("Creating " + newSchemaFile.getCanonicalPath());
			FileUtils.copyFile(oldSchemaFile, newSchemaFile);
			File newDatabaseDTDFile = new File(outputDir + "/" + databaseDTDFilename);
			File oldDatabaseDTDFile = new File(sourceDir + "/" + databaseDTDFilename);
			getLog().info("Creating " + newDatabaseDTDFile.getCanonicalPath());
			FileUtils.copyFile(oldDatabaseDTDFile, newDatabaseDTDFile);
		} catch (IOException e) {
			throw new IllegalStateException("Unexpected IO error", e);
		}
	}

	protected String[] parseAll(String s, String open, String close) {
		String[] tokens = StringUtils.substringsBetween(s, open, close);
		if (tokens == null) {
			return null;
		}
		for (int i = 0; i < tokens.length; i++) {
			tokens[i] = open + tokens[i] + close;
		}
		return tokens;

	}

	protected void handleDataDTD() throws IOException {
		File schemaFile = new File(sourceDir + "/" + schemaFilename);
		String contents = FileUtils.readFileToString(schemaFile);
		String[] tables = getTables(contents);
		getLog().info("Located " + tables.length + " schema tables");
		List realTables = new ArrayList();
		for (String table : tables) {
			CloverETLTable realTable = getDataDTDTable(table);
			realTables.add(realTable);
		}
		String content = getDataDTDContent(realTables);
		File dataDTDFile = new File(outputDir + "/" + dataDTDFilename);
		getLog().info("Creating " + dataDTDFile.getCanonicalPath());
		FileUtils.writeStringToFile(dataDTDFile, content);
	}

	protected String getDataDTDContent(List tables) {
		StringBuilder sb = new StringBuilder();
		sb.append(getProlog(tables));
		for (CloverETLTable table : tables) {
			sb.append(getTableDTDContent(table));
		}
		return sb.toString();
	}

	protected String getTableDTDContent(CloverETLTable table) {
		StringBuilder sb = new StringBuilder();
		sb.append("\n");
		sb.append("\n");
		return sb.toString();
	}

	protected String getColumnDTDContent(List columns) {
		StringBuilder sb = new StringBuilder();
		for (CloverETLColumn column : columns) {
			sb.append("    ");
			sb.append(column.getName());
			sb.append(" ");
			sb.append("CDATA");
			sb.append(" ");
			sb.append(column.isRequired() ? "#IMPLIED" : "#REQUIRED");
			sb.append("\n");
		}
		return sb.toString();
	}

	protected String getProlog(List tables) {
		StringBuilder sb = new StringBuilder();
		sb.append("\n");
			}
		}
		return sb.toString();
	}

	protected CloverETLTable getDataDTDTable(String s) {
		String tablename = StringUtils.substringBetween(s, "");
		List realColumns = new ArrayList();
		for (String column : columns) {
			realColumns.add(getCloverETLColumn(column));
		}

		CloverETLTable table = new CloverETLTable();
		table.setName(tablename);
		table.setEtlColumns(realColumns);
		return table;
	}

	protected CloverETLColumn getCloverETLColumn(String s) {
		String columnName = StringUtils.substringBetween(s, "name=\"", "\"");
		boolean required = s.contains("required=\"true\"");
		CloverETLColumn cec = new CloverETLColumn();
		cec.setName(columnName);
		cec.setRequired(required);
		return cec;
	}

	protected String[] getTables(String contents) {
		String begin = "
rows = getRows(content, columns, file); CloverETLTable table = new CloverETLTable(); table.setName(tablename); table.setColumns(Arrays.asList(columns)); table.setRows(rows); return table; } @SuppressWarnings("unchecked") protected List readLines(String s) { try { InputStream in = new ByteArrayInputStream(s.getBytes()); return IOUtils.readLines(in); } catch (IOException e) { throw new IllegalStateException("Unexpected IO error", e); } } protected List getRows(String content, String[] columns, File file) { List lines = readLines(content); List rows = new ArrayList(); for (int i = 1; i < lines.size(); i++) { String line = lines.get(i); while (!line.endsWith(delimiter)) { i = i + 1; line = line + "\n" + lines.get(i); } String[] row = StringUtils.splitByWholeSeparatorPreserveAllTokens(line, delimiter); if (row.length != columns.length) { throw new IllegalStateException("Column count doesn't match. [" + file.getAbsolutePath() + ",row " + i + "] columns=" + columns.length + " row=" + row.length); } for (int j = 0; j < row.length; j++) { row[j] = escape(row[j]); } rows.add(row); } return rows; } protected String escape(String s) { return s.replace("&", "&").replace("<", "<").replace("\"", """).replace("\n", " ").replace("\r", " "); } protected String getXml(CloverETLTable table) { StringBuilder sb = new StringBuilder(); sb.append("\n"); sb.append("\n"); sb.append("\n"); sb.append("\n"); List rows = table.getRows(); List columns = table.getColumns(); for (int i = 0; i < rows.size(); i++) { sb.append(" <" + table.getName()); String[] row = rows.get(i); for (int j = 0; j < columns.size(); j++) { String column = columns.get(j); String value = row[j]; if (!StringUtils.isBlank(value)) { sb.append(" " + column + "=" + '"' + value + '"'); } } sb.append(" />\n"); } sb.append("\n"); return sb.toString(); } public File getSourceDir() { return sourceDir; } public void setSourceDir(File sourceDir) { this.sourceDir = sourceDir; } public File getOutputDir() { return outputDir; } public void setOutputDir(File outputDir) { this.outputDir = outputDir; } public String getDelimiter() { return delimiter; } public void setDelimiter(String delimiter) { this.delimiter = delimiter; } public String getSchemaFilename() { return schemaFilename; } public void setSchemaFilename(String schemaFilename) { this.schemaFilename = schemaFilename; } public String getDatabaseDTDFilename() { return databaseDTDFilename; } public void setDatabaseDTDFilename(String databaseDTDFilename) { this.databaseDTDFilename = databaseDTDFilename; } public String getDataDTDFilename() { return dataDTDFilename; } public void setDataDTDFilename(String dataDTDFilename) { this.dataDTDFilename = dataDTDFilename; } }