You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by ep...@apache.org on 2006/03/16 04:39:21 UTC
svn commit: r386249 - in /maven/plugins/trunk/maven-idea-plugin: ./
src/main/java/org/apache/maven/plugin/idea/
Author: epunzalan
Date: Wed Mar 15 19:39:19 2006
New Revision: 386249
URL: http://svn.apache.org/viewcvs?rev=386249&view=rev
Log:
PR: MIDEA-32
Submitted by: Johann Reyes
replaced xpp3Dom with dom4j
Modified:
maven/plugins/trunk/maven-idea-plugin/pom.xml
maven/plugins/trunk/maven-idea-plugin/src/main/java/org/apache/maven/plugin/idea/AbstractIdeaMojo.java
maven/plugins/trunk/maven-idea-plugin/src/main/java/org/apache/maven/plugin/idea/IdeaModuleMojo.java
maven/plugins/trunk/maven-idea-plugin/src/main/java/org/apache/maven/plugin/idea/IdeaProjectMojo.java
maven/plugins/trunk/maven-idea-plugin/src/main/java/org/apache/maven/plugin/idea/IdeaWorkspaceMojo.java
Modified: maven/plugins/trunk/maven-idea-plugin/pom.xml
URL: http://svn.apache.org/viewcvs/maven/plugins/trunk/maven-idea-plugin/pom.xml?rev=386249&r1=386248&r2=386249&view=diff
==============================================================================
--- maven/plugins/trunk/maven-idea-plugin/pom.xml (original)
+++ maven/plugins/trunk/maven-idea-plugin/pom.xml Wed Mar 15 19:39:19 2006
@@ -48,5 +48,10 @@
<artifactId>wagon-provider-api</artifactId>
<version>1.0-alpha-5</version>
</dependency>
+ <dependency>
+ <groupId>dom4j</groupId>
+ <artifactId>dom4j</artifactId>
+ <version>1.6.1</version>
+ </dependency>
</dependencies>
</project>
Modified: maven/plugins/trunk/maven-idea-plugin/src/main/java/org/apache/maven/plugin/idea/AbstractIdeaMojo.java
URL: http://svn.apache.org/viewcvs/maven/plugins/trunk/maven-idea-plugin/src/main/java/org/apache/maven/plugin/idea/AbstractIdeaMojo.java?rev=386249&r1=386248&r2=386249&view=diff
==============================================================================
--- maven/plugins/trunk/maven-idea-plugin/src/main/java/org/apache/maven/plugin/idea/AbstractIdeaMojo.java (original)
+++ maven/plugins/trunk/maven-idea-plugin/src/main/java/org/apache/maven/plugin/idea/AbstractIdeaMojo.java Wed Mar 15 19:39:19 2006
@@ -34,13 +34,16 @@
import org.apache.maven.project.ProjectBuildingException;
import org.apache.maven.project.artifact.InvalidDependencyVersionException;
import org.codehaus.plexus.util.StringUtils;
-import org.codehaus.plexus.util.xml.Xpp3Dom;
+import org.dom4j.Element;
+import org.dom4j.DocumentException;
+import org.dom4j.Document;
+import org.dom4j.io.SAXReader;
+import org.dom4j.io.XMLWriter;
+import org.dom4j.io.OutputFormat;
import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.FileReader;
-import java.io.InputStreamReader;
-import java.io.Reader;
+import java.io.FileWriter;
+import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
@@ -116,6 +119,36 @@
this.overwrite = overwrite;
}
+ protected Document readXmlDocument( File file, String altFilename )
+ throws DocumentException
+ {
+ SAXReader reader = new SAXReader();
+ if ( file.exists() && !overwrite )
+ {
+ return reader.read( file );
+ }
+ else
+ {
+ File altFile = new File( project.getBasedir(), "src/main/idea/" + altFilename );
+ if ( altFile.exists() )
+ {
+ return reader.read( altFile );
+ }
+ else
+ {
+ return reader.read( getClass().getResourceAsStream( "/templates/default/" + altFilename ) );
+ }
+ }
+ }
+
+ protected void writeXmlDocument( File file, Document document )
+ throws IOException
+ {
+ XMLWriter writer = new XMLWriter( new FileWriter( file ), OutputFormat.createPrettyPrint() );
+ writer.write( document );
+ writer.close();
+ }
+
/**
* Finds element from the module element.
*
@@ -123,33 +156,27 @@
* @param name Name attribute to find
* @return component Returns the Xpp3Dom element found.
*/
- protected Xpp3Dom findComponent( Xpp3Dom module, String name )
+ protected Element findComponent( Element module, String name )
+ {
+ return findElement( module, "component", name );
+ }
+
+ protected Element findElement( Element element, String elementName, String attributeName )
{
- Xpp3Dom[] components = module.getChildren( "component" );
- for ( int i = 0; i < components.length; i++ )
+ for ( Iterator children = element.elementIterator( elementName ); children.hasNext(); )
{
- if ( name.equals( components[i].getAttribute( "name" ) ) )
+ Element child = (Element) children.next();
+ if ( attributeName.equals( child.attributeValue( "name" ) ) )
{
- return components[i];
+ return child;
}
}
-
- Xpp3Dom component = createElement( module, "component" );
- component.setAttribute( "name", name );
- return component;
+ return createElement( element, elementName ).addAttribute( "name", attributeName );
}
- /**
- * Finds an element from Xpp3Dom component.
- *
- * @param component Xpp3Dom component
- * @param name Name of the element to find.
- * @return the element
- */
- protected Xpp3Dom findElement( Xpp3Dom component, String name )
+ protected Element findElement( Element component, String name )
{
- Xpp3Dom element = component.getChild( name );
-
+ Element element = component.element( name );
if ( element == null )
{
element = createElement( component, name );
@@ -164,11 +191,9 @@
* @param name Name of the element
* @return component Xpp3Dom element
*/
- protected Xpp3Dom createElement( Xpp3Dom module, String name )
+ protected Element createElement( Element module, String name )
{
- Xpp3Dom component = new Xpp3Dom( name );
- module.addChild( component );
- return component;
+ return module.addElement( name );
}
/**
@@ -202,15 +227,14 @@
* @param content Xpp3Dom element
* @param name Name of the element to be removed
*/
- protected void removeOldElements( Xpp3Dom content, String name )
+ protected void removeOldElements( Element content, String name )
{
- Xpp3Dom[] children = content.getChildren();
- for ( int i = children.length - 1; i >= 0; i-- )
+ for ( Iterator children = content.elementIterator(); children.hasNext(); )
{
- Xpp3Dom child = children[i];
- if ( child.getName().equals( name ) )
+ Element child = (Element) children.next();
+ if ( name.equals( child.getName() ) )
{
- content.removeChild( i );
+ content.remove( child );
}
}
}
@@ -243,7 +267,7 @@
msg.append( "An error occurred during dependency resolution.\n\n" );
msg.append( " Failed to retrieve " + e.getDownloadUrl() + "\n" );
msg.append( "from the following repositories:" );
- for( Iterator repositories = e.getRemoteRepositories().iterator(); repositories.hasNext(); )
+ for ( Iterator repositories = e.getRemoteRepositories().iterator(); repositories.hasNext(); )
{
ArtifactRepository repository = (ArtifactRepository) repositories.next();
msg.append( "\n " + repository.getId() + "(" + repository.getUrl() + ")" );
@@ -346,28 +370,5 @@
}
return log;
- }
-
- protected Reader getXmlReader( String file )
- {
- File altFile = new File( project.getBasedir(), "src/main/idea/" + file );
- if ( altFile.exists() )
- {
- try
- {
- return new FileReader( altFile );
- }
- catch ( FileNotFoundException e )
- {
- // this shouldn't happen, since we just verified it exists,
- // but we'll print the error out in the off change
- getLog().error( "File not found even though we just verified it exists. Failing.", e );
- throw new RuntimeException( e );
- }
- }
- else
- {
- return new InputStreamReader( getClass().getResourceAsStream( "/templates/default/" + file ) );
- }
}
}
Modified: maven/plugins/trunk/maven-idea-plugin/src/main/java/org/apache/maven/plugin/idea/IdeaModuleMojo.java
URL: http://svn.apache.org/viewcvs/maven/plugins/trunk/maven-idea-plugin/src/main/java/org/apache/maven/plugin/idea/IdeaModuleMojo.java?rev=386249&r1=386248&r2=386249&view=diff
==============================================================================
--- maven/plugins/trunk/maven-idea-plugin/src/main/java/org/apache/maven/plugin/idea/IdeaModuleMojo.java (original)
+++ maven/plugins/trunk/maven-idea-plugin/src/main/java/org/apache/maven/plugin/idea/IdeaModuleMojo.java Wed Mar 15 19:39:19 2006
@@ -28,20 +28,14 @@
import org.apache.maven.project.MavenProject;
import org.apache.maven.wagon.ResourceDoesNotExistException;
import org.apache.maven.wagon.TransferFailedException;
-import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.StringUtils;
-import org.codehaus.plexus.util.xml.Xpp3Dom;
-import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
-import org.codehaus.plexus.util.xml.Xpp3DomWriter;
-import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
+import org.dom4j.Element;
+import org.dom4j.Document;
+import org.dom4j.DocumentException;
import java.io.File;
-import java.io.FileReader;
-import java.io.FileWriter;
import java.io.IOException;
-import java.io.Reader;
import java.util.ArrayList;
-import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
@@ -217,25 +211,9 @@
File moduleFile = new File( project.getBasedir(), project.getArtifactId() + ".iml" );
try
{
- Reader reader;
- if ( moduleFile.exists() && !overwrite )
- {
- reader = new FileReader( moduleFile );
- }
- else
- {
- reader = getXmlReader( "module.xml" );
- }
+ Document document = readXmlDocument( moduleFile, "module.xml" );
- Xpp3Dom module;
- try
- {
- module = Xpp3DomBuilder.build( reader );
- }
- finally
- {
- IOUtil.close( reader );
- }
+ Element module = document.getRootElement();
// TODO: how can we let the WAR/EJBs plugin hook in and provide this?
// TODO: merge in ejb-module, etc.
@@ -248,14 +226,14 @@
addEjbModule( module );
}
- Xpp3Dom component = findComponent( module, "NewModuleRootManager" );
- Xpp3Dom output = findElement( component, "output" );
- output.setAttribute( "url", getModuleFileUrl( project.getBuild().getOutputDirectory() ) );
+ Element component = findComponent( module, "NewModuleRootManager" );
+ Element output = findElement( component, "output" );
+ output.addAttribute( "url", getModuleFileUrl( project.getBuild().getOutputDirectory() ) );
- Xpp3Dom outputTest = findElement( component, "output-test" );
- outputTest.setAttribute( "url", getModuleFileUrl( project.getBuild().getTestOutputDirectory() ) );
+ Element outputTest = findElement( component, "output-test" );
+ outputTest.addAttribute( "url", getModuleFileUrl( project.getBuild().getTestOutputDirectory() ) );
- Xpp3Dom content = findElement( component, "content" );
+ Element content = findElement( component, "content" );
removeOldElements( content, "sourceFolder" );
@@ -291,7 +269,7 @@
File classes = new File( project.getBuild().getOutputDirectory() );
File testClasses = new File( project.getBuild().getTestOutputDirectory() );
- List sourceFolders = Arrays.asList( content.getChildren( "sourceFolder" ) );
+ List sourceFolders = content.elements( "sourceFolder" );
List filteredExcludes = new ArrayList();
filteredExcludes.addAll( getExcludedDirectories( target, filteredExcludes, sourceFolders ) );
@@ -303,7 +281,7 @@
String[] dirs = exclude.split( "[,\\s]+" );
for ( int i = 0; i < dirs.length; i++ )
{
- File excludedDir = new File( dirs[ i ] );
+ File excludedDir = new File( dirs[i] );
filteredExcludes.add( getExcludedDirectories( excludedDir, filteredExcludes, sourceFolders ) );
}
}
@@ -336,25 +314,24 @@
moduleName = a.getArtifactId();
}
- Xpp3Dom dep = null;
+ Element dep = null;
- Xpp3Dom[] orderEntries = component.getChildren( "orderEntry" );
- for ( int idx = 0; idx < orderEntries.length; idx++ )
+ for ( Iterator children = component.elementIterator( "orderEntry" ); children.hasNext(); )
{
- Xpp3Dom orderEntry = orderEntries[idx];
+ Element orderEntry = (Element) children.next();
- if ( orderEntry.getAttribute( "type" ).equals( "module" ) )
+ if ( orderEntry.attributeValue( "type" ).equals( "module" ) )
{
- if ( orderEntry.getAttribute( "module-name" ).equals( moduleName ) )
+ if ( orderEntry.attributeValue( "module-name" ).equals( moduleName ) )
{
dep = orderEntry;
break;
}
}
- else if ( orderEntry.getAttribute( "type" ).equals( "module-library" ) )
+ else if ( orderEntry.attributeValue( "type" ).equals( "module-library" ) )
{
- Xpp3Dom lib = orderEntry.getChild( "library" );
- if ( lib.getAttribute( "name" ).equals( moduleName ) )
+ Element lib = orderEntry.element( "library" );
+ if ( lib.attributeValue( "name" ).equals( moduleName ) )
{
dep = orderEntry;
break;
@@ -374,19 +351,19 @@
if ( isIdeaModule )
{
- dep.setAttribute( "type", "module" );
- dep.setAttribute( "module-name", moduleName );
+ dep.addAttribute( "type", "module" );
+ dep.addAttribute( "module-name", moduleName );
}
}
if ( a.getFile() != null && !isIdeaModule )
{
- dep.setAttribute( "type", "module-library" );
+ dep.addAttribute( "type", "module-library" );
removeOldElements( dep, "library" );
dep = createElement( dep, "library" );
- dep.setAttribute( "name", moduleName );
+ dep.addAttribute( "name", moduleName );
- Xpp3Dom el = createElement( dep, "CLASSES" );
+ Element el = createElement( dep, "CLASSES" );
if ( library != null && library.getSplitClasses().length > 0 )
{
String[] libraryClasses = library.getSplitClasses();
@@ -394,29 +371,29 @@
{
String classpath = libraryClasses[k];
extractMacro( classpath );
- Xpp3Dom classEl = createElement( el, "root" );
- classEl.setAttribute( "url", classpath );
+ Element classEl = createElement( el, "root" );
+ classEl.addAttribute( "url", classpath );
}
}
else
{
el = createElement( el, "root" );
File file = a.getFile();
- el.setAttribute( "url", "jar://" + file.getAbsolutePath().replace( '\\', '/' ) + "!/" );
+ el.addAttribute( "url", "jar://" + file.getAbsolutePath().replace( '\\', '/' ) + "!/" );
}
boolean usedSources = false;
if ( library != null && library.getSplitSources().length > 0 )
{
- Xpp3Dom sourcesElement = createElement( dep, "SOURCES" );
+ Element sourcesElement = createElement( dep, "SOURCES" );
usedSources = true;
String[] sources = library.getSplitSources();
for ( int k = 0; k < sources.length; k++ )
{
String source = sources[k];
extractMacro( source );
- Xpp3Dom sourceEl = createElement( sourcesElement, "root" );
- sourceEl.setAttribute( "url", source );
+ Element sourceEl = createElement( sourcesElement, "root" );
+ sourceEl.addAttribute( "url", source );
}
}
@@ -440,17 +417,9 @@
addResources( component, resourceDir );
}
- FileWriter writer = new FileWriter( moduleFile );
- try
- {
- Xpp3DomWriter.write( writer, module );
- }
- finally
- {
- IOUtil.close( writer );
- }
+ writeXmlDocument( moduleFile, document );
}
- catch ( XmlPullParserException e )
+ catch ( DocumentException e )
{
throw new MojoExecutionException( "Error parsing existing IML file " + moduleFile.getAbsolutePath(), e );
}
@@ -460,16 +429,16 @@
}
}
- private void addEjbModule( Xpp3Dom module )
+ private void addEjbModule( Element module )
{
- module.setAttribute( "type", "J2EE_EJB_MODULE" );
+ module.addAttribute( "type", "J2EE_EJB_MODULE" );
String explodedDir = project.getBuild().getDirectory() + "/" + project.getArtifactId();
- Xpp3Dom component = findComponent( module, "EjbModuleBuildComponent" );
+ Element component = findComponent( module, "EjbModuleBuildComponent" );
- Xpp3Dom setting = findSetting( component, "EXPLODED_URL" );
- setting.setAttribute( "value", getModuleFileUrl( explodedDir ) );
+ Element setting = findSetting( component, "EXPLODED_URL" );
+ setting.addAttribute( "value", getModuleFileUrl( explodedDir ) );
component = findComponent( module, "EjbModuleProperties" );
@@ -479,7 +448,7 @@
{
Artifact artifact = (Artifact) i.next();
- Xpp3Dom containerElement = createElement( component, "containerElement" );
+ Element containerElement = createElement( component, "containerElement" );
boolean linkAsModule = false;
if ( linkModules )
@@ -489,35 +458,35 @@
if ( linkAsModule )
{
- containerElement.setAttribute( "type", "module" );
- containerElement.setAttribute( "name", artifact.getArtifactId() );
- Xpp3Dom methodAttribute = createElement( containerElement, "attribute" );
- methodAttribute.setAttribute( "name", "method" );
- methodAttribute.setAttribute( "value", "6" );
- Xpp3Dom uriAttribute = createElement( containerElement, "attribute" );
- uriAttribute.setAttribute( "name", "URI" );
- uriAttribute.setAttribute( "value", "/WEB-INF/classes" );
+ containerElement.addAttribute( "type", "module" );
+ containerElement.addAttribute( "name", artifact.getArtifactId() );
+ Element methodAttribute = createElement( containerElement, "attribute" );
+ methodAttribute.addAttribute( "name", "method" );
+ methodAttribute.addAttribute( "value", "6" );
+ Element uriAttribute = createElement( containerElement, "attribute" );
+ uriAttribute.addAttribute( "name", "URI" );
+ uriAttribute.addAttribute( "value", "/WEB-INF/classes" );
}
else if ( artifact.getFile() != null )
{
- containerElement.setAttribute( "type", "library" );
- containerElement.setAttribute( "level", "module" );
- containerElement.setAttribute( "name", artifact.getArtifactId() );
- Xpp3Dom methodAttribute = createElement( containerElement, "attribute" );
- methodAttribute.setAttribute( "name", "method" );
- methodAttribute.setAttribute( "value", "2" );
- Xpp3Dom uriAttribute = createElement( containerElement, "attribute" );
- uriAttribute.setAttribute( "name", "URI" );
- uriAttribute.setAttribute( "value", "/WEB-INF/lib/" + artifact.getFile().getName() );
+ containerElement.addAttribute( "type", "library" );
+ containerElement.addAttribute( "level", "module" );
+ containerElement.addAttribute( "name", artifact.getArtifactId() );
+ Element methodAttribute = createElement( containerElement, "attribute" );
+ methodAttribute.addAttribute( "name", "method" );
+ methodAttribute.addAttribute( "value", "2" );
+ Element uriAttribute = createElement( containerElement, "attribute" );
+ uriAttribute.addAttribute( "name", "URI" );
+ uriAttribute.addAttribute( "value", "/WEB-INF/lib/" + artifact.getFile().getName() );
}
}
- Xpp3Dom element = findElement( component, "deploymentDescriptor" );
- if ( element.getAttribute( "name" ) == null )
+ Element element = findElement( component, "deploymentDescriptor" );
+ if ( element.attributeValue( "name" ) == null )
{
- element.setAttribute( "name", "ejb-jar.xml" );
+ element.addAttribute( "name", "ejb-jar.xml" );
}
- element.setAttribute( "url", getModuleFileUrl( "src/main/resources/META-INF/ejb-jar.xml" ) );
+ element.addAttribute( "url", getModuleFileUrl( "src/main/resources/META-INF/ejb-jar.xml" ) );
}
private void extractMacro( String path )
@@ -574,7 +543,7 @@
boolean addToExclude = true;
for ( Iterator sources = sourceFolders.iterator(); sources.hasNext(); )
{
- String source = ( (Xpp3Dom) sources.next() ).getAttribute( "url" );
+ String source = ( (Element) sources.next() ).attributeValue( "url" );
if ( source.equals( url ) )
{
addToExclude = false;
@@ -618,7 +587,7 @@
*
* @param module Xpp3Dom element
*/
- private void addWebModule( Xpp3Dom module )
+ private void addWebModule( Element module )
{
// TODO: this is bad - reproducing war plugin defaults, etc!
// --> this is where the OGNL out of a plugin would be helpful as we could run package first and
@@ -647,11 +616,11 @@
String warSrc = "src/main/webapp";
String webXml = warSrc + "/WEB-INF/web.xml";
- module.setAttribute( "type", "J2EE_WEB_MODULE" );
+ module.addAttribute( "type", "J2EE_WEB_MODULE" );
- Xpp3Dom component = findComponent( module, "WebModuleBuildComponent" );
- Xpp3Dom setting = findSetting( component, "EXPLODED_URL" );
- setting.setAttribute( "value", getModuleFileUrl( warWebapp ) );
+ Element component = findComponent( module, "WebModuleBuildComponent" );
+ Element setting = findSetting( component, "EXPLODED_URL" );
+ setting.addAttribute( "value", getModuleFileUrl( warWebapp ) );
component = findComponent( module, "WebModuleProperties" );
@@ -661,7 +630,7 @@
{
Artifact artifact = (Artifact) i.next();
- Xpp3Dom containerElement = createElement( component, "containerElement" );
+ Element containerElement = createElement( component, "containerElement" );
boolean linkAsModule = false;
if ( linkModules )
@@ -671,48 +640,48 @@
if ( linkAsModule )
{
- containerElement.setAttribute( "type", "module" );
- containerElement.setAttribute( "name", artifact.getArtifactId() );
- Xpp3Dom methodAttribute = createElement( containerElement, "attribute" );
- methodAttribute.setAttribute( "name", "method" );
- methodAttribute.setAttribute( "value", "5" );
- Xpp3Dom uriAttribute = createElement( containerElement, "attribute" );
- uriAttribute.setAttribute( "name", "URI" );
- uriAttribute.setAttribute( "value", "/WEB-INF/classes" );
+ containerElement.addAttribute( "type", "module" );
+ containerElement.addAttribute( "name", artifact.getArtifactId() );
+ Element methodAttribute = createElement( containerElement, "attribute" );
+ methodAttribute.addAttribute( "name", "method" );
+ methodAttribute.addAttribute( "value", "5" );
+ Element uriAttribute = createElement( containerElement, "attribute" );
+ uriAttribute.addAttribute( "name", "URI" );
+ uriAttribute.addAttribute( "value", "/WEB-INF/classes" );
}
else if ( artifact.getFile() != null )
{
- containerElement.setAttribute( "type", "library" );
- containerElement.setAttribute( "level", "module" );
- containerElement.setAttribute( "name", artifact.getArtifactId() );
- Xpp3Dom methodAttribute = createElement( containerElement, "attribute" );
- methodAttribute.setAttribute( "name", "method" );
- methodAttribute.setAttribute( "value", "1" ); // IntelliJ 5.0.2 is bugged and doesn't read it
- Xpp3Dom uriAttribute = createElement( containerElement, "attribute" );
- uriAttribute.setAttribute( "name", "URI" );
- uriAttribute.setAttribute( "value", "/WEB-INF/lib/" + artifact.getFile().getName() );
+ containerElement.addAttribute( "type", "library" );
+ containerElement.addAttribute( "level", "module" );
+ containerElement.addAttribute( "name", artifact.getArtifactId() );
+ Element methodAttribute = createElement( containerElement, "attribute" );
+ methodAttribute.addAttribute( "name", "method" );
+ methodAttribute.addAttribute( "value", "1" ); // IntelliJ 5.0.2 is bugged and doesn't read it
+ Element uriAttribute = createElement( containerElement, "attribute" );
+ uriAttribute.addAttribute( "name", "URI" );
+ uriAttribute.addAttribute( "value", "/WEB-INF/lib/" + artifact.getFile().getName() );
}
}
- Xpp3Dom element = findElement( component, "deploymentDescriptor" );
- if ( element.getAttribute( "version" ) == null )
+ Element element = findElement( component, "deploymentDescriptor" );
+ if ( element.attributeValue( "version" ) == null )
{
// TODO: should derive from web.xml - does IDEA do this if omitted?
// element.setAttribute( "version", "2.3" );
}
- if ( element.getAttribute( "name" ) == null )
+ if ( element.attributeValue( "name" ) == null )
{
- element.setAttribute( "name", "web.xml" );
+ element.addAttribute( "name", "web.xml" );
}
- element.setAttribute( "url", getModuleFileUrl( webXml ) );
+ element.addAttribute( "url", getModuleFileUrl( webXml ) );
element = findElement( component, "webroots" );
removeOldElements( element, "root" );
element = createElement( element, "root" );
- element.setAttribute( "relative", "/" );
- element.setAttribute( "url", getModuleFileUrl( warSrc ) );
+ element.addAttribute( "relative", "/" );
+ element.addAttribute( "url", getModuleFileUrl( warSrc ) );
}
/**
@@ -739,20 +708,20 @@
* @param directory Directory to set as url.
* @param isTest True if directory isTestSource.
*/
- private void addSourceFolder( Xpp3Dom content, String directory, boolean isTest )
+ private void addSourceFolder( Element content, String directory, boolean isTest )
{
if ( !StringUtils.isEmpty( directory ) && new File( directory ).isDirectory() )
{
- Xpp3Dom sourceFolder = createElement( content, "sourceFolder" );
- sourceFolder.setAttribute( "url", getModuleFileUrl( directory ) );
- sourceFolder.setAttribute( "isTestSource", Boolean.toString( isTest ) );
+ Element sourceFolder = createElement( content, "sourceFolder" );
+ sourceFolder.addAttribute( "url", getModuleFileUrl( directory ) );
+ sourceFolder.addAttribute( "isTestSource", Boolean.toString( isTest ) );
}
}
- private void addExcludeFolder( Xpp3Dom content, String directory )
+ private void addExcludeFolder( Element content, String directory )
{
- Xpp3Dom excludeFolder = createElement( content, "excludeFolder" );
- excludeFolder.setAttribute( "url", getModuleFileUrl( directory ) );
+ Element excludeFolder = createElement( content, "excludeFolder" );
+ excludeFolder.addAttribute( "url", getModuleFileUrl( directory ) );
}
/**
@@ -760,15 +729,14 @@
*
* @param component Xpp3Dom element
*/
- private void removeOldDependencies( Xpp3Dom component )
+ private void removeOldDependencies( Element component )
{
- Xpp3Dom[] children = component.getChildren();
- for ( int i = children.length - 1; i >= 0; i-- )
+ for ( Iterator children = component.elementIterator(); children.hasNext(); )
{
- Xpp3Dom child = children[i];
- if ( "orderEntry".equals( child.getName() ) && "module-library".equals( child.getAttribute( "type" ) ) )
+ Element child = (Element) children.next();
+ if ( "orderEntry".equals( child.getName() ) && "module-library".equals( child.attributeValue( "type" ) ) )
{
- component.removeChild( i );
+ component.remove( child );
}
}
}
@@ -789,7 +757,7 @@
return false;
}
- private void resolveClassifier( Xpp3Dom element, Artifact a, String classifier )
+ private void resolveClassifier( Element element, Artifact a, String classifier )
{
String id = a.getId() + '-' + classifier;
@@ -810,7 +778,7 @@
{
String jarPath = "jar://" + path + "!/";
getLog().debug( "Setting " + classifier + " for " + id + " to " + jarPath );
- createElement( element, "root" ).setAttribute( "url", jarPath );
+ createElement( element, "root" ).addAttribute( "url", jarPath );
}
}
@@ -854,16 +822,16 @@
}
}
- private void addResources( Xpp3Dom component, String directory )
+ private void addResources( Element component, String directory )
{
- Xpp3Dom dep = createElement( component, "orderEntry" );
- dep.setAttribute( "type", "module-library" );
+ Element dep = createElement( component, "orderEntry" );
+ dep.addAttribute( "type", "module-library" );
dep = createElement( dep, "library" );
- dep.setAttribute( "name", "resources" );
+ dep.addAttribute( "name", "resources" );
- Xpp3Dom el = createElement( dep, "CLASSES" );
+ Element el = createElement( dep, "CLASSES" );
el = createElement( el, "root" );
- el.setAttribute( "url", getModuleFileUrl( directory ) );
+ el.addAttribute( "url", getModuleFileUrl( directory ) );
}
/**
@@ -873,19 +841,8 @@
* @param name Setting attribute to find
* @return setting Xpp3Dom element
*/
- private Xpp3Dom findSetting( Xpp3Dom component, String name )
+ private Element findSetting( Element component, String name )
{
- Xpp3Dom[] settings = component.getChildren( "setting" );
- for ( int i = 0; i < settings.length; i++ )
- {
- if ( name.equals( settings[i].getAttribute( "name" ) ) )
- {
- return settings[i];
- }
- }
-
- Xpp3Dom setting = createElement( component, "setting" );
- setting.setAttribute( "name", name );
- return setting;
+ return findElement( component, "setting", name );
}
}
Modified: maven/plugins/trunk/maven-idea-plugin/src/main/java/org/apache/maven/plugin/idea/IdeaProjectMojo.java
URL: http://svn.apache.org/viewcvs/maven/plugins/trunk/maven-idea-plugin/src/main/java/org/apache/maven/plugin/idea/IdeaProjectMojo.java?rev=386249&r1=386248&r2=386249&view=diff
==============================================================================
--- maven/plugins/trunk/maven-idea-plugin/src/main/java/org/apache/maven/plugin/idea/IdeaProjectMojo.java (original)
+++ maven/plugins/trunk/maven-idea-plugin/src/main/java/org/apache/maven/plugin/idea/IdeaProjectMojo.java Wed Mar 15 19:39:19 2006
@@ -23,18 +23,13 @@
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.MavenProject;
-import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.StringUtils;
-import org.codehaus.plexus.util.xml.Xpp3Dom;
-import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
-import org.codehaus.plexus.util.xml.Xpp3DomWriter;
-import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
+import org.dom4j.Element;
+import org.dom4j.Document;
+import org.dom4j.DocumentException;
import java.io.File;
-import java.io.FileReader;
-import java.io.FileWriter;
import java.io.IOException;
-import java.io.Reader;
import java.util.Iterator;
import java.util.Set;
import java.util.StringTokenizer;
@@ -133,27 +128,12 @@
throws MojoExecutionException
{
File projectFile = new File( project.getBasedir(), project.getArtifactId() + ".ipr" );
+
try
{
- Reader reader;
- if ( projectFile.exists() && !overwrite )
- {
- reader = new FileReader( projectFile );
- }
- else
- {
- reader = getXmlReader( "project.xml" );
- }
+ Document document = readXmlDocument( projectFile, "project.xml" );
- Xpp3Dom module;
- try
- {
- module = Xpp3DomBuilder.build( reader );
- }
- finally
- {
- IOUtil.close( reader );
- }
+ Element module = document.getRootElement();
// Set the jdk name if set
if ( jdkName != null )
@@ -179,17 +159,17 @@
setWildcardResourcePatterns( module, wildcardResourcePatterns );
- Xpp3Dom component = findComponent( module, "ProjectModuleManager" );
- Xpp3Dom modules = findElement( component, "modules" );
+ Element component = findComponent( module, "ProjectModuleManager" );
+ Element modules = findElement( component, "modules" );
removeOldElements( modules, "module" );
if ( project.getCollectedProjects().size() > 0 )
{
- Xpp3Dom m = createElement( modules, "module" );
+ Element m = createElement( modules, "module" );
String projectPath =
new File( project.getBasedir(), project.getArtifactId() + ".iml" ).getAbsolutePath();
- m.setAttribute( "filepath", "$PROJECT_DIR$/" + toRelative( project.getBasedir(), projectPath ) );
+ m.addAttribute( "filepath", "$PROJECT_DIR$/" + toRelative( project.getBasedir(), projectPath ) );
for ( Iterator i = project.getCollectedProjects().iterator(); i.hasNext(); )
{
@@ -197,41 +177,33 @@
m = createElement( modules, "module" );
String modulePath = new File( p.getBasedir(), p.getArtifactId() + ".iml" ).getAbsolutePath();
- m.setAttribute( "filepath", "$PROJECT_DIR$/" + toRelative( project.getBasedir(), modulePath ) );
+ m.addAttribute( "filepath", "$PROJECT_DIR$/" + toRelative( project.getBasedir(), modulePath ) );
}
}
else
{
- Xpp3Dom m = createElement( modules, "module" );
+ Element m = createElement( modules, "module" );
String modulePath =
new File( project.getBasedir(), project.getArtifactId() + ".iml" ).getAbsolutePath();
- m.setAttribute( "filepath", "$PROJECT_DIR$/" + toRelative( project.getBasedir(), modulePath ) );
+ m.addAttribute( "filepath", "$PROJECT_DIR$/" + toRelative( project.getBasedir(), modulePath ) );
}
// add any PathMacros we've come across
- if ( macros != null && module.getChildren( "UsedPathMacros" ).length > 0 )
+ if ( macros != null && module.elements( "UsedPathMacros" ).size() > 0 )
{
- Xpp3Dom usedPathMacros = module.getChildren( "UsedPathMacros" )[0];
+ Element usedPathMacros = (Element) module.elements( "UsedPathMacros" ).get( 0 );
removeOldElements( usedPathMacros, "macro" );
for ( Iterator iterator = macros.iterator(); iterator.hasNext(); )
{
String macro = (String) iterator.next();
- Xpp3Dom macroElement = createElement( usedPathMacros, "macro" );
- macroElement.setAttribute( "name", macro );
+ Element macroElement = createElement( usedPathMacros, "macro" );
+ macroElement.addAttribute( "name", macro );
}
}
- FileWriter writer = new FileWriter( projectFile );
- try
- {
- Xpp3DomWriter.write( writer, module );
- }
- finally
- {
- IOUtil.close( writer );
- }
+ writeXmlDocument( projectFile, document );
}
- catch ( XmlPullParserException e )
+ catch ( DocumentException e )
{
throw new MojoExecutionException( "Error parsing existing IPR file: " + projectFile.getAbsolutePath(), e );
}
@@ -247,10 +219,10 @@
* @param content Xpp3Dom element.
* @param jdkName Name of the JDK to use.
*/
- private void setJdkName( Xpp3Dom content, String jdkName )
+ private void setJdkName( Element content, String jdkName )
{
- Xpp3Dom component = findComponent( content, "ProjectRootManager" );
- component.setAttribute( "project-jdk-name", jdkName );
+ Element component = findComponent( content, "ProjectRootManager" );
+ component.addAttribute( "project-jdk-name", jdkName );
String jdkLevel = this.jdkLevel;
if ( jdkLevel == null )
@@ -260,17 +232,17 @@
if ( jdkLevel.startsWith( "1.4" ) )
{
- component.setAttribute( "assert-keyword", "true" );
- component.setAttribute( "jdk-15", "false" );
+ component.addAttribute( "assert-keyword", "true" );
+ component.addAttribute( "jdk-15", "false" );
}
else if ( jdkLevel.compareTo( "1.5" ) >= 0 )
{
- component.setAttribute( "assert-keyword", "true" );
- component.setAttribute( "jdk-15", "true" );
+ component.addAttribute( "assert-keyword", "true" );
+ component.addAttribute( "jdk-15", "true" );
}
else
{
- component.setAttribute( "assert-keyword", "false" );
+ component.addAttribute( "assert-keyword", "false" );
}
}
@@ -280,20 +252,20 @@
* @param content Xpp3Dom element.
* @param wildcardResourcePatterns The wilcard resource patterns.
*/
- private void setWildcardResourcePatterns( Xpp3Dom content, String wildcardResourcePatterns )
+ private void setWildcardResourcePatterns( Element content, String wildcardResourcePatterns )
{
- Xpp3Dom compilerConfigurationElement = findComponent( content, "CompilerConfiguration" );
+ Element compilerConfigurationElement = findComponent( content, "CompilerConfiguration" );
if ( !StringUtils.isEmpty( wildcardResourcePatterns ) )
{
removeOldElements( compilerConfigurationElement, "wildcardResourcePatterns" );
- Xpp3Dom wildcardResourcePatternsElement =
+ Element wildcardResourcePatternsElement =
createElement( compilerConfigurationElement, "wildcardResourcePatterns" );
StringTokenizer wildcardResourcePatternsTokenizer = new StringTokenizer( wildcardResourcePatterns, ";" );
while ( wildcardResourcePatternsTokenizer.hasMoreTokens() )
{
String wildcardResourcePattern = wildcardResourcePatternsTokenizer.nextToken();
- Xpp3Dom entryElement = createElement( wildcardResourcePatternsElement, "entry" );
- entryElement.setAttribute( "name", wildcardResourcePattern );
+ Element entryElement = createElement( wildcardResourcePatternsElement, "entry" );
+ entryElement.addAttribute( "name", wildcardResourcePattern );
}
}
}
Modified: maven/plugins/trunk/maven-idea-plugin/src/main/java/org/apache/maven/plugin/idea/IdeaWorkspaceMojo.java
URL: http://svn.apache.org/viewcvs/maven/plugins/trunk/maven-idea-plugin/src/main/java/org/apache/maven/plugin/idea/IdeaWorkspaceMojo.java?rev=386249&r1=386248&r2=386249&view=diff
==============================================================================
--- maven/plugins/trunk/maven-idea-plugin/src/main/java/org/apache/maven/plugin/idea/IdeaWorkspaceMojo.java (original)
+++ maven/plugins/trunk/maven-idea-plugin/src/main/java/org/apache/maven/plugin/idea/IdeaWorkspaceMojo.java Wed Mar 15 19:39:19 2006
@@ -17,23 +17,17 @@
*/
import org.apache.maven.plugin.MojoExecutionException;
-import org.codehaus.plexus.util.IOUtil;
-import org.codehaus.plexus.util.xml.Xpp3Dom;
-import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
-import org.codehaus.plexus.util.xml.Xpp3DomWriter;
-import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
+import org.dom4j.Element;
+import org.dom4j.Document;
+import org.dom4j.DocumentException;
import java.io.File;
-import java.io.FileReader;
-import java.io.FileWriter;
import java.io.IOException;
-import java.io.Reader;
/**
* @author Edwin Punzalan
* @goal workspace
* @execute phase="generate-sources"
- * @todo use dom4j or something. Xpp3Dom can't cope properly with entities and so on
*/
public class IdeaWorkspaceMojo
extends AbstractIdeaMojo
@@ -64,31 +58,17 @@
{
File workspaceFile = new File( project.getBasedir(), project.getArtifactId() + ".iws" );
- FileWriter writer = null;
-
- Reader reader = null;
-
- Xpp3Dom module;
-
try
{
- if ( workspaceFile.exists() && !overwrite )
- {
- reader = new FileReader( workspaceFile );
- }
- else
- {
- reader = getXmlReader( "workspace.xml" );
- }
- module = Xpp3DomBuilder.build( reader );
+ Document document = readXmlDocument( workspaceFile, "workspace.xml" );
- setProjectScmType( module );
+ Element module = document.getRootElement();
- writer = new FileWriter( workspaceFile );
+ setProjectScmType( module );
- Xpp3DomWriter.write( writer, module );
+ writeXmlDocument( workspaceFile, document );
}
- catch ( XmlPullParserException e )
+ catch ( DocumentException e )
{
throw new MojoExecutionException( "Error parsing existing IWS file: " + workspaceFile.getAbsolutePath(),
e );
@@ -97,30 +77,22 @@
{
throw new MojoExecutionException( "Unable to create workspace file", e );
}
- finally
- {
- IOUtil.close( reader );
-
- IOUtil.close( writer );
- }
}
/**
* Sets the SCM type of the project
*/
- private void setProjectScmType( Xpp3Dom content )
+ private void setProjectScmType( Element content )
{
- String scmType;
-
- scmType = getScmType();
+ String scmType = getScmType();
if ( scmType != null )
{
- Xpp3Dom component = findComponent( content, "VcsManagerConfiguration" );
+ Element component = findComponent( content, "VcsManagerConfiguration" );
- Xpp3Dom element = findElementName( component, "option", "ACTIVE_VCS_NAME" );
+ Element element = findElement( component, "option", "ACTIVE_VCS_NAME" );
- element.setAttribute( "value", scmType );
+ element.addAttribute( "value", scmType );
}
}
@@ -169,28 +141,5 @@
}
}
return null;
- }
-
- /**
- * Returns a an Xpp3Dom element with (child) tag name and (name) attribute name.
- *
- * @param component Xpp3Dom element
- * @param name Setting attribute to find
- * @return option Xpp3Dom element
- */
- private Xpp3Dom findElementName( Xpp3Dom component, String child, String name )
- {
- Xpp3Dom[] elements = component.getChildren( child );
- for ( int i = 0; i < elements.length; i++ )
- {
- if ( name.equals( elements[i].getAttribute( "name" ) ) )
- {
- return elements[i];
- }
- }
-
- Xpp3Dom element = createElement( component, child );
- element.setAttribute( "name", name );
- return element;
}
}