All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.beangle.struts2.freemarker.BeangleObjectWrapper Maven / Gradle / Ivy

There is a newer version: 3.5.3
Show newest version
/*
 * Beangle, Agile Java/Scala Development Scaffold and Toolkit
 *
 * Copyright (c) 2005-2013, Beangle Software.
 *
 * Beangle is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Beangle is distributed in the hope that it will be useful.
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Beangle.  If not, see .
 */
package org.beangle.struts2.freemarker;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

import freemarker.core.CollectionAndSequence;
import freemarker.ext.beans.CollectionModel;
import freemarker.ext.beans.MapModel;
import freemarker.ext.util.ModelFactory;
import freemarker.template.*;

/**
 * BeangleObjectWrapper不同于StrutsBeanWrapper,他扩展了DefaultObjectWrapper。提供了
* 1)缺省的对象包装
* 2)可以在set等没有顺序的模型使用?seq_contains等方法 * 3)恢复了在Map上的get方法(被DefaultObjectWrapper覆盖掉的)
* 4)提供了map上的values方法(等同于StrutsBeanWrapper的功能) */ public class BeangleObjectWrapper extends DefaultObjectWrapper { private boolean altMapWrapper; public BeangleObjectWrapper(boolean altMapWrapper) { this.altMapWrapper = altMapWrapper; } /** * 特殊包装set和map */ public TemplateModel wrap(Object obj) throws TemplateModelException { if (obj == null) { return super.wrap(null); } if (obj instanceof List) { return new CollectionModel((Collection) obj, this); } // 使得set等集合可以排序 if (obj instanceof Collection) { return new SimpleSequence((Collection) obj, this); } if (obj instanceof Map) { if (altMapWrapper) { return new FriendlyMapModel((Map) obj, this); } else { return new MapModel((Map) obj, this); } } return super.wrap(obj); } // attempt to get the best of both the SimpleMapModel and the MapModel // of FM. @SuppressWarnings("rawtypes") protected ModelFactory getModelFactory(Class clazz) { if (altMapWrapper && Map.class.isAssignableFrom(clazz)) { return FriendlyMapModel.FACTORY; } return super.getModelFactory(clazz); } /** * Attempting to get the best of both worlds of FM's MapModel and * simplemapmodel, by reimplementing the isEmpty(), keySet() and values() * methods. ?keys and ?values built-ins are thus available, just as well as * plain Map methods. */ private final static class FriendlyMapModel extends MapModel implements TemplateHashModelEx, TemplateMethodModelEx, AdapterTemplateModel { static final ModelFactory FACTORY = new ModelFactory() { public TemplateModel create(Object object, ObjectWrapper wrapper) { return new FriendlyMapModel((Map) object, (BeangleObjectWrapper) wrapper); } }; public FriendlyMapModel(Map map, BeangleObjectWrapper wrapper) { super(map, wrapper); } // Struts2将父类的&& super.isEmpty()省去了,原因不知 public boolean isEmpty() { return ((Map) object).isEmpty(); } // 此处实现与MapModel不同,MapModel中复制了一个集合 // 影响了?keySet,?size方法 protected Set keySet() { return ((Map) object).keySet(); } // add feature public TemplateCollectionModel values() { return new CollectionAndSequence(new SimpleSequence(((Map) object).values(), wrapper)); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy