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  
17  package org.apache.commons.jelly.tags.core;
18  
19  import org.apache.commons.jelly.JellyException;
20  import org.apache.commons.jelly.JellyTagException;
21  import org.apache.commons.jelly.MissingAttributeException;
22  import org.apache.commons.jelly.TagSupport;
23  import org.apache.commons.jelly.XMLOutput;
24  
25  /*** Imports another script.
26   *
27   *  <p>
28   *  By default, the imported script does not have access to
29   *  the parent script's variable context.  This behaviour
30   *  may be modified using the <code>inherit</code> attribute.
31   *  </p>
32   *
33   * @author <a href="mailto:bob@eng.werken.com">bob mcwhirter</a>
34   * @version $Revision: 155420 $
35   */
36  
37  public class ImportTag extends TagSupport {
38  
39      /***
40       * the location of the script being imported, relative to the
41       * current script
42       */
43      private String uri;
44  
45      /***
46       * Whether the imported script has access to the caller's variables
47       */
48      private boolean inherit;
49  
50      /***
51       * The file to be imported. Mutually exclusive with uri.
52       * uri takes precedence.
53       */
54      private String file;
55  
56      /***
57       * Create a new Import tag.
58       */
59      public ImportTag() {
60      }
61  
62  
63      // Tag interface
64      //-------------------------------------------------------------------------
65      /***
66       * Perform tag processing
67       * @param output the destination for output
68       * @throws MissingAttributeException if a required attribute is missing
69       * @throws JellyTagException on any other errors
70       */
71      public void doTag(XMLOutput output) throws MissingAttributeException, JellyTagException {
72          if (uri == null && file == null) {
73              throw new MissingAttributeException( "uri" );
74          }
75  
76          try {
77              if (uri != null) {
78                  // we need to create a new JellyContext of the URI
79                  // take off the script name from the URL
80                  context.runScript(uri, output, true, isInherit() );
81              } else {
82                  context.runScript(new java.io.File(file), output, true,
83                    isInherit());
84              }
85          }
86          catch (JellyException e) {
87              throw new JellyTagException("could not import script",e);
88          }
89      }
90  
91      // Properties
92      //-------------------------------------------------------------------------
93  
94      /***
95       * @return whether property inheritence is enabled
96       */
97      public boolean isInherit() {
98          return inherit;
99      }
100 
101     /***
102      * Sets whether property inheritence is enabled or disabled
103      */
104     public void setInherit(boolean inherit) {
105         this.inherit = inherit;
106     }
107 
108     /***
109      * Sets the URI (relative URI or absolute URL) for the script to evaluate.
110      */
111     public void setUri(String uri) {
112         this.uri = uri;
113     }
114 
115 
116     /***
117      * Sets the file for the script to evaluate.
118      * @param file The file to set
119      */
120     public void setFile(String file) {
121         this.file = file;
122     }
123 
124 }