Search in sources :

Example 16 with MatchResult

use of com.google.gwt.regexp.shared.MatchResult in project webprotege by protegeproject.

the class MentionParser method parseMentions.

/**
 * Parses a list of Mentions from the given text.
 * @param text The text
 * @return The list of parsed Mentions
 */
@Nonnull
public List<ParsedMention> parseMentions(@Nonnull String text) {
    checkNotNull(text);
    List<ParsedMention> parsedMentions = new ArrayList<>();
    MatchResult matchResult = pattern.exec(text);
    while (matchResult != null) {
        ParsedMention mention = parseMentionFromMatch(matchResult);
        parsedMentions.add(mention);
        pattern.setLastIndex(matchResult.getIndex() + 1);
        matchResult = pattern.exec(text);
    }
    return parsedMentions;
}
Also used : ArrayList(java.util.ArrayList) MatchResult(com.google.gwt.regexp.shared.MatchResult) Nonnull(javax.annotation.Nonnull)

Example 17 with MatchResult

use of com.google.gwt.regexp.shared.MatchResult in project perun by CESNET.

the class JsonClient method parseResponse.

/**
 * Parses the response to an object.
 *
 * @param resp JSON string
 * @return
 */
protected JavaScriptObject parseResponse(String callbackName, String resp) {
    // trims the whitespace
    resp = resp.trim();
    // short comparing
    if ((callbackName + "(null);").equalsIgnoreCase(resp)) {
        return null;
    }
    // if starts with callbackName( and ends with ) or ); - wrapped, must be unwrapped
    RegExp re = RegExp.compile("^" + callbackName + "\\((.*)\\)|\\);$");
    MatchResult result = re.exec(resp);
    if (result != null) {
        resp = result.getGroup(1);
    }
    // if response = null - return null
    if (resp.equals("null")) {
        return null;
    }
    // normal object
    JavaScriptObject jso = JsonUtils.parseJson(resp);
    return jso;
}
Also used : RegExp(com.google.gwt.regexp.shared.RegExp) JavaScriptObject(com.google.gwt.core.client.JavaScriptObject) MatchResult(com.google.gwt.regexp.shared.MatchResult)

Example 18 with MatchResult

use of com.google.gwt.regexp.shared.MatchResult in project perun by CESNET.

the class JsonPostClient method parseResponse.

/**
 * Parses the responce to an object.
 *
 * @param resp JSON string
 * @return
 */
protected JavaScriptObject parseResponse(String callbackName, String resp) {
    // trims the whitespace
    resp = resp.trim();
    // short comparing
    if ((callbackName + "(null);").equalsIgnoreCase(resp)) {
        return null;
    }
    // if starts with callbackName( and ends with ) or ); - wrapped, must be unwrapped
    RegExp re = RegExp.compile("^" + callbackName + "\\((.*)\\)|\\);$");
    MatchResult result = re.exec(resp);
    if (result != null) {
        resp = result.getGroup(1);
    }
    // if response = null - return null
    if (resp.equals("null")) {
        return null;
    }
    // normal object
    JavaScriptObject jso = JsonUtils.parseJson(resp);
    return jso;
}
Also used : RegExp(com.google.gwt.regexp.shared.RegExp) JavaScriptObject(com.google.gwt.core.client.JavaScriptObject) MatchResult(com.google.gwt.regexp.shared.MatchResult)

Example 19 with MatchResult

use of com.google.gwt.regexp.shared.MatchResult in project perun by CESNET.

the class Utils method parseCommonName.

/**
 * Try to parse rawName to keys: "titleBefore" "firstName" "lastName" "titleAfter"
 *
 * If rawName is null or empty, return map with empty values of all keys.
 *
 * Parsing procedure:
 * 1] prepare array of parts by replacing all characters "," and "_" by spaces
 * 2] change all sequence of invisible characters (space, tabulator etc.) to one space
 * 3] one by one try to parsing parts from array
 *  - A] try to find all titleBefore parts
 *  - B] try to find one firstName part
 *  - C] try to find all lastName parts
 *  - D] if the rest is not lastName so save it to the title after
 *
 * Example of parsing rawName:
 * 1] rawName = "Mgr. et Mgr.    Petr_Jiri R. Sojka, Ph.D., CSc."
 * 2] convert all ',' and '_' to spaces: rawName = "Mgr. et Mgr.    Petr Jiri R. Sojka  Ph.D.  CSc."
 * 3] convert more than 1 invisible char to 1 space: rawName = "Mgr. et Mgr. Petr Jiri R. Sojka Ph.D. CSc."
 * 4] parse string to array of parts by space: ArrayOfParts= ["Mgr.","et","Mgr.","Petr","Jiri","R.","Sojka","Ph.D.","CSc."]
 * 5] first fill everything what can be in title before: titleBefore="Mgr. et Mgr."
 * 6] then fill everything what can be in first name (maximum 1 part): firstName="Petr"
 * 7] then fill everything what can be in last name: lastName="Jiri R. Sojka"
 * 8] everything else put to the title after: titleAfter="Ph.D. CSc."
 * 9] put these variables to map like key=value, for ex.: Map["titleBefore"="Mgr. et Mgr.",firstName="Petr", ... ] and return this map
 *
 * @param rawName
 * @return map string to string where are 4 keys (titleBefore,titleAfter,firstName and lastName) with their values (value can be null)
 */
public static Map<String, String> parseCommonName(String rawName) {
    // prepare variables and map
    Map<String, String> parsedName = new HashMap<String, String>();
    String titleBefore = "";
    String firstName = "";
    String lastName = "";
    String titleAfter = "";
    // if raw name is null or empty, skip this part and only return map with null values for keys
    if (rawName != null && !rawName.isEmpty()) {
        // all characters ',' replace by ' ' for rawName
        rawName = rawName.replaceAll(",", " ").trim();
        // all characters '_' replace by ' ' for rawName
        rawName = rawName.replaceAll("_", " ").trim();
        // replace all invisible chars in row for
        rawName = rawName.replaceAll("\\s+", " ").trim();
        // split parts by space
        String[] nameParts = rawName.split(" ");
        // if length of nameParts is 1, save it to the lastName
        if (nameParts.length == 1) {
            lastName = nameParts[0];
        // if length of nameParts is more than 1, try to choose which part belong to which value
        } else {
            // variables for states
            boolean titleBeforeDone = false;
            boolean firstNameDone = false;
            boolean lastNameDone = false;
            // for every part try to get which one it is
            for (int i = 0; i < nameParts.length; i++) {
                String part = nameParts[i];
                // trim this value (remove spaces before and after string)
                part = part.trim();
                // if titleBeforeDone is false, this string can be title before
                if (!titleBeforeDone) {
                    MatchResult titleBeforeMatcher = titleBeforePattern.exec(part);
                    // if title before matches
                    if (titleBeforeMatcher != null) {
                        // add space if this title is not first title before
                        if (titleBefore.isEmpty())
                            titleBefore += part;
                        else
                            titleBefore += " " + part;
                        // go on next part
                        continue;
                    } else {
                        // this is not title before, so end part of title before and go next
                        titleBeforeDone = true;
                    }
                }
                // if firstNameDone is false, this string can be first name
                if (!firstNameDone) {
                    MatchResult firstNameMatcher = firstNamePattern.exec(part);
                    // if first name matches
                    if (firstNameMatcher != null) {
                        // first name can be only one
                        firstName = part;
                        // go on next part
                        firstNameDone = true;
                        continue;
                    }
                    // if this is not firstName skip firstName because only first word after titleBefore can be firstName
                    firstNameDone = true;
                }
                // if lastNameDone is false, this string can be lastName
                if (!lastNameDone) {
                    MatchResult lastNameMatcher = lastNamePattern.exec(part);
                    // if last name matches
                    if (lastNameMatcher != null) {
                        // add space if this name is not first last name
                        if (lastName.isEmpty())
                            lastName += part;
                        else
                            lastName += " " + part;
                        // go on next part
                        continue;
                    // if last name not matches
                    } else {
                        // because last name can't be empty, save this part to lastName even if not matches
                        if (lastName.isEmpty()) {
                            lastName = part;
                            lastNameDone = true;
                            // go on next part
                            continue;
                        } else {
                            // if there is already something in lastName, go on title after
                            lastNameDone = true;
                        }
                    }
                }
                // rest of parts if lastName exists go to the title after
                if (lastNameDone) {
                    // add space if this is not first title after
                    if (titleAfter.isEmpty())
                        titleAfter += part;
                    else
                        titleAfter += " " + part;
                }
            }
        }
    }
    // empty string means null, add variables to map
    if (titleBefore.isEmpty())
        titleBefore = null;
    parsedName.put("titleBefore", titleBefore);
    if (firstName.isEmpty())
        firstName = null;
    parsedName.put("firstName", firstName);
    if (lastName.isEmpty())
        lastName = null;
    parsedName.put("lastName", lastName);
    if (titleAfter.isEmpty())
        titleAfter = null;
    parsedName.put("titleAfter", titleAfter);
    return parsedName;
}
Also used : MatchResult(com.google.gwt.regexp.shared.MatchResult)

Example 20 with MatchResult

use of com.google.gwt.regexp.shared.MatchResult in project perun by CESNET.

the class Utils method clearFederationCookies.

/**
 * Clear all cookies provided by federation IDP in user's browser.
 */
public static void clearFederationCookies() {
    final String SHIBBOLETH_COOKIE_FORMAT = "^_shib.+$";
    // retrieves all the cookies
    Collection<String> cookies = Cookies.getCookieNames();
    // regexp
    RegExp regExp = RegExp.compile(SHIBBOLETH_COOKIE_FORMAT);
    for (String cookie : cookies) {
        // shibboleth cookie?
        MatchResult matcher = regExp.exec(cookie);
        // equivalent to regExp.test(inputStr);
        boolean matchFound = (matcher != null);
        if (matchFound) {
            // remove it
            Cookies.removeCookieNative(cookie, "/");
        }
    }
}
Also used : RegExp(com.google.gwt.regexp.shared.RegExp) MatchResult(com.google.gwt.regexp.shared.MatchResult)

Aggregations

MatchResult (com.google.gwt.regexp.shared.MatchResult)47 RegExp (com.google.gwt.regexp.shared.RegExp)23 ArrayList (java.util.ArrayList)7 JavaScriptObject (com.google.gwt.core.client.JavaScriptObject)3 ProjectId (edu.stanford.bmir.protege.web.shared.project.ProjectId)3 JsArrayString (com.google.gwt.core.client.JsArrayString)1 Node (com.google.gwt.dom.client.Node)1 PreElement (com.google.gwt.dom.client.PreElement)1 ClickEvent (com.google.gwt.event.dom.client.ClickEvent)1 ClickHandler (com.google.gwt.event.dom.client.ClickHandler)1 SafeHtml (com.google.gwt.safehtml.shared.SafeHtml)1 BasicOverlayType (cz.metacentrum.perun.webgui.model.BasicOverlayType)1 ItemTexts (cz.metacentrum.perun.webgui.model.ItemTexts)1 AutoCompletionChoice (edu.stanford.bmir.gwtcodemirror.client.AutoCompletionChoice)1 AutoCompletionResult (edu.stanford.bmir.gwtcodemirror.client.AutoCompletionResult)1 EditorPosition (edu.stanford.bmir.gwtcodemirror.client.EditorPosition)1 CollectionId (edu.stanford.bmir.protege.web.shared.collection.CollectionId)1 CollectionItem (edu.stanford.bmir.protege.web.shared.collection.CollectionItem)1 FormId (edu.stanford.bmir.protege.web.shared.form.FormId)1 GetUserIdCompletionsAction (edu.stanford.bmir.protege.web.shared.itemlist.GetUserIdCompletionsAction)1