Example usage for java.util NavigableMap toString

List of usage examples for java.util NavigableMap toString

Introduction

In this page you can find the example usage for java.util NavigableMap toString.

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

From source file:org.wisdom.raml.visitor.RamlControllerVisitor.java

/**
 * Navigate through the Controller routes, and create {@link org.raml.model.Resource} from them.
 * If the <code>parent</code> is not null, then the created route will be added has children of the parent, otherwise
 * a new Resource is created and will be added directly to the <code>raml</code> model.
 *
 * @param routes The @{link ControllerRoute}
 * @param parent The parent {@link Resource}
 * @param raml   The {@link Raml} model/*ww  w . ja va2s.c  o m*/
 */
private void navigateTheRoutes(NavigableMap<String, Collection<ControllerRouteModel<Raml>>> routes,
        Resource parent, Raml raml) {
    //nothing to see here
    if (routes == null || routes.isEmpty()) {
        return;
    }

    String headUri = routes.firstKey();
    LOGGER.debug("Routes " + routes.toString());
    LOGGER.debug("Parent " + parent);
    Collection<ControllerRouteModel<Raml>> siblings = routes.get(headUri);
    String relativeUri;

    Resource res = new Resource();
    if (parent != null) {
        res.setParentResource(parent);
        res.setParentUri(parent.getUri());
        //Get the relative part of the url
        relativeUri = normalizeActionPath(parent, headUri);
        res.setRelativeUri(relativeUri);
        parent.getResources().put(res.getRelativeUri(), res);
    } else {
        // We don't have a parent, check whether we should create one.
        if (headUri.endsWith("/")) {
            // We have to create a 'fake' parent when we have such kind of url: /foo/
            // We create a parent /foo and a sub-resource /, this is because /foo and /foo/ are different
            // Create a parent - this parent doest not have any action attached.
            String parentUri = normalizeParentPath(headUri);

            // However we do have a tricky case here, if parentURi == "/", we are the parent.
            if (!parentUri.equals("/")) {
                parent = new Resource();
                parent.setParentUri("");
                parent.setRelativeUri(parentUri);
                raml.getResources().put(parentUri, parent);

                // Now manage the current resource, it's uri is necessarily /
                relativeUri = "/";
                res.setParentUri(parent.getUri());
                res.setRelativeUri(relativeUri);
                parent.getResources().put(relativeUri, res);
            } else {
                // We are the root.
                res.setParentUri("");
                relativeUri = normalizeParentPath(headUri);
                res.setRelativeUri(relativeUri);
                raml.getResources().put(res.getRelativeUri(), res);
            }
        } else {
            // No parent
            res.setParentUri("");
            relativeUri = normalizeParentPath(headUri);
            res.setRelativeUri(relativeUri);
            raml.getResources().put(res.getRelativeUri(), res);
        }
    }

    //Add the action from the brother routes
    for (ControllerRouteModel<Raml> bro : siblings) {
        addActionFromRouteElem(bro, res);
    }

    //visit the children route
    NavigableMap<String, Collection<ControllerRouteModel<Raml>>> child = routes.tailMap(headUri, false);

    //no more route element
    if (child.isEmpty()) {
        return;
    }

    final String next = child.firstKey();
    final Resource maybeParent = findParent(next, raml);
    navigateTheRoutes(child, maybeParent, raml);
}