Java Code Examples for elemental.json.JsonObject#getString()

The following examples show how to use elemental.json.JsonObject#getString() . 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: ExecuteJavaScriptProcessor.java    From flow with Apache License 2.0 6 votes vote down vote up
private boolean isVirtualChildAwaitingInitialization(StateNode node) {
    if (node.getDomNode() != null
            || node.getTree().getNode(node.getId()) == null) {
        return false;
    }
    if (node.getMap(NodeFeatures.ELEMENT_DATA)
            .hasPropertyValue(NodeProperties.PAYLOAD)) {
        Object value = node.getMap(NodeFeatures.ELEMENT_DATA)
                .getProperty(NodeProperties.PAYLOAD).getValue();
        if (value instanceof JsonObject) {
            JsonObject object = (JsonObject) value;
            String type = object.getString(NodeProperties.TYPE);
            return NodeProperties.INJECT_BY_ID.equals(type)
                    || NodeProperties.TEMPLATE_IN_TEMPLATE.equals(type);
        }
    }
    return false;
}
 
Example 2
Source File: UidlWriterTest.java    From flow with Apache License 2.0 6 votes vote down vote up
private void assertInlineDependencies(List<JsonObject> inlineDependencies) {
    assertThat("Should have an inline dependency", inlineDependencies,
            hasSize(1));
    assertThat("Eager dependencies should not have urls",
            inlineDependencies.stream()
                    .filter(json -> json.hasKey(Dependency.KEY_URL))
                    .collect(Collectors.toList()),
            hasSize(0));

    JsonObject inlineDependency = inlineDependencies.get(0);

    String url = inlineDependency.getString(Dependency.KEY_CONTENTS);
    assertEquals("inline.css", url);
    assertEquals(Dependency.Type.STYLESHEET, Dependency.Type
            .valueOf(inlineDependency.getString(Dependency.KEY_TYPE)));
}
 
Example 3
Source File: UidlRequestHandler.java    From flow with Apache License 2.0 6 votes vote down vote up
private String removeHashInChange(JsonArray change) {
    if (change.length() < 3
            || !change.get(2).getType().equals(JsonType.ARRAY)) {
        return null;
    }
    JsonArray value = change.getArray(2);
    if (value.length() < 2
            || !value.get(1).getType().equals(JsonType.OBJECT)) {
        return null;
    }
    JsonObject location = value.getObject(1);
    if (!location.hasKey(LOCATION)) {
        return null;
    }
    String url = location.getString(LOCATION);
    Matcher match = URL_PATTERN.matcher(url);
    if (match.find()) {
        location.put(LOCATION, match.group(1));
    }
    return url;
}
 
Example 4
Source File: FrontendUtils.java    From flow with Apache License 2.0 6 votes vote down vote up
private static CssImportData createCssData(JsonObject object) {
    String value = null;
    String id = null;
    String include = null;
    String themeFor = null;
    if (object.hasKey("value")) {
        value = object.getString("value");
    }
    if (object.hasKey("id")) {
        id = object.getString("id");
    }
    if (object.hasKey("include")) {
        include = object.getString("include");
    }
    if (object.hasKey("themeFor")) {
        themeFor = object.getString("themeFor");
    }
    return new CssImportData(value, id, include, themeFor);
}
 
Example 5
Source File: TaskRunNpmInstall.java    From flow with Apache License 2.0 6 votes vote down vote up
/**
 * Updates the local hash to node_modules.
 * <p>
 * This is for handling updated package to the code repository by another
 * developer as then the hash is updated and we may just be missing one
 * module.
 */
private void updateLocalHash() {
    try {
        final JsonObject vaadin = packageUpdater.getPackageJson()
                .getObject(VAADIN_DEP_KEY);
        if (vaadin == null) {
            packageUpdater.log().warn("No vaadin object in package.json");
            return;
        }
        final String hash = vaadin.getString(HASH_KEY);

        final JsonObject localHash = Json.createObject();
        localHash.put(HASH_KEY, hash);

        final File localHashFile = getLocalHashFile();
        FileUtils.forceMkdirParent(localHashFile);
        String content = stringify(localHash, 2) + "\n";
        FileUtils.writeStringToFile(localHashFile, content, UTF_8.name());

    } catch (IOException e) {
        packageUpdater.log().warn("Failed to update node_modules hash.", e);
    }
}
 
Example 6
Source File: VersionsJsonFilter.java    From flow with Apache License 2.0 5 votes vote down vote up
private boolean isUserChanged(String key, JsonObject vaadinDep,
        JsonObject dependencies) {
    if (vaadinDep.hasKey(key)) {
        FrontendVersion vaadin = new FrontendVersion(
                vaadinDep.getString(key));
        FrontendVersion dep = new FrontendVersion(
                dependencies.getString(key));
        return !vaadin.isEqualTo(dep);
    }
    // User changed if not in vaadin dependency
    return true;
}
 
Example 7
Source File: NodeUpdater.java    From flow with Apache License 2.0 5 votes vote down vote up
int addDependency(JsonObject json, String key, String pkg, String version) {
    Objects.requireNonNull(json, "Json object need to be given");
    Objects.requireNonNull(key, "Json sub object needs to be give.");
    Objects.requireNonNull(pkg, "dependency package needs to be defined");

    JsonObject vaadinDeps = json.getObject(VAADIN_DEP_KEY);
    if (!json.hasKey(key)) {
        json.put(key, Json.createObject());
    }
    json = json.get(key);
    vaadinDeps = vaadinDeps.getObject(key);

    if (vaadinDeps.hasKey(pkg)) {
        if (version == null) {
            version = vaadinDeps.getString(pkg);
        }
        return handleExistingVaadinDep(json, pkg, version, vaadinDeps);
    } else {
        vaadinDeps.put(pkg, version);
        if (!json.hasKey(pkg) || new FrontendVersion(version)
                .isNewerThan(toVersion(json, pkg))) {
            json.put(pkg, version);
            log().debug("Added \"{}\": \"{}\" line.", pkg, version);
            return 1;
        }
    }
    return 0;
}
 
Example 8
Source File: ComboBoxMultiselectConnector.java    From vaadin-combobox-multiselect with Apache License 2.0 5 votes vote down vote up
private void updateSuggestions(final int start, final int end) {
    for (int i = start; i < end; ++i) {
        JsonObject row = getDataSource().getRow(i);
        if (row == null) {
            getDataSource().ensureAvailability(start, end);
            row = getDataSource().getRow(i);
        }
        
        if (row != null) {
            final String key = getRowKey(row);

            final String caption = row.getString(DataCommunicatorConstants.NAME);
            final String style = row.getString(ComboBoxMultiselectConstants.STYLE);
            final String untranslatedIconUri = row.getString(ComboBoxMultiselectConstants.ICON);
            final boolean checked = row.getString(CHECKED) != null ? Boolean.TRUE : Boolean.FALSE;
            
            final ComboBoxMultiselectSuggestion suggestion = getWidget().new ComboBoxMultiselectSuggestion(key, caption,
                                                                                                           style, untranslatedIconUri);
            suggestion.setChecked(checked);
            if (checked) {
                getWidget().selectedOptionKeys.add(key);
            }
            
            getWidget().currentSuggestions.add(suggestion);
        } else {
            // there is not enough options to fill the page
            return;
        }
    }
}
 
Example 9
Source File: TreeChangeProcessor.java    From flow with Apache License 2.0 5 votes vote down vote up
private static MapProperty findProperty(JsonObject change, StateNode node) {
    int nsId = (int) change.getNumber(JsonConstants.CHANGE_FEATURE);
    NodeMap map = node.getMap(nsId);
    String key = change.getString(JsonConstants.CHANGE_MAP_KEY);

    return map.getProperty(key);
}
 
Example 10
Source File: BootstrapHandler.java    From flow with Apache License 2.0 5 votes vote down vote up
private Element createDependencyElement(BootstrapContext context,
        JsonObject dependencyJson) {
    String type = dependencyJson.getString(Dependency.KEY_TYPE);
    if (Dependency.Type.contains(type)) {
        Dependency.Type dependencyType = Dependency.Type.valueOf(type);
        return createDependencyElement(context.getUriResolver(),
                LoadMode.INLINE, dependencyJson, dependencyType);
    }
    return Jsoup.parse(
            dependencyJson.getString(Dependency.KEY_CONTENTS), "",
            Parser.xmlParser());
}
 
Example 11
Source File: BasicElementStateProvider.java    From flow with Apache License 2.0 5 votes vote down vote up
@Override
public void visit(StateNode node, NodeVisitor visitor) {
    Element element = Element.get(node);
    ElementData data = node.getFeature(ElementData.class);
    JsonValue payload = data.getPayload();

    boolean visitDescendants;
    if (payload instanceof JsonObject) {
        JsonObject object = (JsonObject) payload;
        String type = object.getString(NodeProperties.TYPE);
        if (NodeProperties.IN_MEMORY_CHILD.equals(type)) {
            visitDescendants = visitor
                    .visit(NodeVisitor.ElementType.VIRTUAL, element);
        } else if (NodeProperties.INJECT_BY_ID.equals(type)
                || NodeProperties.TEMPLATE_IN_TEMPLATE.equals(type)) {
            visitDescendants = visitor.visit(
                    NodeVisitor.ElementType.VIRTUAL_ATTACHED, element);
        } else {
            throw new IllegalStateException(
                    "Unexpected payload type : " + type);
        }
    } else if (payload == null) {
        visitDescendants = visitor.visit(NodeVisitor.ElementType.REGULAR,
                element);
    } else {
        throw new IllegalStateException(
                "Unexpected payload in element data : " + payload.toJson());
    }

    if (visitDescendants) {
        visitDescendants(element, visitor);

        element.getShadowRoot().ifPresent(root -> root.accept(visitor));
    }
}
 
Example 12
Source File: ComboBoxMultiselectConnector.java    From vaadin-combobox-multiselect with Apache License 2.0 5 votes vote down vote up
private void updateSuggestions(final int start, final int end) {
    for (int i = start; i < end; ++i) {
        JsonObject row = getDataSource().getRow(i);
        if (row == null) {
            getDataSource().ensureAvailability(start, end);
            row = getDataSource().getRow(i);
        }
        
        if (row != null) {
            final String key = getRowKey(row);

            final String caption = row.getString(DataCommunicatorConstants.NAME);
            final String style = row.getString(ComboBoxMultiselectConstants.STYLE);
            final String untranslatedIconUri = row.getString(ComboBoxMultiselectConstants.ICON);
            final boolean checked = row.getString(CHECKED) != null ? Boolean.TRUE : Boolean.FALSE;
            
            final ComboBoxMultiselectSuggestion suggestion = getWidget().new ComboBoxMultiselectSuggestion(key, caption,
                                                                                                           style, untranslatedIconUri);
            suggestion.setChecked(checked);
            if (checked) {
                getWidget().selectedOptionKeys.add(key);
            }
            
            getWidget().currentSuggestions.add(suggestion);
        } else {
            // there is not enough options to fill the page
            return;
        }
    }
}
 
Example 13
Source File: MapSyncRpcHandlerTestServlet.java    From flow with Apache License 2.0 5 votes vote down vote up
@Override
protected Optional<Runnable> handleNode(StateNode node,
        JsonObject invocationJson) {
    String property = invocationJson
            .getString(JsonConstants.RPC_PROPERTY);
    if (property.equals("value")) {
        // Replace 'value' property with fake unsync property
        invocationJson.put(JsonConstants.RPC_PROPERTY, "foo");
    }
    return super.handleNode(node, invocationJson);
}
 
Example 14
Source File: NodeUpdater.java    From flow with Apache License 2.0 4 votes vote down vote up
private static FrontendVersion toVersion(JsonObject json, String key) {
    return new FrontendVersion(json.getString(key));
}
 
Example 15
Source File: CubaSideMenuWidget.java    From cuba with Apache License 2.0 4 votes vote down vote up
protected void addItems(JsonArray items, HasWidgets container) {
    for (int i = 0; i < items.length(); i++) {
        JsonObject itemJson = items.getObject(i);

        Icon icon = null;
        String iconId = itemJson.getString("icon");
        if (menuItemIconSupplier != null && iconId != null) {
            icon = menuItemIconSupplier.apply(iconId);
        }

        boolean captionAsHtml = false;
        if (itemJson.hasKey("captionAsHtml")) {
            captionAsHtml = itemJson.getBoolean("captionAsHtml");
        }

        MenuItemWidget menuItemWidget = new MenuItemWidget(this,
                itemJson.getString("id"),
                icon,
                itemJson.getString("styleName"),
                itemJson.getString("caption"),
                captionAsHtml);

        menuItemWidget.setDescription(itemJson.getString("description"));
        menuItemWidget.setCubaId(itemJson.getString("cubaId"));
        menuItemWidget.setBadgeText(itemJson.getString("badgeText"));

        container.add(menuItemWidget);

        JsonArray childrenItemsJson = itemJson.getArray("children");
        if (childrenItemsJson != null) {
            MenuContainerWidget menuContainerWidget = new MenuContainerWidget(this, menuItemWidget);
            addItems(childrenItemsJson, menuContainerWidget);

            container.add(menuContainerWidget);

            menuItemWidget.setSubMenu(menuContainerWidget);

            if (itemJson.hasKey("expanded")
                    && itemJson.getBoolean("expanded")) {
                menuContainerWidget.setExpanded(true);
            }
        }
    }
}
 
Example 16
Source File: BootstrapHandler.java    From flow with Apache License 2.0 4 votes vote down vote up
private void appendNpmBundle(Element head, VaadinService service,
        BootstrapContext context) throws IOException {
    String content = FrontendUtils.getStatsAssetsByChunkName(service);
    if (content == null) {
        StringBuilder message = new StringBuilder(
                "The stats file from webpack (stats.json) was not found.\n");
        if (service.getDeploymentConfiguration().isProductionMode()) {
            message.append(
                    "The application is running in production mode.");
            message.append(
                    "Verify that build-frontend task has executed successfully and that stats.json is on the classpath.");
            message.append(
                    "Or switch application to development mode.");
        } else if (!service.getDeploymentConfiguration()
                .enableDevServer()) {
            message.append(
                    "Dev server is disabled for the application.");
            message.append(
                    "Verify that build-frontend task has executed successfully and that stats.json is on the classpath.");
        } else {
            message.append(
                    "This typically mean that you have started the application without executing the 'prepare-frontend' Maven target.\n");
            message.append(
                    "If you are using Spring Boot and are launching the Application class directly, ");
            message.append(
                    "you need to run \"mvn install\" once first or launch the application using \"mvn spring-boot:run\"");
        }
        throw new IOException(message.toString());
    }
    JsonObject chunks = Json.parse(content);
    for (String key : getChunkKeys(chunks)) {
        String chunkName;
        if (chunks.get(key).getType().equals(JsonType.ARRAY)) {
            chunkName = getArrayChunkName(chunks, key);
        } else {
            chunkName = chunks.getString(key);
        }
        Element script = createJavaScriptElement(
                "./" + VAADIN_MAPPING + chunkName, false);
        head.appendChild(script.attr("type", "module")
                .attr("data-app-id",
                        context.getUI().getInternals().getAppId())
                // Fixes basic auth in Safari #6560
                .attr("crossorigin", true));
    }
}
 
Example 17
Source File: BundleParser.java    From flow with Apache License 2.0 4 votes vote down vote up
private static String getSourceFromObject(JsonObject module,
        String fileName) {
    String source = null;
    if (validKey(module, MODULES, ARRAY)) {
        source = getSourceFromArray(module.getArray(MODULES), fileName);
    }
    if (source == null && validKey(module, CHUNKS, ARRAY)) {
        source = getSourceFromArray(module.getArray(CHUNKS), fileName);
    }
    if (source == null && validKey(module, NAME, STRING)
            && validKey(module, SOURCE, STRING)) {
        String name = module.getString(NAME);

        // append `.js` extension if not yet as webpack does
        fileName = fileName.replaceFirst("(\\.js|)$", ".js");

        String alternativeFileName = fileName
                // Replace frontend part since webpack entry-point is
                // already in the frontend folder
                .replaceFirst("^(\\./)frontend/", "$1")
                // Replace the flow frontend protocol
                .replaceFirst("^frontend://", ".");

        // For polymer templates inside add-ons we will not find the sources
        // using ./ as the actual path contains
        // "node_modules/@vaadin/flow-frontend/" instead of "./"
        // "target/flow-frontend/" instead of "./"
        if (name.contains(FLOW_NPM_PACKAGE_NAME) ||
                name.contains(DEAULT_FLOW_RESOURCES_FOLDER)) {
            alternativeFileName = alternativeFileName.replaceFirst("\\./",
                    "");
        }

        // Remove query-string used by webpack modules like babel (e.g
        // ?babel-target=es6)
        name = name.replaceFirst("\\?.+$", "");

        // Do check on the original fileName and the alternative one
        if (name.endsWith(fileName) || name.endsWith(alternativeFileName)) {
            source = module.getString(SOURCE);
        }
    }
    return source;
}
 
Example 18
Source File: LockDevDepVersionsMojo.java    From flow with Apache License 2.0 4 votes vote down vote up
private void addDependency(JsonObject target, String name, JsonObject dep) {
    if (dep.hasKey(VERSION)) {
        String version = dep.getString(VERSION);
        target.put(name, version);
    }
}
 
Example 19
Source File: TaskUpdatePackages.java    From flow with Apache License 2.0 3 votes vote down vote up
/**
 * Compares vaadin-shrinkwrap dependency version from the
 * {@code dependencies} object with the current vaadin-shrinkwrap version
 * (retrieved from various sources like package.json, package-lock.json).
 * Removes package-lock.json file and node_modules,
 * target/frontend/node_modules folders in case the versions are different.
 *
 * @param dependencies
 *            dependencies object with the vaadin-shrinkwrap version
 * @throws IOException
 */
private boolean ensureReleaseVersion(JsonObject dependencies)
        throws IOException {
    String shrinkWrapVersion = null;
    if (dependencies.hasKey(SHRINK_WRAP)) {
        shrinkWrapVersion = dependencies.getString(SHRINK_WRAP);
    }

    return Objects.equals(shrinkWrapVersion, getCurrentShrinkWrapVersion());
}
 
Example 20
Source File: AbstractGridExtensionConnector.java    From GridExtensionPack with Apache License 2.0 2 votes vote down vote up
/**
 * Gets a row key from parent connector for mapping client-side rows to
 * server-side items.
 * 
 * @param row
 *            row object
 * @return row key
 */
public String getRowKey(JsonObject row) {
	return row.getString(DataCommunicatorConstants.KEY);
}