Skip to main content
summaryrefslogtreecommitdiffstats
blob: a0812fe9ee97c1e7da52dd507135200d772cb85c (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
/*******************************************************************************
 * Copyright (c) 2007 Oracle Corporation.
 * 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 - initial API and implementation
 *    
 ********************************************************************************/
package org.eclipse.jst.jsf.common.metadata.internal;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.impl.BasicEObjectImpl;
import org.eclipse.emf.ecore.util.FeatureMap;
import org.eclipse.emf.ecore.xml.type.AnyType;
import org.eclipse.emf.ecore.xml.type.SimpleAnyType;
import org.eclipse.jst.jsf.common.JSFCommonPlugin;
import org.eclipse.jst.jsf.common.metadata.Trait;
import org.eclipse.jst.jsf.common.metadata.traittypes.traittypes.BooleanValue;
import org.eclipse.jst.jsf.common.metadata.traittypes.traittypes.ListOfValues;
import org.eclipse.jst.jsf.common.metadata.traittypes.traittypes.StringValue;
import org.eclipse.osgi.util.NLS;

/**
 * Experimental/prototype class to assist with decoding trait values.
 * This could/should be made API at some point after "hardening". (https://bugs.eclipse.org/bugs/show_bug.cgi?id=192052) 
 */
public class TraitValueHelper {
	private static final String KEY_NOT_FOUND = Messages.Key_not_found;
	/**
	 * @param trait
	 * @return EClass of trait value
	 */
	public static EClass getValueType(final Trait trait){
		if (trait == null)
			return null;
		if (trait.getValue() != null)
			return trait.getValue().eClass();
		return null;
	}
	
	/**
	 * @param trait
	 * @return value of trait as String Object.  
	 */
	public static Object getValue(final Trait trait){
		if (trait == null)
			return null;
		
		if (trait.getValue() == null)
			return null;
		
		if (trait.getValue() instanceof StringValue) {
			return ((StringValue)trait.getValue()).getValue();
		}
		else if (trait.getValue() instanceof SimpleAnyType){
			return ((SimpleAnyType)trait.getValue()).getRawValue();
		}
		else if (trait.getValue() instanceof AnyType){
			AnyType any = (AnyType)trait.getValue();
			FeatureMap map = any.getMixed();			
			return getTextValueFromFeatureMap(map);
		}
		else if ( trait.getValue().eIsProxy() && trait.getValue() instanceof BasicEObjectImpl){
			BasicEObjectImpl o = (BasicEObjectImpl)trait.getValue();
			return o.eProxyURI().toString();
		}
		return trait.getValue();
	}
	
	private static String getTextValueFromFeatureMap(final FeatureMap map) {
		for (final Iterator it=map.iterator();it.hasNext();){
			final FeatureMap.Entry entry = (FeatureMap.Entry)it.next();
			if (entry.getEStructuralFeature().getName().equals("text"))		 //$NON-NLS-1$
				return (String)entry.getValue();
		}
		return null;
	}

	/**
	 * @param trait
	 * @return value of trait as String.  If externalized, will resolve from resource bundle.
	 */
	public static String getValueAsString(final Trait trait){
		final Object val = getValue(trait);
		if (val instanceof String){			
			return getNLSValue(trait, (String)val);			
		}
		else if (val instanceof BooleanValue)
			return String.valueOf(((BooleanValue)val).isTrue());
		
		return null;
	}
	
	/**
	 * 
	 * @param trait whose value a {@link ListOfValues} or is a single string
	 * @return List of Strings.  If externalized, will resolve from resource bundle 
	 * using getNLSValue(Trait trait, String rawValue)
	 */
	public synchronized static List getValueAsListOfStrings(final Trait trait){
		//PROTO ONLY!!! Need to make WAY more robust!
		final List ret = new ArrayList();
		if (trait.getValue() instanceof ListOfValues) {
			for(final Iterator it=trait.getValue().eContents().iterator();it.hasNext();){
				final Object o = it.next();				
				if (o instanceof SimpleAnyType){
					final SimpleAnyType sat = (SimpleAnyType)o;
					final String rawValue = getTextValueFromFeatureMap(sat.getMixed());
					final String nlsValue = getNLSValue(trait, rawValue);
					
					ret.add(nlsValue);
				}	
			}
		} 
		else {
			//may be single value
			String o = getValueAsString(trait);
			if (o != null)
				ret.add(o);
		}
		return ret;
	}
	
	/**
	 * Looks for '%' (and not '%%') at beginning of rawValue.   If found, looks to the
	 * traits sourceModelProvider for resource bundle to resolve the key after 
	 * stripping the '%' sign.
	 * @param trait
	 * @param rawValue of string in from metadata
	 * @return the NLS Value or rawValue if it cannot be located
	 */
	public static String getNLSValue(final Trait trait, final String rawValue) {
		String result = rawValue;
		if (rawValue.startsWith("%") && !rawValue.startsWith("%%")){   //$NON-NLS-1$//$NON-NLS-2$
			final String key = rawValue.substring(1);
			result = getNLSPropertyValue(trait, key);	
			if (result == null){
				result = rawValue;
			}
		}
		return result == null ? null : result.trim();
	}

	//will return null if there is an IOException with ResourceBundle
	private static String getNLSPropertyValue(final Trait trait, final String key){
		try{			
			IMetaDataSourceModelProvider provider = trait.getSourceModelProvider();
			IResourceBundleProvider resourceBundleProvider = (IResourceBundleProvider)provider.getAdapter(IResourceBundleProvider.class);		
			if (resourceBundleProvider != null){
				ResourceBundle resourceBundle_ = resourceBundleProvider.getResourceBundle();				
				if (resourceBundle_ != null){
					String replVal = resourceBundle_.getString(key);
					return replVal;
				}				
			}
			//return original string 
			return key; 

		} catch (MissingResourceException e){
			//fall thru
			JSFCommonPlugin.log(e,  NLS.bind(Messages.MissingResource_exception, new String[]{key}));
		}
		return key + KEY_NOT_FOUND;
	}

	/**
	 * If trait type is {@link BooleanValue} returns value, otherwise
	 * it will get the value as a String and attempt to coerce to boolean.
	 * Will return 'false' if coercion fails, or value was null.
	 * @param trait
	 * @return true or false 
	 */
	public static boolean getValueAsBoolean(final Trait trait) {
		if (trait != null && trait.getValue() instanceof BooleanValue) {
			return ((BooleanValue)trait.getValue()).isTrue();
		}
		final String val = getValueAsString(trait);
		if (val == null)
			return false;
		
		return Boolean.valueOf(val).booleanValue();
	}
	
//	/**
//	 * @param trait
//	 * @return name of class or primitive datatype that the value is defined as.   
//	 * For SimpleAnyTypes, the getInstanceType().getInstanceClassName() is returned.
//	 * For AnyType, java.lang.String is always returned.
//	 * For all others, the class.getName() is returned.
//	 */
//	public static String getValueInstanceClassName(Trait trait) {String.class.getName();
//		if (trait.getValue() instanceof SimpleAnyType) {
//			return ((SimpleAnyType)trait.getValue()).getInstanceType().getInstanceClassName();
//		}
//		else if (trait.getValue() instanceof AnyType) {
//			return String.class.getName();
//		}
//		else {
//			return trait.getValue().getClass().getName();
//		}
//		
//	}

}

Back to the top