DocViewProperty { public static DocViewProperty parse(String name, String value) { boolean isMulti = false; boolean isBinaryRef = false; int type = PropertyType.UNDEFINED; int pos = 0; char state = 'b'; List<String> vals = null; StringBuffer tmp = new StringBuffer(); int unicode = 0; int unicodePos = 0; while (pos < value.length()) { char c = value.charAt(pos++); switch (state) { case 'b': if (c == '{') { state = 't'; } else if (c == '[') { isMulti = true; state = 'v'; } else if (c == '\\') { state = 'e'; } else { tmp.append(c); state = 'v'; } break; case 'a': if (c == '[') { isMulti = true; state = 'v'; } else if (c == '\\') { state = 'e'; } else { tmp.append(c); state = 'v'; } break; case 't': if (c == '}') { if (BINARY_REF.equals(tmp.toString())) { type = PropertyType.BINARY; isBinaryRef = true; } else { type = PropertyType.valueFromName(tmp.toString()); } tmp.setLength(0); state = 'a'; } else { tmp.append(c); } break; case 'v': if (c == '\\') { state = 'e'; } else if (c == ',' && isMulti) { if (vals == null) { vals = new LinkedList<String>(); } vals.add(tmp.toString()); tmp.setLength(0); } else if (c == ']' && isMulti && pos == value.length()) { if (tmp.length() > 0 || vals != null) { if (vals == null) { vals = new LinkedList<String>(); } vals.add(tmp.toString()); tmp.setLength(0); } } else { tmp.append(c); } break; case 'e': if (c == 'u') { state = 'u'; unicode = 0; unicodePos = 0; } else if (c == '0') { state = 'v'; if (vals == null) { vals = new LinkedList<String>(); } } else { state = 'v'; tmp.append(c); } break; case 'u': unicode = (unicode << 4) + Character.digit(c, 16); if (++unicodePos == 4) { tmp.appendCodePoint(unicode); state = 'v'; } break; } } String[] values; if (isMulti) { if (tmp.length() > 0) { if (vals == null) { vals = new LinkedList<String>(); } vals.add(tmp.toString()); } if (vals == null) { values = Constants.EMPTY_STRING_ARRAY; } else { values = vals.toArray(new String[vals.size()]); } } else { values = new String[]{tmp.toString()}; } return new DocViewProperty(name, values, isMulti, type, isBinaryRef); } DocViewProperty(String name, String[] values, boolean multi, int type);  DocViewProperty(String name, String[] values, boolean multi, int type, boolean isRef); static DocViewProperty parse(String name, String value); static String format(Property prop); static String format(Property prop, boolean sort, boolean useBinaryReferences); static boolean isAmbiguous(Property prop); boolean apply(Node node); @Override int hashCode(); @Override boolean equals(Object obj); @Override String toString(); final String name; final String[] values; final boolean isMulti; final int type; final boolean isReferenceProperty; }
@Test public void testParseUndefined() { DocViewProperty p = DocViewProperty.parse("foo", "hello"); Assert.assertEquals(new DocViewProperty("foo", new String[] {"hello"}, false, PropertyType.UNDEFINED), p); }
@Test public void testParseMVLong() { DocViewProperty p = DocViewProperty.parse("foo", "{Long}[1,2]"); assertEquals(p, true, PropertyType.LONG, "1", "2"); }
@Test public void testParseMVLongEmpty() { DocViewProperty p = DocViewProperty.parse("foo", "{Long}[]"); assertEquals(p, true, PropertyType.LONG); }
@Test public void testParseMVStringEmpty() { DocViewProperty p = DocViewProperty.parse("foo", "[]"); assertEquals(p, true, PropertyType.UNDEFINED); }
@Test public void testParseLong() { DocViewProperty p = DocViewProperty.parse("foo", "{Long}1234"); Assert.assertEquals(new DocViewProperty("foo", new String[] {"1234"}, false, PropertyType.LONG), p); }
@Test public void testParseEmpty() { DocViewProperty p = DocViewProperty.parse("foo", "{Binary}"); Assert.assertEquals(new DocViewProperty("foo", new String[] { ""}, false, PropertyType.BINARY), p); }
@Test public void testParseSpecial() { DocViewProperty p = DocViewProperty.parse("foo", "\\{hello, world}"); assertEquals(p, false, PropertyType.UNDEFINED, "{hello, world}"); p = DocViewProperty.parse("foo", "{String}\\[hello"); assertEquals(p, false, PropertyType.STRING, "[hello"); }
@Test public void testParseStringTyped() { DocViewProperty p = DocViewProperty.parse("foo", "{String}hello"); assertEquals(p, false, PropertyType.STRING, "hello"); }
@Test public void testParseStringUnicode() { DocViewProperty p = DocViewProperty.parse("foo", "{String}he\\u000fllo"); assertEquals(p, false, PropertyType.STRING, "he\u000fllo"); }
@Test public void testParseMVString() { DocViewProperty p = DocViewProperty.parse("foo", "[hello,world]"); assertEquals(p, true, PropertyType.UNDEFINED, "hello", "world"); p = DocViewProperty.parse("foo", "[hello\\,world]"); assertEquals(p, true, PropertyType.UNDEFINED, "hello,world"); }
@Test public void testParseEmptyMVStrings() { DocViewProperty p = DocViewProperty.parse("foo", "[,a,b,c]"); assertEquals(p, true, PropertyType.UNDEFINED, "", "a", "b", "c"); p = DocViewProperty.parse("foo", "[a,b,c,]"); assertEquals(p, true, PropertyType.UNDEFINED, "a", "b", "c", ""); p = DocViewProperty.parse("foo", "[,,,]"); assertEquals(p, true, PropertyType.UNDEFINED, "", "", "", ""); }
@Test public void testParseMVSpecial() { DocViewProperty p = DocViewProperty.parse("foo", "[\\[hello,world]"); assertEquals(p, true, PropertyType.UNDEFINED, "[hello", "world"); p = DocViewProperty.parse("foo", "[[hello],[world]]"); assertEquals(p, true, PropertyType.UNDEFINED, "[hello]", "[world]"); p = DocViewProperty.parse("foo", "[he\\[llo,world]"); assertEquals(p, true, PropertyType.UNDEFINED, "he[llo", "world"); p = DocViewProperty.parse("foo", "[hello\\[,world]"); assertEquals(p, true, PropertyType.UNDEFINED, "hello[", "world"); p = DocViewProperty.parse("foo", "[hello,\\[world]"); assertEquals(p, true, PropertyType.UNDEFINED, "hello", "[world"); p = DocViewProperty.parse("foo", "[hello,world\\[]"); assertEquals(p, true, PropertyType.UNDEFINED, "hello", "world["); p = DocViewProperty.parse("foo", "[hello,world"); assertEquals(p, true, PropertyType.UNDEFINED, "hello", "world"); p = DocViewProperty.parse("foo", "[bla{a\\,b},foo{a\\,b},bar{a\\,b}]"); assertEquals(p, true, PropertyType.UNDEFINED, "bla{a,b}", "foo{a,b}", "bar{a,b}"); p = DocViewProperty.parse("foo", "[/content/[a-z]{2\\,3}/[a-z]{2\\,3}(/.*)]"); assertEquals(p, true, PropertyType.UNDEFINED, "/content/[a-z]{2,3}/[a-z]{2,3}(/.*)"); }