blob: 6c55e1c4c61ba60f90b3747821275b6dfdbfdddb [file] [log] [blame]
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Carsten Ziegelerefdde672007-08-20 07:33:20 +000019package org.apache.felix.scrplugin.xml;
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +000020
Carsten Ziegeler55c96d32012-06-13 12:03:35 +000021import java.io.File;
Carsten Ziegelera21b5842013-01-23 13:00:46 +000022import java.io.IOException;
Carsten Ziegeler55c96d32012-06-13 12:03:35 +000023import java.io.InputStream;
24import java.util.ArrayList;
25import java.util.List;
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +000026import java.util.StringTokenizer;
27
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +000028import javax.xml.transform.TransformerException;
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +000029
Carsten Ziegeler59c95d52012-08-31 07:00:42 +000030import org.apache.felix.scrplugin.Log;
31import org.apache.felix.scrplugin.Options;
Felix Meschberger1fd58292009-10-02 12:59:27 +000032import org.apache.felix.scrplugin.SCRDescriptorException;
Carsten Ziegeler59c95d52012-08-31 07:00:42 +000033import org.apache.felix.scrplugin.SCRDescriptorFailureException;
Carsten Ziegeler44ee8942012-06-14 15:03:37 +000034import org.apache.felix.scrplugin.SpecVersion;
Carsten Ziegeler55c96d32012-06-13 12:03:35 +000035import org.apache.felix.scrplugin.description.ClassDescription;
36import org.apache.felix.scrplugin.description.ComponentConfigurationPolicy;
37import org.apache.felix.scrplugin.description.ComponentDescription;
Carsten Ziegeler55c96d32012-06-13 12:03:35 +000038import org.apache.felix.scrplugin.description.PropertyDescription;
39import org.apache.felix.scrplugin.description.PropertyType;
Carsten Ziegeler61a12e22012-07-03 09:33:50 +000040import org.apache.felix.scrplugin.description.PropertyUnbounded;
Carsten Ziegeler55c96d32012-06-13 12:03:35 +000041import org.apache.felix.scrplugin.description.ReferenceCardinality;
42import org.apache.felix.scrplugin.description.ReferenceDescription;
43import org.apache.felix.scrplugin.description.ReferencePolicy;
Carsten Ziegelerd156e362012-06-14 15:34:11 +000044import org.apache.felix.scrplugin.description.ReferencePolicyOption;
Carsten Ziegeler55c96d32012-06-13 12:03:35 +000045import org.apache.felix.scrplugin.description.ReferenceStrategy;
46import org.apache.felix.scrplugin.description.ServiceDescription;
Carsten Ziegeler9a496fe2012-06-27 06:20:28 +000047import org.apache.felix.scrplugin.helper.ComponentContainer;
Carsten Ziegeler73903902013-01-30 21:09:50 +000048import org.apache.felix.scrplugin.helper.ComponentContainerUtil;
49import org.apache.felix.scrplugin.helper.ComponentContainerUtil.ComponentContainerContainer;
Carsten Ziegeler9a496fe2012-06-27 06:20:28 +000050import org.apache.felix.scrplugin.helper.DescriptionContainer;
Carsten Ziegeler55c96d32012-06-13 12:03:35 +000051import org.apache.felix.scrplugin.helper.IssueLog;
Carsten Ziegeler55c96d32012-06-13 12:03:35 +000052import org.xml.sax.Attributes;
53import org.xml.sax.ContentHandler;
54import org.xml.sax.SAXException;
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +000055import org.xml.sax.helpers.AttributesImpl;
56import org.xml.sax.helpers.DefaultHandler;
57
58/**
59 * <code>ComponentDescriptorIO</code>
60 *
61 * is a helper class to read and write component descriptor files.
62 *
63 */
64public class ComponentDescriptorIO {
65
Carsten Ziegeler482c3302012-07-03 09:58:43 +000066 private static final String PROPERTY_ATTR_TYPE = "type";
67
Carsten Ziegeler61a12e22012-07-03 09:33:50 +000068 /** General attribute for the name (component, reference, property) */
69 private static final String ATTR_NAME = "name";
70
71 private static final String ATTR_CARDINALITY = "cardinality";
72
73 private static final String ATTR_DESCRIPTION = "description";
74
75 private static final String ATTR_LABEL = "label";
76
Carsten Ziegelerc9325f72009-06-12 14:06:07 +000077 /** The inner namespace - used for all inner elements. */
Carsten Ziegeler0ad4f782008-08-22 07:27:00 +000078 public static final String INNER_NAMESPACE_URI = "";
79
Carsten Ziegelerc9325f72009-06-12 14:06:07 +000080 /** The prefix used for the namespace. */
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +000081 private static final String PREFIX = "scr";
82
Carsten Ziegelerc9325f72009-06-12 14:06:07 +000083 /** The root element. */
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +000084 private static final String COMPONENTS = "components";
85
Carsten Ziegelerc9325f72009-06-12 14:06:07 +000086 /** The component element. */
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +000087 private static final String COMPONENT = "component";
88
Carsten Ziegelerc9325f72009-06-12 14:06:07 +000089 /** The qualified component element. */
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +000090 private static final String COMPONENT_QNAME = PREFIX + ':' + COMPONENT;
91
Carsten Ziegeler84fc4352009-06-12 14:11:02 +000092 /** The enabled attribute. */
93 private static final String COMPONENT_ATTR_ENABLED = "enabled";
94
Carsten Ziegeler35a53ef2009-06-12 14:48:14 +000095 /** Component: The policy attribute. */
Carsten Ziegelerc9325f72009-06-12 14:06:07 +000096 private static final String COMPONENT_ATTR_POLICY = "configuration-policy";
97
Carsten Ziegeler35a53ef2009-06-12 14:48:14 +000098 /** Component: The factory attribute. */
Carsten Ziegeler84fc4352009-06-12 14:11:02 +000099 private static final String COMPONENT_ATTR_FACTORY = "factory";
100
Carsten Ziegeler35a53ef2009-06-12 14:48:14 +0000101 /** Component: The immediate attribute. */
Carsten Ziegeler84fc4352009-06-12 14:11:02 +0000102 private static final String COMPONENT_ATTR_IMMEDIATE = "immediate";
103
Carsten Ziegeler51c4e862009-07-13 07:18:41 +0000104 /** Component: The activate attribute. */
Carsten Ziegeler35a53ef2009-06-12 14:48:14 +0000105 private static final String COMPONENT_ATTR_ACTIVATE = "activate";
106
Carsten Ziegeler51c4e862009-07-13 07:18:41 +0000107 /** Component: The deactivate attribute. */
Carsten Ziegeler35a53ef2009-06-12 14:48:14 +0000108 private static final String COMPONENT_ATTR_DEACTIVATE = "deactivate";
109
Carsten Ziegeler51c4e862009-07-13 07:18:41 +0000110 /** Component: The modified attribute. */
111 private static final String COMPONENT_ATTR_MODIFIED = "modified";
112
Carsten Ziegeler66005132012-06-14 15:48:32 +0000113 /** Component: The configuration pid attribute. */
114 private static final String COMPONENT_ATTR_CONFIGURATION_PID = "configuration-pid";
115
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000116 private static final String IMPLEMENTATION = "implementation";
117
Carsten Ziegelerd2df1a32008-08-22 12:19:56 +0000118 private static final String IMPLEMENTATION_QNAME = IMPLEMENTATION;
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000119
Carsten Ziegeler61a12e22012-07-03 09:33:50 +0000120 private static final String IMPLEMENTATION_ATTR_CLASS = "class";
121
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000122 private static final String SERVICE = "service";
123
Carsten Ziegelerd2df1a32008-08-22 12:19:56 +0000124 private static final String SERVICE_QNAME = SERVICE;
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000125
Carsten Ziegeler61a12e22012-07-03 09:33:50 +0000126 private static final String SERVICE_ATTR_FACTORY = "servicefactory";
127
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000128 private static final String PROPERTY = "property";
129
Carsten Ziegelerd2df1a32008-08-22 12:19:56 +0000130 private static final String PROPERTY_QNAME = PROPERTY;
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000131
Carsten Ziegeler61a12e22012-07-03 09:33:50 +0000132 private static final String PROPERTY_ATTR_VALUE = "value";
133
Carsten Ziegeler482c3302012-07-03 09:58:43 +0000134 private static final String PROPERTY_ATTR_PRIVATE = "private";
135
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000136 private static final String REFERENCE = "reference";
137
Carsten Ziegelerd2df1a32008-08-22 12:19:56 +0000138 private static final String REFERENCE_QNAME = REFERENCE;
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000139
Carsten Ziegeler61a12e22012-07-03 09:33:50 +0000140 private static final String REFERENCE_ATTR_POLICY = "policy";
141
142 private static final String REFERENCE_ATTR_POLICY_OPTION = "policy-option";
143
144 private static final String REFERENCE_ATTR_UPDATED = "updated";
145
146 private static final String REFERENCE_ATTR_UNBIND = "unbind";
147
148 private static final String REFERENCE_ATTR_BIND = "bind";
149
150 private static final String REFERENCE_ATTR_TARGET = "target";
151
Carsten Ziegeler482c3302012-07-03 09:58:43 +0000152 private static final String REFERENCE_ATTR_STRATEGY = "strategy";
153
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000154 private static final String INTERFACE = "provide";
155
Carsten Ziegelerd2df1a32008-08-22 12:19:56 +0000156 private static final String INTERFACE_QNAME = INTERFACE;
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000157
Carsten Ziegeler61a12e22012-07-03 09:33:50 +0000158 private static final String INTERFACE_ATTR_NAME = "interface";
159
Carsten Ziegeler482c3302012-07-03 09:58:43 +0000160 private static final String PROPERTIES = "properties";
161
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000162 public static List<ClassDescription> read(final InputStream file,
Carsten Ziegeler32064312012-10-04 12:51:49 +0000163 final ClassLoader classLoader,
164 final IssueLog iLog, final String location) throws SCRDescriptorException {
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000165 try {
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000166 final XmlHandler xmlHandler = new XmlHandler(classLoader, iLog, location);
Carsten Ziegeler1da03592007-08-24 07:27:04 +0000167 IOUtils.parse(file, xmlHandler);
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000168 return xmlHandler.components;
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000169 } catch (final TransformerException e) {
Carsten Ziegeler9a8d32e2012-06-27 14:07:35 +0000170 throw new SCRDescriptorException("Unable to read xml", location, e);
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000171 }
172 }
173
174 /**
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000175 * Generate the xml top level element and start streaming
176 * the components.
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000177 *
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000178 * @param components
179 * @param contentHandler
180 * @throws SAXException
181 */
Carsten Ziegeler910f9a32015-02-18 15:22:11 +0000182 private static void generateXML(final DescriptionContainer module,
Carsten Ziegeler32064312012-10-04 12:51:49 +0000183 final List<ComponentContainer> components,
184 final File descriptorFile,
Carsten Ziegelera21b5842013-01-23 13:00:46 +0000185 final Log logger) throws SAXException, IOException, TransformerException {
Carsten Ziegeler59c95d52012-08-31 07:00:42 +0000186 logger.info("Writing " + components.size() + " Service Component Descriptors to "
Carsten Ziegeler32064312012-10-04 12:51:49 +0000187 + descriptorFile);
Carsten Ziegeler59c95d52012-08-31 07:00:42 +0000188 final ContentHandler contentHandler = IOUtils.getSerializer(descriptorFile);
Carsten Ziegelerc9325f72009-06-12 14:06:07 +0000189 // detect namespace to use
Carsten Ziegeler9a496fe2012-06-27 06:20:28 +0000190 final String namespace = module.getOptions().getSpecVersion().getNamespaceUrl();
Carsten Ziegeler98e992a2012-06-13 15:50:00 +0000191
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000192 contentHandler.startDocument();
Carsten Ziegelerc9325f72009-06-12 14:06:07 +0000193 contentHandler.startPrefixMapping(PREFIX, namespace);
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000194
Carsten Ziegeler910f9a32015-02-18 15:22:11 +0000195 IOUtils.newline(contentHandler);
Carsten Ziegeler2cb92bd2015-02-18 15:12:31 +0000196 // wrapper element to generate well formed xml if 0 or more than 1 component
Carsten Ziegeler910f9a32015-02-18 15:22:11 +0000197 int startIndent = 0;
Carsten Ziegeler2cb92bd2015-02-18 15:12:31 +0000198 if ( components.size() != 1 ) {
199 contentHandler.startElement("", ComponentDescriptorIO.COMPONENTS, ComponentDescriptorIO.COMPONENTS, new AttributesImpl());
200 IOUtils.newline(contentHandler);
Carsten Ziegeler910f9a32015-02-18 15:22:11 +0000201 startIndent = 1;
Carsten Ziegeler2cb92bd2015-02-18 15:12:31 +0000202 }
Carsten Ziegeler59c95d52012-08-31 07:00:42 +0000203 for (final ComponentContainer component : components) {
Carsten Ziegeler910f9a32015-02-18 15:22:11 +0000204 generateXML(namespace, module, component, contentHandler, startIndent);
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000205 }
Carsten Ziegeler9a496fe2012-06-27 06:20:28 +0000206
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000207 // end wrapper element
Carsten Ziegeler2cb92bd2015-02-18 15:12:31 +0000208 if ( components.size() != 1 ) {
209 contentHandler.endElement("", ComponentDescriptorIO.COMPONENTS, ComponentDescriptorIO.COMPONENTS);
210 IOUtils.newline(contentHandler);
211 }
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000212 contentHandler.endPrefixMapping(PREFIX);
213 contentHandler.endDocument();
214 }
215
216 /**
Carsten Ziegeler6f98fee2013-09-27 11:40:07 +0000217 * Write the xml for a Component
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000218 *
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000219 * @param component
220 * @param contentHandler
221 * @throws SAXException
222 */
Carsten Ziegeler910f9a32015-02-18 15:22:11 +0000223 private static void generateXML(final String namespace,
Carsten Ziegeler32064312012-10-04 12:51:49 +0000224 final DescriptionContainer module,
225 final ComponentContainer container,
Carsten Ziegeler910f9a32015-02-18 15:22:11 +0000226 final ContentHandler contentHandler,
227 final int indent)
228 throws SAXException {
Carsten Ziegeler9a496fe2012-06-27 06:20:28 +0000229 final ComponentDescription component = container.getComponentDescription();
230
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000231 final AttributesImpl ai = new AttributesImpl();
Carsten Ziegeler9a496fe2012-06-27 06:20:28 +0000232 IOUtils.addAttribute(ai, COMPONENT_ATTR_ENABLED, component.getEnabled());
233 IOUtils.addAttribute(ai, COMPONENT_ATTR_IMMEDIATE, component.getImmediate());
Carsten Ziegeler61a12e22012-07-03 09:33:50 +0000234 IOUtils.addAttribute(ai, ATTR_NAME, component.getName());
Carsten Ziegeler84fc4352009-06-12 14:11:02 +0000235 IOUtils.addAttribute(ai, COMPONENT_ATTR_FACTORY, component.getFactory());
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000236
Carsten Ziegelerc9325f72009-06-12 14:06:07 +0000237 // attributes new in 1.1
Carsten Ziegeler561e4e22012-06-27 06:38:28 +0000238 if (module.getOptions().getSpecVersion().ordinal() >= SpecVersion.VERSION_1_1.ordinal() ) {
Carsten Ziegeler133a6a42012-07-23 08:42:54 +0000239 if ( component.getConfigurationPolicy() != null
240 && component.getConfigurationPolicy() != ComponentConfigurationPolicy.OPTIONAL ) {
Carsten Ziegelerd156e362012-06-14 15:34:11 +0000241 IOUtils.addAttribute(ai, COMPONENT_ATTR_POLICY, component.getConfigurationPolicy().name().toLowerCase());
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000242 }
Carsten Ziegeler35a53ef2009-06-12 14:48:14 +0000243 IOUtils.addAttribute(ai, COMPONENT_ATTR_ACTIVATE, component.getActivate());
244 IOUtils.addAttribute(ai, COMPONENT_ATTR_DEACTIVATE, component.getDeactivate());
Carsten Ziegeler51c4e862009-07-13 07:18:41 +0000245 IOUtils.addAttribute(ai, COMPONENT_ATTR_MODIFIED, component.getModified());
Carsten Ziegelerc9325f72009-06-12 14:06:07 +0000246 }
Carsten Ziegeler66005132012-06-14 15:48:32 +0000247 // attributes new in 1.2
Carsten Ziegeler561e4e22012-06-27 06:38:28 +0000248 if ( module.getOptions().getSpecVersion().ordinal() >= SpecVersion.VERSION_1_2.ordinal() ) {
Carsten Ziegeler66005132012-06-14 15:48:32 +0000249 if ( component.getConfigurationPid() != null && !component.getConfigurationPid().equals(component.getName())) {
250 IOUtils.addAttribute(ai, COMPONENT_ATTR_CONFIGURATION_PID, component.getConfigurationPid());
251 }
252 }
Carsten Ziegeler910f9a32015-02-18 15:22:11 +0000253 IOUtils.indent(contentHandler, indent);
Carsten Ziegelerc9325f72009-06-12 14:06:07 +0000254 contentHandler.startElement(namespace, ComponentDescriptorIO.COMPONENT, ComponentDescriptorIO.COMPONENT_QNAME, ai);
Carsten Ziegeler1d4f1e52008-06-03 07:47:40 +0000255 IOUtils.newline(contentHandler);
Carsten Ziegeler910f9a32015-02-18 15:22:11 +0000256 generateImplementationXML(container, contentHandler, indent+1);
Carsten Ziegeler9a496fe2012-06-27 06:20:28 +0000257 if (container.getServiceDescription() != null) {
Carsten Ziegeler910f9a32015-02-18 15:22:11 +0000258 generateServiceXML(container.getServiceDescription(), contentHandler, indent+1);
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000259 }
Carsten Ziegeler9a496fe2012-06-27 06:20:28 +0000260 for (final PropertyDescription property : container.getProperties().values()) {
Carsten Ziegeler910f9a32015-02-18 15:22:11 +0000261 generatePropertyXML(property, contentHandler, indent+1);
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000262 }
Carsten Ziegeler9a496fe2012-06-27 06:20:28 +0000263
264 for (final ReferenceDescription reference : container.getReferences().values()) {
Carsten Ziegeler910f9a32015-02-18 15:22:11 +0000265 generateReferenceXML(component, module, reference, contentHandler, indent+1);
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000266 }
Carsten Ziegeler9a496fe2012-06-27 06:20:28 +0000267
Carsten Ziegeler910f9a32015-02-18 15:22:11 +0000268 IOUtils.indent(contentHandler, indent);
Carsten Ziegelerc9325f72009-06-12 14:06:07 +0000269 contentHandler.endElement(namespace, ComponentDescriptorIO.COMPONENT, ComponentDescriptorIO.COMPONENT_QNAME);
Carsten Ziegeler1d4f1e52008-06-03 07:47:40 +0000270 IOUtils.newline(contentHandler);
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000271 }
272
273 /**
Carsten Ziegeler6f98fee2013-09-27 11:40:07 +0000274 * Write the xml for an Implementation.
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000275 *
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000276 * @param implementation
277 * @param contentHandler
278 * @throws SAXException
279 */
Carsten Ziegeler910f9a32015-02-18 15:22:11 +0000280 private static void generateImplementationXML(final ComponentContainer component,
281 final ContentHandler contentHandler,
282 final int indent)
283 throws SAXException {
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000284 final AttributesImpl ai = new AttributesImpl();
Carsten Ziegeler61a12e22012-07-03 09:33:50 +0000285 IOUtils.addAttribute(ai, IMPLEMENTATION_ATTR_CLASS, component.getClassDescription().getDescribedClass().getName());
Carsten Ziegeler910f9a32015-02-18 15:22:11 +0000286 IOUtils.indent(contentHandler, indent);
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000287 contentHandler.startElement(INNER_NAMESPACE_URI, ComponentDescriptorIO.IMPLEMENTATION,
Carsten Ziegeler32064312012-10-04 12:51:49 +0000288 ComponentDescriptorIO.IMPLEMENTATION_QNAME, ai);
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000289 contentHandler.endElement(INNER_NAMESPACE_URI, ComponentDescriptorIO.IMPLEMENTATION,
Carsten Ziegeler32064312012-10-04 12:51:49 +0000290 ComponentDescriptorIO.IMPLEMENTATION_QNAME);
Carsten Ziegeler1d4f1e52008-06-03 07:47:40 +0000291 IOUtils.newline(contentHandler);
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000292 }
293
294 /**
Carsten Ziegeler6f98fee2013-09-27 11:40:07 +0000295 * Write the xml for a service.
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000296 *
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000297 * @param service
298 * @param contentHandler
299 * @throws SAXException
300 */
Carsten Ziegeler910f9a32015-02-18 15:22:11 +0000301 private static void generateServiceXML(
Carsten Ziegeler32064312012-10-04 12:51:49 +0000302 final ServiceDescription service,
Carsten Ziegeler910f9a32015-02-18 15:22:11 +0000303 final ContentHandler contentHandler,
304 final int indent)
305 throws SAXException {
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000306 final AttributesImpl ai = new AttributesImpl();
Carsten Ziegeler61a12e22012-07-03 09:33:50 +0000307 IOUtils.addAttribute(ai, SERVICE_ATTR_FACTORY, String.valueOf(service.isServiceFactory()));
Carsten Ziegeler910f9a32015-02-18 15:22:11 +0000308 IOUtils.indent(contentHandler, indent);
Carsten Ziegeler0ad4f782008-08-22 07:27:00 +0000309 contentHandler.startElement(INNER_NAMESPACE_URI, ComponentDescriptorIO.SERVICE, ComponentDescriptorIO.SERVICE_QNAME, ai);
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000310 if (service.getInterfaces() != null && service.getInterfaces().size() > 0) {
Carsten Ziegelerfe62b032008-06-03 11:45:32 +0000311 IOUtils.newline(contentHandler);
Carsten Ziegeler9a496fe2012-06-27 06:20:28 +0000312 for (final String interf : service.getInterfaces()) {
Carsten Ziegeler910f9a32015-02-18 15:22:11 +0000313 generateServiceXML(interf, contentHandler, indent+1);
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000314 }
Carsten Ziegeler910f9a32015-02-18 15:22:11 +0000315 IOUtils.indent(contentHandler, indent);
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000316 }
Carsten Ziegeler0ad4f782008-08-22 07:27:00 +0000317 contentHandler.endElement(INNER_NAMESPACE_URI, ComponentDescriptorIO.SERVICE, ComponentDescriptorIO.SERVICE_QNAME);
Carsten Ziegeler1d4f1e52008-06-03 07:47:40 +0000318 IOUtils.newline(contentHandler);
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000319 }
320
321 /**
Carsten Ziegeler6f98fee2013-09-27 11:40:07 +0000322 * Write the xml for a interface
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000323 *
Carsten Ziegeler9e6bee02007-12-06 12:40:08 +0000324 * @param interf
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000325 * @param contentHandler
326 * @throws SAXException
327 */
Carsten Ziegeler910f9a32015-02-18 15:22:11 +0000328 private static void generateServiceXML(final String interfaceName,
329 final ContentHandler contentHandler,
330 final int indent)
331 throws SAXException {
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000332 final AttributesImpl ai = new AttributesImpl();
Carsten Ziegeler61a12e22012-07-03 09:33:50 +0000333 IOUtils.addAttribute(ai, INTERFACE_ATTR_NAME, interfaceName);
Carsten Ziegeler910f9a32015-02-18 15:22:11 +0000334 IOUtils.indent(contentHandler, indent);
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000335 contentHandler.startElement(INNER_NAMESPACE_URI, ComponentDescriptorIO.INTERFACE, ComponentDescriptorIO.INTERFACE_QNAME,
Carsten Ziegeler32064312012-10-04 12:51:49 +0000336 ai);
Carsten Ziegeler0ad4f782008-08-22 07:27:00 +0000337 contentHandler.endElement(INNER_NAMESPACE_URI, ComponentDescriptorIO.INTERFACE, ComponentDescriptorIO.INTERFACE_QNAME);
Carsten Ziegeler1d4f1e52008-06-03 07:47:40 +0000338 IOUtils.newline(contentHandler);
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000339 }
340
341 /**
Carsten Ziegeler6f98fee2013-09-27 11:40:07 +0000342 * Write the xml for a property.
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000343 *
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000344 * @param property
345 * @param contentHandler
346 * @throws SAXException
347 */
Carsten Ziegeler910f9a32015-02-18 15:22:11 +0000348 private static void generatePropertyXML(final PropertyDescription property,
349 final ContentHandler contentHandler,
350 final int indent)
351 throws SAXException {
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000352 final AttributesImpl ai = new AttributesImpl();
Carsten Ziegeler61a12e22012-07-03 09:33:50 +0000353 IOUtils.addAttribute(ai, ATTR_NAME, property.getName());
Carsten Ziegelerd04ccae2013-05-08 13:04:39 +0000354 if ( property.getType() != PropertyType.String && property.getType() != PropertyType.Password) {
Carsten Ziegeler482c3302012-07-03 09:58:43 +0000355 IOUtils.addAttribute(ai, PROPERTY_ATTR_TYPE, property.getType());
Carsten Ziegeler0f368082012-07-02 10:57:16 +0000356 }
Carsten Ziegeler274d2fd2012-11-30 10:53:38 +0000357 String value = property.getValue();
358 if ( value != null ) {
359 if ( property.getType() == PropertyType.Character || property.getType() == PropertyType.Char ) {
360 value = String.valueOf((int)value.charAt(0));
361 }
362 IOUtils.addAttribute(ai, PROPERTY_ATTR_VALUE, value);
363 }
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000364
Carsten Ziegeler910f9a32015-02-18 15:22:11 +0000365 IOUtils.indent(contentHandler, indent);
Carsten Ziegeler0ad4f782008-08-22 07:27:00 +0000366 contentHandler.startElement(INNER_NAMESPACE_URI, ComponentDescriptorIO.PROPERTY, ComponentDescriptorIO.PROPERTY_QNAME, ai);
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000367 if (property.getMultiValue() != null && property.getMultiValue().length > 0) {
Carsten Ziegeler57a58632007-08-28 16:03:34 +0000368 // generate a new line first
369 IOUtils.text(contentHandler, "\n");
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000370 for (int i = 0; i < property.getMultiValue().length; i++) {
Carsten Ziegeler910f9a32015-02-18 15:22:11 +0000371 IOUtils.indent(contentHandler, indent + 1);
Carsten Ziegeler274d2fd2012-11-30 10:53:38 +0000372 value = property.getMultiValue()[i];
373 if ( property.getType() == PropertyType.Character || property.getType() == PropertyType.Char ) {
374 value = String.valueOf((int)value.charAt(0));
375 }
376 IOUtils.text(contentHandler, value);
Carsten Ziegeler1d4f1e52008-06-03 07:47:40 +0000377 IOUtils.newline(contentHandler);
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000378 }
Carsten Ziegeler910f9a32015-02-18 15:22:11 +0000379 IOUtils.indent(contentHandler, indent);
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000380 }
Carsten Ziegeler0ad4f782008-08-22 07:27:00 +0000381 contentHandler.endElement(INNER_NAMESPACE_URI, ComponentDescriptorIO.PROPERTY, ComponentDescriptorIO.PROPERTY_QNAME);
Carsten Ziegeler1d4f1e52008-06-03 07:47:40 +0000382 IOUtils.newline(contentHandler);
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000383 }
384
385 /**
Carsten Ziegeler6f98fee2013-09-27 11:40:07 +0000386 * Write the xml for a Reference.
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000387 *
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000388 * @param reference
389 * @param contentHandler
390 * @throws SAXException
391 */
Carsten Ziegeler910f9a32015-02-18 15:22:11 +0000392 private static void generateReferenceXML(final ComponentDescription component,
Carsten Ziegeler32064312012-10-04 12:51:49 +0000393 final DescriptionContainer module,
394 final ReferenceDescription reference,
Carsten Ziegeler910f9a32015-02-18 15:22:11 +0000395 final ContentHandler contentHandler,
396 final int indent)
397 throws SAXException {
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000398 final AttributesImpl ai = new AttributesImpl();
Carsten Ziegeler61a12e22012-07-03 09:33:50 +0000399 IOUtils.addAttribute(ai, ATTR_NAME, reference.getName());
400 IOUtils.addAttribute(ai, INTERFACE_ATTR_NAME, reference.getInterfaceName());
401 IOUtils.addAttribute(ai, ATTR_CARDINALITY, reference.getCardinality().getCardinalityString());
402 IOUtils.addAttribute(ai, REFERENCE_ATTR_POLICY, reference.getPolicy().name().toLowerCase());
403 IOUtils.addAttribute(ai, REFERENCE_ATTR_TARGET, reference.getTarget());
404 IOUtils.addAttribute(ai, REFERENCE_ATTR_BIND, reference.getBind());
405 IOUtils.addAttribute(ai, REFERENCE_ATTR_UNBIND, reference.getUnbind());
Carsten Ziegelereff7ff72009-12-07 15:33:57 +0000406
407 // attributes new in 1.1-felix (FELIX-1893)
Carsten Ziegeler561e4e22012-06-27 06:38:28 +0000408 if (module.getOptions().getSpecVersion().ordinal() >= SpecVersion.VERSION_1_1_FELIX.ordinal() ) {
Carsten Ziegeler61a12e22012-07-03 09:33:50 +0000409 IOUtils.addAttribute(ai, REFERENCE_ATTR_UPDATED, reference.getUpdated());
Carsten Ziegelereff7ff72009-12-07 15:33:57 +0000410 }
411
Carsten Ziegelerd156e362012-06-14 15:34:11 +0000412 // attributes new in 1.2
Carsten Ziegeler561e4e22012-06-27 06:38:28 +0000413 if (module.getOptions().getSpecVersion().ordinal() >= SpecVersion.VERSION_1_2.ordinal() ) {
Carsten Ziegelerd156e362012-06-14 15:34:11 +0000414 if ( reference.getPolicyOption() != ReferencePolicyOption.RELUCTANT ) {
Carsten Ziegeler61a12e22012-07-03 09:33:50 +0000415 IOUtils.addAttribute(ai, REFERENCE_ATTR_POLICY_OPTION, reference.getPolicyOption().name().toLowerCase());
Carsten Ziegelerd156e362012-06-14 15:34:11 +0000416 }
417 }
418
Carsten Ziegeler910f9a32015-02-18 15:22:11 +0000419 IOUtils.indent(contentHandler, indent);
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000420 contentHandler.startElement(INNER_NAMESPACE_URI, ComponentDescriptorIO.REFERENCE, ComponentDescriptorIO.REFERENCE_QNAME,
Carsten Ziegeler32064312012-10-04 12:51:49 +0000421 ai);
Carsten Ziegeler0ad4f782008-08-22 07:27:00 +0000422 contentHandler.endElement(INNER_NAMESPACE_URI, ComponentDescriptorIO.REFERENCE, ComponentDescriptorIO.REFERENCE_QNAME);
Carsten Ziegeler1d4f1e52008-06-03 07:47:40 +0000423 IOUtils.newline(contentHandler);
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000424 }
425
426 /**
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000427 * A content handler for parsing the component descriptions.
428 *
429 */
430 protected static final class XmlHandler extends DefaultHandler {
431
432 /** The components container. */
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000433 private final List<ClassDescription> components = new ArrayList<ClassDescription>();
434
435 /** Spec version. */
436 private SpecVersion specVersion;
437
438 /** A reference to the current class. */
439 private ClassDescription currentClass;
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000440
441 /** A reference to the current component. */
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000442 private ComponentDescription currentComponent;
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000443
444 /** The current service. */
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000445 private ServiceDescription currentService;
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000446
447 /** Pending property. */
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000448 private PropertyDescription pendingProperty;
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000449
450 /** Flag for detecting the first element. */
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000451 private boolean firstElement = true;
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000452
Carsten Ziegeler0ad4f782008-08-22 07:27:00 +0000453 /** Flag for elements inside a component element */
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000454 private boolean isComponent = false;
Carsten Ziegeler0ad4f782008-08-22 07:27:00 +0000455
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000456 /** Override namespace. */
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000457 private String overrideNamespace;
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000458
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000459 /** The issue log. */
460 private final IssueLog iLog;
461
462 /** XML file location. */
463 private final String location;
464
465 /** Classloader. */
466 private final ClassLoader classLoader;
467
468 public XmlHandler(final ClassLoader classLoader, final IssueLog iLog, final String loc) {
469 this.iLog = iLog;
470 this.location = loc;
471 this.classLoader = classLoader;
472 }
473
474 /**
475 * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
476 */
Carsten Ziegeler32064312012-10-04 12:51:49 +0000477 @Override
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000478 public void startElement(String uri, final String localName, final String name, final Attributes attributes)
Carsten Ziegeler32064312012-10-04 12:51:49 +0000479 throws SAXException {
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000480 // according to the spec, the elements should have the namespace,
481 // except when the root element is the "component" element
482 // So we check this for the first element, we receive.
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000483 if (this.firstElement) {
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000484 this.firstElement = false;
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000485 if (localName.equals(COMPONENT) && "".equals(uri)) {
Carsten Ziegeler98e992a2012-06-13 15:50:00 +0000486 this.overrideNamespace = SpecVersion.VERSION_1_0.getNamespaceUrl();
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000487 }
488 }
489
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000490 if (this.overrideNamespace != null && "".equals(uri)) {
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000491 uri = this.overrideNamespace;
492 }
493
Carsten Ziegeler0ad4f782008-08-22 07:27:00 +0000494 // however the spec also states that the inner elements
495 // of a component are unqualified, so they don't have
496 // the namespace - we allow both: with or without namespace!
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000497 if (this.isComponent && "".equals(uri)) {
Carsten Ziegeler98e992a2012-06-13 15:50:00 +0000498 uri = SpecVersion.VERSION_1_0.getNamespaceUrl();
Carsten Ziegeler0ad4f782008-08-22 07:27:00 +0000499 }
500
501 // from here on, uri has the namespace regardless of the used xml format
Carsten Ziegeler98e992a2012-06-13 15:50:00 +0000502 specVersion = SpecVersion.fromNamespaceUrl(uri);
503 if (specVersion != null) {
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000504
505 if (localName.equals(COMPONENT)) {
Carsten Ziegeler0ad4f782008-08-22 07:27:00 +0000506 this.isComponent = true;
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000507
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000508 final ComponentDescription desc = new ComponentDescription(null);
Carsten Ziegeler61a12e22012-07-03 09:33:50 +0000509 desc.setName(attributes.getValue(ATTR_NAME));
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000510
511 // enabled attribute is optional
Carsten Ziegeler84fc4352009-06-12 14:11:02 +0000512 if (attributes.getValue(COMPONENT_ATTR_ENABLED) != null) {
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000513 desc.setEnabled(Boolean.valueOf(attributes.getValue(COMPONENT_ATTR_ENABLED)));
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000514 }
515
516 // immediate attribute is optional
Carsten Ziegeler84fc4352009-06-12 14:11:02 +0000517 if (attributes.getValue(COMPONENT_ATTR_IMMEDIATE) != null) {
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000518 desc.setImmediate(Boolean.valueOf(attributes.getValue(COMPONENT_ATTR_IMMEDIATE)));
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000519 }
520
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000521 desc.setFactory(attributes.getValue(COMPONENT_ATTR_FACTORY));
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000522
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000523 desc.setConfigurationPolicy(ComponentConfigurationPolicy.OPTIONAL);
Carsten Ziegeler84fc4352009-06-12 14:11:02 +0000524 // check for version 1.1 attributes
Carsten Ziegeler44ee8942012-06-14 15:03:37 +0000525 if (specVersion.ordinal() >= SpecVersion.VERSION_1_1.ordinal()) {
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000526 final String policy = attributes.getValue(COMPONENT_ATTR_POLICY);
527 if ( policy != null ) {
528 try {
Carsten Ziegelerfaba0eb2012-06-27 16:22:55 +0000529 desc.setConfigurationPolicy(ComponentConfigurationPolicy.valueOf(policy.toUpperCase()));
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000530 } catch (final IllegalArgumentException iae) {
531 iLog.addWarning("Invalid value for attribute " + COMPONENT_ATTR_POLICY + " : " + policy, this.location);
532 }
533 }
534 if ( attributes.getValue(COMPONENT_ATTR_ACTIVATE) != null ) {
Carsten Ziegeler9a496fe2012-06-27 06:20:28 +0000535 desc.setActivate(attributes.getValue(COMPONENT_ATTR_ACTIVATE));
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000536 }
537 if ( attributes.getValue(COMPONENT_ATTR_DEACTIVATE) != null ) {
Carsten Ziegeler9a496fe2012-06-27 06:20:28 +0000538 desc.setDeactivate(attributes.getValue(COMPONENT_ATTR_DEACTIVATE));
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000539 }
540 if ( attributes.getValue(COMPONENT_ATTR_MODIFIED) != null ) {
Carsten Ziegeler9a496fe2012-06-27 06:20:28 +0000541 desc.setModified(attributes.getValue(COMPONENT_ATTR_MODIFIED));
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000542 }
Carsten Ziegeler84fc4352009-06-12 14:11:02 +0000543 }
Carsten Ziegeler9a8d32e2012-06-27 14:07:35 +0000544
545 this.currentComponent = desc;
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000546 } else if (localName.equals(IMPLEMENTATION)) {
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000547 // now we can create the class description and attach the component description
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000548 // Set the implementation class name (mandatory)
Carsten Ziegeler61a12e22012-07-03 09:33:50 +0000549 final String className = attributes.getValue(IMPLEMENTATION_ATTR_CLASS);
Carsten Ziegeleraa66ac02012-06-27 15:49:34 +0000550 Class<?> cl = null;
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000551 try {
Carsten Ziegeleraa66ac02012-06-27 15:49:34 +0000552 cl = this.classLoader.loadClass(className);
553 } catch (final Throwable e) {
Carsten Ziegeler834d5272012-07-02 05:51:56 +0000554 // this doesn't have an effect as the classes we processed are loaded
555 // anyway.
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000556 }
Carsten Ziegeleraa66ac02012-06-27 15:49:34 +0000557 this.currentClass = new ClassDescription(cl, "classpath:" + className);
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000558 this.currentClass.add(this.currentComponent);
Carsten Ziegeler4ca92cc2012-06-28 18:26:31 +0000559 this.components.add(this.currentClass);
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000560
561 } else if (localName.equals(PROPERTY)) {
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000562
Felix Meschberger1fd58292009-10-02 12:59:27 +0000563 // read the property, unless it is the service.pid
564 // property which must not be inherited
Carsten Ziegeler61a12e22012-07-03 09:33:50 +0000565 final String propName = attributes.getValue(ATTR_NAME);
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000566 if (!org.osgi.framework.Constants.SERVICE_PID.equals(propName)) {
567 final PropertyDescription prop = new PropertyDescription(null);
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000568
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000569 prop.setName(propName);
Carsten Ziegeler482c3302012-07-03 09:58:43 +0000570 final String type = attributes.getValue(PROPERTY_ATTR_TYPE);
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000571 if ( type != null ) {
572 try {
573 prop.setType(PropertyType.valueOf(type));
574 } catch (final IllegalArgumentException iae) {
575 iLog.addWarning("Invalid value for attribute type : " + type, this.location);
576 }
Felix Meschberger1fd58292009-10-02 12:59:27 +0000577 }
Carsten Ziegeler0338cc62012-07-02 18:02:41 +0000578 if ( prop.getType() == null ) {
579 prop.setType(PropertyType.String);
580 }
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000581
Carsten Ziegeler61a12e22012-07-03 09:33:50 +0000582 if (attributes.getValue(PROPERTY_ATTR_VALUE) != null) {
Carsten Ziegeler720febb2012-11-30 11:00:00 +0000583 if ( prop.getType() == PropertyType.Char || prop.getType() == PropertyType.Character ) {
584 final int val = Integer.valueOf(attributes.getValue(PROPERTY_ATTR_VALUE));
585 final Character c = Character.valueOf((char)val);
586 prop.setValue(c.toString());
587 } else {
588 prop.setValue(attributes.getValue(PROPERTY_ATTR_VALUE));
589 }
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000590 this.currentClass.add(prop);
591 } else {
Felix Meschberger1fd58292009-10-02 12:59:27 +0000592 // hold the property pending as we have a multi value
593 this.pendingProperty = prop;
594 }
595 // check for abstract properties
Carsten Ziegeler61a12e22012-07-03 09:33:50 +0000596 prop.setLabel(attributes.getValue(ATTR_LABEL));
597 prop.setDescription(attributes.getValue(ATTR_DESCRIPTION));
598 final String cardinality = attributes.getValue(ATTR_CARDINALITY);
599 prop.setUnbounded(PropertyUnbounded.DEFAULT);
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000600 if ( cardinality != null ) {
601 prop.setCardinality(Integer.valueOf(cardinality));
Carsten Ziegeler61a12e22012-07-03 09:33:50 +0000602 if ( prop.getCardinality() == Integer.MAX_VALUE ) {
603 prop.setCardinality(0);
604 prop.setUnbounded(PropertyUnbounded.ARRAY);
605 } else if ( prop.getCardinality() == Integer.MIN_VALUE ) {
606 prop.setCardinality(0);
607 prop.setUnbounded(PropertyUnbounded.VECTOR);
608 }
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000609 }
Carsten Ziegeler482c3302012-07-03 09:58:43 +0000610 final String pValue = attributes.getValue(PROPERTY_ATTR_PRIVATE);
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000611 if (pValue != null) {
612 prop.setPrivate(Boolean.valueOf(pValue));
Felix Meschberger1fd58292009-10-02 12:59:27 +0000613 }
Carsten Ziegelerd2c1b092008-03-04 13:02:15 +0000614 }
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000615
Carsten Ziegeler482c3302012-07-03 09:58:43 +0000616 } else if (localName.equals(PROPERTIES)) {
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000617
618 // TODO: implement the properties tag
619
620 } else if (localName.equals(SERVICE)) {
621
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000622 this.currentService = new ServiceDescription(null);
623 this.currentClass.add(this.currentService);
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000624
Carsten Ziegeler61a12e22012-07-03 09:33:50 +0000625 if (attributes.getValue(SERVICE_ATTR_FACTORY) != null) {
626 this.currentService.setServiceFactory(Boolean.valueOf(attributes.getValue(SERVICE_ATTR_FACTORY)));
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000627 }
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000628
629 } else if (localName.equals(INTERFACE)) {
Carsten Ziegeler61a12e22012-07-03 09:33:50 +0000630 this.currentService.addInterface(attributes.getValue(INTERFACE_ATTR_NAME));
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000631
632 } else if (localName.equals(REFERENCE)) {
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000633 final ReferenceDescription ref = new ReferenceDescription(null);
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000634
Carsten Ziegeler61a12e22012-07-03 09:33:50 +0000635 ref.setName(attributes.getValue(ATTR_NAME));
636 ref.setInterfaceName(attributes.getValue(INTERFACE_ATTR_NAME));
637 final String cardinality = attributes.getValue(ATTR_CARDINALITY);
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000638 if ( cardinality != null ) {
639 ref.setCardinality(ReferenceCardinality.fromValue(cardinality));
640 if ( ref.getCardinality() == null ) {
641 iLog.addWarning("Invalid value for attribute cardinality : " + cardinality, this.location);
642 }
643 }
Carsten Ziegelerd156e362012-06-14 15:34:11 +0000644 ref.setPolicy(ReferencePolicy.STATIC);
Carsten Ziegeler61a12e22012-07-03 09:33:50 +0000645 final String policy = attributes.getValue(REFERENCE_ATTR_POLICY);
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000646 if ( policy != null ) {
647 try {
Carsten Ziegelerd156e362012-06-14 15:34:11 +0000648 ref.setPolicy(ReferencePolicy.valueOf(policy.toUpperCase()));
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000649 } catch (final IllegalArgumentException iae) {
650 iLog.addWarning("Invalid value for attribute policy : " + policy, this.location);
651 }
652 }
Carsten Ziegelerd156e362012-06-14 15:34:11 +0000653 ref.setPolicyOption(ReferencePolicyOption.RELUCTANT);
Carsten Ziegeler61a12e22012-07-03 09:33:50 +0000654 final String policyOption = attributes.getValue(REFERENCE_ATTR_POLICY_OPTION);
Carsten Ziegelerd156e362012-06-14 15:34:11 +0000655 if ( policyOption != null ) {
656 try {
657 ref.setPolicyOption(ReferencePolicyOption.valueOf(policyOption.toUpperCase()));
658 } catch (final IllegalArgumentException iae) {
659 iLog.addWarning("Invalid value for attribute policy-option : " + policyOption, this.location);
660 }
661 }
Carsten Ziegeler61a12e22012-07-03 09:33:50 +0000662 ref.setTarget(attributes.getValue(REFERENCE_ATTR_TARGET));
663 if ( attributes.getValue(REFERENCE_ATTR_BIND) != null ) {
664 ref.setBind(attributes.getValue(REFERENCE_ATTR_BIND));
Carsten Ziegeler46d03502008-07-25 10:07:56 +0000665 }
Carsten Ziegeler61a12e22012-07-03 09:33:50 +0000666 if ( attributes.getValue(REFERENCE_ATTR_UNBIND) != null ) {
667 ref.setUnbind(attributes.getValue(REFERENCE_ATTR_UNBIND));
Carsten Ziegeler9a496fe2012-06-27 06:20:28 +0000668 }
Carsten Ziegeler61a12e22012-07-03 09:33:50 +0000669 if ( attributes.getValue(REFERENCE_ATTR_UPDATED) != null ) {
670 ref.setUnbind(attributes.getValue(REFERENCE_ATTR_UPDATED));
Carsten Ziegeler4723a302011-06-06 14:20:01 +0000671 }
Carsten Ziegeler46d03502008-07-25 10:07:56 +0000672
Carsten Ziegeler482c3302012-07-03 09:58:43 +0000673 final String strategy = attributes.getValue(REFERENCE_ATTR_STRATEGY);
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000674 if ( strategy != null ) {
675 try {
Carsten Ziegelerd156e362012-06-14 15:34:11 +0000676 ref.setStrategy(ReferenceStrategy.valueOf(strategy.toUpperCase()));
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000677 } catch (final IllegalArgumentException iae) {
678 throw new SAXException("Invalid value for attribute strategy : " + strategy);
679 }
680 }
681
682 this.currentClass.add(ref);
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000683 }
684 }
685 }
686
687 /**
688 * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
689 */
Carsten Ziegeler32064312012-10-04 12:51:49 +0000690 @Override
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000691 public void endElement(String uri, String localName, String name) throws SAXException {
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000692 if (this.overrideNamespace != null && "".equals(uri)) {
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000693 uri = this.overrideNamespace;
694 }
695
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000696 if (this.isComponent && "".equals(uri)) {
Carsten Ziegeler98e992a2012-06-13 15:50:00 +0000697 uri = SpecVersion.VERSION_1_0.getNamespaceUrl();
Carsten Ziegeler0ad4f782008-08-22 07:27:00 +0000698 }
699
Carsten Ziegeler98e992a2012-06-13 15:50:00 +0000700 if (SpecVersion.fromNamespaceUrl(uri) != null) {
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000701 if (localName.equals(COMPONENT)) {
702 this.currentClass = null;
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000703 this.currentComponent = null;
Carsten Ziegeler0ad4f782008-08-22 07:27:00 +0000704 this.isComponent = false;
705 } else if (localName.equals(PROPERTY) && this.pendingProperty != null) {
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000706 // now split the value
707 final String text = this.pendingProperty.getValue();
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000708 if (text != null) {
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000709 final StringTokenizer st = new StringTokenizer(text);
710 final String[] values = new String[st.countTokens()];
711 int index = 0;
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000712 while (st.hasMoreTokens()) {
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000713 values[index] = st.nextToken();
Carsten Ziegeler720febb2012-11-30 11:00:00 +0000714 if ( this.pendingProperty.getType() == PropertyType.Char || this.pendingProperty.getType() == PropertyType.Character ) {
715 final int val = Integer.valueOf(values[index]);
716 final Character c = Character.valueOf((char)val);
717 values[index] = c.toString();
718 }
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000719 index++;
720 }
721 this.pendingProperty.setMultiValue(values);
722 }
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000723 this.currentClass.add(this.pendingProperty);
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000724 this.pendingProperty = null;
725 }
726 }
727 }
728
729 /**
730 * @see org.xml.sax.helpers.DefaultHandler#characters(char[], int, int)
731 */
Carsten Ziegeler32064312012-10-04 12:51:49 +0000732 @Override
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000733 public void characters(char[] ch, int start, int length) throws SAXException {
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000734 if (this.pendingProperty != null) {
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000735 final String text = new String(ch, start, length);
Carsten Ziegeler55c96d32012-06-13 12:03:35 +0000736 if (this.pendingProperty.getValue() == null) {
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000737 this.pendingProperty.setValue(text);
738 } else {
739 this.pendingProperty.setValue(this.pendingProperty.getValue() + text);
740 }
741 }
742 }
743 }
Carsten Ziegeler59c95d52012-08-31 07:00:42 +0000744
745 private static final String PARENT_NAME = "OSGI-INF";
746
747 /**
748 * Generate descriptor file(s)
749 */
750 public static List<String> generateDescriptorFiles(final DescriptionContainer module, final Options options, final Log logger)
Carsten Ziegeler32064312012-10-04 12:51:49 +0000751 throws SCRDescriptorException, SCRDescriptorFailureException {
Carsten Ziegeler73903902013-01-30 21:09:50 +0000752 // get the list of all relevant containers
Carsten Ziegeler878ef9d2012-08-31 08:04:23 +0000753 final List<ComponentContainer> components = new ArrayList<ComponentContainer>();
754 for(final ComponentContainer container : module.getComponents()) {
755 if (!container.getComponentDescription().isCreateDs()) {
756 logger.debug("Ignoring descriptor for DS : " + container);
757 } else if (!container.getComponentDescription().isAbstract()) {
758 logger.debug("Adding descriptor for DS : " + container);
759 components.add(container);
760 }
761 }
762
Carsten Ziegeler59c95d52012-08-31 07:00:42 +0000763 // check descriptor file
Carsten Ziegeler32064312012-10-04 12:51:49 +0000764 final File descriptorDir = options.getComponentDescriptorDirectory();
Carsten Ziegeler59c95d52012-08-31 07:00:42 +0000765
766 // terminate if there is nothing else to write
Carsten Ziegeler878ef9d2012-08-31 08:04:23 +0000767 if (components.isEmpty()) {
Carsten Ziegeler59c95d52012-08-31 07:00:42 +0000768 logger.debug("No Service Component Descriptors found in project.");
Carsten Ziegelere5fef1c2013-07-17 12:32:32 +0000769 // remove files if it exists
Carsten Ziegeler6f98fee2013-09-27 11:40:07 +0000770 if ( descriptorDir.exists() && !options.isIncremental()) {
Carsten Ziegelere5fef1c2013-07-17 12:32:32 +0000771 for(final File f : descriptorDir.listFiles()) {
772 if ( f.isFile() ) {
773 logger.debug("Removing obsolete service descriptor " + f);
774 f.delete();
775 }
776 }
Carsten Ziegeler59c95d52012-08-31 07:00:42 +0000777 }
Carsten Ziegelere5fef1c2013-07-17 12:32:32 +0000778
Carsten Ziegeler59c95d52012-08-31 07:00:42 +0000779 return null;
780 }
781
782 // finally the descriptors have to be written ....
783 descriptorDir.mkdirs(); // ensure parent dir
784
785 final List<String> fileNames = new ArrayList<String>();
Carsten Ziegelere5fef1c2013-07-17 12:32:32 +0000786 final List<ComponentContainerContainer> containers = ComponentContainerUtil.split(components);
Carsten Ziegeler73903902013-01-30 21:09:50 +0000787 for(final ComponentContainerContainer ccc : containers) {
Carsten Ziegeler1c79bdc2013-01-23 14:35:48 +0000788 final SpecVersion globalVersion = module.getOptions().getSpecVersion();
Carsten Ziegeler59c70f02013-01-30 20:49:13 +0000789
Carsten Ziegelere5fef1c2013-07-17 12:32:32 +0000790 SpecVersion sv = null;
791 for(final ComponentContainer cc : ccc.components ) {
792 if ( sv == null || sv.ordinal() < cc.getComponentDescription().getSpecVersion().ordinal() ) {
793 sv = cc.getComponentDescription().getSpecVersion();
Carsten Ziegeler59c70f02013-01-30 20:49:13 +0000794 }
Carsten Ziegeler59c95d52012-08-31 07:00:42 +0000795 }
Carsten Ziegelere5fef1c2013-07-17 12:32:32 +0000796 module.getOptions().setSpecVersion(sv);
797 final File useFile = new File(descriptorDir, ccc.className + ".xml");
Carsten Ziegeler59c95d52012-08-31 07:00:42 +0000798 try {
Carsten Ziegeler73903902013-01-30 21:09:50 +0000799 ComponentDescriptorIO.generateXML(module, ccc.components, useFile, logger);
Carsten Ziegelera21b5842013-01-23 13:00:46 +0000800 } catch (final IOException e) {
Carsten Ziegelere5fef1c2013-07-17 12:32:32 +0000801 throw new SCRDescriptorException("Unable to generate xml", useFile.toString(), e);
Carsten Ziegelera21b5842013-01-23 13:00:46 +0000802 } catch (final TransformerException e) {
Carsten Ziegelere5fef1c2013-07-17 12:32:32 +0000803 throw new SCRDescriptorException("Unable to generate xml", useFile.toString(), e);
Carsten Ziegeler59c95d52012-08-31 07:00:42 +0000804 } catch (final SAXException e) {
Carsten Ziegelere5fef1c2013-07-17 12:32:32 +0000805 throw new SCRDescriptorException("Unable to generate xml", useFile.toString(), e);
Carsten Ziegeler59c95d52012-08-31 07:00:42 +0000806 }
Carsten Ziegeler73903902013-01-30 21:09:50 +0000807 fileNames.add(PARENT_NAME + '/' + useFile.getName());
Carsten Ziegeler59c95d52012-08-31 07:00:42 +0000808
Carsten Ziegeler73903902013-01-30 21:09:50 +0000809 module.getOptions().setSpecVersion(globalVersion);
Carsten Ziegeler59c95d52012-08-31 07:00:42 +0000810 }
Carsten Ziegeler73903902013-01-30 21:09:50 +0000811
Carsten Ziegeler59c95d52012-08-31 07:00:42 +0000812 return fileNames;
813 }
Carsten Ziegeler4ccaaeb2007-08-20 07:20:03 +0000814}