Skip to main content
summaryrefslogtreecommitdiffstats
blob: 109ab9ebcb9f3ffff434d49f5d56e033728c37bf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*******************************************************************************
 * Copyright (c) 2002, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   IBM - Initial API and implementation
 *   Jens Lukowski/Innoopract - initial renaming/restructuring
 * 	 Gerry Kessler/Oracle - code borrowed and repurposed for JSF subproject
 *
 *******************************************************************************/
package org.eclipse.jst.jsf.contentmodel.annotation.internal;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.jst.jsf.common.JSFCommonPlugin;
import org.eclipse.jst.jsf.contentmodel.annotation.internal.provisional.ICMAnnotationSourceFileInfo;
import org.eclipse.osgi.util.NLS;

/**
 * This internal class is used to associate one or more annotation files with a grammar
 * file mapped by a key (uri).
 * 
 * Using the CMAnnotationFileRegistryReader, extenders of <code>org.eclipse.jst.jsf.contentmodel.annotations.annotationFiles</code> are loaded
 * into the annotationFilesMap keyed by uri.   There can be multiple annotationFiles mapped to the same uri.
 * 
 * This registry should only be accessed by clients using the <code>org.eclipse.jst.jsf.contentmodel.annotation.internal.provisional.CMAnnotationHelper</code>.
 * Only when the system attempts to access an annotation for a content model, will the file be parsed and cached.
 * 
 * When queried, CMAnnotationPropertyValue objects are returned for each named property found in each annotation file for a particular uri.
 * 
 * also see org.eclipse.jst.jsf.contentmodel.annotations.annotationFiles extension-point
 * @see CMAnnotationFileRegistryReader
 * @see CMAnnotationMap
 * @see org.eclipse.jst.jsf.contentmodel.annotation.internal.provisional.ICMAnnotationSourceFileInfo
 * 
 * @deprecated see common.metadata package
 */
public final class CMAnnotationFileRegistry {
	//all annotation files registered using "org.eclipse.jst.jsf.contentmodel.annotations.annotationFiles" ext-pt. and keyed by uri.  
	private Map annotationFilesMap 	= new HashMap(1);
	//will return list of CMAnnotationMap (parsed annotation files) objects keyed by uri
	private Map parsedFilesMap 		= new HashMap(1);

	private static CMAnnotationFileRegistry reg;
	
	private static final boolean DISABLED = true;
	
	public static CMAnnotationFileRegistry getInstance() {
		if (reg == null){
			reg = new CMAnnotationFileRegistry();
		}
		return reg;
		
	}
	
	//constructor reads the annotationFile registry and populates the annotationFilesMap
	private CMAnnotationFileRegistry() {
		if (!(DISABLED))
			new CMAnnotationFileRegistryReader(this).readRegistry();
	}
	
	private synchronized List getAnnotationFilesInfos(String uri) {
		List theList = (List) annotationFilesMap.get(uri);
		return theList != null ? theList : new ArrayList();
	}
		
	/**
	 * For use by registry reader only
	 * @param uri
	 * @param fileInfo 
	 */
	public synchronized void addAnnotationFileInfo(String uri, ICMAnnotationSourceFileInfo fileInfo) {
		List fileInfos = (List) annotationFilesMap.get(uri);
		if (fileInfos == null) {
			fileInfos = new ArrayList();
			annotationFilesMap.put(uri, fileInfos);
		}
		fileInfos.add(fileInfo);
	}	
	
	private synchronized void addParsedFileToMap(String uri, CMAnnotationMap map) {
		List maps = (List) parsedFilesMap.get(uri);
		if (maps == null) {
			maps = new ArrayList();
			parsedFilesMap.put(uri, maps);
		}
		maps.add(map);
	}

	/**
	 * Return list of CMAnnotationMaps for a given uri.  
	 * By calling this method, unparsed annotation files for the given 
	 * uri will be parsed.
	 * 
	 * @param uri
	 * @return List of CMAnnotationMaps
	 */
	public synchronized List getAnnotationMaps(String uri) {
		if (DISABLED){
			JSFCommonPlugin.log(IStatus.ERROR, "Attempted metadata access using CMAnnotationFiles for uri: "+uri+".   Use org.eclipse.jst.jsf.common.metadata, instead." );
			return Collections.EMPTY_LIST;
		}
		
		List list = (List)parsedFilesMap.get(uri);
		if (list == null){
			loadAnnotationsForGrammar(uri);
			list = (List)parsedFilesMap.get(uri);
		}
		return list;
	}
	

	/**
	 * Does this bundle define an annotationFile extension for the given uri.
	 * 
	 * @param bundleId
	 * @param uri
	 * @return boolean
	 */
	public boolean hasAnnotations(String bundleId, String uri) {
//		if (DISABLED){
//			
//		}
		List maps = (List)annotationFilesMap.get(uri);
		if (maps == null || maps.size() ==0)
			return false;
		
		for (int i=0;i<maps.size();i++){
			if (((ICMAnnotationSourceFileInfo)maps.get(i)).getBundleId().equals(bundleId))
				return true;
		}
		return false;
	}
	/**
	 * Are there any annotationFile extensions defined for the given uri.
	 * @param uri
	 * @return boolean
	 */
	public boolean hasAnnotations(String uri){
		if (DISABLED){
			JSFCommonPlugin.log(IStatus.ERROR, "Attempted metadata access using CMAnnotationFiles for uri: "+uri+".   Use org.eclipse.jst.jsf.common.metadata, instead." );
			return false;
		}
		if (annotationFilesMap.get(uri) != null)
			return true;
		return false;
	}

	private boolean areAnnotationsParsed(String uri) {
		if (hasAnnotations(uri)){
			if (parsedFilesMap.get(uri) != null)
				return true;
		}
		return false;
	}
	
	private void loadAnnotationsForGrammar(String uri) {
		if (areAnnotationsParsed(uri))
			return;
		
		if (DISABLED)
			return;
		
		List annotationFiles = getAnnotationFilesInfos(uri);
		for (Iterator i = annotationFiles.iterator(); i.hasNext();) {
			try {					
				CMAnnotationFileInfo annotationFileInfo = (CMAnnotationFileInfo) i.next();
				CMAnnotationMap map = new CMAnnotationMap(annotationFileInfo);
				CMAnnotationFileParserHelper parserHelper = new CMAnnotationFileParserHelper();
				parserHelper.parse(map);
				addParsedFileToMap(uri, map);
			}
			catch (Exception e) {
                JSFCommonPlugin.log(e, NLS.bind(Messages.CMAnnotationFileRegistry_load_error, new String[]{uri}));				
			}
		}		
	}

}

Back to the top