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    @Override
051    public void normalize( Model model, ModelBuildingRequest request )
052    {
053        if ( model == null )
054        {
055            return;
056        }
057
058        model.setUrl( normalize( model.getUrl() ) );
059
060        Scm scm = model.getScm();
061        if ( scm != null )
062        {
063            scm.setUrl( normalize( scm.getUrl() ) );
064            scm.setConnection( normalize( scm.getConnection() ) );
065            scm.setDeveloperConnection( normalize( scm.getDeveloperConnection() ) );
066        }
067
068        DistributionManagement dist = model.getDistributionManagement();
069        if ( dist != null )
070        {
071            Site site = dist.getSite();
072            if ( site != null )
073            {
074                site.setUrl( normalize( site.getUrl() ) );
075            }
076        }
077    }
078
079    private String normalize( String url )
080    {
081        return urlNormalizer.normalize( url );
082    }
083
084}