View Javadoc
1   /**
2    * This file Copyright (c) 2008-2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.cms.util;
35  
36  import info.magnolia.cms.beans.runtime.FileProperties;
37  import info.magnolia.cms.core.Content;
38  import info.magnolia.cms.core.NodeData;
39  import info.magnolia.link.LinkException;
40  import info.magnolia.link.LinkTransformerManager;
41  
42  import java.util.ArrayList;
43  import java.util.Collection;
44  import java.util.HashSet;
45  import java.util.Iterator;
46  import java.util.Map;
47  import java.util.Set;
48  
49  import javax.jcr.PropertyType;
50  import javax.jcr.RepositoryException;
51  import javax.jcr.Value;
52  
53  import org.apache.commons.beanutils.PropertyUtils;
54  import org.slf4j.Logger;
55  import org.slf4j.LoggerFactory;
56  
57  
58  /**
59   * Wrapper for a content Node which exposes a Map interface, used to access its content using jstl.
60   *
61   * @deprecated since 4.5, declared deprecated in 5.6.
62   */
63  @Deprecated
64  public class NodeMapWrapper extends ContentWrapper implements Map {
65  
66      private static final Logger log = LoggerFactory.getLogger(NodeMapWrapper.class);
67  
68      /**
69       * Handle used to construct links.
70       */
71      private final String handle;
72  
73      /**
74       * Instantiates a new NodeMapWrapper for the given node.
75       *
76       * @param node Content node
77       * @param handle Parent page handle or other prefix for links.
78       */
79      public NodeMapWrapper(Content node, String handle) {
80          super(node);
81          this.handle = handle;
82      }
83  
84      @Override
85      protected Content wrap(Content node) {
86          return new NodeMapWrapper(node, handle);
87      }
88  
89      /**
90       * @see java.util.Map#size()
91       */
92      @Override
93      public int size() {
94          return getWrappedContent().getNodeDataCollection().size();
95      }
96  
97      /**
98       * @see java.util.Map#isEmpty()
99       */
100     @Override
101     public boolean isEmpty() {
102         return getWrappedContent().getNodeDataCollection().isEmpty();
103     }
104 
105     /**
106      * @see java.util.Map#containsKey(java.lang.Object)
107      */
108     @Override
109     public boolean containsKey(Object key) {
110         return this.getWrappedContent().getNodeData((String) key).isExist() || hasProperty(key);
111     }
112 
113     /**
114      * @see java.util.Map#containsValue(java.lang.Object)
115      */
116     @Override
117     public boolean containsValue(Object value) {
118         // not implemented, only get() is needed
119         return false;
120     }
121 
122     /**
123      * Shortcut for Content.getNodeData(name).getString() or Content.getNodeData(name).getName().
124      *
125      * @see java.util.Map#get(Object)
126      */
127     @Override
128     public Object get(Object key) {
129         try {
130             if (!getWrappedContent().hasNodeData((String) key)) {
131                 // support the old lower case value
132                 if ("uuid".equalsIgnoreCase((String) key)) {
133                     key = "UUID";
134                 }
135                 if (hasProperty(key)) {
136                     try {
137                         return PropertyUtils.getProperty(this.getWrappedContent(), (String) key);
138                     } catch (Exception e) {
139                         log.error("can't read property {} from the node {}", key, this.getWrappedContent(), e);
140                     }
141                 }
142             }
143         } catch (RepositoryException e) {
144             // should really not happen
145             log.error("can't check for node data {{}}", key, e);
146         }
147 
148         NodeData nodeData = getWrappedContent().getNodeData((String) key);
149         Object value;
150         int type = nodeData.getType();
151         if (type == PropertyType.DATE) {
152             value = nodeData.getDate();
153         } else if (type == PropertyType.BINARY) {
154             // only file path is supported
155             FileProperties props = new FileProperties(getWrappedContent(), (String) key);
156             value = props.getProperty(FileProperties.PATH);
157         } else if (nodeData.isMultiValue() == NodeData.MULTIVALUE_TRUE) {
158 
159             Value[] values = nodeData.getValues();
160 
161             String[] valueStrings = new String[values.length];
162 
163             for (int j = 0; j < values.length; j++) {
164                 try {
165                     valueStrings[j] = values[j].getString();
166                 } catch (RepositoryException e) {
167                     log.debug(e.getMessage());
168                 }
169             }
170 
171             value = valueStrings;
172         } else {
173             try {
174                 value = info.magnolia.link.LinkUtil.convertLinksFromUUIDPattern(nodeData.getString(), LinkTransformerManager.getInstance().getBrowserLink(handle));
175             } catch (LinkException e) {
176                 log.warn("Failed to parse links with from {}", nodeData.getName(), e);
177                 value = nodeData.getString();
178             }
179         }
180         return value;
181     }
182 
183     protected boolean hasProperty(Object key) {
184         try {
185             return PropertyUtils.getPropertyDescriptor(this.getWrappedContent(), (String) key) != null;
186         } catch (Exception e) {
187             return false;
188         }
189     }
190 
191     /**
192      * @see java.util.Map#put(java.lang.Object, java.lang.Object)
193      */
194     @Override
195     public Object put(Object arg0, Object arg1) {
196         // not implemented, only get() is needed
197         return null;
198     }
199 
200     /**
201      * @see java.util.Map#remove(java.lang.Object)
202      */
203     @Override
204     public Object remove(Object key) {
205         // not implemented, only get() is needed
206         return null;
207     }
208 
209     /**
210      * @see java.util.Map#putAll(java.util.Map)
211      */
212     @Override
213     public void putAll(Map t) {
214         // not implemented, only get() is needed
215     }
216 
217     /**
218      * @see java.util.Map#clear()
219      */
220     @Override
221     public void clear() {
222         // not implemented, only get() is needed
223     }
224 
225     /**
226      * @see java.util.Map#keySet()
227      */
228     @Override
229     public Set<String> keySet() {
230         Collection<NodeData> nodeDataCollection = getWrappedContent().getNodeDataCollection();
231         Set<String> keys = new HashSet<String>();
232         for (Iterator<NodeData> iter = nodeDataCollection.iterator(); iter.hasNext(); ) {
233             keys.add(iter.next().getName());
234         }
235 
236         return keys;
237     }
238 
239     /**
240      * @see java.util.Map#values()
241      */
242     @Override
243     public Collection<String> values() {
244         Collection<NodeData> nodeDataCollection = getWrappedContent().getNodeDataCollection();
245         Collection<String> values = new ArrayList<String>();
246         for (Iterator<NodeData> iter = nodeDataCollection.iterator(); iter.hasNext(); ) {
247             values.add((iter.next()).getString());
248         }
249 
250         return values;
251     }
252 
253     /**
254      * @see java.util.Map#entrySet()
255      */
256     @Override
257     public Set entrySet() {
258         Collection<NodeData> nodeDataCollection = getWrappedContent().getNodeDataCollection();
259         Set<Map.Entry> keys = new HashSet<Map.Entry>();
260         for (Iterator<NodeData> iter = nodeDataCollection.iterator(); iter.hasNext(); ) {
261             NodeData nd = iter.next();
262             final String key = nd.getName();
263             final String value = nd.getString();
264             keys.add(new Map.Entry() {
265 
266                 @Override
267                 public Object getKey() {
268                     return key;
269                 }
270 
271                 @Override
272                 public Object getValue() {
273                     return value;
274                 }
275 
276                 @Override
277                 public Object setValue(Object value) {
278                     return value;
279                 }
280             });
281         }
282 
283         return keys;
284     }
285 }