|
| 1 | +/******************************************************************************* |
| 2 | + * Java2Script Pacemaker (http://j2s.sourceforge.net) |
| 3 | + * |
| 4 | + * Copyright (c) 2006 ognize.com and others. |
| 5 | + * All rights reserved. This program and the accompanying materials |
| 6 | + * are made available under the terms of the Eclipse Public License v1.0 |
| 7 | + * which accompanies this distribution, and is available at |
| 8 | + * http://www.eclipse.org/legal/epl-v10.html |
| 9 | + * |
| 10 | + * Contributors: |
| 11 | + * ognize.com - initial API and implementation |
| 12 | + *******************************************************************************/ |
| 13 | +package net.sf.j2s.core.compiler; |
| 14 | + |
| 15 | +import java.util.HashMap; |
| 16 | +import java.util.Map; |
| 17 | +import org.eclipse.core.runtime.CoreException; |
| 18 | +import org.eclipse.core.runtime.IConfigurationElement; |
| 19 | +import org.eclipse.core.runtime.IExtension; |
| 20 | +import org.eclipse.core.runtime.IExtensionPoint; |
| 21 | +import org.eclipse.core.runtime.IExtensionRegistry; |
| 22 | +import org.eclipse.core.runtime.Platform; |
| 23 | + |
| 24 | +/** |
| 25 | + * @author zhou renjian |
| 26 | + * |
| 27 | + * 2006-10-26 |
| 28 | + */ |
| 29 | +public class ExtendedVisitors { |
| 30 | + |
| 31 | + public static Map visitors = new HashMap(); |
| 32 | + |
| 33 | + private static boolean isExtensionPointsChecked = false; |
| 34 | + |
| 35 | + private static void checkExtensionPoints() { |
| 36 | + if (isExtensionPointsChecked) { |
| 37 | + return; |
| 38 | + } |
| 39 | + isExtensionPointsChecked = true; |
| 40 | + IExtensionRegistry extensionRegistry = Platform.getExtensionRegistry(); |
| 41 | + IExtensionPoint extensionPoint = extensionRegistry |
| 42 | + .getExtensionPoint("net.sf.j2s.core.extendedASTScriptVisitor"); //$NON-NLS-1$ |
| 43 | + if (extensionPoint == null) { |
| 44 | + return; |
| 45 | + } |
| 46 | + IExtension[] extensions = extensionPoint.getExtensions(); |
| 47 | + // For each extension ... |
| 48 | + for (int i = 0; i < extensions.length; i++) { |
| 49 | + IExtension extension = extensions[i]; |
| 50 | + IConfigurationElement[] elements = extension |
| 51 | + .getConfigurationElements(); |
| 52 | + // For each member of the extension ... |
| 53 | + for (int j = 0; j < elements.length; j++) { |
| 54 | + IConfigurationElement element = elements[j]; |
| 55 | + if ("extendedASTScriptVisitor".equals(element.getName())) { //$NON-NLS-1$ |
| 56 | + String className = element.getAttribute("class"); //$NON-NLS-1$ |
| 57 | + String id = element.getAttribute("id"); //$NON-NLS-1$ |
| 58 | + if (className != null && className.trim().length() != 0 |
| 59 | + && id != null && id.trim().length() != 0) { |
| 60 | + try { |
| 61 | + Object callback = element |
| 62 | + .createExecutableExtension("class"); |
| 63 | + if (callback instanceof IExtendedVisitor) { |
| 64 | + visitors.put(id.trim(), callback); |
| 65 | + } |
| 66 | + } catch (CoreException e) { |
| 67 | + e.printStackTrace(); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + public static void register(String visitorID, IExtendedVisitor visitor) { |
| 76 | + if (visitorID != null && visitorID.trim().length() != 0 |
| 77 | + && visitor != null) { |
| 78 | + visitors.put(visitorID, visitor); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + public static IExtendedVisitor deregister(String visitorID) { |
| 83 | + if (visitorID != null && visitorID.trim().length() != 0) { |
| 84 | + return (IExtendedVisitor) visitors.remove(visitorID); |
| 85 | + } |
| 86 | + return null; |
| 87 | + } |
| 88 | + |
| 89 | + public static IExtendedVisitor[] getExistedVisitors() { |
| 90 | + checkExtensionPoints(); |
| 91 | + return (IExtendedVisitor[]) visitors.values().toArray(new IExtendedVisitor[0]); |
| 92 | + } |
| 93 | + |
| 94 | + public static IExtendedVisitor getExistedVisitor(String id) { |
| 95 | + checkExtensionPoints(); |
| 96 | + return (IExtendedVisitor) visitors.get(id); |
| 97 | + } |
| 98 | + |
| 99 | +} |
0 commit comments