Skip to content

Commit

Permalink
MONDRIAN: Fix parsing of members which have ']' followed by '.'. Cont…
Browse files Browse the repository at this point in the history
…ributed by rehdie_easyplex.

[git-p4: depot-paths = "//open/mondrian/": change = 8846]
  • Loading branch information
julianhyde committed Mar 8, 2007
1 parent 43cbf97 commit 4b751b6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 15 deletions.
38 changes: 23 additions & 15 deletions src/main/mondrian/olap/Util.java
Expand Up @@ -347,36 +347,44 @@ public static StringBuilder replace(
return buf;
}

public static String[] explode(String s) {
public static String[] explode(String s) {
if (!s.startsWith("[")) {
return new String[]{s};
}

List<String> list = new ArrayList<String>();
int i = 0;

while (i < s.length()) {
if (s.charAt(i) != '[') {
throw MondrianResource.instance().MdxInvalidMember.ex(s);
}
// s may contain extra ']' characters, so look for a ']' followed
// by a '.'
int j = s.indexOf("].", i);

int j = getEndIndex(s, i + 1);
if (j == -1) {
j = s.lastIndexOf(']');
}
if (j <= i) {
throw MondrianResource.instance().MdxInvalidMember.ex(s);
}
String sub = s.substring(i + 1, j);
sub = replace(sub, "]]", "]");
list.add(sub);
if (j + 1 < s.length()) {
if (s.charAt(j + 1) != '.') {
throw MondrianResource.instance().MdxInvalidMember.ex(s);

list.add(replace(s.substring(i + 1, j), "]]", "]"));
i = j + 2;
}
return list.toArray(new String[list.size()]);
}

private static int getEndIndex(String s, int i) {
while (i < s.length()) {
char ch = s.charAt(i);
if (ch == ']') {
if (i + 1 < s.length() && s.charAt(i + 1) == ']') { // found ]] => skip
i += 2;
} else {
return i;
}
} else {
i++;
}
i = j + 2;
}
return (String[]) list.toArray(new String[list.size()]);
return -1;
}

/**
Expand Down
24 changes: 24 additions & 0 deletions testsrc/main/mondrian/olap/UtilTestCase.java
Expand Up @@ -264,6 +264,30 @@ public void testExplode() {
Util.explode("[string].[with].[a [bracket]] in it]");
assertEquals(3, strings.length);
assertEquals("a [bracket] in it", strings[2]);

strings = Util.explode("[Worklog].[All].[calendar-[LANGUAGE]].js]");
assertEquals(3, strings.length);
assertEquals("calendar-[LANGUAGE].js", strings[2]);

try {
strings = Util.explode("[foo].bar");
Util.discard(strings);
fail("expected exception");
} catch (MondrianException e) {
assertEquals(
"Mondrian Error:Invalid member identifier '[foo].bar'",
e.getMessage());
}

try {
strings = Util.explode("[foo].[bar");
Util.discard(strings);
fail("expected exception");
} catch (MondrianException e) {
assertEquals(
"Mondrian Error:Invalid member identifier '[foo].[bar'",
e.getMessage());
}
}

public void testReplaceProperties() {
Expand Down

0 comments on commit 4b751b6

Please sign in to comment.