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