| 1 | |
package org.apache.maven.plugin.eclipse.writers.workspace; |
| 2 | |
|
| 3 | |
import java.io.ByteArrayOutputStream; |
| 4 | |
import java.io.IOException; |
| 5 | |
import java.io.InputStream; |
| 6 | |
import java.io.InputStreamReader; |
| 7 | |
import java.io.Reader; |
| 8 | |
import java.net.URL; |
| 9 | |
|
| 10 | |
import org.apache.maven.plugin.MojoExecutionException; |
| 11 | |
import org.apache.maven.plugin.eclipse.Messages; |
| 12 | |
import org.codehaus.plexus.util.IOUtil; |
| 13 | |
import org.codehaus.plexus.util.xml.Xpp3Dom; |
| 14 | |
import org.codehaus.plexus.util.xml.Xpp3DomBuilder; |
| 15 | |
import org.codehaus.plexus.util.xml.pull.XmlPullParserException; |
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | 1 | public class EclipseCodeFormatterProfile |
| 24 | |
{ |
| 25 | |
private static final String ELT_PROFILE = "profile"; |
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
private String content; |
| 31 | |
|
| 32 | |
private String profileName; |
| 33 | |
|
| 34 | |
public EclipseCodeFormatterProfile init( URL url, String profileName ) |
| 35 | |
throws MojoExecutionException |
| 36 | |
{ |
| 37 | |
|
| 38 | 1 | this.profileName = profileName; |
| 39 | |
|
| 40 | 1 | if ( this.profileName == null ) |
| 41 | |
{ |
| 42 | 1 | loadDefaultProfileName( url ); |
| 43 | |
} |
| 44 | |
|
| 45 | 1 | this.convertFormatterToString( url ); |
| 46 | |
|
| 47 | 1 | return this; |
| 48 | |
} |
| 49 | |
|
| 50 | |
private void loadDefaultProfileName( URL url ) |
| 51 | |
throws MojoExecutionException |
| 52 | |
{ |
| 53 | 1 | Reader reader = null; |
| 54 | |
try |
| 55 | |
{ |
| 56 | 1 | reader = new InputStreamReader( url.openStream() ); |
| 57 | 1 | Xpp3Dom dom = Xpp3DomBuilder.build( reader ); |
| 58 | |
|
| 59 | 1 | Xpp3Dom[] existingProfiles = dom.getChildren( ELT_PROFILE ); |
| 60 | 1 | if ( existingProfiles.length != 0 ) |
| 61 | |
{ |
| 62 | 1 | Xpp3Dom firstProfile = existingProfiles[0]; |
| 63 | 1 | this.profileName = firstProfile.getAttribute( "name" ); |
| 64 | |
} |
| 65 | |
} |
| 66 | 0 | catch ( XmlPullParserException e ) |
| 67 | |
{ |
| 68 | 0 | throw new MojoExecutionException( Messages.getString( "EclipsePlugin.cantparseexisting", url.toString() ) ); |
| 69 | |
} |
| 70 | 0 | catch ( IOException e ) |
| 71 | |
{ |
| 72 | 0 | throw new MojoExecutionException( Messages.getString( "EclipsePlugin.cantparseexisting", url.toString() ) ); |
| 73 | |
} |
| 74 | |
finally |
| 75 | |
{ |
| 76 | 1 | IOUtil.close( reader ); |
| 77 | 1 | } |
| 78 | 1 | } |
| 79 | |
|
| 80 | |
private void convertFormatterToString( URL url ) |
| 81 | |
throws MojoExecutionException |
| 82 | |
{ |
| 83 | 1 | InputStream is = null; |
| 84 | |
|
| 85 | 1 | ByteArrayOutputStream os = new ByteArrayOutputStream(); |
| 86 | |
|
| 87 | |
try |
| 88 | |
{ |
| 89 | 1 | is = url.openStream(); |
| 90 | |
|
| 91 | 1 | IOUtil.copy( is, os ); |
| 92 | |
} |
| 93 | 0 | catch ( IOException e ) |
| 94 | |
{ |
| 95 | 0 | throw new MojoExecutionException( Messages.getString( "EclipsePlugin.cantreadfile", url.toString() ), e ); |
| 96 | |
} |
| 97 | |
finally |
| 98 | |
{ |
| 99 | 1 | IOUtil.close( is ); |
| 100 | 1 | } |
| 101 | |
|
| 102 | 1 | content = os.toString(); |
| 103 | |
|
| 104 | 1 | } |
| 105 | |
|
| 106 | |
public String getContent() |
| 107 | |
{ |
| 108 | 0 | return this.content; |
| 109 | |
} |
| 110 | |
|
| 111 | |
public String getProfileName() |
| 112 | |
{ |
| 113 | 1 | return this.profileName; |
| 114 | |
} |
| 115 | |
|
| 116 | |
} |