jlanuti | d46b173 | 2006-08-16 21:50:22 +0000 | [diff] [blame] | 1 | /*************************************************************************************************** |
| 2 | * Copyright (c) 2003, 2006 IBM Corporation and others. All rights reserved. This program and the |
| 3 | * accompanying materials are made available under the terms of the Eclipse Public License v1.0 |
| 4 | * which accompanies this distribution, and is available at |
| 5 | * http://www.eclipse.org/legal/epl-v10.html |
| 6 | * |
| 7 | * Contributors: IBM Corporation - initial API and implementation |
| 8 | **************************************************************************************************/ |
| 9 | package org.eclipse.wst.common.internal.emf.utilities; |
| 10 | |
| 11 | import java.util.HashMap; |
| 12 | import java.util.Map; |
| 13 | |
| 14 | import org.eclipse.wst.common.internal.emf.resource.Translator; |
| 15 | import org.eclipse.wst.common.internal.emf.utilities.TranslatorManager.TranslatorDescriptor; |
| 16 | |
| 17 | /** |
| 18 | * This is the service class to deliver API to use to retrieve Translator extensions from |
| 19 | * the TranslatorManager and its RegistryReader. |
| 20 | */ |
| 21 | public class TranslatorService { |
| 22 | |
| 23 | /** |
| 24 | * Static Key value pair of descriptors as keys and Translator instances as values |
| 25 | */ |
| 26 | private static final Map translators = new HashMap(); |
| 27 | |
| 28 | /** |
| 29 | * Static empty array used when no extensions found |
| 30 | */ |
david_williams | 8bb8587 | 2006-11-25 04:51:30 +0000 | [diff] [blame] | 31 | // never used |
| 32 | //private static final Translator[] NO_TRANSLATORS = new Translator[0]; |
jlanuti | d46b173 | 2006-08-16 21:50:22 +0000 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Singleton instance of the Translator service |
| 36 | */ |
| 37 | private static final TranslatorService INSTANCE = new TranslatorService(); |
| 38 | |
| 39 | /** |
| 40 | * Default constructor |
| 41 | */ |
| 42 | private TranslatorService() { |
| 43 | super(); |
| 44 | } |
| 45 | |
| 46 | public static TranslatorService getInstance() { |
| 47 | return INSTANCE; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * This will return the associated extension point TranslatorDescriptor objects from the manager |
| 52 | * @return TranslatorDescriptor[] |
| 53 | */ |
| 54 | public TranslatorDescriptor[] getTranslatorDescriptors() { |
| 55 | return TranslatorManager.getInstance().findTranslators(); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * This retrieves the extended Translators using the extension point manager and descriptors |
| 60 | * @return Translator[] (Note, the return value may contain null entries.) |
| 61 | */ |
| 62 | public Translator[] getTranslators() { |
| 63 | TranslatorDescriptor[] descriptors = getTranslatorDescriptors(); |
| 64 | Translator[] result = new Translator[descriptors.length]; |
| 65 | //The result index could differ from the descriptors index. |
| 66 | int resultIndex = 0; |
| 67 | for (int i=0; i<descriptors.length; i++) { |
| 68 | Translator instance = getTranslator(descriptors[i]); |
| 69 | if (instance!=null) { |
| 70 | result[resultIndex] = instance; |
| 71 | resultIndex++; |
| 72 | } |
| 73 | } |
| 74 | return result; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Retrieve the existing associated Translator instance for the descriptor, or create a new |
| 79 | * one and cache on the Set. |
| 80 | * |
| 81 | * @param translatorDescriptor |
| 82 | * @return Translator associated with the descriptor |
| 83 | */ |
| 84 | public Translator getTranslator(TranslatorDescriptor translatorDescriptor) { |
| 85 | Translator translator = (Translator) translators.get(translatorDescriptor); |
| 86 | if (translator != null) |
| 87 | return translator; |
| 88 | |
| 89 | synchronized (translators) { |
| 90 | translator = translatorDescriptor.createTranslator(); |
| 91 | if (translator != null) |
| 92 | translators.put(translatorDescriptor, translator); |
| 93 | } |
| 94 | return translator; |
| 95 | } |
| 96 | } |