01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */package org.apache.cxf.maven;
19:
20: import java.io.IOException;
21: import java.io.InputStream;
22: import java.util.Iterator;
23: import java.util.jar.JarEntry;
24: import java.util.jar.JarOutputStream;
25:
26: import org.codehaus.mojo.shade.resource.ResourceTransformer;
27: import org.jdom.Attribute;
28: import org.jdom.Content;
29: import org.jdom.Document;
30: import org.jdom.Element;
31: import org.jdom.JDOMException;
32: import org.jdom.input.SAXBuilder;
33: import org.jdom.output.Format;
34: import org.jdom.output.XMLOutputter;
35:
36: public class PluginTransformer implements ResourceTransformer {
37: public static final String XSI_NS = "http://www.w3.org/2001/XMLSchema-instance";
38:
39: String resource;
40: Document doc;
41:
42: public PluginTransformer() {
43: super ();
44: }
45:
46: public boolean canTransformResource(String r) {
47: r = r.toLowerCase();
48:
49: if (resource != null && resource.toLowerCase().equals(r)) {
50: return true;
51: }
52:
53: return false;
54: }
55:
56: public void processResource(InputStream is) throws IOException {
57: Document r;
58: try {
59: r = new SAXBuilder().build(is);
60: } catch (JDOMException e) {
61: throw new RuntimeException(e);
62: }
63:
64: if (doc == null) {
65: doc = r;
66:
67: Element el = doc.getRootElement();
68: el.setAttribute("name", "default");
69: el.setAttribute("provider", "cxf.apache.org");
70: } else {
71: Element root = r.getRootElement();
72:
73: for (Iterator itr = root.getChildren().iterator(); itr
74: .hasNext();) {
75: Content n = (Content) itr.next();
76: itr.remove();
77:
78: doc.getRootElement().addContent(n);
79: }
80: }
81: }
82:
83: public boolean hasTransformedResource() {
84: return true;
85: }
86:
87: public void modifyOutputStream(JarOutputStream jos)
88: throws IOException {
89: jos.putNextEntry(new JarEntry(resource));
90:
91: new XMLOutputter(Format.getPrettyFormat()).output(doc, jos);
92: }
93: }
|