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 | 2 | 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 | 2 | this.profileName = profileName; |
39 | |
|
40 | 2 | if ( this.profileName == null ) |
41 | |
{ |
42 | 2 | loadDefaultProfileName( url ); |
43 | |
} |
44 | |
|
45 | 2 | this.convertFormatterToString( url ); |
46 | |
|
47 | 2 | return this; |
48 | |
} |
49 | |
|
50 | |
private void loadDefaultProfileName( URL url ) |
51 | |
throws MojoExecutionException |
52 | |
{ |
53 | 2 | Reader reader = null; |
54 | |
try |
55 | |
{ |
56 | 2 | reader = new InputStreamReader( url.openStream() ); |
57 | 2 | Xpp3Dom dom = Xpp3DomBuilder.build( reader ); |
58 | |
|
59 | 2 | Xpp3Dom[] existingProfiles = dom.getChildren( ELT_PROFILE ); |
60 | 2 | if ( existingProfiles.length != 0 ) |
61 | |
{ |
62 | 2 | Xpp3Dom firstProfile = existingProfiles[0]; |
63 | 2 | 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 | 2 | IOUtil.close( reader ); |
77 | 2 | } |
78 | 2 | } |
79 | |
|
80 | |
private void convertFormatterToString( URL url ) |
81 | |
throws MojoExecutionException |
82 | |
{ |
83 | 2 | InputStream is = null; |
84 | |
|
85 | 2 | ByteArrayOutputStream os = new ByteArrayOutputStream(); |
86 | |
|
87 | |
try |
88 | |
{ |
89 | 2 | is = url.openStream(); |
90 | |
|
91 | 2 | 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 | 2 | IOUtil.close( is ); |
100 | 2 | } |
101 | |
|
102 | 2 | content = os.toString(); |
103 | |
|
104 | 2 | } |
105 | |
|
106 | |
public String getContent() |
107 | |
{ |
108 | 0 | return this.content; |
109 | |
} |
110 | |
|
111 | |
public String getProfileName() |
112 | |
{ |
113 | 2 | return this.profileName; |
114 | |
} |
115 | |
|
116 | |
} |