001: /*
002: * Copyright 2002,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.commons.jelly.tags.core;
018:
019: import org.apache.commons.jelly.JellyException;
020: import org.apache.commons.jelly.JellyTagException;
021: import org.apache.commons.jelly.MissingAttributeException;
022: import org.apache.commons.jelly.TagSupport;
023: import org.apache.commons.jelly.XMLOutput;
024:
025: /** Imports another script.
026: *
027: * <p>
028: * By default, the imported script does not have access to
029: * the parent script's variable context. This behaviour
030: * may be modified using the <code>inherit</code> attribute.
031: * </p>
032: *
033: * @author <a href="mailto:bob@eng.werken.com">bob mcwhirter</a>
034: * @version $Revision: 155420 $
035: */
036:
037: public class ImportTag extends TagSupport {
038:
039: /**
040: * the location of the script being imported, relative to the
041: * current script
042: */
043: private String uri;
044:
045: /**
046: * Whether the imported script has access to the caller's variables
047: */
048: private boolean inherit;
049:
050: /**
051: * The file to be imported. Mutually exclusive with uri.
052: * uri takes precedence.
053: */
054: private String file;
055:
056: /**
057: * Create a new Import tag.
058: */
059: public ImportTag() {
060: }
061:
062: // Tag interface
063: //-------------------------------------------------------------------------
064: /**
065: * Perform tag processing
066: * @param output the destination for output
067: * @throws MissingAttributeException if a required attribute is missing
068: * @throws JellyTagException on any other errors
069: */
070: public void doTag(XMLOutput output)
071: throws MissingAttributeException, JellyTagException {
072: if (uri == null && file == null) {
073: throw new MissingAttributeException("uri");
074: }
075:
076: try {
077: if (uri != null) {
078: // we need to create a new JellyContext of the URI
079: // take off the script name from the URL
080: context.runScript(uri, output, true, isInherit());
081: } else {
082: context.runScript(new java.io.File(file), output, true,
083: isInherit());
084: }
085: } catch (JellyException e) {
086: throw new JellyTagException("could not import script", e);
087: }
088: }
089:
090: // Properties
091: //-------------------------------------------------------------------------
092:
093: /**
094: * @return whether property inheritence is enabled
095: */
096: public boolean isInherit() {
097: return inherit;
098: }
099:
100: /**
101: * Sets whether property inheritence is enabled or disabled
102: */
103: public void setInherit(boolean inherit) {
104: this .inherit = inherit;
105: }
106:
107: /**
108: * Sets the URI (relative URI or absolute URL) for the script to evaluate.
109: */
110: public void setUri(String uri) {
111: this .uri = uri;
112: }
113:
114: /**
115: * Sets the file for the script to evaluate.
116: * @param file The file to set
117: */
118: public void setFile(String file) {
119: this.file = file;
120: }
121:
122: }
|