Coverage Report - org.apache.maven.plugins.shade.resource.XmlAppendingTransformer
 
Classes in this File Line Coverage Branch Coverage Complexity
XmlAppendingTransformer
13%
4/30
21%
3/14
3,25
 
 1  
 package org.apache.maven.plugins.shade.resource;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *   http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 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  
 }