blob: 4012a7b5b44cdff96354e3f5ca6b4fc12a46d544 [file] [log] [blame]
jlanutid46b1732006-08-16 21:50:22 +00001/***************************************************************************************************
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 **************************************************************************************************/
9package org.eclipse.wst.common.internal.emf.utilities;
10
11import java.util.HashMap;
12import java.util.Map;
13
14import org.eclipse.wst.common.internal.emf.resource.Translator;
15import 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 */
21public 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_williams8bb85872006-11-25 04:51:30 +000031 // never used
32 //private static final Translator[] NO_TRANSLATORS = new Translator[0];
jlanutid46b1732006-08-16 21:50:22 +000033
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}