View Javadoc
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  public class DefaultComponentDescriptorWriter
49      implements ComponentDescriptorWriter
50  {
51      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              XMLWriter w = new PrettyPrintXMLWriter( writer );
59  
60              w.startElement( containerDescriptor ? "plexus" : "component-set" );
61  
62              writeComponents( w, componentSetDescriptor.getComponents() );
63  
64              writeDependencies( w, componentSetDescriptor.getDependencies() );
65  
66              w.endElement();
67  
68              writer.write( LS );
69  
70              // Flush, but don't close the writer... we are not its owner
71              writer.flush();
72          }
73          catch ( PlexusConfigurationException e )
74          {
75              throw new ComponentDescriptorWriteException( "Internal error while writing out the configuration", e );
76          }
77  
78      }
79  
80      private void writeComponents( XMLWriter w, List<ComponentDescriptor<?>> componentDescriptors )
81          throws ComponentDescriptorWriteException, PlexusConfigurationException
82      {
83          if ( componentDescriptors == null )
84          {
85              return;
86          }
87  
88          w.startElement( "components" );
89  
90          for ( ComponentDescriptor<?> cd : componentDescriptors )
91          {
92              w.startElement( "component" );
93  
94              element( w, "role", cd.getRole() );
95              
96              element( w, "role-hint", cd.getRoleHint() );
97  
98              element( w, "implementation", cd.getImplementation() );
99  
100             element( w, "version", cd.getVersion() );
101 
102             element( w, "component-type", cd.getComponentType() );
103 
104             element( w, "instantiation-strategy", cd.getInstantiationStrategy() );
105 
106             element( w, "lifecycle-handler", cd.getLifecycleHandler() );
107 
108             element( w, "component-profile", cd.getComponentProfile() );
109 
110             element( w, "component-composer", cd.getComponentComposer() );
111 
112             element( w, "component-configurator", cd.getComponentConfigurator() );
113 
114             element( w, "component-factory", cd.getComponentFactory() );
115 
116             element( w, "description", cd.getDescription() );
117 
118             element( w, "alias", cd.getAlias() );
119 
120             element( w, "isolated-realm", Boolean.toString( cd.isIsolatedRealm() ) );
121 
122             writeRequirements( w, cd.getRequirements() );
123 
124             writeConfiguration( w, cd.getConfiguration() );
125 
126             w.endElement();
127         }
128 
129         w.endElement();
130     }
131 
132     public void writeDependencies( XMLWriter w, List<ComponentDependency> deps )
133     {
134         if ( deps == null || deps.size() == 0 )
135         {
136             return;
137         }
138 
139         w.startElement( "dependencies" );
140 
141         for ( ComponentDependency dep : deps )
142         {
143             writeDependencyElement( dep, w );
144         }
145 
146         w.endElement();
147     }
148 
149     private void writeDependencyElement( ComponentDependency dependency, XMLWriter w )
150     {
151         w.startElement( "dependency" );
152 
153         String groupId = dependency.getGroupId();
154 
155         element( w, "groupId", groupId );
156 
157         String artifactId = dependency.getArtifactId();
158 
159         element( w, "artifactId", artifactId );
160 
161         String type = dependency.getType();
162 
163         if ( type != null )
164         {
165             element( w, "type", type );
166         }
167 
168         String version = dependency.getVersion();
169 
170         element( w, "version", version );
171 
172         w.endElement();
173     }
174 
175     private void writeRequirements( XMLWriter w, List<ComponentRequirement> requirements )
176     {
177         if ( requirements == null || requirements.size() == 0 )
178         {
179             return;
180         }
181 
182         w.startElement( "requirements" );
183         
184         for ( ComponentRequirement cr : requirements )
185         {
186             w.startElement( "requirement" );
187 
188             element( w, "role", cr.getRole() );
189 
190             if ( cr instanceof ComponentRequirementList )
191             {
192                 List<String> hints = ( (ComponentRequirementList) cr ).getRoleHints();
193 
194                 if ( hints != null )
195                 {
196                     w.startElement( "role-hints" );
197 
198                     for ( String roleHint : hints )
199                     {
200                         w.startElement( "role-hint" );
201 
202                         w.writeText( roleHint );
203 
204                         w.endElement();
205                     }
206 
207                     w.endElement();
208                 }
209             }
210             else
211             {
212                 // ensure there's no <role-hint/> written, which causes ComponentLookupException
213                 element( w, "role-hint", "".equals( cr.getRoleHint() ) ? null : cr.getRoleHint() );
214             }
215 
216             element( w, "field-name", cr.getFieldName() );
217 
218             element( w, "optional", cr.isOptional() ? Boolean.TRUE.toString() : null );
219 
220             w.endElement();
221         }
222 
223         w.endElement();
224     }
225 
226     private void writeConfiguration( XMLWriter w, PlexusConfiguration configuration )
227         throws ComponentDescriptorWriteException, PlexusConfigurationException
228     {
229         if ( configuration == null || configuration.getChildCount() == 0 )
230         {
231             return;
232         }
233 
234         if ( !configuration.getName().equals( "configuration" ) )
235         {
236             throw new ComponentDescriptorWriteException( "The root node of the configuration must be 'configuration'.");
237         }
238 
239         writePlexusConfiguration( w, configuration );
240     }
241 
242     private void writePlexusConfiguration( XMLWriter xmlWriter, PlexusConfiguration c )
243         throws PlexusConfigurationException
244     {
245         if ( c.getAttributeNames().length == 0 && c.getChildCount() == 0 && c.getValue() == null )
246         {
247             return;
248         }
249 
250         xmlWriter.startElement( c.getName() );
251 
252         // ----------------------------------------------------------------------
253         // Write the attributes
254         // ----------------------------------------------------------------------
255 
256         String[] attributeNames = c.getAttributeNames();
257 
258         for ( String attributeName : attributeNames )
259         {
260             xmlWriter.addAttribute( attributeName, c.getAttribute( attributeName ) );
261         }
262 
263         // ----------------------------------------------------------------------
264         // Write the children
265         // ----------------------------------------------------------------------
266 
267         PlexusConfiguration[] children = c.getChildren();
268 
269         if ( children.length > 0 )
270         {
271             for ( PlexusConfiguration aChildren : children )
272             {
273                 writePlexusConfiguration( xmlWriter, aChildren );
274             }
275         }
276         else
277         {
278             String value = c.getValue();
279 
280             if ( value != null )
281             {
282                 xmlWriter.writeText( value );
283             }
284         }
285 
286         xmlWriter.endElement();
287     }
288 
289     private void element( XMLWriter w, String name, String value )
290     {
291         if ( value == null )
292         {
293             return;
294         }
295 
296         w.startElement( name );
297 
298         w.writeText( value );
299 
300         w.endElement();
301     }
302 
303 }