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.util;
18  
19  import java.io.File;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.net.MalformedURLException;
23  import java.net.URL;
24  
25  import org.apache.commons.jelly.JellyTagException;
26  import org.apache.commons.jelly.TagSupport;
27  import org.apache.commons.jelly.XMLOutput;
28  
29  /***
30   * A tag which evaluates its body if the given file is available.
31   * The file can be specified via a File object or via a relative or absolute
32   * URI from the current Jelly script.
33   *
34   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
35   * @version $Revision: 155420 $
36   */
37  public class AvailableTag extends TagSupport {
38  
39      private File file;
40      private String uri;
41  
42      public AvailableTag() {
43      }
44  
45      // Tag interface
46      //-------------------------------------------------------------------------
47      public void doTag(final XMLOutput output) throws JellyTagException {
48          boolean available = false;
49  
50          if (file != null) {
51              available = file.exists();
52          }
53          else if (uri != null) {
54              try {
55                  URL url = context.getResource(uri);
56                  String fileName = url.getFile();
57                  InputStream is = url.openStream();
58                  available = (is != null);
59                  is.close();
60              } catch (MalformedURLException e) {
61                  throw new JellyTagException(e);
62              } catch (IOException ioe) {
63                  available = false;
64              }
65          }
66  
67          if (available) {
68              invokeBody(output);
69          }
70      }
71  
72      // Properties
73      //-------------------------------------------------------------------------
74  
75  
76      /***
77       * Returns the file.
78       * @return File
79       */
80      public File getFile() {
81          return file;
82      }
83  
84      /***
85       * Returns the uri.
86       * @return String
87       */
88      public String getUri() {
89          return uri;
90      }
91  
92      /***
93       * Sets the file to use to test whether it exists or not.
94       * @param file the file to test for
95       */
96      public void setFile(File file) {
97          this.file = file;
98      }
99  
100     /***
101      * Sets the URI to use to test for availability.
102      * The URI can be a full file based URL or a relative URI
103      * or an absolute URI from the root context.
104      *
105      * @param uri the URI of the file to test
106      */
107     public void setUri(String uri) {
108         this.uri = uri;
109     }
110 
111 }