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