aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api/src/main
diff options
context:
space:
mode:
authorJenkins CI <ci@sonarsource.com>2014-10-21 15:38:27 +0200
committerJenkins CI <ci@sonarsource.com>2014-10-21 15:38:27 +0200
commit97ab97d876c4676dc1cdf5d1a2f4741fceaba8af (patch)
tree60a4e0295b55325ac716781c4fdf45627ea65645 /sonar-plugin-api/src/main
parent07e7caec293ab173dd7659289b60fee216eae941 (diff)
parentcfa6a434a3743479ac38dc769ec1573f2a0ad8d2 (diff)
downloadsonarqube-97ab97d876c4676dc1cdf5d1a2f4741fceaba8af.tar.gz
sonarqube-97ab97d876c4676dc1cdf5d1a2f4741fceaba8af.zip
Automatic merge from branch-4.5
* origin/branch-4.5: Refactor KeyValueFormat Reduce complexity
Diffstat (limited to 'sonar-plugin-api/src/main')
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/utils/KeyValueFormat.java75
1 files changed, 45 insertions, 30 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/KeyValueFormat.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/KeyValueFormat.java
index 37c16c9c8f7..c2a619e2108 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/KeyValueFormat.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/KeyValueFormat.java
@@ -71,6 +71,13 @@ public final class KeyValueFormat {
// only static methods
}
+ private static class FieldParserContext {
+ private final StringBuilder result = new StringBuilder();
+ private boolean escaped = false;
+ private char firstChar;
+ private char previous = (char) -1;
+ }
+
static class FieldParser {
private static final char DOUBLE_QUOTE = '"';
private final String csv;
@@ -95,41 +102,49 @@ public final class KeyValueFormat {
if (position >= csv.length()) {
return null;
}
- StringBuilder result = new StringBuilder();
- boolean escaped = false;
- char firstChar = csv.charAt(position);
- char previous = (char) -1;
+ FieldParserContext context = new FieldParserContext();
+ context.firstChar = csv.charAt(position);
// check if value is escaped by analyzing first character
- if (firstChar == DOUBLE_QUOTE) {
- escaped = true;
- position++;
- previous = firstChar;
+ checkEscaped(context);
+
+ boolean isEnd = false;
+ while (position < csv.length() && !isEnd) {
+ isEnd = advance(separator, context);
}
+ return context.result.toString();
+ }
+ private boolean advance(char separator, FieldParserContext context) {
boolean end = false;
- while (position < csv.length() && !end) {
- char c = csv.charAt(position);
- if (c == separator && !escaped) {
- end = true;
- position++;
- } else if (c == '\\' && escaped && position < csv.length() + 1 && csv.charAt(position + 1) == DOUBLE_QUOTE) {
- // on a backslash that escapes double-quotes -> keep double-quotes and jump after
- previous = DOUBLE_QUOTE;
- result.append(previous);
- position += 2;
- } else if (c == '"' && escaped && previous != '\\') {
- // on unescaped double-quotes -> end of escaping.
- // assume that next character is a separator (= or ;). This could be
- // improved to enforce check.
- end = true;
- position += 2;
- } else {
- result.append(c);
- previous = c;
- position++;
- }
+ char c = csv.charAt(position);
+ if (c == separator && !context.escaped) {
+ end = true;
+ position++;
+ } else if (c == '\\' && context.escaped && position < csv.length() + 1 && csv.charAt(position + 1) == DOUBLE_QUOTE) {
+ // on a backslash that escapes double-quotes -> keep double-quotes and jump after
+ context.previous = DOUBLE_QUOTE;
+ context.result.append(context.previous);
+ position += 2;
+ } else if (c == '"' && context.escaped && context.previous != '\\') {
+ // on unescaped double-quotes -> end of escaping.
+ // assume that next character is a separator (= or ;). This could be
+ // improved to enforce check.
+ end = true;
+ position += 2;
+ } else {
+ context.result.append(c);
+ context.previous = c;
+ position++;
+ }
+ return end;
+ }
+
+ private void checkEscaped(FieldParserContext context) {
+ if (context.firstChar == DOUBLE_QUOTE) {
+ context.escaped = true;
+ position++;
+ context.previous = context.firstChar;
}
- return result.toString();
}
}