From cd1b7df86bf68a3e704990441bc9cb5d664e22db Mon Sep 17 00:00:00 2001 From: Rodrigo Ruiz Date: Sat, 9 May 2015 12:35:15 -0400 Subject: [PATCH 1/4] keep attributes on components --- .../interceptor/commands/Component.groovy | 37 ++++++++++++++++++- .../interceptor/commands/ComponentTest.groovy | 24 +++++++++--- .../ComponentInterceptorTest.groovy | 2 +- .../components/NewComponentContent.component | 8 ++++ 4 files changed, 64 insertions(+), 7 deletions(-) create mode 100644 src/test/groovy/org/fundacionjala/gradle/plugins/enforce/interceptor/resources/components/NewComponentContent.component diff --git a/src/main/groovy/org/fundacionjala/gradle/plugins/enforce/interceptor/commands/Component.groovy b/src/main/groovy/org/fundacionjala/gradle/plugins/enforce/interceptor/commands/Component.groovy index f062e66..d534e42 100644 --- a/src/main/groovy/org/fundacionjala/gradle/plugins/enforce/interceptor/commands/Component.groovy +++ b/src/main/groovy/org/fundacionjala/gradle/plugins/enforce/interceptor/commands/Component.groovy @@ -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 = '' + private final String COMPONENT_REGEX = /(.*([^\n]*?\n+?)*?.*)<\/apex:component>/ + private final String ATTRIBUTE_REGEX = // + private final String ATTRIBUTE_BY_DEFAULT = "" + 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 } } diff --git a/src/test/groovy/org/fundacionjala/gradle/plugins/enforce/interceptor/commands/ComponentTest.groovy b/src/test/groovy/org/fundacionjala/gradle/plugins/enforce/interceptor/commands/ComponentTest.groovy index 0a6dba0..d01f9d2 100644 --- a/src/test/groovy/org/fundacionjala/gradle/plugins/enforce/interceptor/commands/ComponentTest.groovy +++ b/src/test/groovy/org/fundacionjala/gradle/plugins/enforce/interceptor/commands/ComponentTest.groovy @@ -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 = '''\n''' + 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 = ''' + + + + +''' + File file = new File("${TRUNCATED_PATH}/NewComponentContent.component") + when: + componentContent.execute(file) + then: + file.text == expectedContent } - def cleanupSpec() { new File(TRUNCATED_PATH).deleteDir() } diff --git a/src/test/groovy/org/fundacionjala/gradle/plugins/enforce/interceptor/interceptors/ComponentInterceptorTest.groovy b/src/test/groovy/org/fundacionjala/gradle/plugins/enforce/interceptor/interceptors/ComponentInterceptorTest.groovy index 7d3717f..bed6c0c 100644 --- a/src/test/groovy/org/fundacionjala/gradle/plugins/enforce/interceptor/interceptors/ComponentInterceptorTest.groovy +++ b/src/test/groovy/org/fundacionjala/gradle/plugins/enforce/interceptor/interceptors/ComponentInterceptorTest.groovy @@ -24,7 +24,7 @@ class ComponentInterceptorTest extends Specification { when: componentInterceptor.loadFiles(path) then: - componentInterceptor.files.size() == 1 + componentInterceptor.files.size() == 2 } } diff --git a/src/test/groovy/org/fundacionjala/gradle/plugins/enforce/interceptor/resources/components/NewComponentContent.component b/src/test/groovy/org/fundacionjala/gradle/plugins/enforce/interceptor/resources/components/NewComponentContent.component new file mode 100644 index 0000000..68f1b36 --- /dev/null +++ b/src/test/groovy/org/fundacionjala/gradle/plugins/enforce/interceptor/resources/components/NewComponentContent.component @@ -0,0 +1,8 @@ + + + + + + + \ No newline at end of file From d67155befcfd47aff28c64394b2ae3c48a6e567b Mon Sep 17 00:00:00 2001 From: Rodrigo Ruiz Date: Sat, 9 May 2015 12:38:55 -0400 Subject: [PATCH 2/4] reformat code in component class --- .../enforce/interceptor/commands/Component.groovy | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/groovy/org/fundacionjala/gradle/plugins/enforce/interceptor/commands/Component.groovy b/src/main/groovy/org/fundacionjala/gradle/plugins/enforce/interceptor/commands/Component.groovy index d534e42..564cc25 100644 --- a/src/main/groovy/org/fundacionjala/gradle/plugins/enforce/interceptor/commands/Component.groovy +++ b/src/main/groovy/org/fundacionjala/gradle/plugins/enforce/interceptor/commands/Component.groovy @@ -14,7 +14,8 @@ class Component { public static final String EMPTY_COMPONENT = '' private final String COMPONENT_REGEX = /(.*([^\n]*?\n+?)*?.*)<\/apex:component>/ private final String ATTRIBUTE_REGEX = // - private final String ATTRIBUTE_BY_DEFAULT = "" + private + final String ATTRIBUTE_BY_DEFAULT = "" private final String NAME_ATTRIBUTE = "name" private final int INDEX_COMPONENT = 1 private final int INDEX_ATTRIBUTE = 0 @@ -37,13 +38,13 @@ class Component { int indexEndName componentMatcher.each { componentIt -> content = componentIt[INDEX_COMPONENT] - if(content) { + 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) + indexIniName = attribute.indexOf("\"", indexName) + indexEndName = attribute.indexOf("\"", indexIniName + 1) attributeName = attribute.substring(indexIniName, indexEndName + 1) newContent += "\n${String.format(ATTRIBUTE_BY_DEFAULT, attributeName)}" } From c72a47f33a4c438439730333e522acc59af19d14 Mon Sep 17 00:00:00 2001 From: Rodrigo Ruiz Date: Sat, 9 May 2015 12:56:57 -0400 Subject: [PATCH 3/4] Truncate process was improved --- .../interceptor/commands/ObjectField.groovy | 6 ++++-- .../interceptor/commands/ObjectFormula.groovy | 19 +++++++++++-------- .../interceptor/commands/ObjectWebLink.groovy | 8 +++++--- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/main/groovy/org/fundacionjala/gradle/plugins/enforce/interceptor/commands/ObjectField.groovy b/src/main/groovy/org/fundacionjala/gradle/plugins/enforce/interceptor/commands/ObjectField.groovy index 585cc19..cd0c206 100644 --- a/src/main/groovy/org/fundacionjala/gradle/plugins/enforce/interceptor/commands/ObjectField.groovy +++ b/src/main/groovy/org/fundacionjala/gradle/plugins/enforce/interceptor/commands/ObjectField.groovy @@ -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) { @@ -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 } } diff --git a/src/main/groovy/org/fundacionjala/gradle/plugins/enforce/interceptor/commands/ObjectFormula.groovy b/src/main/groovy/org/fundacionjala/gradle/plugins/enforce/interceptor/commands/ObjectFormula.groovy index 69b6324..d0f33d7 100644 --- a/src/main/groovy/org/fundacionjala/gradle/plugins/enforce/interceptor/commands/ObjectFormula.groovy +++ b/src/main/groovy/org/fundacionjala/gradle/plugins/enforce/interceptor/commands/ObjectFormula.groovy @@ -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 @@ -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 } } diff --git a/src/main/groovy/org/fundacionjala/gradle/plugins/enforce/interceptor/commands/ObjectWebLink.groovy b/src/main/groovy/org/fundacionjala/gradle/plugins/enforce/interceptor/commands/ObjectWebLink.groovy index a3f3dfb..8e8f3a5 100644 --- a/src/main/groovy/org/fundacionjala/gradle/plugins/enforce/interceptor/commands/ObjectWebLink.groovy +++ b/src/main/groovy/org/fundacionjala/gradle/plugins/enforce/interceptor/commands/ObjectWebLink.groovy @@ -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 @@ -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 } } From 89e1d6c1b3bbc0224a829724134d71c0de31a84b Mon Sep 17 00:00:00 2001 From: Rodrigo Ruiz Date: Mon, 11 May 2015 10:59:02 -0400 Subject: [PATCH 4/4] Code observations applied --- .../plugins/enforce/interceptor/commands/Component.groovy | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/groovy/org/fundacionjala/gradle/plugins/enforce/interceptor/commands/Component.groovy b/src/main/groovy/org/fundacionjala/gradle/plugins/enforce/interceptor/commands/Component.groovy index 564cc25..cd6fbbb 100644 --- a/src/main/groovy/org/fundacionjala/gradle/plugins/enforce/interceptor/commands/Component.groovy +++ b/src/main/groovy/org/fundacionjala/gradle/plugins/enforce/interceptor/commands/Component.groovy @@ -14,8 +14,7 @@ class Component { public static final String EMPTY_COMPONENT = '' private final String COMPONENT_REGEX = /(.*([^\n]*?\n+?)*?.*)<\/apex:component>/ private final String ATTRIBUTE_REGEX = // - private - final String ATTRIBUTE_BY_DEFAULT = "" + private final String ATTRIBUTE_BY_DEFAULT = "" private final String NAME_ATTRIBUTE = "name" private final int INDEX_COMPONENT = 1 private final int INDEX_ATTRIBUTE = 0