001package org.apache.maven.model.building;
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.Model;
023import org.apache.maven.model.composition.DefaultDependencyManagementImporter;
024import org.apache.maven.model.composition.DependencyManagementImporter;
025import org.apache.maven.model.inheritance.DefaultInheritanceAssembler;
026import org.apache.maven.model.inheritance.InheritanceAssembler;
027import org.apache.maven.model.interpolation.ModelInterpolator;
028import org.apache.maven.model.interpolation.StringSearchModelInterpolator;
029import org.apache.maven.model.io.DefaultModelReader;
030import org.apache.maven.model.io.ModelReader;
031import org.apache.maven.model.locator.DefaultModelLocator;
032import org.apache.maven.model.locator.ModelLocator;
033import org.apache.maven.model.management.DefaultDependencyManagementInjector;
034import org.apache.maven.model.management.DefaultPluginManagementInjector;
035import org.apache.maven.model.management.DependencyManagementInjector;
036import org.apache.maven.model.management.PluginManagementInjector;
037import org.apache.maven.model.normalization.DefaultModelNormalizer;
038import org.apache.maven.model.normalization.ModelNormalizer;
039import org.apache.maven.model.path.DefaultModelPathTranslator;
040import org.apache.maven.model.path.DefaultModelUrlNormalizer;
041import org.apache.maven.model.path.DefaultPathTranslator;
042import org.apache.maven.model.path.DefaultUrlNormalizer;
043import org.apache.maven.model.path.ModelPathTranslator;
044import org.apache.maven.model.path.ModelUrlNormalizer;
045import org.apache.maven.model.path.PathTranslator;
046import org.apache.maven.model.path.UrlNormalizer;
047import org.apache.maven.model.plugin.DefaultPluginConfigurationExpander;
048import org.apache.maven.model.plugin.DefaultReportConfigurationExpander;
049import org.apache.maven.model.plugin.DefaultReportingConverter;
050import org.apache.maven.model.plugin.LifecycleBindingsInjector;
051import org.apache.maven.model.plugin.PluginConfigurationExpander;
052import org.apache.maven.model.plugin.ReportConfigurationExpander;
053import org.apache.maven.model.plugin.ReportingConverter;
054import org.apache.maven.model.profile.DefaultProfileInjector;
055import org.apache.maven.model.profile.DefaultProfileSelector;
056import org.apache.maven.model.profile.ProfileInjector;
057import org.apache.maven.model.profile.ProfileSelector;
058import org.apache.maven.model.profile.activation.FileProfileActivator;
059import org.apache.maven.model.profile.activation.JdkVersionProfileActivator;
060import org.apache.maven.model.profile.activation.OperatingSystemProfileActivator;
061import org.apache.maven.model.profile.activation.ProfileActivator;
062import org.apache.maven.model.profile.activation.PropertyProfileActivator;
063import org.apache.maven.model.superpom.DefaultSuperPomProvider;
064import org.apache.maven.model.superpom.SuperPomProvider;
065import org.apache.maven.model.validation.DefaultModelValidator;
066import org.apache.maven.model.validation.ModelValidator;
067
068/**
069 * A factory to create model builder instances when no dependency injection is available. <em>Note:</em> This class is
070 * only meant as a utility for developers that want to employ the model builder outside of the Maven build system, Maven
071 * plugins should always acquire model builder instances via dependency injection. Developers might want to subclass
072 * this factory to provide custom implementations for some of the components used by the model builder.
073 *
074 * @author Benjamin Bentmann
075 */
076public class DefaultModelBuilderFactory
077{
078
079    protected ModelProcessor newModelProcessor()
080    {
081        DefaultModelProcessor processor = new DefaultModelProcessor();
082        processor.setModelLocator( newModelLocator() );
083        processor.setModelReader( newModelReader() );
084        return processor;
085    }
086
087    protected ModelLocator newModelLocator()
088    {
089        return new DefaultModelLocator();
090    }
091
092    protected ModelReader newModelReader()
093    {
094        return new DefaultModelReader();
095    }
096
097    protected ProfileSelector newProfileSelector()
098    {
099        DefaultProfileSelector profileSelector = new DefaultProfileSelector();
100
101        for ( ProfileActivator activator : newProfileActivators() )
102        {
103            profileSelector.addProfileActivator( activator );
104        }
105
106        return profileSelector;
107    }
108
109    protected ProfileActivator[] newProfileActivators()
110    {
111        return new ProfileActivator[] { new JdkVersionProfileActivator(), new OperatingSystemProfileActivator(),
112            new PropertyProfileActivator(), new FileProfileActivator().setPathTranslator( newPathTranslator() ) };
113    }
114
115    protected UrlNormalizer newUrlNormalizer()
116    {
117        return new DefaultUrlNormalizer();
118    }
119
120    protected PathTranslator newPathTranslator()
121    {
122        return new DefaultPathTranslator();
123    }
124
125    protected ModelInterpolator newModelInterpolator()
126    {
127        UrlNormalizer normalizer = newUrlNormalizer();
128        PathTranslator pathTranslator = newPathTranslator();
129        return new StringSearchModelInterpolator().setPathTranslator( pathTranslator ).setUrlNormalizer( normalizer );
130    }
131
132    protected ModelValidator newModelValidator()
133    {
134        return new DefaultModelValidator();
135    }
136
137    protected ModelNormalizer newModelNormalizer()
138    {
139        return new DefaultModelNormalizer();
140    }
141
142    protected ModelPathTranslator newModelPathTranslator()
143    {
144        return new DefaultModelPathTranslator().setPathTranslator( newPathTranslator() );
145    }
146
147    protected ModelUrlNormalizer newModelUrlNormalizer()
148    {
149        return new DefaultModelUrlNormalizer().setUrlNormalizer( newUrlNormalizer() );
150    }
151
152    protected InheritanceAssembler newInheritanceAssembler()
153    {
154        return new DefaultInheritanceAssembler();
155    }
156
157    protected ProfileInjector newProfileInjector()
158    {
159        return new DefaultProfileInjector();
160    }
161
162    protected SuperPomProvider newSuperPomProvider()
163    {
164        return new DefaultSuperPomProvider().setModelProcessor( newModelProcessor() );
165    }
166
167    protected DependencyManagementImporter newDependencyManagementImporter()
168    {
169        return new DefaultDependencyManagementImporter();
170    }
171
172    protected DependencyManagementInjector newDependencyManagementInjector()
173    {
174        return new DefaultDependencyManagementInjector();
175    }
176
177    protected LifecycleBindingsInjector newLifecycleBindingsInjector()
178    {
179        return new StubLifecycleBindingsInjector();
180    }
181
182    protected PluginManagementInjector newPluginManagementInjector()
183    {
184        return new DefaultPluginManagementInjector();
185    }
186
187    protected PluginConfigurationExpander newPluginConfigurationExpander()
188    {
189        return new DefaultPluginConfigurationExpander();
190    }
191
192    protected ReportConfigurationExpander newReportConfigurationExpander()
193    {
194        return new DefaultReportConfigurationExpander();
195    }
196
197    protected ReportingConverter newReportingConverter()
198    {
199        return new DefaultReportingConverter();
200    }
201
202    /**
203     * Creates a new model builder instance.
204     *
205     * @return The new model builder instance, never {@code null}.
206     */
207    public DefaultModelBuilder newInstance()
208    {
209        DefaultModelBuilder modelBuilder = new DefaultModelBuilder();
210
211        modelBuilder.setModelProcessor( newModelProcessor() );
212        modelBuilder.setModelValidator( newModelValidator() );
213        modelBuilder.setModelNormalizer( newModelNormalizer() );
214        modelBuilder.setModelPathTranslator( newModelPathTranslator() );
215        modelBuilder.setModelUrlNormalizer( newModelUrlNormalizer() );
216        modelBuilder.setModelInterpolator( newModelInterpolator() );
217        modelBuilder.setInheritanceAssembler( newInheritanceAssembler() );
218        modelBuilder.setProfileInjector( newProfileInjector() );
219        modelBuilder.setProfileSelector( newProfileSelector() );
220        modelBuilder.setSuperPomProvider( newSuperPomProvider() );
221        modelBuilder.setDependencyManagementImporter( newDependencyManagementImporter() );
222        modelBuilder.setDependencyManagementInjector( newDependencyManagementInjector() );
223        modelBuilder.setLifecycleBindingsInjector( newLifecycleBindingsInjector() );
224        modelBuilder.setPluginManagementInjector( newPluginManagementInjector() );
225        modelBuilder.setPluginConfigurationExpander( newPluginConfigurationExpander() );
226        modelBuilder.setReportConfigurationExpander( newReportConfigurationExpander() );
227        modelBuilder.setReportingConverter( newReportingConverter() );
228
229        return modelBuilder;
230    }
231
232    private static class StubLifecycleBindingsInjector
233        implements LifecycleBindingsInjector
234    {
235
236        @Override
237        public void injectLifecycleBindings( Model model, ModelBuildingRequest request, ModelProblemCollector problems )
238        {
239        }
240
241    }
242
243}