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