org.apache.maven.project.validation.DefaultModelValidator Maven / Gradle / Ivy
Go to download
This library is used to not only read Maven project object model files, but to assemble inheritence
and to retrieve remote models as required.
package org.apache.maven.project.validation;
/*
* Copyright 2001-2005 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Model;
import java.util.Iterator;
/**
* @author Trygve Laugstøl
* @version $Id: DefaultModelValidator.java 191634 2005-06-21 06:49:49Z brett $
*/
public class DefaultModelValidator
implements ModelValidator
{
///////////////////////////////////////////////////////////////////////////
// ModelValidator Implementation
public ModelValidationResult validate( Model model )
{
ModelValidationResult result = new ModelValidationResult();
validateStringNotEmpty( "modelVersion", result, model.getModelVersion() );
validateStringNotEmpty( "groupId", result, model.getGroupId() );
validateStringNotEmpty( "artifactId", result, model.getArtifactId() );
validateStringNotEmpty( "packaging", result, model.getPackaging() );
validateStringNotEmpty( "version", result, model.getVersion() );
for ( Iterator it = model.getDependencies().iterator(); it.hasNext(); )
{
Dependency d = (Dependency) it.next();
validateStringNotEmpty( "dependencies.dependency.artifactId", result, d.getArtifactId() );
validateStringNotEmpty( "dependencies.dependency.groupId", result, d.getGroupId() );
validateStringNotEmpty( "dependencies.dependency.type", result, d.getType() );
validateStringNotEmpty( "dependencies.dependency.version", result, d.getVersion() );
}
return result;
}
///////////////////////////////////////////////////////////////////////////
// Field validator
/**
* Asserts:
*
*
* string.length != null
* string.length > 0
*
*/
private boolean validateStringNotEmpty( String fieldName, ModelValidationResult result, String string )
{
if ( !validateNotNull( fieldName, result, string ) )
{
return false;
}
if ( string.length() > 0 )
{
return true;
}
result.addMessage( "'" + fieldName + "' is empty." );
return false;
}
/**
* Asserts:
*
*
* string != null
*
*/
private boolean validateNotNull( String fieldName, ModelValidationResult result, Object object )
{
if ( object != null )
{
return true;
}
result.addMessage( "'" + fieldName + "' is missing." );
return false;
}
}