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