Skip to main content
summaryrefslogtreecommitdiffstats
blob: c1f2f9e16c316ad7cb7b1fdd30e1e21c0b0547c7 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*******************************************************************************
 * Copyright (c) 2001, 2007 Oracle 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:
 *     Oracle Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.jsf.common.metadata.internal;

import java.util.Iterator;

import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.emf.ecore.xml.type.SimpleAnyType;
import org.eclipse.emf.ecore.xml.type.XMLTypeFactory;
import org.eclipse.jst.jsf.common.metadata.Entity;
import org.eclipse.jst.jsf.common.metadata.MetadataFactory;
import org.eclipse.jst.jsf.common.metadata.MetadataPackage;
import org.eclipse.jst.jsf.common.metadata.Model;
import org.eclipse.jst.jsf.common.metadata.Trait;
import org.eclipse.wst.xml.core.internal.contentmodel.CMDocument;
import org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration;

/**
 * Abstract class that the taglib domain translators use to 
 * convert the CM model to a standard meta data model
 *
 */
public abstract class AbstractTagLibDomainContentModelMetaDataTranslator {
	
	/**
	 * {@link IMetaDataModelMergeAssistant} to use
	 */
	protected IMetaDataModelMergeAssistant _assistant;

	/**
	 * Set the assistant to use during the translation.   Must be called prior to doTransalate(doc);
	 * @param assistant
	 */
	protected void setAssistant(IMetaDataModelMergeAssistant assistant){
		_assistant = assistant;
	}
	
	/**
	 * Transforms the CMDocument into entities and traits of a standard metadata model 
	 * using the assistant that must be set before this call.
	 * @param doc
	 */
	protected void doTranslate(CMDocument doc){
		createTags(doc);
		createTagfileTraits(doc);		
	}
	
	/**
	 * @param entity
	 * @param key
	 * @param value
	 */
	protected void createSimpleStringEntityTraitIfNecessary(final Entity entity, final String key,
			final String value) {
		Trait t = findTraitOnEntityById(entity, key);
		if (t == null){
			t = internalCreateTrait(entity, key);

			SimpleAnyType val = XMLTypeFactory.eINSTANCE.createSimpleAnyType();
			val.setInstanceType(EcorePackage.eINSTANCE.getEString());
			val.setValue(value);
			
			t.setValue(val);
		}
	}

	/**
	 * @param entity
	 * @param key
	 * @param value
	 */
	protected void createSimpleBooleanObjectEntityTraitIfNecessary(final Entity entity,
			String key, boolean value) {
		
		Trait t = findTraitOnEntityById(entity, key);
		if (t == null){
			t = internalCreateTrait(entity, key);

			SimpleAnyType val = XMLTypeFactory.eINSTANCE.createSimpleAnyType();
			val.setInstanceType(EcorePackage.eINSTANCE.getEBoolean());
			val.setRawValue(value == true ? "true" : "false");
			
			t.setValue(val);
		}
		
	}
	
	/**
	 * @param entity
	 * @param key
	 * @return Trait
	 */
	protected Trait internalCreateTrait(final Entity entity, final String key) {
		Trait t = MetadataFactory.eINSTANCE.createTrait();
		t.setId(key);
		t.setSourceModelProvider(_assistant.getSourceModelProvider());
		entity.getTraits().add(t);
		return t;
	}
	
	/**
	 * @param entity
	 * @param key
	 * @return Trait
	 */
	protected Trait findTraitOnEntityById(final Entity entity, final String key) {
		for (Iterator it=entity.getTraits().iterator();it.hasNext();){
			Trait t = (Trait)it.next();
			if (key.equals(t.getId()))
				return t;
		}
		return null;
	}

	/**
	 * @param nodeName
	 * @return Entity
	 */
	protected Entity findTagEntity(final String nodeName) {
		for (Iterator it=getMergedModel().getChildEntities().iterator();it.hasNext();){
			Entity entity = (Entity)it.next();
			if (nodeName.equals(entity.getId()))
				return entity;
		}
		return null;
	}

	/**
	 * Create entities for tags
	 * @param doc
	 */
	protected void createTags(final CMDocument doc) {
		for (Iterator it=doc.getElements().iterator();it.hasNext();){
			CMElementDeclaration tag = (CMElementDeclaration)it.next();
			Entity entity = findTagEntity(tag.getNodeName());
			if (entity == null){
				entity = MetadataFactory.eINSTANCE.createEntity();
				entity.setId(tag.getNodeName());
				entity.setType("tag");
				getMergedModel().getChildEntities().add(entity);
			}
			setTagEntityTraits(tag, entity);
		}
		
	}
	
	/**
	 * Sets the standard traits for a tag entity from the element declaration
	 * @param tag
	 * @param entity
	 */
	protected void setTagEntityTraits(CMElementDeclaration tag, Entity entity) {
		createSimpleStringEntityTraitIfNecessary(entity, "display-label", getTagDisplayName(tag));
		createSimpleStringEntityTraitIfNecessary(entity, "description", getTagDescription(tag));	
		createSimpleStringEntityTraitIfNecessary(entity, "small-icon", getTagSmallIcon(tag));
		createSimpleStringEntityTraitIfNecessary(entity, "large-icon", getTagLargeIcon(tag));
		createSimpleBooleanObjectEntityTraitIfNecessary(entity, "expert", getTagIsExpert(tag));
		createSimpleBooleanObjectEntityTraitIfNecessary(entity, "hidden", getTagIsHidden(tag));
		
//		createRequiredAttrTraits(entity, tag);
	}
	
	/**
	 * @param tag
	 * @return false.   subclass should override if CMElementDeclaration has the metadata.
	 */
	protected boolean getTagIsHidden(CMElementDeclaration tag) {return false;}

	/**
	 * @param tag
	 * @return false.   subclass should override if CMElementDeclaration has the metadata.
	 */
	protected boolean getTagIsExpert(CMElementDeclaration tag) {return false;}

	/**
	 * @param tag
	 * @return null.   subclass should override if CMElementDeclaration has the metadata.
	 */
	protected String getTagLargeIcon(CMElementDeclaration tag) {return null;}

	/**
	 * @param tag
	 * @return null.   subclass should override if CMElementDeclaration has the metadata.
	 */
	protected String getTagSmallIcon(CMElementDeclaration tag) {return null;}

	/**
	 * @param tag
	 * @return null.   subclass should override if CMElementDeclaration has the metadata.
	 */
	protected String getTagDescription(CMElementDeclaration tag) {return null;}

	/**
	 * @param tag
	 * @return tag.getElementName()
	 */
	protected String getTagDisplayName(CMElementDeclaration tag) {return tag.getElementName();}

	/**
	 * Creates standard traits for tag file entity from CMDocument metadata
	 * @param doc
	 */
	protected void createTagfileTraits(CMDocument doc) {
		Model model = getMergedModel();

		createSimpleStringEntityTraitIfNecessary(model, "display-label", getURIDisplayLabel());			
		createSimpleStringEntityTraitIfNecessary(model, "description", getURIDescription());
		createSimpleStringEntityTraitIfNecessary(model, "default-prefix", getURIDefaultPrefix());
		createSimpleBooleanObjectEntityTraitIfNecessary(model, "expert", getURIExpert());
		createSimpleBooleanObjectEntityTraitIfNecessary(model, "hidden", getURIHidden());
		
	}
	
	/**
	 * @return the display label to use for this model.  Subclasses should override if model has the meta data.
	 */
	protected String getURIDisplayLabel(){
		return getMergedModel().getId();
	}
	
	/**
	 * @return default prefix to use for tags.  Returns null. Subclasses should override if model has the meta data.
	 */
	protected String getURIDefaultPrefix(){
		return null;
	}

	/**
	 * @return description to use.  Default is the URI.  Subclasses should override if model has the meta data.
	 */
	protected String getURIDescription(){
		return getMergedModel().getId();
	}

	/**
	 * @return false.  Subclasses should override if model has the meta data.
	 */
	protected boolean getURIExpert() { return false;}

	/**
	 * @return false.  Subclasses should override if model has the meta data.
	 */
	protected boolean getURIHidden() {return false;	}

	/**
	 * @return a model of all entities and traits
	 */
	protected Model getMergedModel() {
		Model model = (Model)_assistant.getMergedModel().getRoot();
		if (model == null){
			//need to create model key object
			Model tld = createTLDModel();
			_assistant.getMergedModel().setRoot(tld);
			model = (Model)_assistant.getMergedModel().getRoot();
		}
		return model;
	}

	/**
	 * @return new model 
	 */
	protected Model createTLDModel() {
		Model entity = getFactory().createModel();
		entity.setId(_assistant.getMergedModel().getModelKey().getUri());
		return entity;
	}	
	
	/**
	 * @return MetadataFactory instance
	 */
	protected MetadataFactory getFactory(){
		return (MetadataFactory) MetadataPackage.eINSTANCE.getEFactoryInstance(); 
	}
	
	/**
	 * @return CMDocument being used for the current translate call
	 */
	protected CMDocument getSourceModel() {
		return (CMDocument)_assistant.getSourceModelProvider().getSourceModel();
	}
}

Back to the top