Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…-plugin into fields_tracking
  • Loading branch information
Rodrigo Ruiz committed May 13, 2015
2 parents 44151e8 + 89e1d6c commit f517530
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,51 @@

package org.fundacionjala.gradle.plugins.enforce.interceptor.commands

import java.util.regex.Matcher

/**
* Implements the truncated algorithm to truncate the component content
*/
class Component {
public static final String EMPTY_COMPONENT = '<apex:component></apex:component>'
private final String COMPONENT_REGEX = /<apex:component.*>(.*([^\n]*?\n+?)*?.*)<\/apex:component>/
private final String ATTRIBUTE_REGEX = /<apex:attribute([^\n]*?\n+?)*?.*\/>/
private final String ATTRIBUTE_BY_DEFAULT = "<apex:attribute name=%s type=\"Object\" required=\"true\" description=\"Description\"/>"
private final String NAME_ATTRIBUTE = "name"
private final int INDEX_COMPONENT = 1
private final int INDEX_ATTRIBUTE = 0

/**
* A closure to truncate the component content
*/
Closure execute = { file ->
file.text = EMPTY_COMPONENT
if (!file) {
return
}
String component = file.text
Matcher componentMatcher = component =~ COMPONENT_REGEX
String content = ""
String newContent = ""
String attribute = ""
String attributeName = ""
int indexName
int indexIniName
int indexEndName
componentMatcher.each { componentIt ->
content = componentIt[INDEX_COMPONENT]
if (content) {
Matcher attributeMatcher = component =~ ATTRIBUTE_REGEX
attributeMatcher.each { attributeIt ->
attribute = attributeIt[INDEX_ATTRIBUTE]
indexName = attribute.indexOf(NAME_ATTRIBUTE)
indexIniName = attribute.indexOf("\"", indexName)
indexEndName = attribute.indexOf("\"", indexIniName + 1)
attributeName = attribute.substring(indexIniName, indexEndName + 1)
newContent += "\n${String.format(ATTRIBUTE_BY_DEFAULT, attributeName)}"
}
component = component.replace(content, "${newContent}\n")
}
}
file.text = component
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ class ObjectField {
if (!file) {
return
}
Matcher fieldMatcher = file.text =~ FIELDS_REGEX
String objectField = file.text
Matcher fieldMatcher = objectField =~ FIELDS_REGEX
fieldMatcher.each { fieldIt ->
String field = fieldIt[FIELD_INDEX]
if (field) {
Expand All @@ -41,8 +42,9 @@ class ObjectField {
helpTextMatcher.each { helpTextIt ->
newField = newField.replace(helpTextIt[HELP_TEXT_INDEX].toString(), HELP_TEXT_TAG)
}
file.text = file.text.replace(field, newField)
objectField = objectField.replace(field, newField)
}
}
file.text = objectField
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ class ObjectFormula {
*/
Closure execute = { file ->
if (!file) return
Matcher fieldMatcher = file.text =~ FIELDS_REGEX

String objectFormula = file.text
Matcher fieldMatcher = objectFormula =~ FIELDS_REGEX
fieldMatcher.each { fieldIt->
String field = fieldIt[CONTENT_MATCHED_INDEX]
String formula
Expand All @@ -47,34 +49,35 @@ class ObjectFormula {
switch (type) {
case FormulaType.PERCENT.value():
replacement = field.replace(formula, String.format(TAG_FORMULA, NUMBER_BY_DEFAULT))
file.text = file.text.replace(target, replacement)
objectFormula = objectFormula.replace(target, replacement)
break
case FormulaType.NUMBER.value():
replacement = field.replace(formula, String.format(TAG_FORMULA, NUMBER_BY_DEFAULT))
file.text = file.text.replace(target, replacement)
objectFormula = objectFormula.replace(target, replacement)
break
case FormulaType.TIME.value():
replacement = field.replace(formula, String.format(TAG_FORMULA, DATE_BY_DEFAULT))
file.text = file.text.replace(target, replacement)
objectFormula = objectFormula.replace(target, replacement)
break
case FormulaType.DATE.value():
replacement = field.replace(formula, String.format(TAG_FORMULA, DATE_BY_DEFAULT))
file.text = file.text.replace(target, replacement)
objectFormula = objectFormula.replace(target, replacement)
break
case FormulaType.CURRENCY.value():
replacement = field.replace(formula, String.format(TAG_FORMULA, NUMBER_BY_DEFAULT))
file.text = file.text.replace(target, replacement)
objectFormula = objectFormula.replace(target, replacement)
break
case FormulaType.CHECKBOK.value():
replacement = field.replace(formula, String.format(TAG_FORMULA, CHECKBOK_BY_DEFAULT))
file.text = file.text.replace(target, replacement)
objectFormula = objectFormula.replace(target, replacement)
break
case FormulaType.TEXT.value():
replacement = field.replace(formula, String.format(TAG_FORMULA, "\"\""))
file.text = file.text.replace(target, replacement)
objectFormula = objectFormula.replace(target, replacement)
break
}
}
}
file.text = objectFormula
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ class ObjectWebLink {
*/
Closure execute = { file ->
if (!file) return
Matcher webLinkMatcher = file.text =~ WEB_LINK_REGEX
String objectWebLink = file.text
Matcher webLinkMatcher = objectWebLink =~ WEB_LINK_REGEX
webLinkMatcher.each { webLinkIt->
String webLink = webLinkIt[CONTENT_MATCHED_INDEX]
String url
Expand All @@ -47,15 +48,16 @@ class ObjectWebLink {
switch (type) {
case LinkType.JAVASCRIPT.value():
replacement = webLink.replace(url, String.format(TAG_URL, URL_JS_BY_DEFAULT))
file.text = file.text.replace(target, replacement)
objectWebLink = objectWebLink.replace(target, replacement)
break
case LinkType.URL.value():
replacement = webLink.replace(url, String.format(TAG_URL, URL_BY_DEFAULT))
file.text = file.text.replace(target, replacement)
objectWebLink = objectWebLink.replace(target, replacement)
break
}
}
}
file.text = objectWebLink
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,29 @@ class ComponentTest extends Specification {
}


def "Should truncate a component"() {
setup:
File file = new File("${TRUNCATED_PATH}/Component1.component")
def "Should truncate a simple component"() {
given:
def expectedContent = '''<apex:component >\n</apex:component>'''
File file = new File("${TRUNCATED_PATH}/ComponentContent.component")
when:
componentContent.execute(file)
then:
file.text == Component.EMPTY_COMPONENT
file.text == expectedContent
}
def "Should truncate a component with attributes"() {
given:
def expectedContent = '''<!-- MyComponent -->
<apex:component controller="MyComponentController" allowDML="true">
<apex:attribute name="record" type="Object" required="true" description="Description"/>
<apex:attribute name=" myObject " type="Object" required="true" description="Description"/>
<apex:attribute name="myClass" type="Object" required="true" description="Description"/>
</apex:component>'''
File file = new File("${TRUNCATED_PATH}/NewComponentContent.component")
when:
componentContent.execute(file)
then:
file.text == expectedContent
}

def cleanupSpec() {
new File(TRUNCATED_PATH).deleteDir()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ComponentInterceptorTest extends Specification {
when:
componentInterceptor.loadFiles(path)
then:
componentInterceptor.files.size() == 1
componentInterceptor.files.size() == 2

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!-- MyComponent -->
<apex:component controller="MyComponentController" allowDML="true">
<apex:attribute name="record" description="The type of record we are viewing."
type="Object" required="true"/>
<apex:attribute name= " myObject " type="My_Object__c" assignTo="{!objectRef}" required="true" description="Description" />
<apex:attribute name="myClass" type="WrapperClass" assignTo="{!classRef}" required="true" description="Description" />
<apex:outputText value="{!classRef.something}"/>
</apex:component>

0 comments on commit f517530

Please sign in to comment.