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 EclipseLayout decodes artifacts into the Eclipse specified 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]-[version]/[name].[type]</code>.
028     * Example; <code>artifact:jar:eclipse/plugins/eclipse-osgi-runtime/core#3.1.0</code>
029     * would return the path <code>eclipse/plugins/eclipse-osgi-runtime-3.1.0/core.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 EclipseLayout extends AbstractLayout
035        implements Layout
036    {
037        /**
038         * Return the base path for an artifact.  The base path is derived from
039         * the artifact group and version.  For an artifact group of "metro/cache" and a
040         * version equal to "1.3", the base value will be translated using the pattern
041         * "[group]-[version]" to form "metro/cache-1.3".  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 artifact to resolve the base path from
046         * @return the base path
047         */
048        public final String resolveBase( Artifact artifact )
049        {
050            if( null == artifact.getGroup() )
051            {
052                return artifact.getVersion();
053            }
054            else
055            {
056                return artifact.getGroup() + "-" + artifact.getVersion();
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
066         * <code>artifact:jar:eclipse/plugins/eclipse-osgi-runtime/core#3.1.0</code>
067         * would return the path
068         * <code>eclipse/plugins/eclipse-osgi-runtime-3.1.0/core.jar</code>.
069         *
070         * @param artifact the artifact to resolve the path from
071         * @see #resolveBase
072         * @see #resolveFilename
073         * @return the logical artifact path
074         */
075        public final String resolvePath( Artifact artifact )
076        {
077            return resolveBase( artifact ) + "/" + resolveFilename( artifact );
078        }
079    
080        /**
081         * Return the expanded filename of the artifact.
082         * The filename is expressed as <code>[name].[type]</code>.
083         *
084         * @param artifact the artifact to resolve
085         * @return the artifact expanded filename
086         */
087        public String resolveFilename( Artifact artifact )
088        {
089            String scheme = artifact.getScheme();
090            String filename = resolveBaseFilename( artifact );
091            if( "artifact".equals( scheme ) )
092            {
093                return filename;
094            }
095            else if( "link".equals( scheme ) )
096            {
097                return filename + ".link";
098            }
099            else
100            {
101                final String error = 
102                  "Protocol not recognized: " + scheme;
103                throw new TransitRuntimeException( error );
104            }
105        }
106    
107        /**
108         * Return the expanded filename of the artifact.
109         * The filename is expressed as <code>[name].[type]</code>.
110         *
111         * @param artifact the artifact to resolve
112         * @return the artifact expanded filename
113         */
114        public String resolveBaseFilename( Artifact artifact )
115        {
116            return artifact.getName() + "." + artifact.getType();
117        }
118    }