View Javadoc
1   /**
2    * This file Copyright (c) 2003-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.freemarker.models;
35  
36  import info.magnolia.cms.beans.runtime.FileProperties;
37  import info.magnolia.cms.core.NodeData;
38  
39  import java.text.ParseException;
40  import java.text.SimpleDateFormat;
41  import java.util.ArrayList;
42  import java.util.Iterator;
43  
44  import javax.jcr.RepositoryException;
45  
46  import org.apache.commons.lang3.StringUtils;
47  
48  import freemarker.template.AdapterTemplateModel;
49  import freemarker.template.TemplateCollectionModel;
50  import freemarker.template.TemplateHashModelEx;
51  import freemarker.template.TemplateModel;
52  import freemarker.template.TemplateModelException;
53  import freemarker.template.TemplateScalarModel;
54  
55  /**
56   * TODO : review this !
57   *
58   * @deprecated since 5.6 without replacement.
59   */
60  @Deprecated
61  public class BinaryNodeDataModel implements TemplateHashModelEx, TemplateScalarModel, AdapterTemplateModel {
62      private final NodeData binaryNodeData;
63      private final MagnoliaObjectWrapper wrapper;
64  
65      BinaryNodeDataModel(NodeData binaryNodeData, MagnoliaObjectWrapper wrapper) {
66          this.binaryNodeData = binaryNodeData;
67          this.wrapper = wrapper;
68      }
69  
70      @Override
71      public int size() throws TemplateModelException {
72          int result = 0;
73  
74          try {
75              result = binaryNodeData.getAttributeNames().size();
76          } catch (RepositoryException e) {
77              // don't care
78          }
79  
80          return result;
81      }
82  
83      @Override
84      public TemplateCollectionModel keys() throws TemplateModelException {
85          Iterator<String> result = null;
86          try {
87              result = binaryNodeData.getAttributeNames().iterator();
88          } catch (RepositoryException e) {
89              // don't care
90          }
91          return (TemplateCollectionModel) wrapper.wrap(result);
92      }
93  
94      @Override
95      public TemplateCollectionModel values() throws TemplateModelException {
96          ArrayList<String> result = new ArrayList<String>();
97          try {
98              Iterator<String> iter = binaryNodeData.getAttributeNames().iterator();
99              while (iter.hasNext()) {
100                 result.add(iter.next());
101             }
102         } catch (RepositoryException e) {
103             // don't care
104         }
105         return (TemplateCollectionModel) wrapper.wrap(result.iterator());
106     }
107 
108     @Override
109     public TemplateModel get(String key) throws TemplateModelException {
110         Object result = null;
111 
112         if (key.startsWith("@")) {
113             if (key.equals("@handle")) {
114                 result = binaryNodeData.getHandle();
115             }
116         } else if (key.equals(FileProperties.CONTENT_TYPE)) {
117             result = binaryNodeData.getAttribute(FileProperties.PROPERTY_CONTENTTYPE);
118         } else if (key.equals(FileProperties.NAME)) {
119             String filename = binaryNodeData.getAttribute(FileProperties.PROPERTY_FILENAME);
120             String ext = binaryNodeData.getAttribute(FileProperties.PROPERTY_EXTENSION);
121             result = filename + ((StringUtils.isEmpty(ext)) ? "" : "." + ext);
122         } else if (key.equals(FileProperties.PROPERTY_FILENAME)) {
123             result = binaryNodeData.getAttribute(FileProperties.PROPERTY_FILENAME);
124         } else if (key.equals(FileProperties.PROPERTY_EXTENSION)) {
125             result = binaryNodeData.getAttribute(FileProperties.PROPERTY_EXTENSION);
126         } else if (key.equals(FileProperties.PROPERTY_LASTMODIFIED)) {
127             try {
128                 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
129                 result = format.parse(binaryNodeData.getAttribute(FileProperties.PROPERTY_LASTMODIFIED));
130             } catch (ParseException e) {
131                 // do nothing.
132             }
133         } else {
134             result = binaryNodeData.getAttribute(key);
135         }
136         return wrapper.wrap(result);
137     }
138 
139     @Override
140     public boolean isEmpty() throws TemplateModelException {
141         return (size() == 0);
142     }
143 
144     // this reproduces the logic found in the cms out tag.
145     @Override
146     public String getAsString() throws TemplateModelException {
147         String handle = binaryNodeData.getHandle();
148         String filename = binaryNodeData.getAttribute(FileProperties.PROPERTY_FILENAME);
149         String ext = binaryNodeData.getAttribute(FileProperties.PROPERTY_EXTENSION);
150         return handle + "/" + filename + ((StringUtils.isEmpty(ext)) ? "" : "." + ext);
151     }
152 
153     public NodeData asNodeData() {
154         return this.binaryNodeData;
155     }
156 
157     @Override
158     public Object getAdaptedObject(Class hint) {
159         return asNodeData();
160     }
161 }