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.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  0
             }
 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  0
             }
 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  
 }