001/*
002 * (C) Copyright 2006-2008 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bstefanescu
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.webengine.ui.tree;
023
024import java.util.Collection;
025
026import net.sf.json.JSONArray;
027import net.sf.json.JSONObject;
028
029/**
030 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
031 */
032public class JSonTreeSerializer implements TreeItemVisitor {
033
034    /**
035     * Must be overridden to provide real URLs
036     */
037    public String getUrl(TreeItem item) {
038        return item.getPath().toString();
039    }
040
041    public JSONArray toJSON(Collection<TreeItem> items) {
042        JSONArray ar = new JSONArray();
043        for (TreeItem item : items) {
044            ar.add(toJSON(item));
045        }
046        return ar;
047    }
048
049    public JSONArray toJSON(TreeItem[] items) {
050        JSONArray ar = new JSONArray();
051        for (TreeItem item : items) {
052            ar.add(toJSON(item));
053        }
054        return ar;
055    }
056
057    public JSONObject toJSON(TreeItem root) {
058        return (JSONObject) root.accept(this);
059    }
060
061    @Override
062    public Object visit(TreeItem item) {
063        JSONArray jsons = null;
064        if (item.isExpanded()) {
065            TreeItem[] children = item.getChildren();
066            if (children != null && children.length > 0) {
067                jsons = new JSONArray();
068                for (TreeItem child : children) {
069                    JSONObject childJson = (JSONObject) visit(child);
070                    jsons.add(childJson);
071                }
072            }
073        }
074        return item2JSON(item, jsons);
075    }
076
077    /**
078     * You may override this method to change the output JSON.
079     */
080    protected JSONObject item2JSON(TreeItem item, JSONArray children) {
081        JSONObject json = new JSONObject();
082        json.element("text", item.getLabel()).element("id", item.getPath().toString()).element("href", getUrl(item));
083        json.element("expanded", item.isExpanded());
084        if (item.isContainer()) {
085            if (item.isContainer()) {
086                if (item.hasChildren()) {
087                    json.element("children", children);
088                } else {
089                    json.element("hasChildren", true);
090                }
091            } else {
092                json.element("hasChildren", false);
093            }
094        }
095        return json;
096    }
097
098}