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