org.cactoos.text.Split Java Examples

The following examples show how to use org.cactoos.text.Split. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HtHeaders.java    From cactoos-http with MIT License 6 votes vote down vote up
/**
 * Ctor.
 * @param head Response head part
 */
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
public HtHeaders(final Input head) {
    super(() -> {
        final Map<String, List<String>> map = new HashMap<>();
        final ListOf<Text> texts = new ListOf<>(
            new Split(new TextOf(head), "\r\n")
        );
        for (final Text line : texts.subList(1, texts.size())) {
            final String[] parts = line.asString().split(":", 2);
            map.merge(
                new Lowered(
                    new Trimmed(new TextOf(parts[0])), Locale.ENGLISH
                ).asString(),
                new ListOf<>(
                    new Trimmed(new TextOf(parts[1])).asString()
                ),
                (first, second) -> new Joined<String>(first, second)
            );
        }
        return map;
    });
}
 
Example #2
Source File: OpsOf.java    From jpeek with MIT License 6 votes vote down vote up
@Override
public void visitMethodInsn(final int opcode,
    final String owner, final String mtd,
    final String dsc, final boolean itf) {
    super.visitMethodInsn(opcode, owner, mtd, dsc, itf);
    this.target.strict(1).addIf("ops").add("op");
    this.target
        .attr("code", "call")
        .add("name")
        .set(owner.replace("/", ".").concat(".").concat(mtd))
        .up().add("args");
    final Iterable<Text> args = new Split(
        new Sub(dsc, dsc.indexOf('(') + 1, dsc.indexOf(')')),
        ";"
    );
    for (final Text arg : args) {
        this.target.add("arg").attr("type", arg).set("?").up();
    }
    this.target.up().up().up();
}
 
Example #3
Source File: MediaTypes.java    From takes with MIT License 6 votes vote down vote up
/**
 * Ctor.
 * @param text Text to parse
 * @return List of media types
 */
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
private static SortedSet<MediaType> parse(final String text) {
    final SortedSet<MediaType> list = new TreeSet<>();
    final Iterable<Text> parts = new Split(
        new UncheckedText(new Lowered(text)),
        ","
    );
    for (final Text part : parts) {
        final String name = new UncheckedText(part).asString();
        if (!name.isEmpty()) {
            list.add(new MediaType(name));
        }
    }
    return list;
}
 
Example #4
Source File: ResponseOf.java    From takes with MIT License 6 votes vote down vote up
/**
 * Apply header to servlet response.
 * @param sresp Response
 * @param header Header
 */
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
private static void applyHeader(final HttpServletResponse sresp,
    final String header) {
    final Iterator<Text> split = new Split(header, ":").iterator();
    final UncheckedText name = new UncheckedText(new Trimmed(split.next()));
    final UncheckedText val = new UncheckedText(new Trimmed(split.next()));
    if (new Equality<Text>(
        new TextOf("set-cookie"),
        new Lowered(name)
    ).value()
    ) {
        for (final HttpCookie cck : HttpCookie.parse(header)) {
            sresp.addCookie(
                new Cookie(cck.getName(), cck.getValue())
            );
        }
    } else {
        sresp.setHeader(name.asString(), val.asString());
    }
}
 
Example #5
Source File: HtCookies.java    From cactoos-http with MIT License 5 votes vote down vote up
/**
 * Ctor.
 * @param rsp Response
 */
public HtCookies(final Input rsp) {
    super(() -> new Grouped<>(
        new Mapped<>(
            (Text entry) -> {
                final Iterable<Text> parts = new Split(entry, "=");
                if (new LengthOf(parts).intValue() != 2) {
                    throw new IllegalArgumentException(
                        "Incorrect HTTP Response cookie"
                    );
                }
                final Iterator<Text> iter = parts.iterator();
                return new MapEntry<>(
                    iter.next().asString(),
                    iter.next().asString()
                );
            },
            new Joined<>(
                new Mapped<>(
                    e -> new Split(e, ";\\s+"),
                    new HtHeaders(rsp).get("set-cookie")
                )
            )
        ),
        MapEntry::getKey,
        MapEntry::getValue
    ));
}
 
Example #6
Source File: CcPlain.java    From takes with MIT License 5 votes vote down vote up
@Override
public Identity decode(final byte[] bytes) throws IOException {
    final List<Text> parts = new ListOf<>(
        new Split(
            new TextOf(new BytesOf(bytes)), ";"
        )
    );
    final Map<String, String> map = new HashMap<>(parts.size());
    for (int idx = 1; idx < parts.size(); ++idx) {
        final List<Text> pair = new ListOf<>(
            new Split(parts.get(idx), "=")
        );
        try {
            map.put(
                new UncheckedText(pair.get(0)).asString(),
                CcPlain.decode(new UncheckedText(pair.get(1)).asString())
            );
        } catch (final IllegalArgumentException ex) {
            throw new DecodingException(ex);
        }
    }
    return new Identity.Simple(
        CcPlain.decode(
            new UncheckedText(parts.get(0)).asString()
        ), map
    );
}