Created
March 25, 2014 01:00
-
-
Save bennadel/9753269 to your computer and use it in GitHub Desktop.
RESwitch / RECase ColdFusion Custom Tags For Regular Expression Switch Statements
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--- Set a value to test (quoted string). ---> | |
<cfset strValue = """Hey Sugar""" /> | |
<!--- Check to see what kind of value we are working with. ---> | |
<cf_reswitch expression="#strValue#"> | |
<cf_recase | |
pattern="^(\d+)\.(\d)+$" | |
group1="intInteger" | |
group2="intDecimal"> | |
Float found: #intInteger#.#intDecimal#. | |
</cf_recase> | |
<cf_recase pattern="^\d+$"> | |
Integer found: #strValue#. | |
</cf_recase> | |
<cf_recase | |
pattern="^([^@]+)@(.+)\.(\w{2,})$" | |
group1="strName" | |
group2="strDomain" | |
group3="strExtension"> | |
Email found: #strName#@#strDomain#.#strExtension#. | |
</cf_recase> | |
<cf_recase | |
pattern="^(""([^""]+)""|([^""]+))$" | |
group2="strQuotedValue" | |
group3="strNonQuotedValue"> | |
<cfif Len( strQuotedValue )> | |
Quoted string found: #strQuotedValue#. | |
<cfelse> | |
Non-Quoted string found: #strNonQuotedValue#. | |
</cfif> | |
</cf_recase> | |
<!--- Default case. ---> | |
<cf_recase pattern="^[\w\W]*$"> | |
Unknown string value found: #strValue#. | |
</cf_recase> | |
</cf_reswitch> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--- 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="RESwitch.MissingEndTag" | |
message="This RESwitch tag requires an end tag." | |
/> | |
</cfif> | |
<!--- Param tag attributes. ---> | |
<!--- | |
This is the expression that we are testing (the value | |
against which we are going to try and match our patterns). | |
---> | |
<cfparam | |
name="ATTRIBUTES.Expression" | |
type="string" | |
/> | |
<!--- | |
Set the match flag to indicate whether or not any of the | |
child tags has successfully matched a tag. | |
---> | |
<cfset VARIABLES.IsPatternMatched = false /> | |
<!--- | |
Create a Pattern object that we can use to compile our | |
patterns in the child tags. | |
---> | |
<cfset VARIABLES.PatternClass = CreateObject( | |
"java", | |
"java.util.regex.Pattern" | |
) /> | |
<cfelse> | |
<!--- End mode. ---> | |
</cfif> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--- 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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment