Esempio n. 1
0
  /**
   * Adds the string representation of an object:
   *
   * <ul>
   *   <li>objects of type {@link Throwable} are converted to a string representation via {@link
   *       Util#message}.
   *   <li>objects of type {@link Class} are converted via {@link Util#className(Class)}.
   *   <li>{@code null} references are replaced by the string {@code "null"}.
   *   <li>byte arrays are directly inserted as tokens.
   *   <li>for all other typed, {@link Object#toString} is called.
   * </ul>
   *
   * The specified string may contain {@code "%"} characters as place holders. All place holders
   * will be replaced by the specified extensions. If a digit is specified after the place holder
   * character, it will be interpreted as insertion position.
   *
   * @param object string to be extended
   * @param ext optional extensions
   * @return self reference
   */
  public TokenBuilder addExt(final Object object, final Object... ext) {
    final byte[] t;
    if (object instanceof byte[]) {
      t = (byte[]) object;
    } else {
      final String s;
      if (object == null) {
        s = "null";
      } else if (object instanceof Throwable) {
        s = Util.message((Throwable) object);
      } else if (object instanceof Class<?>) {
        s = Util.className((Class<?>) object);
      } else {
        s = object.toString();
      }
      t = token(s);
    }

    for (int i = 0, e = 0; i < t.length; ++i) {
      if (t[i] != '%' || e == ext.length) {
        addByte(t[i]);
      } else {
        final byte c = i + 1 < t.length ? t[i + 1] : 0;
        final boolean d = c >= '1' && c <= '9';
        if (d) ++i;
        final int n = d ? c - '1' : e++;
        final Object o = n < ext.length ? ext[n] : null;
        addExt(o);
      }
    }
    return this;
  }
Esempio n. 2
0
 @Override
 public String toString() {
   final TokenBuilder tb = new TokenBuilder(Util.className(this)).add('[');
   for (int i = 1; i < size; i++) {
     tb.add(Integer.toString(keys[i])).add(": ").addExt(get(keys[i]));
     if (i < size - 1) tb.add(",\n\t");
   }
   return tb.add(']').toString();
 }
Esempio n. 3
0
  @Override
  public JapaneseTokenizer init(final byte[] txt) {
    String source = string(txt);
    if (wc) { // convert wide-space to space
      source = source.replace('\u3000', '\u0020');
    }
    final ArrayList<?> morpheme = (ArrayList<?>) Reflect.invoke(parse, tagger, source);
    final ArrayList<Morpheme> list = new ArrayList<>();
    try {
      int prev = 0;
      final int ms = morpheme.size();
      for (int i = 0; i < ms; i++) {
        final Object m = morpheme.get(i);
        final String srfc = surface.get(m).toString();
        final String ftr = feature.get(m).toString();
        final int strt = start.getInt(m);
        if (i != 0) {
          final int l = strt - prev;
          if (l != 0) {
            list.add(new Morpheme(source.substring(strt - 1, strt + l - 1), KIGOU_FEATURE));
          }
        }
        prev = srfc.length() + strt;

        // separates continuous mark (ASCII)
        boolean cont = true;
        final ArrayList<Morpheme> marks = new ArrayList<>();
        final int sl = srfc.length();
        for (int s = 0; s < sl; s++) {
          final String c = String.valueOf(srfc.charAt(s));
          final byte[] t = token(c);
          if (t.length == 1) {
            if (letter(t[0]) || digit(t[0])) cont = false;
            else marks.add(new Morpheme(c, KIGOU_FEATURE));
          } else {
            cont = false;
          }
        }

        if (cont) list.addAll(marks);
        else list.add(new Morpheme(srfc, ftr));
      }
    } catch (final Exception ex) {
      Util.errln(Util.className(this) + ": " + ex);
    }
    tokenList = list;
    tokens = list.iterator();

    return this;
  }
Esempio n. 4
0
  /**
   * Tests writing of request content when @src is set.
   *
   * @throws IOException I/O Exception
   */
  @Test
  public void writeFromResource() throws IOException {
    // Create a file form which will be read
    final IOFile file = new IOFile(Prop.TMP, Util.className(FnHttpTest.class));
    file.write(token("test"));

    // Request
    final HttpRequest req = new HttpRequest();
    req.payloadAttrs.put("src", file.url());
    req.payloadAttrs.put("method", "binary");
    // HTTP connection
    final FakeHttpConnection fakeConn = new FakeHttpConnection(new URL("http://www.test.com"));
    HttpClient.setRequestContent(fakeConn.getOutputStream(), req);

    // Delete file
    file.delete();

    assertEquals(fakeConn.out.toString(Strings.UTF8), "test");
  }
Esempio n. 5
0
 @Override
 public String toString() {
   return Util.className(this) + '[' + newDocs.inputs + ']';
 }