1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.jelly.tags.core;
18
19 import java.io.File;
20
21 import org.apache.commons.jelly.JellyException;
22 import org.apache.commons.jelly.JellyTagException;
23 import org.apache.commons.jelly.MissingAttributeException;
24 import org.apache.commons.jelly.TagSupport;
25 import org.apache.commons.jelly.XMLOutput;
26
27 /*** A tag which conditionally evaluates its body based on some condition
28 *
29 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
30 * @version $Revision: 155420 $
31 */
32
33 public class IncludeTag extends TagSupport {
34
35 private String uri;
36 private File file;
37
38 private boolean shouldExport;
39 private boolean shouldInherit;
40
41 public IncludeTag() {
42 this.shouldExport = false;
43 this.shouldInherit = true;
44 }
45
46 public void setInherit(String inherit) {
47 if ("true".equals(inherit)) {
48 this.shouldInherit = true;
49 } else {
50 this.shouldInherit = false;
51 }
52 }
53
54 public void setExport(String export) {
55 if ("true".equals(export)) {
56 this.shouldExport = true;
57 } else {
58 this.shouldExport = false;
59 }
60 }
61
62 public boolean isInherit() {
63 return this.shouldInherit;
64 }
65
66 public boolean isExport() {
67 return this.shouldExport;
68 }
69
70 /***
71 * @return
72 */
73 public File getFile() {
74 return file;
75 }
76
77 /***
78 * Sets the file to be included which is either an absolute file or a file
79 * relative to the current directory
80 */
81 public void setFile(File file) {
82 this.file = file;
83 }
84
85
86
87
88 public void doTag(XMLOutput output)
89 throws MissingAttributeException, JellyTagException {
90
91 if (uri == null && file == null) {
92 throw new MissingAttributeException("uri");
93 }
94
95
96
97 String text = null;
98 try {
99 if (uri != null) {
100 text = uri;
101 context.runScript(uri, output, isExport(), isInherit());
102 }
103 else {
104 text = file.toString();
105 context.runScript(file, output, isExport(), isInherit());
106 }
107 }
108 catch (JellyException e) {
109 throw new JellyTagException("could not include jelly script: " + text + ". Reason: " + e, e);
110 }
111 }
112
113
114
115 /*** Sets the URI (relative URI or absolute URL) for the script to evaluate. */
116 public void setUri(String uri) {
117 this.uri = uri;
118 }
119 }