001package org.apache.maven.model.path;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.maven.model.DistributionManagement;
023import org.apache.maven.model.Model;
024import org.apache.maven.model.Scm;
025import org.apache.maven.model.Site;
026import org.apache.maven.model.building.ModelBuildingRequest;
027import org.codehaus.plexus.component.annotations.Component;
028import org.codehaus.plexus.component.annotations.Requirement;
029
030/**
031 * Normalizes URLs to remove the ugly parent references "../" that got potentially inserted by URL adjustment during
032 * model inheritance.
033 *
034 * @author Benjamin Bentmann
035 */
036@Component( role = ModelUrlNormalizer.class )
037public class DefaultModelUrlNormalizer
038    implements ModelUrlNormalizer
039{
040
041    @Requirement
042    private UrlNormalizer urlNormalizer;
043
044    public DefaultModelUrlNormalizer setUrlNormalizer( UrlNormalizer urlNormalizer )
045    {
046        this.urlNormalizer = urlNormalizer;
047        return this;
048    }
049
050    public void normalize( Model model, ModelBuildingRequest request )
051    {
052        if ( model == null )
053        {
054            return;
055        }
056
057        model.setUrl( normalize( model.getUrl() ) );
058
059        Scm scm = model.getScm();
060        if ( scm != null )
061        {
062            scm.setUrl( normalize( scm.getUrl() ) );
063            scm.setConnection( normalize( scm.getConnection() ) );
064            scm.setDeveloperConnection( normalize( scm.getDeveloperConnection() ) );
065        }
066
067        DistributionManagement dist = model.getDistributionManagement();
068        if ( dist != null )
069        {
070            Site site = dist.getSite();
071            if ( site != null )
072            {
073                site.setUrl( normalize( site.getUrl() ) );
074            }
075        }
076    }
077
078    private String normalize( String url )
079    {
080        return urlNormalizer.normalize( url );
081    }
082
083}