1 package org.apache.maven.plugins.shade.resource;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.OutputStream;
27 import java.io.Reader;
28 import java.io.Writer;
29 import java.util.Iterator;
30 import java.util.LinkedHashMap;
31 import java.util.Map;
32 import java.util.jar.JarEntry;
33 import java.util.jar.JarOutputStream;
34
35 import org.codehaus.plexus.util.IOUtil;
36 import org.codehaus.plexus.util.ReaderFactory;
37 import org.codehaus.plexus.util.WriterFactory;
38 import org.codehaus.plexus.util.xml.Xpp3Dom;
39 import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
40 import org.codehaus.plexus.util.xml.Xpp3DomWriter;
41
42
43
44
45
46 public class ComponentsXmlResourceTransformer
47 implements ResourceTransformer
48 {
49 private Map components = new LinkedHashMap();
50
51 public static final String COMPONENTS_XML_PATH = "META-INF/plexus/components.xml";
52
53 public boolean canTransformResource( String resource )
54 {
55 return COMPONENTS_XML_PATH.equals( resource );
56 }
57
58 public void processResource( InputStream is )
59 throws IOException
60 {
61
62
63 File f = File.createTempFile( "maven-shade-plugin", "tmp" );
64
65 f.deleteOnExit();
66
67 OutputStream os = new FileOutputStream( f );
68
69 IOUtil.copy( is, os );
70
71 os.close();
72
73
74
75 Reader reader;
76
77 Xpp3Dom newDom;
78
79 try
80 {
81 reader = ReaderFactory.newXmlReader( f );
82
83 newDom = Xpp3DomBuilder.build( reader );
84 }
85 catch ( Exception e )
86 {
87 throw new IOException( "Error parsing components.xml in " + is );
88 }
89
90
91 if ( newDom.getChild( "components" ) == null )
92 {
93 return;
94 }
95
96 Xpp3Dom[] children = newDom.getChild( "components" ).getChildren( "component" );
97
98 for ( int i = 0; i < children.length; i++ )
99 {
100 Xpp3Dom component = children[i];
101
102 String role = component.getChild( "role" ).getValue();
103
104 Xpp3Dom child = component.getChild( "role-hint" );
105
106 String roleHint = child != null ? child.getValue() : "";
107
108 String key = role + roleHint;
109 if ( components.containsKey( key ) )
110 {
111
112
113
114 Xpp3Dom dom = (Xpp3Dom) components.get( key );
115 if ( dom.getChild( "configuration" ) != null )
116 {
117 component.addChild( dom.getChild( "configuration" ) );
118 }
119 }
120
121 components.put( key, component );
122 }
123 }
124
125 public void modifyOutputStream( JarOutputStream jos )
126 throws IOException
127 {
128 Reader reader = ReaderFactory.newXmlReader( getTransformedResource() );
129
130 jos.putNextEntry( new JarEntry( COMPONENTS_XML_PATH ) );
131
132 IOUtil.copy( reader, jos );
133
134 reader.close();
135
136 components.clear();
137 }
138
139 public boolean hasTransformedResource()
140 {
141 return components.size() > 0;
142 }
143
144 public File getTransformedResource()
145 throws IOException
146 {
147 File f = File.createTempFile( "shade-maven-plugin-plx", "tmp" );
148
149 f.deleteOnExit();
150
151 Writer writer = WriterFactory.newXmlWriter( f );
152 try
153 {
154 Xpp3Dom dom = new Xpp3Dom( "component-set" );
155
156 Xpp3Dom componentDom = new Xpp3Dom( "components" );
157
158 dom.addChild( componentDom );
159
160 for ( Iterator i = components.values().iterator(); i.hasNext(); )
161 {
162 Xpp3Dom component = (Xpp3Dom) i.next();
163 componentDom.addChild( component );
164 }
165
166 Xpp3DomWriter.write( writer, dom );
167 }
168 finally
169 {
170 IOUtil.close( writer );
171 }
172
173 return f;
174 }
175
176
177 }