All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bitbucket.rosseti.http.client.Growl Maven / Gradle / Ivy

package org.bitbucket.rosseti.http.client;

import org.bitbucket.javatek.regex.Capture;
import org.bitbucket.javatek.regex.Group;
import org.bitbucket.javatek.regex.Regex;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;

import javax.annotation.Nullable;

import static java.text.MessageFormat.format;

/**
 *
 */
final class Growl {
  private static final Regex GROWL_PATTERN = new Regex(
    "(?x)" +
      "(?(DEFINE)" +
      " (?              true | false             )" +
      " (?               [1-9] | (?: [1-9] \\d+)  )" +
      " " +
      " (? " +
      "     \\s* \\{ \\s* " +
      "       \\s* id         \\s* : \\s* \" (?     messagesform:growl) \"  \\s*" +
      " (?: , \\s* sticky     \\s* : \\s* (?    (?'boolean'))           \\s*)?" +
      " (?: , \\s* life       \\s* : \\s* (?      (?'number'))            \\s*)?" +
      " (?: , \\s* escape     \\s* : \\s* (?    (?'boolean'))           \\s*)?" +
      " (?: , \\s* keepAlive  \\s* : \\s* (? (?'boolean'))           \\s*)?" +
      "     , \\s* msgs       \\s* : \\s* (?'msgs')       \\s*" +
      "  ,? \\s* \\} \\s*" +
      " )" +
      " " +
      " (? " +
      "     \\s* \\[ \\s* " +
      "       (?: (?'msg') | (?: (?'msg') (, (?'msg'))+ ) )?" +
      "  ,? \\s* \\] \\s*" +
      " )" +
      " " +
      " (? " +
      "   \\s* \\{ \\s* " +
      "       \\s* summary    \\s* : \\s* \"  (?   (?: [^\"\\\\] | \\\\. | ([^\"\\\\] | \\\\.)+)?) \"  \\s* " +
      "     , \\s* detail     \\s* : \\s* \"  (?    (?: [^\"\\\\] | \\\\. | ([^\"\\\\] | \\\\.)+)?) \"  \\s* " +
      "     , \\s* severity   \\s* : \\s* '   (?  (?: [^'] | [^']+)?) '     \\s* " +
      "   \\s* \\} \\s* " +
      "  )" +
      ")" +
      "(?'growl')"
  );

  //////////////////////////////////////////////////////////////////////////////

  private final Capture capture;

  Growl(String html) {
    Element growlScript =
      Jsoup
        .parse(html)
        .selectFirst("script[id='messagesform:growl_s']");

    if (growlScript != null) {
      for (Capture capture : GROWL_PATTERN.capture(growlScript.html())) {
        // todo log capture
//        System.out.println(capture);
        this.capture = capture;
        return;
      }
    }

    // todo: put right exception class with trimming of long message
    throw new RuntimeException("Growl not found: " + html);
  }

  @Nullable
  String error() {
    Group msgs = capture.group("msgs");
    for (Group msg : msgs.groups("msg")) {
      String severity = msg.group("severity").value();
      if ("error".equals(severity)) {
        String summary = msg.group("summary").value();
        String detail = msg.group("detail").value();
        return
          !detail.isEmpty()
            ? format("{0} ({1})", summary, detail)
            : summary;
      }
    }
    return null;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy