1 17 package org.eclipse.emf.ecore.util; 18 19 20 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.Map ; 25 26 import org.eclipse.emf.common.util.BasicDiagnostic; 27 import org.eclipse.emf.common.util.Diagnostic; 28 import org.eclipse.emf.common.util.DiagnosticChain; 29 30 import org.eclipse.emf.ecore.EValidator; 31 32 import org.eclipse.emf.ecore.EClass; 33 import org.eclipse.emf.ecore.EDataType; 34 import org.eclipse.emf.ecore.EObject; 35 import org.eclipse.emf.ecore.EStructuralFeature; 36 37 import org.eclipse.emf.ecore.util.EcoreUtil; 38 import org.eclipse.emf.ecore.plugin.EcorePlugin; 39 40 43 public class Diagnostician implements EValidator.SubstitutionLabelProvider, EValidator 44 { 45 public static final Diagnostician INSTANCE = new Diagnostician(); 46 47 protected EValidator.Registry eValidatorRegistry; 48 49 public Diagnostician(EValidator.Registry eValidatorRegistry) 50 { 51 this.eValidatorRegistry = eValidatorRegistry; 52 } 53 54 public Diagnostician() 55 { 56 this(EValidator.Registry.INSTANCE); 57 } 58 59 public String getObjectLabel(EObject eObject) 60 { 61 return EcoreUtil.getIdentification(eObject); 62 } 63 64 public String getFeatureLabel(EStructuralFeature eStructuralFeature) 65 { 66 return eStructuralFeature.getName(); 67 } 68 69 public String getValueLabel(EDataType eDataType, Object value) 70 { 71 return EcoreUtil.convertToString(eDataType, value); 72 } 73 74 public Diagnostic validate(EObject eObject) 75 { 76 Map context = new HashMap (); 77 context.put(EValidator.SubstitutionLabelProvider.class, this); 78 context.put(EValidator.class, this); 79 BasicDiagnostic diagnostics = 80 new BasicDiagnostic 81 (EObjectValidator.DIAGNOSTIC_SOURCE, 82 0, 83 EcorePlugin.INSTANCE.getString 84 ("_UI_DiagnosticRoot_diagnostic", 85 new Object [] { getObjectLabel(eObject) }), 86 new Object [] { eObject }); 87 validate(eObject, diagnostics, context); 88 return diagnostics; 89 } 90 91 97 public boolean validate(EObject eObject, DiagnosticChain diagnostics) 98 { 99 Map context = new HashMap (); 100 context.put(EValidator.SubstitutionLabelProvider.class, this); 101 context.put(EValidator.class, this); 102 return validate(eObject, diagnostics, context); 103 } 104 105 public boolean validate(EObject eObject, DiagnosticChain diagnostics, Map context) 106 { 107 return validate(eObject.eClass(), eObject, diagnostics, context); 108 } 109 110 public boolean validate(EClass eClass, EObject eObject, DiagnosticChain diagnostics, Map context) 111 { 112 Object eValidator; 113 while ((eValidator = eValidatorRegistry.get(eClass.eContainer())) == null) 114 { 115 List eSuperTypes = eClass.getESuperTypes(); 116 if (eSuperTypes.isEmpty()) 117 { 118 eValidator = eValidatorRegistry.get(null); 119 break; 120 } 121 else 122 { 123 eClass = (EClass)eSuperTypes.get(0); 124 } 125 } 126 127 boolean result = ((EValidator)eValidator).validate(eClass, eObject, diagnostics, context); 128 if (result || diagnostics != null) 129 { 130 result &= doValidateContents(eObject, diagnostics, context); 131 } 132 return result; 133 } 134 135 protected boolean doValidateContents(EObject eObject, DiagnosticChain diagnostics, Map context) 136 { 137 List eContents = eObject.eContents(); 138 if (!eContents.isEmpty()) 139 { 140 Iterator i = eContents.iterator(); 141 EObject child = (EObject)i.next(); 142 boolean result = validate(child, diagnostics, context); 143 while (i.hasNext() && (result || diagnostics != null)) 144 { 145 child = (EObject)i.next(); 146 result &= validate(child, diagnostics, context); 147 } 148 return result; 149 } 150 else 151 { 152 return true; 153 } 154 } 155 156 public Diagnostic validate(EDataType eDataType, Object value) 157 { 158 Map context = new HashMap (); 159 context.put(EValidator.SubstitutionLabelProvider.class, this); 160 context.put(EValidator.class, this); 161 BasicDiagnostic diagnostics = 162 new BasicDiagnostic 163 (EObjectValidator.DIAGNOSTIC_SOURCE, 164 0, 165 EcorePlugin.INSTANCE.getString 166 ("_UI_DiagnosticRoot_diagnostic", 167 new Object [] { getValueLabel(eDataType, value) }), 168 new Object [] { value, eDataType }); 169 validate(eDataType, value, diagnostics, context); 170 return diagnostics; 171 } 172 173 public boolean validate(EDataType eDataType, Object value, DiagnosticChain diagnostics, Map context) 174 { 175 Object eValidator = eValidatorRegistry.get(eDataType.eContainer()); 176 if (eValidator == null) 177 { 178 eValidator = eValidatorRegistry.get(null); 179 } 180 181 return ((EValidator)eValidator).validate(eDataType, value, diagnostics, context); 182 } 183 } 184 | Popular Tags |