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 java.io.File;
020:
021: import org.apache.commons.jelly.JellyException;
022: import org.apache.commons.jelly.JellyTagException;
023: import org.apache.commons.jelly.MissingAttributeException;
024: import org.apache.commons.jelly.TagSupport;
025: import org.apache.commons.jelly.XMLOutput;
026:
027: /** A tag which conditionally evaluates its body based on some condition
028: *
029: * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
030: * @version $Revision: 155420 $
031: */
032:
033: public class IncludeTag extends TagSupport {
034:
035: private String uri;
036: private File file;
037:
038: private boolean shouldExport;
039: private boolean shouldInherit;
040:
041: public IncludeTag() {
042: this .shouldExport = false;
043: this .shouldInherit = true;
044: }
045:
046: public void setInherit(String inherit) {
047: if ("true".equals(inherit)) {
048: this .shouldInherit = true;
049: } else {
050: this .shouldInherit = false;
051: }
052: }
053:
054: public void setExport(String export) {
055: if ("true".equals(export)) {
056: this .shouldExport = true;
057: } else {
058: this .shouldExport = false;
059: }
060: }
061:
062: public boolean isInherit() {
063: return this .shouldInherit;
064: }
065:
066: public boolean isExport() {
067: return this .shouldExport;
068: }
069:
070: /**
071: * @return
072: */
073: public File getFile() {
074: return file;
075: }
076:
077: /**
078: * Sets the file to be included which is either an absolute file or a file
079: * relative to the current directory
080: */
081: public void setFile(File file) {
082: this .file = file;
083: }
084:
085: // Tag interface
086: //-------------------------------------------------------------------------
087: public void doTag(XMLOutput output)
088: throws MissingAttributeException, JellyTagException {
089:
090: if (uri == null && file == null) {
091: throw new MissingAttributeException("uri");
092: }
093:
094: // we need to create a new JellyContext of the URI
095: // take off the script name from the URL
096: String text = null;
097: try {
098: if (uri != null) {
099: text = uri;
100: context.runScript(uri, output, isExport(), isInherit());
101: } else {
102: text = file.toString();
103: context
104: .runScript(file, output, isExport(),
105: isInherit());
106: }
107: } catch (JellyException e) {
108: throw new JellyTagException(
109: "could not include jelly script: " + text
110: + ". Reason: " + e, e);
111: }
112: }
113:
114: // Properties
115: //-------------------------------------------------------------------------
116: /** Sets the URI (relative URI or absolute URL) for the script to evaluate. */
117: public void setUri(String uri) {
118: this.uri = uri;
119: }
120: }
|