RESwitch / RECase ColdFusion Custom Tags For Regular Expression Switch Statements

<!--- Check to see which tag mode we are in. --->
<cfif (THISTAG.ExecutionMode EQ "Start")>
 
	<!--- Start mode. --->
 
	<!--- Check to make sure this tag has an end tag. --->
	<cfif NOT THISTAG.HasEndTag>
 
		<cfthrow
			type="RECase.MissingEndTag"
			message="This RECase tag requires an end tag."
			/>
 
	</cfif>
 
 
	<!---
		Get a reference to the parent tag so that we can update
		the tag context as needed.
	--->
	<cfset VARIABLES.SwitchTag = GetBaseTagData( "cf_reswitch" ) />
 
 
	<!---
		Check to see if a pattern has already been matched. If
		so, then we want to exit out immediately.
	--->
	<cfif VARIABLES.SwitchTag.IsPatternMatched>
 
		<!---
			Exit out immediately - we don't want any more of the
			child case tags to execute.
		--->
		<cfexit method="exittag" />
 
	</cfif>
 
 
	<!---
		ASSERT: If we have made it this far then we know that
		no pattern has been matched against our expression yet.
	--->
 
 
	<!--- Param tag attributes. --->
 
	<!---
		This is the patten that we are going to test against
		the parent tag expression.
	--->
	<cfparam
		name="ATTRIBUTES.Pattern"
		type="string"
		/>
 
 
	<!--- Compile this regular expression patterns. --->
	<cfset VARIABLES.Pattern = VARIABLES.SwitchTag.PatternClass.Compile(
		JavaCast( "string", ATTRIBUTES.Pattern )
		) />
 
	<!---
		Get a matcher for this pattern as it is applied to the
		expression we are testing.
	--->
	<cfset VARIABLES.Matcher = VARIABLES.Pattern.Matcher(
		JavaCast(
			"string",
			VARIABLES.SwitchTag.ATTRIBUTES.Expression
			)
		) />
 
 
	<!--- Check to see if this matcher can find a match. --->
	<cfif VARIABLES.Matcher.Find()>
 
		<!---
			Update the parent flag to indicate that this tag has
			matched a pattern.
		--->
		<cfset VARIABLES.SwitchTag.IsPatternMatched = true />
 
		<!---
			Now that we have matched a pattern, let's see if
			we need to move any of the matched groups into
			tag attributes. I have picked 20 arbitrarily.
		--->
		<cfloop
			index="VARIABLES.GroupIndex"
			from="1"
			to="20"
			step="1">
 
			<!---
				Check to see if an attribute exists for this
				matched group.
			--->
			<cfif StructKeyExists( ATTRIBUTES, "group#VARIABLES.GroupIndex#" )>
 
				<!---
					There is chance that this group did not
					actually get matched in the pattern. If that
					is the case, getting it will return a NULL
					which will remove the variable.
				--->
				<cfset VARIABLES.GroupValue = VARIABLES.Matcher.Group(
					JavaCast( "int", VARIABLES.GroupIndex )
					) />
 
				<!--- Check to see if it was matched. --->
				<cfif StructKeyExists( VARIABLES, "GroupValue" )>
 
					<!--- Store the matched pattern. --->
					<cfset CALLER[ ATTRIBUTES[ "group#VARIABLES.GroupIndex#" ] ] = VARIABLES.GroupValue />
 
				<cfelse>
 
					<!---
						That pattern has this group, but it was
						NOT matched. Let's just store an empty
						string in the variable. Not the best
						strategy, but we can always tweak later.
					--->
					<cfset CALLER[ ATTRIBUTES[ "group#VARIABLES.GroupIndex#" ] ] = "" />
 
				</cfif>
 
			</cfif>
 
		</cfloop>
 
	<cfelse>
 
		<!---
			No match was found. Exit out of this tag and let
			the next child tag execute.
		--->
		<cfexit method="exittag" />
 
	</cfif>
 
<cfelse>
 
	<!--- End mode. --->
 
</cfif>

For Cut-and-Paste