Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8e462062935524ed71e8e8d460c8f16c109511bf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*******************************************************************************
 * Copyright (c) 2007 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.internal.p2.engine;

import java.util.*;
import org.eclipse.equinox.internal.p2.persistence.XMLParser;
import org.eclipse.equinox.p2.engine.IProfile;
import org.osgi.framework.BundleContext;
import org.xml.sax.Attributes;

/**
 *	An abstract XML parser class for parsing profiles as written by the ProfileWriter.
 */
public abstract class ProfileParser extends XMLParser implements ProfileXMLConstants {

	public ProfileParser(BundleContext context, String bundleId) {
		super(context, bundleId);
	}

	protected class ProfileHandler extends AbstractHandler {

		private final String[] required = new String[] {ID_ATTRIBUTE};

		private final Map profileHandlers;
		private String profileId = null;
		private String parentId;
		private PropertiesHandler propertiesHandler = null;

		public ProfileHandler(AbstractHandler parentHandler, Attributes attributes, Map profileHandlers) {
			super(parentHandler, PROFILE_ELEMENT);
			profileId = parseRequiredAttributes(attributes, required)[0];
			parentId = parseOptionalAttribute(attributes, PARENT_ID_ATTRIBUTE);
			this.profileHandlers = profileHandlers;
		}

		public void startElement(String name, Attributes attributes) {
			if (PROPERTIES_ELEMENT.equals(name)) {
				if (propertiesHandler == null) {
					propertiesHandler = new PropertiesHandler(this, attributes);
				} else {
					duplicateElement(this, name, attributes);
				}
			} else {
				invalidElement(name, attributes);
			}
		}

		protected void finished() {
			if (isValidXML()) {
				profileHandlers.put(profileId, this);
			}
		}

		public String getProfileId() {
			return profileId;
		}

		public String getParentId() {
			return parentId;
		}

		public Map getProperties() {
			if (propertiesHandler == null)
				return null;
			return propertiesHandler.getProperties();
		}
	}

	protected class ProfilesHandler extends AbstractHandler {

		private Map profileHandlers = null;
		private Map profiles;

		public ProfilesHandler(AbstractHandler parentHandler, Attributes attributes) {
			super(parentHandler, PROFILES_ELEMENT);
			String size = parseOptionalAttribute(attributes, COLLECTION_SIZE_ATTRIBUTE);
			profileHandlers = (size != null ? new HashMap(new Integer(size).intValue()) : new HashMap(4));
		}

		public IProfile[] getProfiles() {
			if (profileHandlers.isEmpty())
				return new IProfile[0];

			profiles = new HashMap();
			for (Iterator it = profileHandlers.keySet().iterator(); it.hasNext();) {
				String profileId = (String) it.next();
				addProfile(profileId);
			}

			return (IProfile[]) profiles.values().toArray(new IProfile[profiles.size()]);
		}

		private void addProfile(String profileId) {
			if (profiles.containsKey(profileId))
				return;

			ProfileHandler profileHandler = (ProfileHandler) profileHandlers.get(profileId);
			Profile parentProfile = null;
			String parentId = profileHandler.parentId;
			if (parentId != null) {
				addProfile(parentId);
				parentProfile = (Profile) profiles.get(parentId);
			}
			IProfile profile = new Profile(profileId, parentProfile, profileHandler.getProperties());
			profiles.put(profileId, profile);
		}

		public void startElement(String name, Attributes attributes) {
			if (name.equals(PROFILE_ELEMENT)) {
				new ProfileHandler(this, attributes, profileHandlers);
			} else {
				invalidElement(name, attributes);
			}
		}
	}
}

Back to the top