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                 element( w, "role-hint", cr.getRoleHint() );
213             }
214 
215             element( w, "field-name", cr.getFieldName() );
216 
217             element( w, "optional", cr.isOptional() ? Boolean.TRUE.toString() : null );
218 
219             w.endElement();
220         }
221 
222         w.endElement();
223     }
224 
225     private void writeConfiguration( XMLWriter w, PlexusConfiguration configuration )
226         throws ComponentDescriptorWriteException, PlexusConfigurationException
227     {
228         if ( configuration == null || configuration.getChildCount() == 0 )
229         {
230             return;
231         }
232 
233         if ( !configuration.getName().equals( "configuration" ) )
234         {
235             throw new ComponentDescriptorWriteException( "The root node of the configuration must be 'configuration'.");
236         }
237 
238         writePlexusConfiguration( w, configuration );
239     }
240 
241     private void writePlexusConfiguration( XMLWriter xmlWriter, PlexusConfiguration c )
242         throws PlexusConfigurationException
243     {
244         if ( c.getAttributeNames().length == 0 && c.getChildCount() == 0 && c.getValue() == null )
245         {
246             return;
247         }
248 
249         xmlWriter.startElement( c.getName() );
250 
251         // ----------------------------------------------------------------------
252         // Write the attributes
253         // ----------------------------------------------------------------------
254 
255         String[] attributeNames = c.getAttributeNames();
256 
257         for ( String attributeName : attributeNames )
258         {
259             xmlWriter.addAttribute( attributeName, c.getAttribute( attributeName ) );
260         }
261 
262         // ----------------------------------------------------------------------
263         // Write the children
264         // ----------------------------------------------------------------------
265 
266         PlexusConfiguration[] children = c.getChildren();
267 
268         if ( children.length > 0 )
269         {
270             for ( PlexusConfiguration aChildren : children )
271             {
272                 writePlexusConfiguration( xmlWriter, aChildren );
273             }
274         }
275         else
276         {
277             String value = c.getValue();
278 
279             if ( value != null )
280             {
281                 xmlWriter.writeText( value );
282             }
283         }
284 
285         xmlWriter.endElement();
286     }
287 
288     private void element( XMLWriter w, String name, String value )
289     {
290         if ( value == null )
291         {
292             return;
293         }
294 
295         w.startElement( name );
296 
297         w.writeText( value );
298 
299         w.endElement();
300     }
301 
302 }