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.IOException; |
23 | |
import java.io.InputStream; |
24 | |
import java.util.Iterator; |
25 | |
import java.util.jar.JarEntry; |
26 | |
import java.util.jar.JarOutputStream; |
27 | |
|
28 | |
import org.jdom.Attribute; |
29 | |
import org.jdom.Content; |
30 | |
import org.jdom.Document; |
31 | |
import org.jdom.Element; |
32 | |
import org.jdom.JDOMException; |
33 | |
import org.jdom.input.SAXBuilder; |
34 | |
import org.jdom.output.Format; |
35 | |
import org.jdom.output.XMLOutputter; |
36 | |
|
37 | 1 | public class XmlAppendingTransformer |
38 | |
implements ResourceTransformer |
39 | |
{ |
40 | |
public static final String XSI_NS = "http://www.w3.org/2001/XMLSchema-instance"; |
41 | |
|
42 | |
String resource; |
43 | |
Document doc; |
44 | |
|
45 | |
public boolean canTransformResource( String r ) |
46 | |
{ |
47 | 3 | if ( resource != null && resource.equalsIgnoreCase( r ) ) |
48 | |
{ |
49 | 2 | return true; |
50 | |
} |
51 | |
|
52 | 1 | return false; |
53 | |
} |
54 | |
|
55 | |
public void processResource( InputStream is ) |
56 | |
throws IOException |
57 | |
{ |
58 | |
Document r; |
59 | |
try |
60 | |
{ |
61 | 0 | r = new SAXBuilder().build( is ); |
62 | |
} |
63 | 0 | catch ( JDOMException e ) |
64 | |
{ |
65 | 0 | throw new RuntimeException( e ); |
66 | 0 | } |
67 | |
|
68 | 0 | if ( doc == null ) |
69 | |
{ |
70 | 0 | doc = r; |
71 | |
} |
72 | |
else |
73 | |
{ |
74 | 0 | Element root = r.getRootElement(); |
75 | |
|
76 | 0 | for ( Iterator itr = root.getAttributes().iterator(); itr.hasNext(); ) |
77 | |
{ |
78 | 0 | Attribute a = (Attribute) itr.next(); |
79 | 0 | itr.remove(); |
80 | |
|
81 | 0 | Element mergedEl = doc.getRootElement(); |
82 | 0 | Attribute mergedAtt = mergedEl.getAttribute( a.getName(), a.getNamespace() ); |
83 | 0 | if ( mergedAtt == null ) |
84 | |
{ |
85 | 0 | mergedEl.setAttribute( a ); |
86 | |
} |
87 | |
} |
88 | |
|
89 | 0 | for ( Iterator itr = root.getChildren().iterator(); itr.hasNext(); ) |
90 | |
{ |
91 | 0 | Content n = (Content) itr.next(); |
92 | 0 | itr.remove(); |
93 | |
|
94 | 0 | doc.getRootElement().addContent( n ); |
95 | |
} |
96 | |
} |
97 | 0 | } |
98 | |
|
99 | |
public boolean hasTransformedResource() |
100 | |
{ |
101 | 0 | return doc != null; |
102 | |
} |
103 | |
|
104 | |
public void modifyOutputStream( JarOutputStream jos ) |
105 | |
throws IOException |
106 | |
{ |
107 | 0 | jos.putNextEntry( new JarEntry( resource ) ); |
108 | |
|
109 | 0 | new XMLOutputter( Format.getPrettyFormat() ).output( doc, jos ); |
110 | |
|
111 | 0 | doc = null; |
112 | 0 | } |
113 | |
} |