001: /*******************************************************************************
002: * Copyright (c) 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.texteditor.rulers;
011:
012: import java.net.URL;
013:
014: import org.osgi.framework.Bundle;
015:
016: import com.ibm.icu.text.MessageFormat;
017:
018: import org.eclipse.core.runtime.Assert;
019: import org.eclipse.core.runtime.FileLocator;
020: import org.eclipse.core.runtime.IConfigurationElement;
021: import org.eclipse.core.runtime.IExtension;
022: import org.eclipse.core.runtime.ILog;
023: import org.eclipse.core.runtime.IStatus;
024: import org.eclipse.core.runtime.InvalidRegistryObjectException;
025: import org.eclipse.core.runtime.Path;
026: import org.eclipse.core.runtime.Platform;
027: import org.eclipse.core.runtime.Status;
028:
029: import org.eclipse.ui.internal.texteditor.TextEditorPlugin;
030:
031: public final class ExtensionPointHelper {
032:
033: private final IConfigurationElement fElement;
034: private final String fName;
035: private final ILog fLog;
036:
037: public ExtensionPointHelper(IConfigurationElement element, ILog log)
038: throws InvalidRegistryObjectException {
039: Assert.isLegal(element != null);
040: Assert.isLegal(log != null);
041: fLog = log;
042: fElement = element;
043: fName = element.getName();
044: // see if we have a conventional 'id' attribute
045: }
046:
047: public String getDefaultAttribute(String attribute, String dflt)
048: throws InvalidRegistryObjectException {
049: String value = fElement.getAttribute(attribute);
050: return value == null ? dflt : value;
051: }
052:
053: public String getNonNullAttribute(String attribute)
054: throws InvalidRegistryObjectException {
055: String value = fElement.getAttribute(attribute);
056: if (value == null)
057: fail(MessageFormat
058: .format(
059: RulerColumnMessages.ExtensionPointHelper_missing_attribute_msg,
060: new Object[] { fName, attribute }));
061: return value;
062: }
063:
064: public float getDefaultAttribute(String attribute, float dflt) {
065: String value = getDefaultAttribute(attribute, null);
066: if (value == null)
067: return dflt;
068:
069: try {
070: return Float.valueOf(value).floatValue();
071: } catch (NumberFormatException x) {
072: fail(MessageFormat
073: .format(
074: RulerColumnMessages.ExtensionPointHelper_invalid_number_attribute_msg,
075: new Object[] { attribute, fName }));
076: return dflt;
077: }
078: }
079:
080: public boolean getDefaultAttribute(String attribute, boolean dflt) {
081: String value = getDefaultAttribute(attribute, null);
082: if (value == null)
083: return dflt;
084:
085: try {
086: return Boolean.valueOf(value).booleanValue();
087: } catch (NumberFormatException x) {
088: fail(MessageFormat
089: .format(
090: RulerColumnMessages.ExtensionPointHelper_invalid_number_attribute_msg,
091: new Object[] { fName, attribute }));
092: return dflt;
093: }
094: }
095:
096: public void fail(String message)
097: throws InvalidRegistryObjectException {
098: String id = findId(fElement);
099: String extensionPointId = fElement.getDeclaringExtension()
100: .getExtensionPointUniqueIdentifier();
101: Object[] args = { fElement.getContributor().getName(), id,
102: extensionPointId };
103: String blame = MessageFormat
104: .format(
105: RulerColumnMessages.ExtensionPointHelper_invalid_contribution_msg,
106: args);
107:
108: IStatus status = new Status(IStatus.WARNING,
109: TextEditorPlugin.PLUGIN_ID, IStatus.OK,
110: blame + message, null);
111: fLog.log(status);
112: throw new InvalidRegistryObjectException();
113: }
114:
115: public static String findId(IConfigurationElement element) {
116: String id = null;
117: while (element != null && id == null) {
118: id = element.getAttribute("id"); //$NON-NLS-1$
119: if (id != null)
120: break;
121: Object parent = element.getParent();
122: if (parent instanceof IExtension) {
123: id = ((IExtension) parent).getUniqueIdentifier();
124: break;
125: } else if (parent instanceof IConfigurationElement) {
126: element = (IConfigurationElement) parent;
127: } else {
128: break;
129: }
130: }
131: return id == null ? "<unknown>" : id; //$NON-NLS-1$
132: }
133:
134: public URL getDefaultResourceURL(String attribute, URL dflt) {
135: String value = getDefaultAttribute(attribute, null);
136: if (value == null)
137: return dflt;
138:
139: Bundle bundle = getBundle();
140: if (bundle == null)
141: return dflt;
142:
143: Path path = new Path(value);
144: return FileLocator.find(bundle, path, null);
145: }
146:
147: private Bundle getBundle() {
148: String namespace = fElement.getDeclaringExtension()
149: .getContributor().getName();
150: Bundle bundle = Platform.getBundle(namespace);
151: return bundle;
152: }
153: }
|