Search in sources :

Example 16 with RegExp

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

the class JsonUtils method isStringWithIds.

/**
 * Whether string contains IDs separated by a comma
 *
 * @param text
 * @return
 */
public static boolean isStringWithIds(String text) {
    final String IDS_PATTERN = "^[0-9, ]+$";
    RegExp regExp = RegExp.compile(IDS_PATTERN);
    MatchResult matcher = regExp.exec(text);
    return (matcher != null);
}
Also used : RegExp(com.google.gwt.regexp.shared.RegExp) JsArrayString(com.google.gwt.core.client.JsArrayString) MatchResult(com.google.gwt.regexp.shared.MatchResult)

Example 17 with RegExp

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

the class TestJSONParserTabItem method draw.

public Widget draw() {
    Button sendMessageButton = new Button("Parse response");
    final FlexTable ft = new FlexTable();
    ft.setCellSpacing(15);
    int row = 0;
    ft.setText(row, 0, "Server response:");
    ft.setWidget(row, 1, returnedValue);
    row++;
    ft.setText(row, 0, "Callback name:");
    ft.setHTML(row, 1, "callbackPost");
    row++;
    ft.setWidget(row, 0, sendMessageButton);
    row++;
    sendMessageButton.addClickHandler(new ClickHandler() {

        @Override
        public void onClick(ClickEvent event) {
            String resp = returnedValue.getText();
            // trims the whitespace
            resp = resp.trim();
            // short comparing
            if (("callbackPost(null);").equalsIgnoreCase(resp)) {
                UiElements.generateInfo("Parser result", "Parsed value is: NULL");
            }
            // if starts with callbackName( and ends with ) or ); - wrapped, must be unwrapped
            RegExp re = RegExp.compile("^" + "callbackPost" + "\\((.*)\\)|\\);$");
            MatchResult result = re.exec(resp);
            if (result != null) {
                resp = result.getGroup(1);
            }
            UiElements.generateInfo("Unwrapped value", "Non-null unwrapped value is: " + resp);
            // if response = null - return null
            if (resp.equals("null")) {
                UiElements.generateInfo("Parser result", "Parsed value is: NULL");
            }
            // normal object
            JavaScriptObject jso = JsonUtils.parseJson(resp);
            BasicOverlayType basic = jso.cast();
            UiElements.generateInfo("Parser result", "Parsed result is: " + basic.getString());
        }
    });
    this.contentWidget.setWidget(ft);
    return getWidget();
}
Also used : ClickHandler(com.google.gwt.event.dom.client.ClickHandler) RegExp(com.google.gwt.regexp.shared.RegExp) JavaScriptObject(com.google.gwt.core.client.JavaScriptObject) ClickEvent(com.google.gwt.event.dom.client.ClickEvent) BasicOverlayType(cz.metacentrum.perun.webgui.model.BasicOverlayType) MatchResult(com.google.gwt.regexp.shared.MatchResult)

Example 18 with RegExp

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

the class AddLoginTabItem method draw.

public Widget draw() {
    titleWidget.setText("Add login");
    VerticalPanel vp = new VerticalPanel();
    vp.setSize("100%", "100%");
    final ExtendedTextBox userLogin = new ExtendedTextBox();
    final ListBox namespace = new ListBox();
    final CustomButton createLogin = TabMenu.getPredefinedButton(ButtonType.ADD, "Add login in selected namespace");
    final Label notice = new Label("Your login will be automatically generated.");
    notice.setVisible(false);
    // offer only available namespaces.
    ArrayList<String> logins = new ArrayList<String>();
    for (Attribute a : usersLogins) {
        logins.add(a.getFriendlyNameParameter());
    }
    for (String s : Utils.getSupportedPasswordNamespaces()) {
        if (!logins.contains(s)) {
            namespace.addItem(s.toUpperCase(), s);
        }
    }
    final ExtendedTextBox.TextBoxValidator loginValidator = new ExtendedTextBox.TextBoxValidator() {

        @Override
        public boolean validateTextBox() {
            if (userLogin.getTextBox().getValue().trim().isEmpty()) {
                userLogin.setError("Login can't be empty!");
                return false;
            }
            RegExp regExp = RegExp.compile(Utils.LOGIN_VALUE_MATCHER);
            boolean match = regExp.test(userLogin.getTextBox().getValue().trim());
            if (!match) {
                userLogin.setError("Invalid format!");
                return false;
            }
            if (userLogin.isProcessing() || userLogin.isHardError()) {
                return false;
            }
            userLogin.setOk();
            return true;
        }
    };
    userLogin.setValidator(loginValidator);
    final FlexTable layout = new FlexTable();
    layout.addStyleName("inputFormFlexTable");
    layout.setHTML(0, 0, "Namespace:");
    layout.setHTML(1, 0, "Login:");
    for (int i = 0; i < layout.getRowCount(); i++) {
        layout.getFlexCellFormatter().addStyleName(i, 0, "itemName");
    }
    layout.setWidget(0, 1, namespace);
    layout.setWidget(1, 1, userLogin);
    layout.getFlexCellFormatter().setColSpan(2, 0, 2);
    layout.setWidget(2, 0, notice);
    layout.getFlexCellFormatter().addStyleName(2, 0, "inputFormInlineComment");
    HTML help = new HTML("Login must<ul><li>start with lower-cased letter<li>be 2-15 characters long<li>consist only of<ul><li>lower-cased non-accented letters<li>digits<li>hyphens and underscores</ul></ul>");
    help.setVisible(false);
    layout.getFlexCellFormatter().setColSpan(3, 0, 2);
    layout.setWidget(3, 0, help);
    layout.getFlexCellFormatter().addStyleName(3, 0, "inputFormInlineComment");
    TabMenu menu = new TabMenu();
    menu.addWidget(createLogin);
    final TabItem tab = this;
    menu.addWidget(TabMenu.getPredefinedButton(ButtonType.CANCEL, "", new ClickHandler() {

        @Override
        public void onClick(ClickEvent event) {
            session.getTabManager().closeTab(tab, isRefreshParentOnClose());
        }
    }));
    // user can't add new login
    if (namespace.getItemCount() == 0) {
        vp.add(new HTML("<p><strong>You already have login in all supported namespaces!</strong></p>"));
        createLogin.setEnabled(false);
    } else {
        // user can add new login
        vp.add(layout);
        // check login availability
        userLogin.getTextBox().addKeyUpHandler(new KeyUpHandler() {

            @Override
            public void onKeyUp(KeyUpEvent keyUpEvent) {
                if (keyUpEvent.isDownArrow() || keyUpEvent.isUpArrow() || keyUpEvent.isLeftArrow() || keyUpEvent.isRightArrow()) {
                    // do not trigger when no text input
                    return;
                }
                final String value = userLogin.getTextBox().getValue().trim();
                // trigger new validation on checked input or if previously was hard error
                if ((!value.isEmpty() && RegExp.compile(Utils.LOGIN_VALUE_MATCHER).test(value)) || userLogin.isHardError()) {
                    new IsLoginAvailable(namespace.getValue(namespace.getSelectedIndex()), userLogin.getTextBox().getValue().trim(), new JsonCallbackEvents() {

                        @Override
                        public void onFinished(JavaScriptObject jso) {
                            if (value.equals(userLogin.getTextBox().getValue().trim())) {
                                BasicOverlayType bo = jso.cast();
                                userLogin.setProcessing(false);
                                if (!bo.getBoolean()) {
                                    userLogin.setHardError("Login is already in use!");
                                } else {
                                    userLogin.removeHardError();
                                    loginValidator.validateTextBox();
                                }
                            }
                        }

                        @Override
                        public void onLoadingStart() {
                            if (value.equals(userLogin.getTextBox().getValue().trim())) {
                                userLogin.removeHardError();
                                userLogin.setProcessing(true);
                            }
                        }

                        @Override
                        public void onError(PerunError error) {
                            if (value.equals(userLogin.getTextBox().getValue().trim())) {
                                if ("InvalidLoginException".equalsIgnoreCase(error.getName())) {
                                    userLogin.setProcessing(false);
                                    String text = error.getErrorInfo();
                                    text = text.split(":", 2)[1];
                                    text = (text == null || text.isEmpty()) ? error.getErrorInfo() : text;
                                    userLogin.setHardError(text);
                                } else {
                                    // generic error
                                    userLogin.setProcessing(false);
                                    userLogin.setHardError("Unable to check if login is available!");
                                }
                            }
                        }
                    }).retrieveData();
                }
            }
        });
        namespace.addChangeHandler(new ChangeHandler() {

            public void onChange(ChangeEvent changeEvent) {
                if (namespace.getSelectedValue().equals("einfra")) {
                    help.setVisible(true);
                } else {
                    help.setVisible(false);
                }
                if (namespace.getSelectedValue().equals("mu")) {
                    userLogin.getTextBox().setValue("");
                    userLogin.removeHardError();
                    userLogin.setOk();
                    userLogin.getTextBox().setEnabled(false);
                    notice.setVisible(true);
                } else {
                    userLogin.getTextBox().setEnabled(true);
                    notice.setVisible(false);
                    final String value = userLogin.getTextBox().getValue().trim();
                    // trigger new validation on checked input or if previously was hard error
                    if ((!value.isEmpty() && RegExp.compile(Utils.LOGIN_VALUE_MATCHER).test(value)) || userLogin.isHardError()) {
                        new IsLoginAvailable(namespace.getValue(namespace.getSelectedIndex()), userLogin.getTextBox().getValue().trim(), new JsonCallbackEvents() {

                            @Override
                            public void onFinished(JavaScriptObject jso) {
                                if (value.equals(userLogin.getTextBox().getValue().trim())) {
                                    BasicOverlayType bo = jso.cast();
                                    userLogin.setProcessing(false);
                                    if (!bo.getBoolean()) {
                                        userLogin.setError("Login is already in use!");
                                    } else {
                                        userLogin.removeHardError();
                                        loginValidator.validateTextBox();
                                    }
                                }
                            }

                            @Override
                            public void onLoadingStart() {
                                if (value.equals(userLogin.getTextBox().getValue().trim())) {
                                    userLogin.removeHardError();
                                    userLogin.setProcessing(true);
                                }
                            }

                            @Override
                            public void onError(PerunError error) {
                                if (value.equals(userLogin.getTextBox().getValue().trim())) {
                                    userLogin.setProcessing(false);
                                    userLogin.setHardError("Error while loading.");
                                }
                            }
                        }).retrieveData();
                    }
                }
            }
        });
        createLogin.addClickHandler(new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {
                if (namespace.getSelectedValue().equals("mu")) {
                    session.getTabManager().addTabToCurrentTab(new SelfPasswordTabItem(user, namespace.getValue(namespace.getSelectedIndex()), userLogin.getTextBox().getValue().trim(), SelfPasswordTabItem.Actions.CREATE));
                } else {
                    if (!loginValidator.validateTextBox())
                        return;
                    SetLogin request = new SetLogin(JsonCallbackEvents.disableButtonEvents(createLogin, new JsonCallbackEvents() {

                        @Override
                        public void onFinished(JavaScriptObject jso) {
                            session.getTabManager().addTabToCurrentTab(new SelfPasswordTabItem(user, namespace.getValue(namespace.getSelectedIndex()), userLogin.getTextBox().getValue().trim(), SelfPasswordTabItem.Actions.CREATE));
                        }
                    }));
                    request.setLogin(user, namespace.getValue(namespace.getSelectedIndex()), userLogin.getTextBox().getValue().trim());
                }
            }
        });
    }
    if (namespace.getSelectedValue().equals("mu")) {
        userLogin.getTextBox().setValue("");
        userLogin.removeHardError();
        userLogin.setOk();
        userLogin.getTextBox().setEnabled(false);
        notice.setVisible(true);
    } else if (namespace.getSelectedValue().equals("einfra")) {
        help.setVisible(true);
    }
    vp.add(menu);
    vp.setCellHorizontalAlignment(menu, HasHorizontalAlignment.ALIGN_RIGHT);
    this.contentWidget.setWidget(vp);
    return getWidget();
}
Also used : JsonCallbackEvents(cz.metacentrum.perun.webgui.json.JsonCallbackEvents) Attribute(cz.metacentrum.perun.webgui.model.Attribute) BasicOverlayType(cz.metacentrum.perun.webgui.model.BasicOverlayType) ArrayList(java.util.ArrayList) IsLoginAvailable(cz.metacentrum.perun.webgui.json.usersManager.IsLoginAvailable) CustomButton(cz.metacentrum.perun.webgui.widgets.CustomButton) RegExp(com.google.gwt.regexp.shared.RegExp) ExtendedTextBox(cz.metacentrum.perun.webgui.widgets.ExtendedTextBox) SetLogin(cz.metacentrum.perun.webgui.json.usersManager.SetLogin) TabMenu(cz.metacentrum.perun.webgui.widgets.TabMenu) TabItem(cz.metacentrum.perun.webgui.tabs.TabItem) JavaScriptObject(com.google.gwt.core.client.JavaScriptObject) PerunError(cz.metacentrum.perun.webgui.model.PerunError)

Example 19 with RegExp

use of com.google.gwt.regexp.shared.RegExp in project che by eclipse.

the class FuzzyMatches method fuzzyMatch.

public List<Match> fuzzyMatch(String word, String wordToMatch, boolean substringMatch) {
    RegExp regExp = regExpCache.get(word);
    if (regExp == null) {
        regExp = convertWordToRegExp(word);
        regExpCache.put(word, regExp);
    }
    MatchResult matchResult = regExp.exec(wordToMatch);
    if (matchResult != null) {
        return Collections.singletonList(new Match(matchResult.getIndex(), matchResult.getIndex() + matchResult.getGroup(0).length()));
    }
    if (substringMatch) {
        return FUZZY_SEPARATE.match(word, wordToMatch);
    } else {
        return FUZZY_CONTIGUOUS.match(word, wordToMatch);
    }
}
Also used : RegExp(com.google.gwt.regexp.shared.RegExp) MatchResult(com.google.gwt.regexp.shared.MatchResult)

Example 20 with RegExp

use of com.google.gwt.regexp.shared.RegExp in project che by eclipse.

the class OutputConsoleViewImpl method print.

@Override
public void print(final String text, boolean carriageReturn, String color) {
    if (this.carriageReturn) {
        Node lastChild = consoleLines.getElement().getLastChild();
        if (lastChild != null) {
            lastChild.removeFromParent();
        }
    }
    this.carriageReturn = carriageReturn;
    final SafeHtml colorOutput = new SafeHtml() {

        @Override
        public String asString() {
            if (Strings.isNullOrEmpty(text)) {
                return " ";
            }
            for (final Pair<RegExp, String> pair : output2Color) {
                final MatchResult matcher = pair.first.exec(text);
                if (matcher != null) {
                    return text.replaceAll(matcher.getGroup(1), "<span style=\"color: " + pair.second + "\">" + matcher.getGroup(1) + "</span>");
                }
            }
            return text;
        }
    };
    PreElement pre = DOM.createElement("pre").cast();
    pre.setInnerSafeHtml(colorOutput);
    if (color != null) {
        pre.getStyle().setColor(color);
    }
    consoleLines.getElement().appendChild(pre);
    followOutput();
}
Also used : RegExp(com.google.gwt.regexp.shared.RegExp) SafeHtml(com.google.gwt.safehtml.shared.SafeHtml) PreElement(com.google.gwt.dom.client.PreElement) Node(com.google.gwt.dom.client.Node) MatchResult(com.google.gwt.regexp.shared.MatchResult)

Aggregations

RegExp (com.google.gwt.regexp.shared.RegExp)34 MatchResult (com.google.gwt.regexp.shared.MatchResult)23 JavaScriptObject (com.google.gwt.core.client.JavaScriptObject)7 JsonCallbackEvents (cz.metacentrum.perun.webgui.json.JsonCallbackEvents)4 BasicOverlayType (cz.metacentrum.perun.webgui.model.BasicOverlayType)4 IsLoginAvailable (cz.metacentrum.perun.webgui.json.usersManager.IsLoginAvailable)3 SetLogin (cz.metacentrum.perun.webgui.json.usersManager.SetLogin)3 TabItem (cz.metacentrum.perun.webgui.tabs.TabItem)3 CustomButton (cz.metacentrum.perun.webgui.widgets.CustomButton)3 ArrayList (java.util.ArrayList)3 BlurHandler (com.google.gwt.event.dom.client.BlurHandler)2 ClickEvent (com.google.gwt.event.dom.client.ClickEvent)2 ClickHandler (com.google.gwt.event.dom.client.ClickHandler)2 KeyDownHandler (com.google.gwt.event.dom.client.KeyDownHandler)2 GetEntityById (cz.metacentrum.perun.webgui.json.GetEntityById)2 CreatePassword (cz.metacentrum.perun.webgui.json.usersManager.CreatePassword)2 GenerateAccount (cz.metacentrum.perun.webgui.json.usersManager.GenerateAccount)2 PerunError (cz.metacentrum.perun.webgui.model.PerunError)2 ExtendedTextBox (cz.metacentrum.perun.webgui.widgets.ExtendedTextBox)2 TabMenu (cz.metacentrum.perun.webgui.widgets.TabMenu)2