示例#1
0
  @Test
  public void shouldConvertPathToString() {
    TextEncoder encoder = new Jsr283Encoder();
    TextEncoder delimEncoder =
        new TextEncoder() {
          public String encode(String text) {
            if ("/".equals(text)) return "\\/";
            if (":".equals(text)) return "\\:";
            if ("{".equals(text)) return "\\{";
            if ("}".equals(text)) return "\\}";
            return text;
          }
        };
    Path path = pathFactory.create("a/b/c");
    assertThat(path.getString(namespaceRegistry), is("a/b/c"));
    assertThat(path.getString(namespaceRegistry, encoder), is("a/b/c"));
    assertThat(path.getString(namespaceRegistry, encoder, delimEncoder), is("a\\/b\\/c"));

    path = pathFactory.create("/a/b/c");
    assertThat(path.getString(namespaceRegistry), is("/a/b/c"));
    assertThat(path.getString(namespaceRegistry, encoder), is("/a/b/c"));
    assertThat(path.getString(namespaceRegistry, encoder, delimEncoder), is("\\/a\\/b\\/c"));

    path = pathFactory.create("/mode:a/b/c");
    assertThat(
        path.getString(encoder),
        is("/{" + encoder.encode(ModeShapeLexicon.Namespace.URI) + "}a/{}b/{}c"));
    assertThat(
        path.getString(null, encoder, delimEncoder),
        is("\\/\\{" + encoder.encode(ModeShapeLexicon.Namespace.URI) + "\\}a\\/\\{\\}b\\/\\{\\}c"));
    assertThat(path.getString(namespaceRegistry), is("/mode:a/b/c"));
    assertThat(path.getString(namespaceRegistry, encoder), is("/mode:a/b/c"));
    assertThat(path.getString(namespaceRegistry, encoder, delimEncoder), is("\\/mode\\:a\\/b\\/c"));
  }
示例#2
0
 protected void write(Property property, Writer stream, ValueFactory<String> strings)
     throws IOException {
   String name = strings.create(property.getName());
   stream.append(encoder.encode(name));
   if (property.isEmpty()) return;
   stream.append(" (");
   PropertyType type = PropertyType.discoverType(property.getFirstValue());
   stream.append(type.getName().toLowerCase());
   stream.append(") ");
   if (property.isMultiple()) {
     stream.append('[');
   }
   boolean first = true;
   boolean quote = type == PropertyType.STRING;
   for (Object value : property) {
     if (first) first = false;
     else stream.append(", ");
     String str = null;
     if (value instanceof Binary) {
       str = StringUtil.getHexString(((Binary) value).getBytes());
     } else {
       str = strings.create(value);
     }
     if (quote) {
       stream.append('"');
       stream.append(quoter.encode(str));
       stream.append('"');
     } else {
       stream.append(str);
     }
   }
   if (property.isMultiple()) {
     stream.append(']');
   }
   stream.append('\n');
   stream.flush();
 }