View Javadoc

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