View Javadoc

    1   /*
    2    * $Id: TagModel.java 500745 2007-01-28 05:38:24Z mrdon $
    3    *
    4    * Licensed to the Apache Software Foundation (ASF) under one
    5    * or more contributor license agreements.  See the NOTICE file
    6    * distributed with this work for additional information
    7    * regarding copyright ownership.  The ASF licenses this file
    8    * to you under the Apache License, Version 2.0 (the
    9    * "License"); you may not use this file except in compliance
    10   * with the License.  You may obtain a copy of the License at
    11   *
    12   *  http://www.apache.org/licenses/LICENSE-2.0
    13   *
    14   * Unless required by applicable law or agreed to in writing,
    15   * software distributed under the License is distributed on an
    16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    17   * KIND, either express or implied.  See the License for the
    18   * specific language governing permissions and limitations
    19   * under the License.
    20   */
    21  package org.apache.struts2.views.freemarker.tags;
    22
    23  import java.io.IOException;
    24  import java.io.Writer;
    25  import java.util.HashMap;
    26  import java.util.Iterator;
    27  import java.util.Map;
    28
    29  import javax.servlet.http.HttpServletRequest;
    30  import javax.servlet.http.HttpServletResponse;
    31
    32  import org.apache.commons.logging.Log;
    33  import org.apache.commons.logging.LogFactory;
    34  import org.apache.struts2.components.ActionComponent;
    35  import org.apache.struts2.components.Component;
    36  import org.apache.struts2.dispatcher.Dispatcher;
    37
    38  import com.opensymphony.xwork2.inject.Container;
    39  import com.opensymphony.xwork2.util.ValueStack;
    40
    41  import freemarker.template.SimpleNumber;
    42  import freemarker.template.SimpleSequence;
    43  import freemarker.template.TemplateModelException;
    44  import freemarker.template.TemplateTransformModel;
    45
    46  public abstract class TagModel implements TemplateTransformModel {
    47      private static final Log LOG = LogFactory.getLog(TagModel.class);
    48
    49      protected ValueStack stack;
    50      protected HttpServletRequest req;
    51      protected HttpServletResponse res;
    52
    53      public TagModel(ValueStack stack, HttpServletRequest req, HttpServletResponse res) {
    54          this.stack = stack;
    55          this.req = req;
    56          this.res = res;
    57      }
    58
    59      public Writer getWriter(Writer writer, Map params) throws TemplateModelException, IOException {
    60          Component bean = getBean();
    61          Container container = Dispatcher.getInstance().getConfigurationManager().getConfiguration().getContainer();
    62          container.inject(bean);
    63          Map basicParams = convertParams(params);
    64          bean.copyParams(basicParams);
    65          bean.addAllParameters(getComplexParams(params));
    66          return new CallbackWriter(bean, writer);
    67      }
    68
    69      protected abstract Component getBean();
    70
    71      protected Map convertParams(Map params) {
    72          HashMap map = new HashMap(params.size());
    73          for (Iterator iterator = params.entrySet().iterator(); iterator.hasNext();) {
    74              Map.Entry entry = (Map.Entry) iterator.next();
    75              Object value = entry.getValue();
    76              if (value != null && !complexType(value)) {
    77                  map.put(entry.getKey(), value.toString());
    78              }
    79          }
    80          return map;
    81      }
    82
    83      protected Map getComplexParams(Map params) {
    84          HashMap map = new HashMap(params.size());
    85          for (Iterator iterator = params.entrySet().iterator(); iterator.hasNext();) {
    86              Map.Entry entry = (Map.Entry) iterator.next();
    87              Object value = entry.getValue();
    88              if (value != null && complexType(value)) {
    89                  if (value instanceof freemarker.ext.beans.BeanModel) {
    90                      map.put(entry.getKey(), ((freemarker.ext.beans.BeanModel) value).getWrappedObject());
    91                  } else if (value instanceof SimpleNumber) {
    92                      map.put(entry.getKey(), ((SimpleNumber) value).getAsNumber());
    93                  } else if (value instanceof SimpleSequence) {
    94                      try {
    95                          map.put(entry.getKey(), ((SimpleSequence) value).toList());
    96                      } catch (TemplateModelException e) {
    97                          LOG.error("There was a problem converting a SimpleSequence to a list", e);
    98                      }
    99                  }
    100             }
    101         }
    102         return map;
    103     }
    104
    105     protected boolean complexType(Object value) {
    106         return value instanceof freemarker.ext.beans.BeanModel
    107                 || value instanceof SimpleNumber
    108                 || value instanceof SimpleSequence;
    109     }
    110 }