1 /*******************************************************************************
2  * Copyright (c) 2000, 2009 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.core.internal.registry;
15 
16 import java.io.File;
17 import java.lang.ref.SoftReference;
18 import org.eclipse.core.runtime.IContributor;
19 
20 /**
21  * An object which represents the user-defined extension point in a
22  * plug-in manifest.
23  */
24 public class ExtensionPoint extends RegistryObject {
25 	public static final ExtensionPoint[] EMPTY_ARRAY = new ExtensionPoint[0];
26 
27 	//Place holder for the label and the schema. It contains either a String[] or a SoftReference to a String[].
28 	//The array layout is [label, schemaReference, fullyQualifiedName, namespace, contributorId]
29 	private Object extraInformation;
30 	//Indexes of the various fields
31 	private static final byte LABEL = 0; //The human readable name for the extension point
32 	private static final byte SCHEMA = 1; //The schema of the extension point
33 	private static final byte QUALIFIED_NAME = 2; //The fully qualified name of the extension point
34 	private static final byte NAMESPACE = 3; //The name of the namespace of the extension point
35 	private static final byte CONTRIBUTOR_ID = 4; //The ID of the actual contributor of the extension point
36 	private static final int EXTRA_SIZE = 5;
37 
ExtensionPoint(ExtensionRegistry registry, boolean persist)38 	protected ExtensionPoint(ExtensionRegistry registry, boolean persist) {
39 		super(registry, persist);
40 	}
41 
ExtensionPoint(int self, int[] children, int dataOffset, ExtensionRegistry registry, boolean persist)42 	protected ExtensionPoint(int self, int[] children, int dataOffset, ExtensionRegistry registry, boolean persist) {
43 		super(registry, persist);
44 
45 		setObjectId(self);
46 		setRawChildren(children);
47 		setExtraDataOffset(dataOffset);
48 	}
49 
getSimpleIdentifier()50 	protected String getSimpleIdentifier() {
51 		return getUniqueIdentifier().substring(getUniqueIdentifier().lastIndexOf('.') + 1);
52 	}
53 
getExtraData()54 	private String[] getExtraData() {
55 		//The extension point has been created by parsing, or does not have any extra data
56 		if (noExtraData()) { //When this is true, the extraInformation is always a String[]. This happens when the object is created by the parser.
57 			if (extraInformation != null)
58 				return (String[]) extraInformation;
59 			return new String[EXTRA_SIZE];
60 		}
61 
62 		//The extension point has been loaded from the cache.
63 		String[] result = null;
64 		if (extraInformation == null || (result = ((extraInformation instanceof SoftReference) ? (String[]) ((SoftReference<?>) extraInformation).get() : (String[]) extraInformation)) == null) {
65 			result = registry.getTableReader().loadExtensionPointExtraData(getExtraDataOffset());
66 			extraInformation = new SoftReference<>(result);
67 		}
68 		return result;
69 	}
70 
71 	/**
72 	 * At the end of this method, extra information will be a string[]
73 	 */
ensureExtraInformationType()74 	private void ensureExtraInformationType() {
75 		if (extraInformation instanceof SoftReference) {
76 			extraInformation = ((SoftReference<?>) extraInformation).get();
77 		}
78 		if (extraInformation == null) {
79 			extraInformation = new String[EXTRA_SIZE];
80 		}
81 	}
82 
getSchemaReference()83 	protected String getSchemaReference() {
84 		String[] result = getExtraData();
85 		return result[1] == null ? "" : result[SCHEMA].replace(File.separatorChar, '/'); //$NON-NLS-1$
86 	}
87 
getLabel()88 	protected String getLabel() {
89 		String[] result = getExtraData();
90 		return result[0] == null ? "" : result[LABEL]; //$NON-NLS-1$
91 	}
92 
getUniqueIdentifier()93 	protected String getUniqueIdentifier() {
94 		return getExtraData()[QUALIFIED_NAME];
95 	}
96 
getNamespace()97 	public String getNamespace() {
98 		return getExtraData()[NAMESPACE];
99 	}
100 
getContributorId()101 	protected String getContributorId() {
102 		return getExtraData()[CONTRIBUTOR_ID];
103 	}
104 
getContributor()105 	public IContributor getContributor() {
106 		return registry.getObjectManager().getContributor(getContributorId());
107 	}
108 
setSchema(String value)109 	void setSchema(String value) {
110 		ensureExtraInformationType();
111 		((String[]) extraInformation)[SCHEMA] = value;
112 	}
113 
setLabel(String value)114 	void setLabel(String value) {
115 		ensureExtraInformationType();
116 		((String[]) extraInformation)[LABEL] = value;
117 	}
118 
setUniqueIdentifier(String value)119 	void setUniqueIdentifier(String value) {
120 		ensureExtraInformationType();
121 		((String[]) extraInformation)[QUALIFIED_NAME] = value;
122 	}
123 
setNamespace(String value)124 	void setNamespace(String value) {
125 		ensureExtraInformationType();
126 		((String[]) extraInformation)[NAMESPACE] = value;
127 	}
128 
setContributorId(String id)129 	void setContributorId(String id) {
130 		ensureExtraInformationType();
131 		((String[]) extraInformation)[CONTRIBUTOR_ID] = id;
132 	}
133 
134 	@Override
toString()135 	public String toString() {
136 		return getUniqueIdentifier();
137 	}
138 
getLabelAsIs()139 	protected String getLabelAsIs() {
140 		String[] result = getExtraData();
141 		return result[0] == null ? "" : result[LABEL]; //$NON-NLS-1$
142 	}
143 
getLabel(String locale)144 	protected String getLabel(String locale) {
145 		registry.logMultiLangError();
146 		return getLabel();
147 	}
148 
149 }
150