Search in sources :

Example 1 with AngularRoute

use of jmri.server.web.spi.AngularRoute in project JMRI by JMRI.

the class JsonManifest method initialize.

private synchronized void initialize() {
    if (!this.initialized) {
        // NOI18N
        Set<File> manifests = FileUtil.findFiles("manifest.json", "web");
        ObjectMapper mapper = new ObjectMapper();
        manifests.forEach((manifest) -> {
            try {
                JsonNode root = mapper.readTree(manifest);
                root.path("scripts").forEach((script) -> {
                    if (!this.scripts.contains(script.asText())) {
                        this.scripts.add(script.asText());
                    }
                });
                root.path("styles").forEach((style) -> {
                    if (!this.styles.contains(style.asText())) {
                        this.styles.add(style.asText());
                    }
                });
                root.path("dependencies").forEach((dependency) -> {
                    if (!this.dependencies.contains(dependency.asText())) {
                        this.dependencies.add(dependency.asText());
                    }
                });
                root.path("navigation").forEach((navigation) -> {
                    JsonMenuItem menuItem = new JsonMenuItem(navigation);
                    if (!this.menuItems.contains(menuItem)) {
                        this.menuItems.add(menuItem);
                    }
                });
                root.path("routes").forEach((route) -> {
                    // NOI18N
                    String when = route.path("when").asText();
                    // NOI18N
                    String template = route.path("template").asText();
                    // NOI18N
                    String controller = route.path("controller").asText();
                    // NOI18N
                    String redirection = route.path("redirection").asText();
                    if (template.isEmpty() || controller.isEmpty()) {
                        template = null;
                        controller = null;
                    }
                    if (redirection.isEmpty()) {
                        redirection = null;
                    }
                    if (when != null && !when.isEmpty()) {
                        try {
                            this.routes.add(new AngularRoute(when, template, controller, redirection));
                        } catch (NullPointerException | IllegalArgumentException ex) {
                            log.error("Unable to add route for {}", when);
                        }
                    }
                });
                root.path("sources").forEach((source) -> {
                    URL url = FileUtil.findURL(source.asText());
                    if (url != null) {
                        this.sources.add(url);
                    }
                });
            } catch (IOException ex) {
                log.error("Unable to read {}", manifest, ex);
            }
        });
        this.initialized = true;
    }
}
Also used : AngularRoute(jmri.server.web.spi.AngularRoute) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) URL(java.net.URL)

Example 2 with AngularRoute

use of jmri.server.web.spi.AngularRoute in project JMRI by JMRI.

the class WebAppManager method getAngularRoutes.

public String getAngularRoutes(Profile profile, Locale locale) {
    // NOI18N
    StringJoiner routes = new StringJoiner("\n", "\n", "");
    Set<AngularRoute> items = new HashSet<>();
    this.getManifests(profile).forEach((WebManifest manifest) -> {
        items.addAll(manifest.getAngularRoutes());
    });
    items.forEach((route) -> {
        if (route.getRedirection() != null) {
            // NOI18N
            routes.add(String.format("      .when('%s', { redirectTo: '%s' })", route.getWhen(), route.getRedirection()));
        } else if (route.getTemplate() != null && route.getController() != null) {
            // NOI18N
            routes.add(String.format("      .when('%s', { templateUrl: '%s', controller: '%s' })", route.getWhen(), route.getTemplate(), route.getController()));
        }
    });
    return routes.toString();
}
Also used : AngularRoute(jmri.server.web.spi.AngularRoute) WebManifest(jmri.server.web.spi.WebManifest) StringJoiner(java.util.StringJoiner) HashSet(java.util.HashSet)

Aggregations

AngularRoute (jmri.server.web.spi.AngularRoute)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 File (java.io.File)1 IOException (java.io.IOException)1 URL (java.net.URL)1 HashSet (java.util.HashSet)1 StringJoiner (java.util.StringJoiner)1 WebManifest (jmri.server.web.spi.WebManifest)1