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 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      // Tag interface
    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          // we need to create a new JellyContext of the URI
    96          // take off the script name from the URL
    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     // Properties
    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 }