2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
18
package org.apache.ivy.osgi.p2;
20
import java.io.IOException;
21
import java.io.InputStream;
23
import java.net.URISyntaxException;
24
import java.text.ParseException;
25
import java.util.ArrayList;
26
import java.util.Iterator;
27
import java.util.List;
30
import javax.xml.parsers.ParserConfigurationException;
32
import org.apache.ivy.osgi.core.BundleCapability;
33
import org.apache.ivy.osgi.core.BundleInfo;
34
import org.apache.ivy.osgi.core.BundleRequirement;
35
import org.apache.ivy.osgi.core.ExportPackage;
36
import org.apache.ivy.osgi.p2.PropertiesParser.PropertiesHandler;
37
import org.apache.ivy.osgi.util.DelegetingHandler;
38
import org.apache.ivy.osgi.util.Version;
39
import org.apache.ivy.osgi.util.VersionRange;
40
import org.apache.ivy.util.Message;
41
import org.apache.ivy.util.XMLHelper;
42
import org.xml.sax.Attributes;
43
import org.xml.sax.SAXException;
45
public class P2MetadataParser implements XMLInputParser {
47
private final P2Descriptor p2Descriptor;
49
public P2MetadataParser(P2Descriptor p2Descriptor) {
50
this.p2Descriptor = p2Descriptor;
53
public void parse(InputStream in) throws ParseException, IOException, SAXException {
54
RepositoryHandler handler = new RepositoryHandler(p2Descriptor);
56
XMLHelper.parse(in, null, handler, null);
57
} catch (ParserConfigurationException e) {
58
throw new SAXException(e);
62
static class RepositoryHandler extends DelegetingHandler {
64
private static final String REPOSITORY = "repository";
66
// private static final String NAME = "name";
68
// private static final String TYPE = "type";
70
// private static final String VERSION = "version";
72
// private static final String DESCRIPTION = "description";
74
// private static final String PROVIDER = "provider";
76
public RepositoryHandler(final P2Descriptor p2Descriptor) {
78
addChild(new PropertiesHandler(), new ChildElementHandler() {
79
public void childHanlded(DelegetingHandler child) {
80
Map properties = ((PropertiesHandler) child).properties;
81
String timestamp = (String) properties.get("p2.timestamp");
82
if (timestamp != null) {
83
p2Descriptor.setTimestamp(Long.parseLong(timestamp));
87
addChild(new UnitsHandler(), new ChildElementHandler() {
88
public void childHanlded(DelegetingHandler child) {
89
Iterator it = ((UnitsHandler) child).bundles.iterator();
90
while (it.hasNext()) {
91
p2Descriptor.addBundle((BundleInfo) it.next());
95
addChild(new ReferencesHandler(), new ChildElementHandler() {
96
public void childHanlded(DelegetingHandler child) {
101
// protected void handleAttributes(Attributes atts) {
102
// String name = atts.getValue(NAME);
103
// String type = atts.getValue(TYPE);
104
// String version = atts.getValue(VERSION);
105
// String description = atts.getValue(DESCRIPTION);
106
// String provider = atts.getValue(PROVIDER);
110
static class ReferencesHandler extends DelegetingHandler {
112
private static final String REFERENCES = "references";
114
private static final String SIZE = "size";
116
List/* <URI> */repositoryUris;
118
public ReferencesHandler() {
120
addChild(new RepositoryReferenceHandler(), new ChildElementHandler() {
121
public void childHanlded(DelegetingHandler child) {
122
repositoryUris.add(((RepositoryReferenceHandler) child).uri);
127
protected void handleAttributes(Attributes atts) throws SAXException {
128
int size = Integer.parseInt(atts.getValue(SIZE));
129
repositoryUris = new ArrayList(size);
133
static class RepositoryReferenceHandler extends DelegetingHandler {
135
private static final String REPOSITORY = "repository";
137
private static final String TYPE = "type";
139
private static final String OPTIONS = "options";
141
private static final String NAME = "name";
143
private static final String URI = "uri";
145
private static final String URL = "url";
147
public RepositoryReferenceHandler() {
159
protected void handleAttributes(Attributes atts) throws SAXException {
160
type = Integer.parseInt(atts.getValue(TYPE));
161
options = Integer.parseInt(atts.getValue(OPTIONS));
162
name = atts.getValue(NAME);
165
String uriAtt = atts.getValue(URI);
166
String url = atts.getValue(URL);
168
uri = new URI(uriAtt);
169
} else if (url != null) {
172
} catch (URISyntaxException e) {
173
// TODO Auto-generated catch block
179
static class UnitsHandler extends DelegetingHandler {
181
private static final String UNITS = "units";
183
private static final String SIZE = "size";
187
public UnitsHandler() {
189
addChild(new UnitHandler(), new ChildElementHandler() {
190
public void childHanlded(DelegetingHandler child) {
191
BundleInfo bundleInfo = ((UnitHandler) child).bundleInfo;
192
if (!bundleInfo.getCapabilities().isEmpty()) {
193
bundles.add(((UnitHandler) child).bundleInfo);
199
protected void handleAttributes(Attributes atts) {
200
int size = Integer.parseInt(atts.getValue(SIZE));
201
bundles = new ArrayList(size);
206
static class UnitHandler extends DelegetingHandler {
208
private static final String UNIT = "unit";
210
private static final String ID = "id";
212
private static final String VERSION = "version";
214
// private static final String SINGLETON = "singleton";
216
BundleInfo bundleInfo;
218
public UnitHandler() {
220
// addChild(new UpdateHandler(), new ChildElementHandler() {
221
// public void childHanlded(DelegetingHandler child) {
224
addChild(new PropertiesHandler(), new ChildElementHandler() {
225
public void childHanlded(DelegetingHandler child) {
226
Map properties = ((PropertiesHandler) child).properties;
227
String category = (String) properties
228
.get("org.eclipse.equinox.p2.type.category");
229
if (category != null && Boolean.valueOf(category).booleanValue()) {
230
// this is a category definition, this is useless, skip this unit
231
child.getParent().skip();
235
addChild(new ProvidesHandler(), new ChildElementHandler() {
236
public void childHanlded(DelegetingHandler child) {
237
Iterator it = ((ProvidesHandler) child).capabilities.iterator();
238
while (it.hasNext()) {
239
bundleInfo.addCapability((BundleCapability) it.next());
243
addChild(new FilterHandler(), new ChildElementHandler() {
244
public void childHanlded(DelegetingHandler child) {
247
addChild(new RequiresHandler(), new ChildElementHandler() {
248
public void childHanlded(DelegetingHandler child) {
249
Iterator it = ((RequiresHandler) child).requirements.iterator();
250
while (it.hasNext()) {
251
bundleInfo.addRequirement((BundleRequirement) it.next());
255
addChild(new HostRequirementsHandler(), new ChildElementHandler() {
256
public void childHanlded(DelegetingHandler child) {
259
addChild(new MetaRequirementsHandler(), new ChildElementHandler() {
260
public void childHanlded(DelegetingHandler child) {
263
addChild(new ArtifactsHandler(), new ChildElementHandler() {
264
public void childHanlded(DelegetingHandler child) {
267
// addChild(new TouchpointHandler(), new ChildElementHandler() {
268
// public void childHanlded(DelegetingHandler child) {
271
// addChild(new TouchpointDataHandler(), new ChildElementHandler() {
272
// public void childHanlded(DelegetingHandler child) {
275
// addChild(new LicensesHandler(), new ChildElementHandler() {
276
// public void childHanlded(DelegetingHandler child) {
279
// addChild(new CopyrightHandler(), new ChildElementHandler() {
280
// public void childHanlded(DelegetingHandler child) {
283
addChild(new ChangesHandler(), new ChildElementHandler() {
284
public void childHanlded(DelegetingHandler child) {
290
protected void handleAttributes(Attributes atts) throws SAXException {
291
String id = atts.getValue(ID);
292
String version = atts.getValue(VERSION);
293
// Boolean singleton = Boolean.valueOf(atts.getValue(SINGLETON));
295
bundleInfo = new BundleInfo(id, new Version(version));
296
} catch (ParseException e) {
297
throw new SAXException("Incorrect version on bundle '" + id + "': " + version
298
+ " (" + e.getMessage() + ")");
304
// static class UpdateHandler extends DelegetingHandler {
306
// private static final String UPDATE = "update";
308
// private static final String ID = "id";
310
// private static final String RANGE = "range";
312
// private static final String SEVERITY = "severity";
314
// public UpdateHandler() {
318
// protected void handleAttributes(Attributes atts) {
319
// String id = atts.getValue(ID);
320
// String range = atts.getValue(RANGE);
321
// String severity = atts.getValue(SEVERITY);
326
static class FilterHandler extends DelegetingHandler {
328
private static final String FILTER = "filter";
330
public FilterHandler() {
332
setBufferingChar(true);
337
private static String namespace2Type(String namespace) {
338
if (namespace.equals("java.package")) {
339
return BundleInfo.PACKAGE_TYPE;
341
if (namespace.equals("osgi.bundle")) {
342
return BundleInfo.BUNDLE_TYPE;
347
static class ProvidesHandler extends DelegetingHandler {
349
private static final String PROVIDES = "provides";
351
private static final String SIZE = "size";
355
public ProvidesHandler() {
357
addChild(new ProvidedHandler(), new ChildElementHandler() {
358
public void childHanlded(DelegetingHandler child) {
359
String name = ((ProvidedHandler) child).name;
360
Version version = ((ProvidedHandler) child).version;
361
String type = namespace2Type(((ProvidedHandler) child).namespace);
363
Message.debug("Unsupported provided capability "
364
+ ((ProvidedHandler) child).namespace + " " + name + " " + version);
367
BundleCapability capability;
368
if (type == BundleInfo.PACKAGE_TYPE) {
369
capability = new ExportPackage(name, version);
371
capability = new BundleCapability(type, name, version);
373
capabilities.add(capability);
378
protected void handleAttributes(Attributes atts) {
379
int size = Integer.parseInt(atts.getValue(SIZE));
380
capabilities = new ArrayList(size);
385
static class ProvidedHandler extends DelegetingHandler {
387
private static final String PROVIDED = "provided";
389
private static final String NAMESPACE = "namespace";
391
private static final String NAME = "name";
393
private static final String VERSION = "version";
401
public ProvidedHandler() {
405
protected void handleAttributes(Attributes atts) throws SAXException {
406
namespace = atts.getValue(NAMESPACE);
407
name = atts.getValue(NAME);
409
version = new Version(atts.getValue(VERSION));
410
} catch (ParseException e) {
411
throw new SAXException("Incorrect version on provided capability: " + version
412
+ " (" + e.getMessage() + ")");
418
static abstract class AbstractRequirementHandler extends DelegetingHandler {
420
private static final String SIZE = "size";
424
public AbstractRequirementHandler(String name) {
426
addChild(new RequiredHandler(), new ChildElementHandler() {
427
public void childHanlded(DelegetingHandler child) {
428
String name = ((RequiredHandler) child).name;
429
VersionRange range = ((RequiredHandler) child).range;
430
String type = namespace2Type(((RequiredHandler) child).namespace);
432
Message.debug("Unsupported required capability "
433
+ ((RequiredHandler) child).namespace + " " + name + " " + range);
435
requirements.add(new BundleRequirement(type, name, range, null));
441
protected void handleAttributes(Attributes atts) {
442
int size = Integer.parseInt(atts.getValue(SIZE));
443
requirements = new ArrayList(size);
448
static class RequiresHandler extends AbstractRequirementHandler {
450
private static final String REQUIRES = "requires";
452
public RequiresHandler() {
458
static class RequiredHandler extends DelegetingHandler {
460
private static final String REQUIRED = "required";
462
private static final String NAMESPACE = "namespace";
464
private static final String NAME = "name";
466
private static final String RANGE = "range";
476
public RequiredHandler() {
478
addChild(new FilterHandler(), new ChildElementHandler() {
479
public void childHanlded(DelegetingHandler child) {
480
filter = child.getBufferedChars().trim();
485
protected void handleAttributes(Attributes atts) {
486
namespace = atts.getValue(NAMESPACE);
487
name = atts.getValue(NAME);
489
range = new VersionRange(atts.getValue(RANGE));
490
} catch (ParseException e) {
491
throw new RuntimeException(e);
497
static class HostRequirementsHandler extends AbstractRequirementHandler {
499
private static final String HOST_REQUIREMENTS = "hostRequirements";
501
public HostRequirementsHandler() {
502
super(HOST_REQUIREMENTS);
507
static class MetaRequirementsHandler extends AbstractRequirementHandler {
509
private static final String META_REQUIREMENTS = "metaRequirements";
511
public MetaRequirementsHandler() {
512
super(META_REQUIREMENTS);
517
static class ArtifactsHandler extends DelegetingHandler {
519
private static final String ARTIFACTS = "artifacts";
521
private static final String SIZE = "size";
525
public ArtifactsHandler() {
527
addChild(new ArtifactHandler(), new ChildElementHandler() {
528
public void childHanlded(DelegetingHandler child) {
529
artifacts.add(((ArtifactHandler) child).artifact);
534
protected void handleAttributes(Attributes atts) {
535
int size = Integer.parseInt(atts.getValue(SIZE));
536
artifacts = new ArrayList(size);
541
static class ArtifactHandler extends DelegetingHandler/* <ArtifactsHandler> */{
543
private static final String ARTIFACT = "artifact";
545
private static final String ID = "id";
547
private static final String VERSION = "version";
549
private static final String CLASSIFIER = "classifier";
553
public ArtifactHandler() {
557
protected void handleAttributes(Attributes atts) throws SAXException {
558
String id = atts.getValue(ID);
559
String version = atts.getValue(VERSION);
560
String classifier = atts.getValue(CLASSIFIER);
562
artifact = new P2Artifact(id, new Version(version), classifier);
563
} catch (ParseException e) {
564
throw new SAXException("Incorrect version on artifact '" + id + "': " + version
565
+ " (" + e.getMessage() + ")");
572
// static class TouchpointHandler extends DelegetingHandler {
574
// private static final String TOUCHPOINT = "touchpoint";
576
// private static final String ID = "id";
578
// private static final String VERSION = "version";
580
// public TouchpointHandler() {
581
// super(TOUCHPOINT);
584
// protected void handleAttributes(Attributes atts) {
585
// String id = atts.getValue(ID);
586
// String version = atts.getValue(VERSION);
591
// static class TouchpointDataHandler extends DelegetingHandler {
593
// private static final String TOUCHPOINTDATA = "touchpointData";
595
// private static final String SIZE = "size";
597
// public TouchpointDataHandler() {
598
// super(TOUCHPOINTDATA);
599
// addChild(new InstructionsHandler(), new ChildElementHandler() {
600
// public void childHanlded(DelegetingHandler child) {
605
// protected void handleAttributes(Attributes atts) {
606
// String size = atts.getValue(SIZE);
611
// static class InstructionsHandler extends DelegetingHandler {
613
// private static final String INSTRUCTIONS = "instructions";
615
// private static final String SIZE = "size";
617
// public InstructionsHandler() {
618
// super(INSTRUCTIONS);
619
// addChild(new InstructionHandler(), new ChildElementHandler() {
620
// public void childHanlded(DelegetingHandler child) {
625
// protected void handleAttributes(Attributes atts) {
626
// String size = atts.getValue(SIZE);
631
// static class InstructionHandler extends DelegetingHandler {
633
// private static final String INSTRUCTION = "instruction";
635
// private static final String KEY = "key";
637
// public InstructionHandler() {
638
// super(INSTRUCTION);
639
// setBufferingChar(true);
642
// protected void handleAttributes(Attributes atts) {
643
// String size = atts.getValue(KEY);
648
// static class LicensesHandler extends DelegetingHandler {
650
// private static final String LICENSES = "licenses";
652
// private static final String SIZE = "size";
654
// public LicensesHandler() {
656
// addChild(new LicenseHandler(), new ChildElementHandler() {
657
// public void childHanlded(DelegetingHandler child) {
662
// protected void handleAttributes(Attributes atts) {
663
// String size = atts.getValue(SIZE);
668
// static class LicenseHandler extends DelegetingHandler {
670
// private static final String LICENSE = "license";
672
// private static final String URI = "uri";
674
// private static final String URL = "url";
676
// public LicenseHandler() {
678
// setBufferingChar(true);
681
// protected void handleAttributes(Attributes atts) {
682
// String uri = atts.getValue(URI);
683
// String url = atts.getValue(URL);
688
// static class CopyrightHandler extends DelegetingHandler {
690
// private static final String COPYRIGHT = "copyright";
692
// private static final String URI = "uri";
694
// private static final String URL = "url";
696
// public CopyrightHandler() {
700
// protected void handleAttributes(Attributes atts) {
701
// String uri = atts.getValue(URI);
702
// String url = atts.getValue(URL);
707
static class ChangesHandler extends DelegetingHandler {
709
private static final String CHANGES = "changes";
711
// private static final String SIZE = "size";
713
public ChangesHandler() {
715
addChild(new ChangeHandler(), new ChildElementHandler() {
716
public void childHanlded(DelegetingHandler child) {
721
protected void handleAttributes(Attributes atts) {
722
// int size = Integer.parseInt(atts.getValue(SIZE));
726
static class ChangeHandler extends DelegetingHandler {
728
private static final String CHANGE = "change";
730
public ChangeHandler() {
735
static class FromHandler extends AbstractRequirementHandler {
737
private static final String FROM = "from";
739
public FromHandler() {
745
static class ToHandler extends AbstractRequirementHandler {
747
private static final String TO = "to";
755
static class PatchScopeHandler extends DelegetingHandler {
757
private static final String PATCH_SCOPE = "patchScope";
759
// private static final String SIZE = "size";
761
public PatchScopeHandler() {
763
addChild(new PatchScopeHandler(), new ChildElementHandler() {
764
public void childHanlded(DelegetingHandler child) {
769
protected void handleAttributes(Attributes atts) {
770
// int size = Integer.parseInt(atts.getValue(SIZE));
774
static class ScopeHandler extends DelegetingHandler {
776
private static final String SCOPE = "scope";
778
public ScopeHandler() {
780
addChild(new RequiresHandler(), new ChildElementHandler() {
781
public void childHanlded(DelegetingHandler child) {
787
static class LifeCycleHandler extends AbstractRequirementHandler {
789
private static final String LIFE_CYCLE = "lifeCycle";
791
public LifeCycleHandler() {