View Javadoc
1   /**
2    * This file Copyright (c) 2003-2014 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.core.Content;
37  import info.magnolia.cms.core.NodeData;
38  import info.magnolia.cms.util.ContentUtil;
39  
40  import java.util.Collection;
41  import java.util.Iterator;
42  
43  import javax.jcr.RepositoryException;
44  
45  import org.apache.commons.collections.IteratorUtils;
46  import org.apache.commons.collections.Transformer;
47  
48  import freemarker.template.AdapterTemplateModel;
49  import freemarker.template.ObjectWrapper;
50  import freemarker.template.SimpleCollection;
51  import freemarker.template.TemplateCollectionModel;
52  import freemarker.template.TemplateHashModelEx;
53  import freemarker.template.TemplateModel;
54  import freemarker.template.TemplateModelException;
55  import freemarker.template.TemplateNodeModel;
56  import freemarker.template.TemplateScalarModel;
57  import freemarker.template.TemplateSequenceModel;
58  
59  /**
60   * A wrapper for Content nodes. Exposes properties using an HashModel (.property, ?size, ...)
61   * a hierarchy (TemplateNodeModel: ?children, ?parent, ...)
62   * and as a scalar (returns the node name)
63   *
64   * @author Chris Miner
65   * @author gjoseph
66   * @version $Revision: $ ($Author: $)
67   */
68  public class ContentModel implements TemplateHashModelEx, TemplateNodeModel, TemplateScalarModel, AdapterTemplateModel {
69      static final MagnoliaModelFactory FACTORY = new MagnoliaModelFactory() {
70          @Override
71          public Class factoryFor() {
72              return Content.class;
73          }
74  
75          @Override
76          public AdapterTemplateModel create(Object object, ObjectWrapper wrapper) {
77              return new ContentModel((Content) object, (MagnoliaObjectWrapper) wrapper);
78          }
79      };
80  
81      private final Content content;
82      private final MagnoliaObjectWrapper wrapper;
83  
84      ContentModel(Content content, MagnoliaObjectWrapper wrapper) {
85          this.content = content;
86          this.wrapper = wrapper;
87      }
88  
89      @Override
90      public String getAsString() {
91          return content.getName();
92      }
93  
94      @Override
95      public TemplateModel get(String key) throws TemplateModelException {
96          final Object result;
97  
98          try {
99              if (key.equals("@handle")) {
100                 result = content.getHandle();
101             } else if (key.equals("@uuid")) {
102                 result = content.getUUID();
103             } else if (key.equals("@name")) {
104                 result = content.getName();
105             } else if (key.equals("@level") || key.equals("@depth")) {
106                 result = content.getLevel();
107             } else if (key.equalsIgnoreCase("metaData")) {
108                 result = content.getMetaData();
109             } else {
110                 // try for node data or child node
111                 if (content.hasNodeData(key)) {
112                     result = content.getNodeData(key);
113                 } else {
114                     if (content.hasContent(key)) {
115                         result = content.getContent(key);
116                     } else {
117                         result = null;
118                     }
119                 }
120             }
121         } catch (RepositoryException e) {
122             throw new TemplateModelException(e);
123         }
124         return wrapper.wrap(result);
125     }
126 
127     @Override
128     public boolean isEmpty() throws TemplateModelException {
129         return (size() == 0);
130     }
131 
132     @Override
133     public int size() throws TemplateModelException {
134         return content.getNodeDataCollection().size();
135     }
136 
137     @Override
138     public TemplateCollectionModel keys() throws TemplateModelException {
139         final Iterator it = IteratorUtils.transformedIterator(content.getNodeDataCollection().iterator(), new Transformer() {
140             @Override
141             public Object transform(Object input) {
142                 return ((NodeData) input).getName();
143             }
144         });
145         return new SimpleCollection(it);
146     }
147 
148     @Override
149     public TemplateCollectionModel values() throws TemplateModelException {
150         return (TemplateCollectionModel) wrapper.wrap(content.getNodeDataCollection().iterator());
151     }
152 
153     @Override
154     public TemplateNodeModel getParentNode() throws TemplateModelException {
155         try {
156             // todo : check if this is the root?
157             // content.getLevel() == 0;
158             final Content parent = content.getParent();
159             return (TemplateNodeModel) wrapper.wrap(parent);
160         } catch (RepositoryException e) {
161             throw new TemplateModelException("Can't get parent of " + content + ":" + e.getMessage(), e);
162         }
163     }
164 
165     /**
166      * This returns all children, except nodes or jcr: types and mgnl:metaData.
167      */
168     @Override
169     public TemplateSequenceModel getChildNodes() throws TemplateModelException {
170         final Collection<Content> children = ContentUtil.getAllChildren(content);
171         return (TemplateSequenceModel) wrapper.wrap(children);
172     }
173 
174     @Override
175     public String getNodeName() throws TemplateModelException {
176         return content.getName();
177     }
178 
179     @Override
180     public String getNodeType() throws TemplateModelException {
181         try {
182             return content.getNodeTypeName();
183         } catch (RepositoryException e) {
184             throw new TemplateModelException("Can't get node type of " + content + ":" + e.getMessage(), e);
185         }
186     }
187 
188     @Override
189     public String getNodeNamespace() throws TemplateModelException {
190         return null; // non XML implementation
191     }
192 
193     public Content asContent() {
194         return this.content;
195     }
196 
197     @Override
198     public Object getAdaptedObject(Class hint) {
199         return asContent();
200     }
201 }