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    public Object visit(TreeItem item) {
062        JSONArray jsons = null;
063        if (item.isExpanded()) {
064            TreeItem[] children = item.getChildren();
065            if (children != null && children.length > 0) {
066                jsons = new JSONArray();
067                for (TreeItem child : children) {
068                    JSONObject childJson = (JSONObject) visit(child);
069                    jsons.add(childJson);
070                }
071            }
072        }
073        return item2JSON(item, jsons);
074    }
075
076    /**
077     * You may override this method to change the output JSON.
078     */
079    protected JSONObject item2JSON(TreeItem item, JSONArray children) {
080        JSONObject json = new JSONObject();
081        json.element("text", item.getLabel()).element("id", item.getPath().toString()).element("href", getUrl(item));
082        json.element("expanded", item.isExpanded());
083        if (item.isContainer()) {
084            if (item.isContainer()) {
085                if (item.hasChildren()) {
086                    json.element("children", children);
087                } else {
088                    json.element("hasChildren", true);
089                }
090            } else {
091                json.element("hasChildren", false);
092            }
093        }
094        return json;
095    }
096
097}