View Javadoc
1   /**
2    * This file Copyright (c) 2003-2015 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.lang.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  public class BinaryNodeDataModel implements TemplateHashModelEx, TemplateScalarModel, AdapterTemplateModel {
59      private final NodeData binaryNodeData;
60      private final MagnoliaObjectWrapper wrapper;
61  
62      BinaryNodeDataModel(NodeData binaryNodeData, MagnoliaObjectWrapper wrapper) {
63          this.binaryNodeData = binaryNodeData;
64          this.wrapper = wrapper;
65      }
66  
67      @Override
68      public int size() throws TemplateModelException {
69          int result = 0;
70  
71          try {
72              result = binaryNodeData.getAttributeNames().size();
73          } catch (RepositoryException e) {
74              // don't care
75          }
76  
77          return result;
78      }
79  
80      @Override
81      public TemplateCollectionModel keys() throws TemplateModelException {
82          Iterator<String> result = null;
83          try {
84              result = binaryNodeData.getAttributeNames().iterator();
85          } catch (RepositoryException e) {
86              // don't care
87          }
88          return (TemplateCollectionModel) wrapper.wrap(result);
89      }
90  
91      @Override
92      public TemplateCollectionModel values() throws TemplateModelException {
93          ArrayList<String> result = new ArrayList<String>();
94          try {
95              Iterator<String> iter = binaryNodeData.getAttributeNames().iterator();
96              while (iter.hasNext()) {
97                  result.add(iter.next());
98              }
99          } catch (RepositoryException e) {
100             // don't care
101         }
102         return (TemplateCollectionModel) wrapper.wrap(result.iterator());
103     }
104 
105     @Override
106     public TemplateModel get(String key) throws TemplateModelException {
107         Object result = null;
108 
109         if (key.startsWith("@")) {
110             if (key.equals("@handle")) {
111                 result = binaryNodeData.getHandle();
112             }
113         } else if (key.equals(FileProperties.CONTENT_TYPE)) {
114             result = binaryNodeData.getAttribute(FileProperties.PROPERTY_CONTENTTYPE);
115         } else if (key.equals(FileProperties.NAME)) {
116             String filename = binaryNodeData.getAttribute(FileProperties.PROPERTY_FILENAME);
117             String ext = binaryNodeData.getAttribute(FileProperties.PROPERTY_EXTENSION);
118             result = filename + ((StringUtils.isEmpty(ext)) ? "" : "." + ext);
119         } else if (key.equals(FileProperties.PROPERTY_FILENAME)) {
120             result = binaryNodeData.getAttribute(FileProperties.PROPERTY_FILENAME);
121         } else if (key.equals(FileProperties.PROPERTY_EXTENSION)) {
122             result = binaryNodeData.getAttribute(FileProperties.PROPERTY_EXTENSION);
123         } else if (key.equals(FileProperties.PROPERTY_LASTMODIFIED)) {
124             try {
125                 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
126                 result = format.parse(binaryNodeData.getAttribute(FileProperties.PROPERTY_LASTMODIFIED));
127             } catch (ParseException e) {
128                 // do nothing.
129             }
130         } else {
131             result = binaryNodeData.getAttribute(key);
132         }
133         return wrapper.wrap(result);
134     }
135 
136     @Override
137     public boolean isEmpty() throws TemplateModelException {
138         return (size() == 0);
139     }
140 
141     // this reproduces the logic found in the cms out tag.
142     @Override
143     public String getAsString() throws TemplateModelException {
144         String handle = binaryNodeData.getHandle();
145         String filename = binaryNodeData.getAttribute(FileProperties.PROPERTY_FILENAME);
146         String ext = binaryNodeData.getAttribute(FileProperties.PROPERTY_EXTENSION);
147         return handle + "/" + filename + ((StringUtils.isEmpty(ext)) ? "" : "." + ext);
148     }
149 
150     public NodeData asNodeData() {
151         return this.binaryNodeData;
152     }
153 
154     @Override
155     public Object getAdaptedObject(Class hint) {
156         return asNodeData();
157     }
158 }