Coverage Report - org.codehaus.plexus.metadata.DefaultComponentDescriptorWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultComponentDescriptorWriter
43 %
47/109
22 %
11/50
5,25
 
 1  
 /*
 2  
  * The MIT License
 3  
  *
 4  
  * Copyright (c) 2007, The Codehaus
 5  
  *
 6  
  * Permission is hereby granted, free of charge, to any person obtaining a copy of
 7  
  * this software and associated documentation files (the "Software"), to deal in
 8  
  * the Software without restriction, including without limitation the rights to
 9  
  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 10  
  * of the Software, and to permit persons to whom the Software is furnished to do
 11  
  * so, subject to the following conditions:
 12  
  *
 13  
  * The above copyright notice and this permission notice shall be included in all
 14  
  * copies or substantial portions of the Software.
 15  
  *
 16  
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 17  
  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 18  
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 19  
  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 20  
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 21  
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 22  
  * SOFTWARE.
 23  
  */
 24  
 
 25  
 package org.codehaus.plexus.metadata;
 26  
 
 27  
 import java.io.IOException;
 28  
 import java.io.Writer;
 29  
 import java.util.List;
 30  
 
 31  
 import org.codehaus.plexus.component.repository.ComponentDependency;
 32  
 import org.codehaus.plexus.component.repository.ComponentDescriptor;
 33  
 import org.codehaus.plexus.component.repository.ComponentRequirement;
 34  
 import org.codehaus.plexus.component.repository.ComponentRequirementList;
 35  
 import org.codehaus.plexus.component.repository.ComponentSetDescriptor;
 36  
 import org.codehaus.plexus.configuration.PlexusConfiguration;
 37  
 import org.codehaus.plexus.configuration.PlexusConfigurationException;
 38  
 import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
 39  
 import org.codehaus.plexus.util.xml.XMLWriter;
 40  
 
 41  
 /**
 42  
  * Serializes a {@link ComponentSetDescriptor}.
 43  
  *
 44  
  * @author <a href="mailto:kenney@neonics.com">Kenney Westerhof</a>
 45  
  * @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
 46  
  * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
 47  
  */
 48  1
 public class DefaultComponentDescriptorWriter
 49  
     implements ComponentDescriptorWriter
 50  
 {
 51  1
     private static final String LS = System.getProperty( "line.separator" );
 52  
 
 53  
     public void writeDescriptorSet( Writer writer, ComponentSetDescriptor componentSetDescriptor, boolean containerDescriptor )
 54  
         throws ComponentDescriptorWriteException, IOException
 55  
     {
 56  
         try
 57  
         {
 58  1
             XMLWriter w = new PrettyPrintXMLWriter( writer );
 59  
 
 60  1
             w.startElement( containerDescriptor ? "plexus" : "component-set" );
 61  
 
 62  1
             writeComponents( w, componentSetDescriptor.getComponents() );
 63  
 
 64  1
             writeDependencies( w, componentSetDescriptor.getDependencies() );
 65  
 
 66  1
             w.endElement();
 67  
 
 68  1
             writer.write( LS );
 69  
 
 70  
             // Flush, but don't close the writer... we are not its owner
 71  1
             writer.flush();
 72  
         }
 73  0
         catch ( PlexusConfigurationException e )
 74  
         {
 75  0
             throw new ComponentDescriptorWriteException( "Internal error while writing out the configuration", e );
 76  1
         }
 77  
 
 78  1
     }
 79  
 
 80  
     private void writeComponents( XMLWriter w, List<ComponentDescriptor<?>> componentDescriptors )
 81  
         throws ComponentDescriptorWriteException, PlexusConfigurationException
 82  
     {
 83  1
         if ( componentDescriptors == null )
 84  
         {
 85  0
             return;
 86  
         }
 87  
 
 88  1
         w.startElement( "components" );
 89  
 
 90  1
         for ( ComponentDescriptor<?> cd : componentDescriptors )
 91  
         {
 92  1
             w.startElement( "component" );
 93  
 
 94  1
             element( w, "role", cd.getRole() );
 95  
             
 96  1
             element( w, "role-hint", cd.getRoleHint() );
 97  
 
 98  1
             element( w, "implementation", cd.getImplementation() );
 99  
 
 100  1
             element( w, "version", cd.getVersion() );
 101  
 
 102  1
             element( w, "component-type", cd.getComponentType() );
 103  
 
 104  1
             element( w, "instantiation-strategy", cd.getInstantiationStrategy() );
 105  
 
 106  1
             element( w, "lifecycle-handler", cd.getLifecycleHandler() );
 107  
 
 108  1
             element( w, "component-profile", cd.getComponentProfile() );
 109  
 
 110  1
             element( w, "component-composer", cd.getComponentComposer() );
 111  
 
 112  1
             element( w, "component-configurator", cd.getComponentConfigurator() );
 113  
 
 114  1
             element( w, "component-factory", cd.getComponentFactory() );
 115  
 
 116  1
             element( w, "description", cd.getDescription() );
 117  
 
 118  1
             element( w, "alias", cd.getAlias() );
 119  
 
 120  1
             element( w, "isolated-realm", Boolean.toString( cd.isIsolatedRealm() ) );
 121  
 
 122  1
             writeRequirements( w, cd.getRequirements() );
 123  
 
 124  1
             writeConfiguration( w, cd.getConfiguration() );
 125  
 
 126  1
             w.endElement();
 127  1
         }
 128  
 
 129  1
         w.endElement();
 130  1
     }
 131  
 
 132  
     public void writeDependencies( XMLWriter w, List<ComponentDependency> deps )
 133  
     {
 134  1
         if ( deps == null || deps.size() == 0 )
 135  
         {
 136  1
             return;
 137  
         }
 138  
 
 139  0
         w.startElement( "dependencies" );
 140  
 
 141  0
         for ( ComponentDependency dep : deps )
 142  
         {
 143  0
             writeDependencyElement( dep, w );
 144  0
         }
 145  
 
 146  0
         w.endElement();
 147  0
     }
 148  
 
 149  
     private void writeDependencyElement( ComponentDependency dependency, XMLWriter w )
 150  
     {
 151  0
         w.startElement( "dependency" );
 152  
 
 153  0
         String groupId = dependency.getGroupId();
 154  
 
 155  0
         element( w, "groupId", groupId );
 156  
 
 157  0
         String artifactId = dependency.getArtifactId();
 158  
 
 159  0
         element( w, "artifactId", artifactId );
 160  
 
 161  0
         String type = dependency.getType();
 162  
 
 163  0
         if ( type != null )
 164  
         {
 165  0
             element( w, "type", type );
 166  
         }
 167  
 
 168  0
         String version = dependency.getVersion();
 169  
 
 170  0
         element( w, "version", version );
 171  
 
 172  0
         w.endElement();
 173  0
     }
 174  
 
 175  
     private void writeRequirements( XMLWriter w, List<ComponentRequirement> requirements )
 176  
     {
 177  1
         if ( requirements == null || requirements.size() == 0 )
 178  
         {
 179  1
             return;
 180  
         }
 181  
 
 182  0
         w.startElement( "requirements" );
 183  
         
 184  0
         for ( ComponentRequirement cr : requirements )
 185  
         {
 186  0
             w.startElement( "requirement" );
 187  
 
 188  0
             element( w, "role", cr.getRole() );
 189  
 
 190  0
             if ( cr instanceof ComponentRequirementList )
 191  
             {
 192  0
                 List<String> hints = ( (ComponentRequirementList) cr ).getRoleHints();
 193  
 
 194  0
                 if ( hints != null )
 195  
                 {
 196  0
                     w.startElement( "role-hints" );
 197  
 
 198  0
                     for ( String roleHint : hints )
 199  
                     {
 200  0
                         w.startElement( "role-hint" );
 201  
 
 202  0
                         w.writeText( roleHint );
 203  
 
 204  0
                         w.endElement();
 205  0
                     }
 206  
 
 207  0
                     w.endElement();
 208  
                 }
 209  0
             }
 210  
             else
 211  
             {
 212  0
                 element( w, "role-hint", cr.getRoleHint() );
 213  
             }
 214  
 
 215  0
             element( w, "field-name", cr.getFieldName() );
 216  
 
 217  0
             element( w, "optional", cr.isOptional() ? Boolean.TRUE.toString() : null );
 218  
 
 219  0
             w.endElement();
 220  0
         }
 221  
 
 222  0
         w.endElement();
 223  0
     }
 224  
 
 225  
     private void writeConfiguration( XMLWriter w, PlexusConfiguration configuration )
 226  
         throws ComponentDescriptorWriteException, PlexusConfigurationException
 227  
     {
 228  1
         if ( configuration == null || configuration.getChildCount() == 0 )
 229  
         {
 230  1
             return;
 231  
         }
 232  
 
 233  0
         if ( !configuration.getName().equals( "configuration" ) )
 234  
         {
 235  0
             throw new ComponentDescriptorWriteException( "The root node of the configuration must be 'configuration'.");
 236  
         }
 237  
 
 238  0
         writePlexusConfiguration( w, configuration );
 239  0
     }
 240  
 
 241  
     private void writePlexusConfiguration( XMLWriter xmlWriter, PlexusConfiguration c )
 242  
         throws PlexusConfigurationException
 243  
     {
 244  0
         if ( c.getAttributeNames().length == 0 && c.getChildCount() == 0 && c.getValue() == null )
 245  
         {
 246  0
             return;
 247  
         }
 248  
 
 249  0
         xmlWriter.startElement( c.getName() );
 250  
 
 251  
         // ----------------------------------------------------------------------
 252  
         // Write the attributes
 253  
         // ----------------------------------------------------------------------
 254  
 
 255  0
         String[] attributeNames = c.getAttributeNames();
 256  
 
 257  0
         for ( String attributeName : attributeNames )
 258  
         {
 259  0
             xmlWriter.addAttribute( attributeName, c.getAttribute( attributeName ) );
 260  
         }
 261  
 
 262  
         // ----------------------------------------------------------------------
 263  
         // Write the children
 264  
         // ----------------------------------------------------------------------
 265  
 
 266  0
         PlexusConfiguration[] children = c.getChildren();
 267  
 
 268  0
         if ( children.length > 0 )
 269  
         {
 270  0
             for ( PlexusConfiguration aChildren : children )
 271  
             {
 272  0
                 writePlexusConfiguration( xmlWriter, aChildren );
 273  
             }
 274  
         }
 275  
         else
 276  
         {
 277  0
             String value = c.getValue();
 278  
 
 279  0
             if ( value != null )
 280  
             {
 281  0
                 xmlWriter.writeText( value );
 282  
             }
 283  
         }
 284  
 
 285  0
         xmlWriter.endElement();
 286  0
     }
 287  
 
 288  
     private void element( XMLWriter w, String name, String value )
 289  
     {
 290  14
         if ( value == null )
 291  
         {
 292  9
             return;
 293  
         }
 294  
 
 295  5
         w.startElement( name );
 296  
 
 297  5
         w.writeText( value );
 298  
 
 299  5
         w.endElement();
 300  5
     }
 301  
 
 302  
 }