001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.xml.xml2json.document;
029
030import org.opencms.file.CmsFile;
031import org.opencms.file.CmsResource;
032import org.opencms.file.CmsResourceFilter;
033import org.opencms.file.types.CmsResourceTypeXmlContent;
034import org.opencms.json.JSONException;
035import org.opencms.json.JSONObject;
036import org.opencms.main.CmsException;
037import org.opencms.main.CmsLog;
038import org.opencms.xml.content.CmsXmlContent;
039import org.opencms.xml.content.CmsXmlContentFactory;
040import org.opencms.xml.xml2json.CmsJsonRequest;
041import org.opencms.xml.xml2json.handler.CmsJsonHandlerException;
042import org.opencms.xml.xml2json.handler.CmsJsonHandlerXmlContent.PathNotFoundException;
043
044import java.util.List;
045
046import org.apache.commons.logging.Log;
047
048/**
049 * Class representing a JSON document for a folder.
050 */
051public class CmsJsonDocumentFolder extends A_CmsJsonDocument implements I_CmsJsonDocument {
052
053    /** The log object for this class. */
054    private static final Log LOG = CmsLog.getLog(CmsJsonDocumentFolder.class);
055
056    /**
057     * Creates a new JSON document.
058     *
059     * @param jsonRequest the JSON request
060     */
061    public CmsJsonDocumentFolder(CmsJsonRequest jsonRequest) {
062
063        super(jsonRequest);
064    }
065
066    /**
067     * @see org.opencms.xml.xml2json.document.I_CmsJsonDocument#getJson()
068     */
069    public Object getJson()
070    throws JSONException, CmsException, CmsJsonHandlerException, PathNotFoundException, Exception {
071
072        CmsResource target = m_context.getResource();
073        int levels = m_jsonRequest.getParamLevels(1).intValue();
074        m_json = folderListingJson(target, levels);
075        return m_json;
076    }
077
078    /**
079     * Formats folder listing as a JSON object, with the individual file names in the folder as keys.
080     *
081     * @param target the folder
082     * @param levelsLeft the number of levels to format (if 1, only the direct children are listed)
083     *
084     * @return the JSON representation of the folder listing
085     * @throws Exception if something goes wrong
086     */
087    protected JSONObject folderListingJson(CmsResource target, int levelsLeft) throws Exception {
088
089        List<CmsResource> children = m_context.getCms().readResources(target, CmsResourceFilter.DEFAULT, false);
090        JSONObject result = new JSONObject(true);
091        for (CmsResource resource : children) {
092            JSONObject childEntry = formatResource(resource);
093            if (resource.isFolder() && (levelsLeft > 1)) {
094                JSONObject childrenJson = folderListingJson(resource, levelsLeft - 1);
095                childEntry.put("children", childrenJson);
096            }
097            result.put(resource.getName(), childEntry);
098        }
099        return result;
100    }
101
102    /**
103     * Formats a resource object as JSON.<p>
104     *
105     * @param resource the resource
106     * @return the JSON
107     * @throws Exception if something goes wrong
108     */
109    private JSONObject formatResource(CmsResource resource) throws Exception {
110
111        boolean isContent = false;
112        if (!resource.isFolder()) {
113            isContent = CmsResourceTypeXmlContent.isXmlContent(resource);
114        }
115        Boolean paramContent = m_jsonRequest.getParamContent();
116        Boolean paramWrapper = m_jsonRequest.getParamWrapper(true);
117        if (isContent && paramContent.booleanValue()) {
118            CmsFile file = m_context.getCms().readFile(resource);
119            CmsXmlContent xmlContent = CmsXmlContentFactory.unmarshal(m_context.getCms(), file);
120            CmsJsonDocumentEmbeddedXmlContent jsonDocumentXmlContent = new CmsJsonDocumentEmbeddedXmlContent(
121                m_jsonRequest,
122                xmlContent);
123            return (JSONObject)jsonDocumentXmlContent.getJson();
124        } else if (paramWrapper.booleanValue()) {
125            CmsJsonDocumentResource jsonDocumentResource = new CmsJsonDocumentResource(m_jsonRequest, resource);
126            return (JSONObject)jsonDocumentResource.getJson();
127        } else {
128            return new JSONObject();
129        }
130    }
131
132}