jvanzyl 2003/06/19 16:26:08 Added: src/java/org/apache/maven DefaultProjectMarshaller.java Log: o New POM marshaller. Revision Changes Path 1.1 maven/src/java/org/apache/maven/DefaultProjectMarshaller.java Index: DefaultProjectMarshaller.java =================================================================== package org.apache.maven; import org.apache.maven.project.Build; import org.apache.maven.project.Contributor; import org.apache.maven.project.Dependency; import org.apache.maven.project.Developer; import org.apache.maven.project.License; import org.apache.maven.project.MailingList; import org.apache.maven.project.Project; import org.apache.maven.project.Resource; import org.apache.maven.project.SourceModification; import org.apache.maven.project.Version; import org.xmlpull.v1.XmlPullParserFactory; import org.xmlpull.v1.XmlSerializer; import java.io.IOException; import java.io.Writer; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.SortedSet; /** * @author <a href="mailto:[EMAIL PROTECTED]">Gilles Dodinet</a> * @version $Id: DefaultProjectMarshaller.java,v 1.4 2003/05/20 21:42:57 * gdodinet Exp $ */ public class DefaultProjectMarshaller { private final static String NAMESPACE = null; private final static String ENCODING = null; private final static Boolean STANDALONE = null; private XmlSerializer serializer; private static String INDENTATION = " "; private static String LS = System.getProperty( "line.separator" ); /** * Constructor for the DefaultProjectMarshaller object * * @exception Exception */ public DefaultProjectMarshaller() throws Exception { XmlPullParserFactory factory = XmlPullParserFactory.newInstance( System.getProperty( XmlPullParserFactory.PROPERTY_NAME ), null ); serializer = factory.newSerializer(); } /** * Description of the Method */ private void initialize( Writer writer ) throws IOException { serializer.setProperty( "http://xmlpull.org/v1/doc/properties.html#serializer-indentation", INDENTATION ); serializer.setProperty( "http://xmlpull.org/v1/doc/properties.html#serializer-line-separator", LS ); serializer.setOutput( writer ); //serializer.startDocument(ENCODING, STANDALONE); } /** * Description of the Method */ public void marshall( Writer pom, Project project ) throws Exception { initialize( pom ); serializer.startTag( NAMESPACE, "project" ); marshallString( project.getExtend(), "extend" ); marshallString( project.getPomVersion(), "pomVersion" ); // We need to make sure the legacy id comes out. marshallRequiredString( Project.standardToLegacyId( project.getId() ), "id" ); marshallRequiredString( project.getName(), "name" ); marshallString( project.getGroupId(), "groupId" ); marshallString( project.getArtifactId(), "artifactId" ); marshallRequiredString( project.getCurrentVersion(), "currentVersion" ); marshallOrganization( project ); marshallString( project.getInceptionYear(), "inceptionYear" ); marshallString( project.getPackage(), "package" ); marshallString( project.getLogo(), "logo" ); marshallString( project.getGumpRepositoryId(), "gumpRepositoryId" ); marshallString( project.getDescription(), "description" ); marshallRequiredString( project.getShortDescription(), "shortDescription" ); marshallString( project.getUrl(), "url" ); marshallString( project.getIssueTrackingUrl(), "issueTrackingUrl" ); marshallString( project.getSiteAddress(), "siteAddress" ); marshallString( project.getSiteDirectory(), "siteDirectory" ); marshallString( project.getDistributionDirectory(), "distributionDirectory" ); marshallRepository( project ); marshallVersions( project ); marshallMailingLists( project ); marshallDevelopers( project ); marshallContributors( project ); marshallLicenses( project ); marshallDependencies( project ); marshallBuild( project ); marshallReports( project ); marshallProperties( project.getProperties() ); serializer.endTag( NAMESPACE, "project" ); serializer.endDocument(); } /** * Description of the Method */ private void marshallOrganization( Project project ) throws Exception { if ( project.getOrganization() != null ) { serializer.startTag( NAMESPACE, "organization" ); marshallRequiredString( project.getOrganization().getName(), "name" ); marshallString( project.getOrganization().getUrl(), "url" ); marshallString( project.getOrganization().getLogo(), "logo" ); serializer.endTag( NAMESPACE, "organization" ); } } /** * Description of the Method */ private void marshallRepository( Project project ) throws Exception { if ( project.getRepository() != null ) { serializer.startTag( NAMESPACE, "repository" ); marshallRequiredString( project.getRepository().getConnection(), "connection" ); //!!! //marshallString(project.getRepository().getDeveloperConnection(), "developerConnection"); marshallString( project.getRepository().getUrl(), "url" ); serializer.endTag( NAMESPACE, "repository" ); } } /** * Description of the Method */ private void marshallVersions( Project project ) throws Exception { List versions = project.getVersions(); if ( versions != null ) { serializer.startTag( NAMESPACE, "versions" ); Version version; for ( int i = 0; i < versions.size(); i++ ) { version = (Version) versions.get( i ); if ( version != null ) { serializer.startTag( NAMESPACE, "version" ); marshallString( version.getId(), "id" ); marshallString( version.getName(), "name" ); marshallString( version.getTag(), "tag" ); serializer.endTag( NAMESPACE, "version" ); } } serializer.endTag( NAMESPACE, "versions" ); } } /** * Description of the Method */ private void marshallMailingLists( Project project ) throws Exception { List mailingLists = project.getMailingLists(); if ( mailingLists != null ) { serializer.startTag( NAMESPACE, "mailingLists" ); MailingList mailingList; for ( int i = 0; i < mailingLists.size(); i++ ) { mailingList = (MailingList) mailingLists.get( i ); if ( mailingList != null ) { serializer.startTag( NAMESPACE, "mailingList" ); marshallRequiredString( mailingList.getName(), "name" ); marshallRequiredString( mailingList.getSubscribe(), "subscribe" ); marshallRequiredString( mailingList.getUnsubscribe(), "unsubscribe" ); marshallString( mailingList.getArchive(), "archive" ); serializer.endTag( NAMESPACE, "mailingList" ); } } serializer.endTag( NAMESPACE, "mailingLists" ); } } /** * Description of the Method */ private void marshallDevelopers( Project project ) throws Exception { List developers = project.getDevelopers(); serializer.startTag( NAMESPACE, "developers" ); Developer developer; for ( int i = 0; i < developers.size(); i++ ) { developer = (Developer) developers.get( i ); if ( developer != null ) { serializer.startTag( NAMESPACE, "developer" ); marshallRequiredString( developer.getName(), "name" ); marshallRequiredString( developer.getId(), "id" ); //!!! //marshallContactDetails(developer); serializer.endTag( NAMESPACE, "developer" ); } } serializer.endTag( NAMESPACE, "developers" ); } /** * Description of the Method */ private void marshallContactDetails( Contributor contributor ) throws IOException { serializer.startTag( NAMESPACE, "contactDetails" ); marshallRequiredString( contributor.getEmail(), "email" ); marshallString( contributor.getOrganization(), "organization" ); marshallRoles( contributor ); marshallString( contributor.getUrl(), "url" ); //timezone skipped serializer.endTag( NAMESPACE, "contactDetails" ); } /** * Description of the Method */ private void marshallRoles( Contributor contributor ) throws IOException { SortedSet roles = contributor.getRoles(); if ( roles != null ) { serializer.startTag( NAMESPACE, "roles" ); Iterator iterator = roles.iterator(); while ( iterator.hasNext() ) { marshallString( (String) iterator.next(), "role" ); } serializer.endTag( NAMESPACE, "roles" ); } } /** * Description of the Method */ private void marshallContributors( Project project ) throws Exception { List contributors = project.getContributors(); if ( contributors != null && contributors.size() > 0 ) { serializer.startTag( NAMESPACE, "contributors" ); Contributor contributor; for ( int i = 0; i < contributors.size(); i++ ) { contributor = (Contributor) contributors.get( i ); if ( contributor != null ) { serializer.startTag( NAMESPACE, "contributor" ); marshallRequiredString( contributor.getName(), "name" ); //marshallRequiredString(contributor.getEmail(), "email"); //!!! //marshallContactDetails(contributor); serializer.endTag( NAMESPACE, "contributor" ); } } serializer.endTag( NAMESPACE, "contributors" ); } } /** * Description of the Method */ private void marshallLicenses( Project project ) throws Exception { List licenses = project.getLicenses(); if ( licenses != null && licenses.size() > 0 ) { serializer.startTag( NAMESPACE, "licenses" ); License license; for ( int i = 0; i < licenses.size(); i++ ) { license = (License) licenses.get( i ); if ( license != null ) { serializer.startTag( NAMESPACE, "license" ); marshallString( license.getName(), "name" ); marshallString( license.getUrl(), "url" ); marshallString( license.getDistribution(), "distribution" ); serializer.endTag( NAMESPACE, "license" ); } } serializer.endTag( NAMESPACE, "licenses" ); } } /** * Description of the Method */ private void marshallDependencies( Project project ) throws Exception { List dependencies = project.getDependencies(); if ( dependencies != null && dependencies.size() > 0 ) { serializer.startTag( NAMESPACE, "dependencies" ); Dependency dependency; for ( int i = 0; i < dependencies.size(); i++ ) { dependency = (Dependency) dependencies.get( i ); if ( dependency != null ) { serializer.startTag( NAMESPACE, "dependency" ); marshallString( Project.standardToLegacyId( dependency.getId() ), "id" ); marshallString( dependency.getGroupId(), "groupId" ); marshallString( dependency.getArtifactId(), "artifactId" ); marshallRequiredString( dependency.getVersion(), "version" ); marshallString( dependency.getJar(), "jar" ); marshallString( dependency.getType(), "type" ); marshallString( dependency.getUrl(), "url" ); marshallProperties( dependency.getProperties() ); serializer.endTag( NAMESPACE, "dependency" ); } } serializer.endTag( NAMESPACE, "dependencies" ); } } /** * Description of the Method */ private void marshallBuild( Project project ) throws Exception { Build build = project.getBuild(); if ( build != null ) { serializer.startTag( NAMESPACE, "build" ); marshallString( build.getNagEmailAddress(), "nagEmailAddress" ); marshallString( build.getSourceDirectory(), "sourceDirectory" ); marshallString( build.getUnitTestSourceDirectory(), "unitTestSourceDirectory" ); marshallString( build.getIntegrationUnitTestSourceDirectory(), "integrationUnitTestSourceDirectory" ); marshallString( build.getAspectSourceDirectory(), "aspectSourceDirectory" ); if ( build.getUnitTest() != null ) { serializer.startTag( NAMESPACE, "unitTest" ); marshallIncludes( build.getUnitTest().getIncludes() ); marshallExcludes( build.getUnitTest().getExcludes() ); marshallResources( build.getUnitTest().getResources() ); serializer.endTag( NAMESPACE, "unitTest" ); } //project.build.integrationUnitTest ? marshallResources( build.getResources() ); marshallSourceModifications( build.getSourceModifications() ); serializer.endTag( NAMESPACE, "build" ); } } private void marshallSourceModifications( List sourceModifications ) throws Exception { if ( sourceModifications != null && sourceModifications.size() > 0 ) { serializer.startTag( NAMESPACE, "sourceModifications" ); SourceModification sourceModification; for ( int i = 0; i < sourceModifications.size(); i++ ) { sourceModification = (SourceModification) sourceModifications.get( i ); if ( sourceModification != null ) { serializer.startTag( NAMESPACE, "sourceModification" ); marshallString( sourceModification.getClassName(), "className" ); //marshallString( sourceModification.getProperty(), "property" ); marshallExcludes( sourceModification.getExcludes() ); serializer.endTag( NAMESPACE, "sourceModification" ); } } serializer.endTag( NAMESPACE, "sourceModifications" ); } } /** * Description of the Method */ private void marshallResources( List resources ) throws IOException { if ( resources != null && resources.size() > 0 ) { serializer.startTag( NAMESPACE, "resources" ); Resource resource; for ( int i = 0; i < resources.size(); i++ ) { resource = (Resource) resources.get( i ); if ( resource != null ) { serializer.startTag( NAMESPACE, "resource" ); marshallString( resource.getDirectory(), "directory" ); marshallString( resource.getTargetPath(), "targetPath" ); marshallIncludes( resource.getIncludes() ); marshallExcludes( resource.getExcludes() ); serializer.endTag( NAMESPACE, "resource" ); } } serializer.endTag( NAMESPACE, "resources" ); } } /** * Description of the Method */ private void marshallExcludes( List excludes ) throws IOException { if ( excludes != null && excludes.size() > 0 ) { serializer.startTag( NAMESPACE, "excludes" ); for ( int i = 0; i < excludes.size(); i++ ) { marshallRequiredString( (String) excludes.get( i ), "exclude" ); } serializer.endTag( NAMESPACE, "excludes" ); } } //see if we can merge marshallIncludes and marshallExcludes in a smart way /** * Description of the Method */ private void marshallIncludes( List includes ) throws IOException { if ( includes != null && includes.size() > 0 ) { serializer.startTag( NAMESPACE, "includes" ); for ( int i = 0; i < includes.size(); i++ ) { marshallRequiredString( (String) includes.get( i ), "include" ); } serializer.endTag( NAMESPACE, "includes" ); } } /** * Description of the Method */ private void marshallReports( Project project ) throws Exception { List reports = project.getReports(); if ( reports != null && reports.size() > 0 ) { serializer.startTag( NAMESPACE, "reports" ); for ( int i = 0; i < reports.size(); i++ ) { marshallString( (String) reports.get( i ), "report" ); } serializer.endTag( NAMESPACE, "reports" ); } } private void marshallProperties( Map properties ) throws Exception { if ( properties != null && properties.size() > 0 ) { serializer.startTag( NAMESPACE, "properties" ); for ( Iterator i = properties.keySet().iterator(); i.hasNext(); ) { String key = (String) i.next(); String value = (String) properties.get( key ); marshallString( value, key ); } serializer.endTag( NAMESPACE, "properties" ); } } /** * Description of the Method */ private void marshallString( String line, String tag ) throws IOException { if ( !isNull( line ) ) { serializer.startTag( NAMESPACE, tag ).text( line ).endTag( NAMESPACE, tag ); } } /** * Description of the Method */ private void marshallRequiredString( String line, String tag ) throws IOException { if ( isNull( line ) ) { System.out.println( "WARNING : " + tag + " should not be null" ); } marshallString( line, tag ); } /** * Gets the null attribute of the DefaultProjectMarshaller object */ private boolean isNull( String property ) { return property == null || property.trim().equals( "" ); } }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
