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; 029 030import org.opencms.file.CmsObject; 031import org.opencms.file.CmsProperty; 032import org.opencms.file.CmsResource; 033import org.opencms.json.JSONException; 034import org.opencms.json.JSONObject; 035import org.opencms.main.CmsException; 036import org.opencms.main.OpenCms; 037 038import java.util.List; 039import java.util.function.Predicate; 040 041/** 042 * Helper class for formatting resource data as JSON. 043 */ 044public class CmsResourceDataJsonHelper { 045 046 /** The CMS context. */ 047 private CmsObject m_cms; 048 049 /** The property filter which decides whether properties should be written to JSON or not. */ 050 private Predicate<String> m_propertyFilter; 051 052 /** The resource. */ 053 private CmsResource m_resource; 054 055 /** 056 * Creates a new instance. 057 * 058 * @param cms the CMS context 059 * @param resource the resource 060 * @param propertyFilter the property filter, which decides whether properties should be written to JSON or not 061 */ 062 public CmsResourceDataJsonHelper(CmsObject cms, CmsResource resource, Predicate<String> propertyFilter) { 063 064 m_cms = cms; 065 m_resource = resource; 066 m_propertyFilter = propertyFilter; 067 } 068 069 /** 070 * Adds path and link fields for the resource to the given JSON object. 071 * 072 * @param json the JSON object to add the fields to 073 * 074 * @throws JSONException if something goes wrong 075 */ 076 public void addPathAndLink(JSONObject json) throws JSONException { 077 078 json.put("path", m_resource.getRootPath()); 079 json.put("link", CmsJsonResourceHandler.link(m_cms, m_resource)); 080 } 081 082 /** 083 * Adds property data to the result object. 084 * 085 * @param result the result object 086 * 087 * @throws CmsException if something goes wrong 088 * @throws JSONException if something goes wrong with the JSON 089 */ 090 public void addProperties(JSONObject result) throws CmsException, JSONException { 091 092 result.put("properties", properties(true)); 093 result.put("own_properties", properties(false)); 094 } 095 096 /** 097 * Creates a JSON object with the attributes of the resource. 098 * 099 * @return the JSON for the attributes 100 * @throws JSONException if something goes wrong 101 */ 102 public JSONObject attributes() throws JSONException { 103 104 JSONObject attributes = new JSONObject(); 105 attributes.put("type", OpenCms.getResourceManager().getResourceType(m_resource).getTypeName()); 106 attributes.put("lastModified", Long.valueOf(m_resource.getDateLastModified())); 107 return attributes; 108 } 109 110 /** 111 * Creates a JSON object with the properties of the resource. 112 * 113 * @param inherited true if inherited properties should be loaded 114 * 115 * @return the JSON object 116 * @throws CmsException if something goes wrong 117 * @throws JSONException if something goes wrong 118 */ 119 public JSONObject properties(boolean inherited) throws CmsException, JSONException { 120 121 List<CmsProperty> props = m_cms.readPropertyObjects(m_resource, inherited); 122 JSONObject propJson = new JSONObject(true); 123 for (CmsProperty prop : props) { 124 if ((m_propertyFilter == null) || m_propertyFilter.test(prop.getName())) { 125 propJson.put(prop.getName(), prop.getValue()); 126 } 127 } 128 return propJson; 129 } 130 131}