View Javadoc

1   /*
2    * Copyright 2002,2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.jelly.tags.bsf;
17  
18  import org.apache.commons.jelly.JellyException;
19  import org.apache.commons.jelly.Tag;
20  import org.apache.commons.jelly.expression.ExpressionFactory;
21  import org.apache.commons.jelly.impl.TagFactory;
22  import org.apache.commons.jelly.tags.core.CoreTagLibrary;
23  import org.xml.sax.Attributes;
24  
25  import org.apache.bsf.BSFEngine;
26  import org.apache.bsf.BSFException;
27  
28  
29  /*** Describes the Taglib. This class could be generated by XDoclet
30    *
31    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
32    * @version $Revision: 155420 $
33    */
34  public class BSFTagLibrary extends CoreTagLibrary {
35  
36      private BSFExpressionFactory expressionFactory = new BSFExpressionFactory();
37  
38      public BSFTagLibrary() {
39          registerTagFactory(
40              "script",
41              new TagFactory() {
42                  public Tag createTag(String name, Attributes attributes)
43                      throws JellyException {
44                      return createScriptTag(name, attributes);
45                  }
46              }
47              );
48      }
49  
50      public BSFTagLibrary(String language) {
51          this();
52          setLanguage(language);
53      }
54  
55      public void setLanguage(String language) {
56          expressionFactory.setLanguage(language);
57      }
58  
59      /*** Allows derived tag libraries to use their own factory */
60      protected ExpressionFactory getExpressionFactory() {
61          return expressionFactory;
62      }
63  
64      protected BSFEngine getBSFEngine() throws BSFException {
65          return expressionFactory.getBSFEngine();
66      }
67  
68      /***
69       * Factory method to create a new ScriptTag with a BSFEngine
70       *
71       * @param name is the name of the tag (typically 'script')
72       * @param attributes the attributes of the tag
73       * @return Tag
74       */
75      protected Tag createScriptTag(String name, Attributes attributes) throws JellyException {
76          try {
77              return new ScriptTag( expressionFactory.getBSFEngine(),
78                                    expressionFactory.getBSFManager());
79          }
80          catch (BSFException e) {
81              throw new JellyException("Failed to create BSFEngine: " + e, e);
82          }
83      }
84  }