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

org.etlunit.util.regexp.TableConstraintExpression Maven / Gradle / Ivy

package org.etlunit.util.regexp;

/*

		Regular expression class compiled by the Regular Expression Compiler 1.5.0-SNAPSHOT

*/

import java.util.Map;
import java.util.HashMap;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

public final class TableConstraintExpression
{
	public static final String PATTERN_RAW_TEXT = "(?'constraintText'[\\s]*ALTER[\\s]*TABLE[\\s]*TST_TGT[\\s]*\\.[\\s]*(?'tableName'[\\w_]+)[\\s]*ADD[\\s]*CONSTRAINT[\\s]*(?'constraintName'[\\w_]+)[\\s]*FOREIGN[\\s]*KEY[\\s]*\\([^\\)]+\\)[\\s]*REFERENCES[\\s]*TST_TGT[\\s]*\\.[\\s]*(?'foreignTableName'[\\w_]+)\\([^\\)]+\\)[\\s]*ON[\\s]*DELETE[\\s]*(NO[\\s+]ACTION|CASCADE)[\\s]*ON[\\s]*UPDATE[\\s]*NO[\\s]*ACTION[\\s]*go[\\s]*)";
	public static final String PATTERN_TEXT;
	public static final Pattern pattern;
	private static final Map groupOffsets = new HashMap();

	private final Matcher matcher;
	private final CharSequence matchText;

	public static interface RegExpIterator
	{
		String replaceMatch(TableConstraintExpression e);
	}

	static
	{
		PATTERN_TEXT = PATTERN_RAW_TEXT.replaceAll("\\(\\?'[^']+'", "(");

		Pattern cpattern = Pattern.compile("(\\\\)?\\((\\?'([^']+)')?");

		Matcher matcher = cpattern.matcher(PATTERN_RAW_TEXT);

		int groupCount = 1;

		while (matcher.find())
		{
			if (matcher.group(1) != null)
			{
				continue;
			}

			String groupName = matcher.group(3);

			if (groupName != null)
			{
				groupOffsets.put(groupName, new Integer(groupCount));
			}

			groupCount++;
		}

		pattern = Pattern.compile(PATTERN_TEXT, Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
	}


	private TableConstraintExpression(Matcher pMatcher, CharSequence ch)
	{
		matcher = pMatcher;
		matchText = ch;
	}

	public TableConstraintExpression(CharSequence ch)
	{
		this(pattern.matcher(ch), ch);
	}

	public static TableConstraintExpression match(CharSequence pText)
	{
		return new TableConstraintExpression(pText);
	}

	public int end()
	{
		return matcher.end();
	}

	public int end(int group)
	{
		return matcher.end(group);
	}

	public int start()
	{
		return matcher.start();
	}

	public int start(int group)
	{
		return matcher.start(group);
	}

	public String replaceAll(String replacement)
	{
		return matcher.replaceAll(replacement);
	}

	public String replaceAll(RegExpIterator it)
	{
		String match = matchText.toString();
		String str = "";
		int end = -1;

		while (hasNext())
		{
			str += match.substring(end == -1 ? 0 : end, start());
			str += it.replaceMatch(this);
			end = end();
		}

		str += match.substring(end == -1 ? 0 : end, match.length());
		return str;
	}

	public boolean matches()
	{
		return matcher.matches();
	}

	public boolean hasNext()
	{
		return matcher.find();
	}

	public int groupCount()
	{
		return matcher.groupCount();
	}

	public String group()
	{
		return matcher.group();
	}

	public String group(int i)
	{
		return matcher.group(i);
	}

	public TableConstraintExpression resetMatch(CharSequence seq)
	{
		return match(seq);
	}

	public String group(String name)
	{
		return matcher.group(groupOffsets.get(name).intValue());
	}

	private static String scope(String text, String name)
	{
		return text.replaceAll("(\\(\\?')(\\w+')", "$1" + name + ".$2");
	}


	public boolean hasConstraintText()
	{
		return group("constraintText") != null;
	}

	public String getConstraintText()
	{
		if (!hasConstraintText())
		{
			throw new IllegalArgumentException("Property not defined: ConstraintText");
		}
		return group("constraintText");
	}

	public boolean hasTableName()
	{
		return group("tableName") != null;
	}

	public String getTableName()
	{
		if (!hasTableName())
		{
			throw new IllegalArgumentException("Property not defined: TableName");
		}
		return group("tableName");
	}

	public boolean hasConstraintName()
	{
		return group("constraintName") != null;
	}

	public String getConstraintName()
	{
		if (!hasConstraintName())
		{
			throw new IllegalArgumentException("Property not defined: ConstraintName");
		}
		return group("constraintName");
	}

	public boolean hasForeignTableName()
	{
		return group("foreignTableName") != null;
	}

	public String getForeignTableName()
	{
		if (!hasForeignTableName())
		{
			throw new IllegalArgumentException("Property not defined: ForeignTableName");
		}
		return group("foreignTableName");
	}

	public String toString()
	{
		return "{TableConstraintExpression, matchText='" + matchText + "'}[" + "(constraintText)=" + (hasConstraintText() ? getConstraintText() : "null") + ", (tableName)=" + (hasTableName() ? getTableName() : "null") + ", (constraintName)=" + (hasConstraintName() ? getConstraintName() : "null") + ", (foreignTableName)=" + (hasForeignTableName() ? getForeignTableName() : "null") + "]";
	}

	public static void main(String [] argv)
	{
		System.out.println(PATTERN_TEXT);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy