001: /*******************************************************************************
002: * Copyright (c) 2007 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.texteditor;
011:
012: import java.util.ArrayList;
013: import java.util.List;
014:
015: import org.eclipse.core.runtime.Assert;
016: import org.eclipse.core.runtime.IConfigurationElement;
017: import org.eclipse.core.runtime.IExtensionRegistry;
018: import org.eclipse.core.runtime.IStatus;
019: import org.eclipse.core.runtime.Platform;
020: import org.eclipse.core.runtime.Status;
021:
022: import org.eclipse.ui.internal.texteditor.NLSUtility;
023: import org.eclipse.ui.internal.texteditor.TextEditorPlugin;
024:
025: /**
026: * Describes a contribution to the 'org.eclipse.ui.workbench.texteditor.hyperlinkDetectorTargets'
027: * extension point.
028: *
029: * @since 3.3
030: */
031: public final class HyperlinkDetectorTargetDescriptor {
032:
033: private static final String HYPERLINK_DETECTOR_TARGETS_EXTENSION_POINT = "org.eclipse.ui.workbench.texteditor.hyperlinkDetectorTargets"; //$NON-NLS-1$
034: private static final String TARGET_ELEMENT = "target"; //$NON-NLS-1$
035: private static final String ID_ATTRIBUTE = "id"; //$NON-NLS-1$
036: private static final String NAME_ATTRIBUTE = "name"; //$NON-NLS-1$
037: private static final String DESCRIPTION_ATTRIBUTE = "description"; //$NON-NLS-1$
038: private static final String CONTEXT_ELEMENT = "context"; //$NON-NLS-1$
039: private static final String TYPE_ATTRIBUTE = "type"; //$NON-NLS-1$
040:
041: private IConfigurationElement fElement;
042:
043: /**
044: * Returns descriptors for all hyperlink detector extensions.
045: *
046: * @return an array with the contributed hyperlink detectors
047: */
048: public static HyperlinkDetectorTargetDescriptor[] getContributedHyperlinkDetectorTargets() {
049: IExtensionRegistry registry = Platform.getExtensionRegistry();
050: IConfigurationElement[] elements = registry
051: .getConfigurationElementsFor(HYPERLINK_DETECTOR_TARGETS_EXTENSION_POINT);
052: HyperlinkDetectorTargetDescriptor[] hyperlinkDetectorDescs = createDescriptors(elements);
053: return hyperlinkDetectorDescs;
054: }
055:
056: /**
057: * Creates a new descriptor from the given configuration element.
058: *
059: * @param element the configuration element
060: */
061: private HyperlinkDetectorTargetDescriptor(
062: IConfigurationElement element) {
063: Assert.isNotNull(element);
064: fElement = element;
065: }
066:
067: //---- XML Attribute accessors ---------------------------------------------
068:
069: /**
070: * Returns the hyperlink detector target's id.
071: *
072: * @return the hyperlink detector target's id
073: */
074: public String getId() {
075: return fElement.getAttribute(ID_ATTRIBUTE);
076: }
077:
078: /**
079: * Returns the hyperlink detector target's name.
080: *
081: * @return the hyperlink detector target's name
082: */
083: public String getName() {
084: return fElement.getAttribute(NAME_ATTRIBUTE);
085: }
086:
087: /**
088: * Returns the types that the context of this
089: * hyperlink detector target supports.
090: *
091: * @return an array with type names that this target's context supports
092: */
093: public String[] getTypes() {
094: IConfigurationElement[] contexts = fElement
095: .getChildren(CONTEXT_ELEMENT);
096: String[] types = new String[contexts.length];
097: for (int i = 0; i < contexts.length; i++)
098: types[i] = contexts[i].getAttribute(TYPE_ATTRIBUTE);
099: return types;
100: }
101:
102: /**
103: * Returns the hyperlink detector target's description.
104: *
105: * @return the hyperlink detector target's description or <code>null</code> if not provided
106: */
107: public String getDescription() {
108: return fElement.getAttribute(DESCRIPTION_ATTRIBUTE);
109: }
110:
111: public boolean equals(Object obj) {
112: if (obj == null || !obj.getClass().equals(this .getClass())
113: || getId() == null)
114: return false;
115: return getId().equals(
116: ((HyperlinkDetectorTargetDescriptor) obj).getId());
117: }
118:
119: public int hashCode() {
120: return getId().hashCode();
121: }
122:
123: private static HyperlinkDetectorTargetDescriptor[] createDescriptors(
124: IConfigurationElement[] elements) {
125: List result = new ArrayList(elements.length);
126: for (int i = 0; i < elements.length; i++) {
127: IConfigurationElement element = elements[i];
128: if (TARGET_ELEMENT.equals(element.getName())) {
129: HyperlinkDetectorTargetDescriptor desc = new HyperlinkDetectorTargetDescriptor(
130: element);
131: if (desc.isValid())
132: result.add(desc);
133: else {
134: String message = NLSUtility
135: .format(
136: EditorMessages.Editor_error_HyperlinkDetectorTarget_invalidExtension_message,
137: new String[] {
138: desc.getId(),
139: element.getContributor()
140: .getName() });
141: TextEditorPlugin.getDefault().getLog().log(
142: new Status(IStatus.ERROR,
143: TextEditorPlugin.PLUGIN_ID,
144: IStatus.OK, message, null));
145: }
146: } else {
147: String message = NLSUtility
148: .format(
149: EditorMessages.Editor_error_HyperlinkDetectorTarget_invalidElementName_message,
150: new String[] {
151: element.getContributor()
152: .getName(),
153: element.getName() });
154: TextEditorPlugin.getDefault().getLog().log(
155: new Status(IStatus.ERROR,
156: TextEditorPlugin.PLUGIN_ID, IStatus.OK,
157: message, null));
158: }
159:
160: }
161: return (HyperlinkDetectorTargetDescriptor[]) result
162: .toArray(new HyperlinkDetectorTargetDescriptor[result
163: .size()]);
164: }
165:
166: private boolean isValid() {
167: return getId() != null && getName() != null;
168: }
169:
170: }
|