KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > ecore > util > Diagnostician


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2004 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: Diagnostician.java,v 1.6 2005/06/08 06:20:10 nickb Exp $
16  */

17 package org.eclipse.emf.ecore.util;
18
19
20 //import java.util.Collections;
21
import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
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 /**
41  * A validity checker for basic EObject constraints.
42  */

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 JavaDoc getObjectLabel(EObject eObject)
60   {
61     return EcoreUtil.getIdentification(eObject);
62   }
63
64   public String JavaDoc getFeatureLabel(EStructuralFeature eStructuralFeature)
65   {
66     return eStructuralFeature.getName();
67   }
68
69   public String JavaDoc getValueLabel(EDataType eDataType, Object JavaDoc value)
70   {
71     return EcoreUtil.convertToString(eDataType, value);
72   }
73
74   public Diagnostic validate(EObject eObject)
75   {
76     Map JavaDoc context = new HashMap JavaDoc();
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 JavaDoc [] { getObjectLabel(eObject) }),
86          new Object JavaDoc [] { eObject });
87     validate(eObject, diagnostics, context);
88     return diagnostics;
89   }
90
91   /**
92    * Validates the object in the given context, optionally producing diagnostics.
93    * @param eObject the object to validate.
94    * @param diagnostics a place to accumulate diagnostics; if it's <code>null</code>, no diagnostics should be produced.
95    * @return whether the object is valid.
96    */

97   public boolean validate(EObject eObject, DiagnosticChain diagnostics)
98   {
99     Map JavaDoc context = new HashMap JavaDoc();
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 JavaDoc context)
106   {
107     return validate(eObject.eClass(), eObject, diagnostics, context);
108   }
109
110   public boolean validate(EClass eClass, EObject eObject, DiagnosticChain diagnostics, Map JavaDoc context)
111   {
112     Object JavaDoc eValidator;
113     while ((eValidator = eValidatorRegistry.get(eClass.eContainer())) == null)
114     {
115       List JavaDoc 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 JavaDoc context)
136   {
137     List JavaDoc eContents = eObject.eContents();
138     if (!eContents.isEmpty())
139     {
140       Iterator JavaDoc 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 JavaDoc value)
157   {
158     Map JavaDoc context = new HashMap JavaDoc();
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 JavaDoc [] { getValueLabel(eDataType, value) }),
168          new Object JavaDoc [] { value, eDataType });
169     validate(eDataType, value, diagnostics, context);
170     return diagnostics;
171   }
172
173   public boolean validate(EDataType eDataType, Object JavaDoc value, DiagnosticChain diagnostics, Map JavaDoc context)
174   {
175     Object JavaDoc 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