View Javadoc

1   package org.apache.velocity.tools.view;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.velocity.tools.Scope;
23  import org.apache.velocity.tools.config.DefaultKey;
24  import org.apache.velocity.tools.config.ValidScope;
25  import org.apache.velocity.tools.view.ImportSupport;
26  
27  /**
28   * General-purpose text-importing view tool for templates.
29   * <p>Usage:<br />
30   * Just call $import.read("http://www.foo.com/bleh.jsp?sneh=bar") to insert the contents of the named
31   * resource into the template.
32   * </p>
33   * <p><pre>
34   * Toolbox configuration:
35   * &lt;tools&gt;
36   *   &lt;toolbox scope="request"&gt;
37   *     &lt;tool class="org.apache.velocity.tools.view.ImportTool"/&gt;
38   *   &lt;/toolbox&gt;
39   * &lt;/tools&gt;
40   * </pre></p>
41   *
42   * @author <a href="mailto:marinoj@centrum.is">Marino A. Jonsson</a>
43   * @since VelocityTools 2.0
44   * @version $Revision: 663722 $ $Date: 2008-06-05 12:59:51 -0700 (Thu, 05 Jun 2008) $
45   */
46  @DefaultKey("import")
47  @ValidScope(Scope.REQUEST)
48  public class ImportTool extends ImportSupport
49  {
50      /**
51       * Returns the supplied URL rendered as a String.
52       *
53       * @param obj the URL to import
54       * @return the URL as a string
55       */
56      public String read(Object obj) {
57          if (obj == null)
58          {
59              LOG.warn("ImportTool.read(): url is null!");
60              return null;
61          }
62          String url = String.valueOf(obj).trim();
63          if (url.length() == 0)
64          {
65              LOG.warn("ImportTool.read(): url is empty string!");
66              return null;
67          }
68          try
69          {
70              return acquireString(url);
71          }
72          catch (Exception ex)
73          {
74              LOG.error("ImportTool.read(): Exception while aquiring '"+url+"'", ex);
75              return null;
76          }
77      }
78  
79  }