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