KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > util > XMLComponentRegistry


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.pde.internal.core.util;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Collections JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Map JavaDoc;
18
19
20 /**
21  * SchemaDescriptionRegistry
22  *
23  */

24 public class XMLComponentRegistry {
25
26     private static XMLComponentRegistry fPinstance;
27     
28     /**
29      * Name value key
30      */

31     public static final String JavaDoc F_NAME = "n"; //$NON-NLS-1$
32

33     /**
34      * Description value key
35      */

36     public static final String JavaDoc F_DESCRIPTION = "d"; //$NON-NLS-1$
37

38     /**
39      * Identifier to access the registry dedicated to schema components
40      */

41     public static final int F_SCHEMA_COMPONENT = 2;
42     
43     /**
44      * Identifier to access the registry dedicated to element components
45      */

46     public static final int F_ELEMENT_COMPONENT = 4;
47
48     /**
49      * Identifier to access the registry dedicated to attribute components
50      */

51     public static final int F_ATTRIBUTE_COMPONENT = 8;
52     
53     private static ArrayList JavaDoc fConsumers = new ArrayList JavaDoc();
54
55     private Map JavaDoc fSchemaComponentMap;
56
57     private Map JavaDoc fAttributeComponentMap;
58
59     private Map JavaDoc fElementComponentMap;
60
61     private XMLComponentRegistry() {
62         fSchemaComponentMap = Collections.synchronizedMap(new HashMap JavaDoc());
63         fAttributeComponentMap = Collections.synchronizedMap(new HashMap JavaDoc());
64         fElementComponentMap = Collections.synchronizedMap(new HashMap JavaDoc());
65     }
66     
67     public static XMLComponentRegistry Instance() {
68         if (fPinstance == null) {
69             fPinstance = new XMLComponentRegistry();
70         }
71         return fPinstance;
72     }
73     
74     public void dispose() {
75         if (fSchemaComponentMap != null) {
76             fSchemaComponentMap.clear();
77         }
78         if (fAttributeComponentMap != null) {
79             fAttributeComponentMap.clear();
80         }
81         if (fElementComponentMap != null) {
82             fElementComponentMap.clear();
83         }
84     }
85
86     public void putDescription(String JavaDoc key, String JavaDoc value, int mapType) {
87         putValue(F_DESCRIPTION, key, value, mapType);
88     }
89
90     public void putName(String JavaDoc key, String JavaDoc value, int mapType) {
91         putValue(F_NAME, key, value, mapType);
92     }
93
94     private Map JavaDoc getTargetMap(int mask) {
95         Map JavaDoc targetMap = null;
96         if (mask == F_SCHEMA_COMPONENT) {
97             targetMap = fSchemaComponentMap;
98         } else if (mask == F_ATTRIBUTE_COMPONENT) {
99             targetMap = fAttributeComponentMap;
100         } else if (mask == F_ELEMENT_COMPONENT) {
101             targetMap = fElementComponentMap;
102         }
103         return targetMap;
104     }
105     
106     private void putValue(String JavaDoc valueKey, String JavaDoc key, String JavaDoc value, int mapType) {
107         if (key != null) {
108             Map JavaDoc targetMap = getTargetMap(mapType);
109             if (targetMap == null) {
110                 return;
111             }
112             HashMap JavaDoc previousValue = (HashMap JavaDoc)targetMap.get(key);
113             if (previousValue == null) {
114                 HashMap JavaDoc newValue = new HashMap JavaDoc();
115                 newValue.put(valueKey, value);
116                 targetMap.put(key, newValue);
117             } else {
118                 previousValue.put(valueKey, value);
119             }
120         }
121     }
122     
123     public void put(String JavaDoc key, HashMap JavaDoc value, int mapType) {
124         if (key != null) {
125             Map JavaDoc targetMap = getTargetMap(mapType);
126             if (targetMap == null) {
127                 return;
128             }
129             targetMap.put(key, value);
130         }
131     }
132     
133     public HashMap JavaDoc get(String JavaDoc key, int mapType) {
134         Map JavaDoc targetMap = getTargetMap(mapType);
135         if (targetMap == null) {
136             return null;
137         }
138         return (HashMap JavaDoc)targetMap.get(key);
139     }
140     
141     private String JavaDoc getValue(String JavaDoc valueKey, String JavaDoc key, int mapType) {
142         if (key != null) {
143             HashMap JavaDoc map = get(key, mapType);
144             if (map != null) {
145                 return (String JavaDoc)map.get(valueKey);
146             }
147         }
148         return null;
149     }
150     
151     public String JavaDoc getDescription(String JavaDoc key, int mapType) {
152         return getValue(F_DESCRIPTION, key, mapType);
153     }
154
155     public String JavaDoc getName(String JavaDoc key, int mapType) {
156         return getValue(F_NAME, key, mapType);
157     }
158     
159     public void connect(Object JavaDoc consumer) {
160         if (!fConsumers.contains(consumer)) {
161             fConsumers.add(consumer);
162         }
163     }
164     
165     public void disconnect(Object JavaDoc consumer) {
166         fConsumers.remove(consumer);
167         if (fConsumers.size() == 0) {
168             dispose();
169         }
170     }
171     
172 }
173
Popular Tags