Attachment 'SitemapPathModule.java'

Download

   1 /*
   2  * Copyright 1999-2005 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 package org.apache.cocoon.components.modules.input;
  17 
  18 import java.util.*;
  19 import org.apache.avalon.framework.component.ComponentException;
  20 import org.apache.avalon.framework.component.ComponentManager;
  21 import org.apache.avalon.framework.component.Composable;
  22 import org.apache.avalon.framework.configuration.Configuration;
  23 import org.apache.avalon.framework.configuration.ConfigurationException;
  24 import org.apache.avalon.framework.thread.ThreadSafe;
  25 import org.apache.cocoon.components.modules.input.AbstractInputModule;
  26 import org.apache.excalibur.source.SourceResolver;
  27 import org.apache.excalibur.source.impl.FileSource;
  28 
  29 /**
  30  * Input Module to get an absolute path from a path relative to the current sitemap.
  31  *
  32  * This input module determines the absolute path to a file-resource, relative to
  33  * the current sitemap. For example, if the sitemap from which this module is used
  34  * is located in /here/is/my/sitemap.xmap, the following results are returned:
  35  *
  36  * <table border="1">
  37  * <tr><th>accessor</th><th>result</th><tr>
  38  * <tr><td>{sitemap-path:}</td><td>/here/is/my</td></tr>
  39  * <tr><td>{sitemap-path:input.xml}</td><td>/here/is/my/input.xml</td></tr>
  40  * <tr><td>{sitemap-path:../something.xsl}</td><td>/here/is/something.xsl</td></tr>
  41  * </table>
  42  * <p>
  43  */
  44 
  45 public class SitemapPathModule extends AbstractInputModule  implements Composable, ThreadSafe {
  46 
  47   protected ComponentManager manager = null;
  48 
  49   /**
  50    * Set the current <code>ComponentManager</code> instance used by this
  51    * <code>Composable</code>.
  52    */
  53   public void compose(ComponentManager manager) throws ComponentException {
  54     this.manager = manager;
  55   }
  56 
  57   public Object getAttribute(String name, Configuration modeConf, Map objectModel)
  58     throws ConfigurationException {
  59     SourceResolver resolver = null;
  60     String fullPath = null;
  61     try {
  62       resolver = (SourceResolver)this.manager.lookup(org.apache.excalibur.source.SourceResolver.ROLE);
  63       try {
  64         FileSource source = (FileSource) resolver.resolveURI(name);
  65         //return source.getFile().getCanonicalPath();
  66         //return source.getFile().getAbsolutePath();
  67         fullPath = source.getURI().substring(5); // Without "file:" prefix.
  68       } catch (Exception e) {
  69         if (this.getLogger().isWarnEnabled())
  70           this.getLogger().warn("Exception resolving URL " + name, e);
  71       }
  72     } catch (ComponentException e) {
  73       if (this.getLogger().isErrorEnabled())
  74         this.getLogger().error("Exception obtaining source resolver ", e);
  75     } finally {
  76       if (resolver != null) this.manager.release(resolver);
  77     }
  78     return fullPath;
  79   }
  80 
  81   public Iterator getAttributeNames(Configuration modeConf, Map objectModel)
  82     throws ConfigurationException {
  83     return (new java.util.ArrayList()).iterator();
  84   }
  85 
  86   public Object[] getAttributeValues(String name, Configuration modeConf, Map objectModel)
  87     throws ConfigurationException {
  88     List values = new LinkedList();
  89     values.add( this.getAttribute(name, modeConf, objectModel) );
  90     return values.toArray();
  91   }
  92 
  93 }

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.

You are not allowed to attach a file to this page.