001/* 002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * bstefanescu 018 * 019 * $Id$ 020 */ 021 022package org.nuxeo.ecm.platform.rendering.fm.adapters; 023 024import org.nuxeo.ecm.core.api.Blob; 025import org.nuxeo.ecm.core.api.DocumentModel; 026import org.nuxeo.ecm.core.api.PropertyException; 027import org.nuxeo.ecm.core.api.model.Property; 028import org.nuxeo.ecm.core.api.model.impl.ArrayProperty; 029import org.nuxeo.ecm.core.api.model.impl.ListProperty; 030import org.nuxeo.ecm.core.api.model.impl.primitives.BlobProperty; 031import org.nuxeo.ecm.platform.rendering.fm.FreemarkerEngine; 032 033import freemarker.ext.beans.ArrayModel; 034import freemarker.template.DefaultObjectWrapper; 035import freemarker.template.TemplateModel; 036import freemarker.template.TemplateModelException; 037 038/** 039 * @author <a href="mailto:[email protected]">Bogdan Stefanescu</a> 040 */ 041public class DocumentObjectWrapper extends DefaultObjectWrapper { 042 043 protected final FreemarkerEngine engine; 044 045 public DocumentObjectWrapper(FreemarkerEngine engine) { 046 this.engine = engine; 047 } 048 049 @Override 050 public final TemplateModel wrap(Object obj) throws TemplateModelException { 051 if (obj == null) { 052 return null; 053 } 054 if (obj instanceof DocumentModel) { 055 return new DocumentTemplate(this, (DocumentModel) obj); 056 } else if (obj instanceof SchemaTemplate.DocumentSchema) { 057 return new SchemaTemplate(this, (SchemaTemplate.DocumentSchema) obj); 058 } else if (obj instanceof Property) { 059 Property p = (Property) obj; 060 if (p.isScalar()) { 061 return new PropertyWrapper(this).wrap(p); 062 } else if (p.isList()) { 063 if (obj instanceof ListProperty) { 064 return new ListPropertyTemplate(this, (ListProperty) obj); 065 } else if (obj instanceof ArrayProperty) { 066 Object value; 067 try { 068 value = ((ArrayProperty) obj).getValue(); 069 } catch (PropertyException e) { 070 throw new IllegalArgumentException("Cannot get array from array property " + obj); 071 } 072 if (value == null) { 073 return TemplateModel.NOTHING; 074 } 075 return new ArrayModel(value, this); 076 } 077 } else if (p.getClass() == BlobProperty.class) { 078 try { 079 Blob blob = (Blob) p.getValue(); 080 if (blob == null) { 081 return TemplateModel.NOTHING; 082 } else { 083 return new BlobTemplate(this, blob); 084 } 085 } catch (PropertyException e) { 086 throw new TemplateModelException(e); 087 } 088 } else { 089 return new ComplexPropertyTemplate(this, (Property) obj); 090 } 091 } 092 return super.wrap(obj); 093 } 094 095}