View Javadoc

1   /**
2    * Copyright (C) 2005-2009 Alfresco Software Limited.
3    *
4    * This file is part of the Spring Surf Extension project.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.springframework.extensions.webscripts;
20  
21  import java.io.StringReader;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.springframework.extensions.config.ConfigService;
26  import org.xml.sax.InputSource;
27  
28  import freemarker.ext.dom.NodeModel;
29  
30  /**
31   * Model representation of configuration for use in scripts.
32   *
33   * @author Gavin Cornwell
34   */
35  public class TemplateConfigModel extends ConfigModel
36  {
37     private NodeModel model;
38     
39     private static Log logger = LogFactory.getLog(TemplateConfigModel.class);
40  
41     /**
42      * Constructor
43      * 
44      * @param configService ConfigService instance
45      * @param scriptConfig The script's config as XML string
46      */
47     public TemplateConfigModel(ConfigService configService, String scriptConfig)
48     {
49        super(configService, scriptConfig);
50        
51        if (logger.isDebugEnabled())
52           logger.debug(this.toString() + " created:\nconfig service: " + 
53                    this.configService + "\nglobal config: " + this.globalConfig +
54                    "\nscript config: " + this.scriptConfig);
55     }
56     
57     /**
58      * Returns the script's config as a Freemarker NodeModel object
59      * 
60      * @return Script config as a Freemarker NodeModel object
61      */
62     @Override
63     public Object getScript()
64     {
65        if (this.model != null)
66        {
67           return this.model;
68        }
69        
70        // if we get this far attempt to create the model, if we have 
71        // something to create with
72        if (this.scriptConfig != null && this.model == null)
73        {
74           StringReader reader = new StringReader(this.scriptConfig);
75           InputSource is = new InputSource(reader);
76           try
77           {
78              this.model = NodeModel.parse(is);
79              return this.model;
80           }
81           catch (Exception e)
82           {
83              if (logger.isWarnEnabled())
84                 logger.warn("Failed to create 'script' config model: " + e.getMessage());
85           }
86        }
87        
88        // if we make it here return empty model
89        return NodeModel.NOTHING;
90     }
91  }