001    /*
002     * Copyright 2004-2005 Stephen J. McConnell.
003     * Copyright 2004 Niclas Hedhman.
004     *
005     * Licensed  under the  Apache License,  Version 2.0  (the "License");
006     * you may not use  this file  except in  compliance with the License.
007     * You may obtain a copy of the License at
008     *
009     *   http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed  under the  License is distributed on an "AS IS" BASIS,
013     * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
014     * implied.
015     *
016     * See the License for the specific language governing permissions and
017     * limitations under the License.
018     */
019    
020    package net.dpml.transit;
021    
022    /** 
023     * The ClassicLayout decodes artifacts into the Classic/Maven layout
024     * of artifacts on a file system or http server.
025     * This format says that for an artifact <code>artifact:[type]:[group]/[name]#[version]</code>
026     * the location of such artifact would be;
027     * <code>[group]/[type]s/[name]-[version].[type]</code>.
028     * Example; <code>artifact:jar:metro/cache/dpml-cache-main#1.0.0</code>
029     * would return the path <code>metro/cache/jars/dpml-cache-main-1.0.0.jar</code>.
030     *
031     * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
032     * @version 1.0.3
033     */
034    public class ClassicLayout
035        implements Layout
036    {
037        /**
038         * Return the base path for an artifact.  The base path is derived from
039         * the artifact group and type.  For an artifact group of "metro/cache" and a
040         * type equal to "jar", the base value will be translated using the pattern
041         * "[group]/[type]s" to form "metro/cache/jars".  The base path value represents
042         * the directory path relative to a repository root of the directory containing
043         * this artifact.
044         *
045         * @param artifact the resource artifact
046         * @return the base path
047         */
048        public final String resolveBase( Artifact artifact )
049        {
050            if( null == artifact.getGroup() )
051            {
052                return artifact.getType() + "s";
053            }
054            else
055            {
056                return artifact.getGroup() + "/" + artifact.getType() + "s";
057            }
058        }
059    
060        /**
061         * Returns the full path of the artifact relative to a logical root directory.
062         * The full path is equivalent to the base path and artifact filename using the
063         * pattern "[base]/[filename]".  Path values may be used to resolve an artifact
064         * from a remote repository or local cache relative to the repository or cache
065         * root. An artifact such as <code>artifact:jar:metro/cache/dpml-cache-main#1.0.0</code>
066         * would return the path <code>metro/cache/jars/dpml-cache-main-1.0.0.jar</code>.
067         *
068         * @param artifact the resource artifact
069         * @see #resolveBase
070         * @see #resolveFilename
071         * @return the logical artifact path
072         */
073        public final String resolvePath( Artifact artifact )
074        {
075            return resolveBase( artifact ) + "/" + resolveFilename( artifact );
076        }
077    
078        /**
079         * Return the expanded filename of the artifact. The filename is expressed
080         * as [name]-[version].[type] or in case of a null version simply [name].[type].
081         *
082         * @param artifact the resource artifact
083         * @return the artifact expanded filename
084         */
085        public String resolveFilename( Artifact artifact )
086        {
087            String scheme = artifact.getScheme();
088            String filename = resolveBaseFilename( artifact );
089            if( "artifact".equals( scheme ) )
090            {
091                return filename;
092            }
093            else if( "link".equals( scheme ) )
094            {
095                return filename + ".link";
096            }
097            else
098            {
099                final String error = 
100                  "Protocol not recognized: " + scheme;
101                throw new TransitRuntimeException( error );
102            }
103        }
104    
105        private String resolveBaseFilename( Artifact artifact )
106        {
107            String ver = artifact.getVersion();
108            if( null == ver )
109            {
110                return artifact.getName() + "." + artifact.getType();
111            }
112            else
113            {
114                return artifact.getName() + "-" + ver + "." + artifact.getType();
115            }
116        }
117    }