KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > tools > xjc > runtime > ValidationContext


1 package com.sun.tools.xjc.runtime;
2
3 import java.util.HashMap JavaDoc;
4 import java.util.HashSet JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.Map JavaDoc;
7
8 import javax.xml.bind.ValidationEvent;
9 import javax.xml.bind.ValidationEventHandler;
10 import javax.xml.bind.helpers.NotIdentifiableEventImpl;
11 import javax.xml.bind.helpers.ValidationEventImpl;
12 import javax.xml.bind.helpers.ValidationEventLocatorImpl;
13
14 import org.xml.sax.SAXException JavaDoc;
15
16 import com.sun.xml.bind.ProxyGroup;
17 import com.sun.xml.bind.serializer.AbortSerializationException;
18 import com.sun.xml.bind.validator.Messages;
19
20 /**
21  * Maintains information that needs to be stored across
22  * validations of multiple objects.
23  *
24  * Specifically, this object is responsible for:
25  *
26  * <ol>
27  * <li>detecting a cycle in a content tree by keeping track of
28  * objects that were validated.
29  *
30  * <li>keeping an instance of NamespaceContextImpl, which is
31  * shared by all MSVValidators.
32  *
33  * <li>keeping a reference to {@link ValidationErrorHandler}.
34  * MSVValidators should use this error handler to report any error.
35  * </ol>
36  */

37 class ValidationContext
38 {
39     final DefaultJAXBContextImpl jaxbContext;
40     /**
41      * @param validateID
42      * if true, ID/IDREF validation will be performed.
43      */

44     ValidationContext( DefaultJAXBContextImpl _context, ValidationEventHandler _eventHandler, boolean validateID ) {
45         this.jaxbContext = _context;
46         this.eventHandler = _eventHandler;
47         this.validateID = validateID;
48     }
49     
50
51     
52 //
53
//
54
// detecting cycles.
55
//
56
//
57

58     /** Set of all validated objects. Used to detect a cycle. */
59     private final IdentityHashSet validatedObjects = new IdentityHashSet();
60     
61     /**
62      * Validates the sub-tree rooted at <code>vo</code> and reports
63      * any errors/warnings to the error handler.
64      */

65     public void validate( ValidatableObject vo ) throws SAXException JavaDoc {
66         if( validatedObjects.add(ProxyGroup.unwrap(vo)) ) {
67             // setup a new validator for this object and validate it.
68
MSVValidator.validate(jaxbContext,this,vo);
69         } else {
70             // this object has already been validated once.
71
reportEvent( vo, Messages.format( Messages.CYCLE_DETECTED ) );
72         }
73     }
74
75     
76 //
77
//
78
// Keeping track of namespace bindings.
79
//
80
//
81

82     /** namespace context. */
83     private final NamespaceContextImpl nsContext = new NamespaceContextImpl(null);
84     
85     public NamespaceContextImpl getNamespaceContext() { return nsContext; }
86     
87
88 //
89
//
90
// ID/IDREF validation
91
//
92
//
93
/** ID/IDREF validation is done only when this flag is true. */
94     private final boolean validateID;
95     
96     private final HashSet JavaDoc IDs = new HashSet JavaDoc();
97     private final HashMap JavaDoc IDREFs = new HashMap JavaDoc();
98     
99     public String JavaDoc onID( XMLSerializable owner, String JavaDoc value ) throws SAXException JavaDoc {
100             
101         if(!validateID) return value;
102         
103         if(!IDs.add(value)) {
104             // this ID value has already been used.
105
//reportEvent(Util.toValidatableObject(owner),
106
// Messages.format(Messages.DUPLICATE_ID,value));
107
reportEvent(jaxbContext.getGrammarInfo().castToValidatableObject(owner),
108                 Messages.format(Messages.DUPLICATE_ID,value));
109         }
110         
111         return value;
112     }
113     public String JavaDoc onIDREF( XMLSerializable referer, String JavaDoc value ) throws SAXException JavaDoc {
114         if(!validateID) return value;
115         
116         if(IDs.contains(value))
117             return value; // this IDREF has the corresponding ID.
118

119         // remember the value to check the value later.
120
IDREFs.put(value,referer);
121         
122         return value;
123     }
124     /** Tests if all IDREFs have corresponding IDs. */
125     protected void reconcileIDs() throws SAXException JavaDoc {
126         if(!validateID) return;
127         
128         for (Iterator JavaDoc itr = IDREFs.entrySet().iterator(); itr.hasNext();) {
129             Map.Entry JavaDoc e = (Map.Entry JavaDoc) itr.next();
130             
131             if(IDs.contains(e.getKey()))
132                 continue; // OK.
133

134             // ID was not found.
135
ValidatableObject source = (ValidatableObject)e.getValue();
136             reportEvent(
137                 source,
138                 new NotIdentifiableEventImpl(
139                     ValidationEvent.ERROR,
140                     Messages.format( Messages.ID_NOT_FOUND, e.getKey() ),
141                     new ValidationEventLocatorImpl( source ) ) );
142         }
143         
144         IDREFs.clear();
145     }
146
147     
148 //
149
//
150
// Keeping track of ValidationErrorHandler
151
//
152
//
153
private final ValidationEventHandler eventHandler;
154     
155     /**
156      * Reports an error to the application.
157      */

158     public void reportEvent(ValidatableObject source, String JavaDoc formattedMessage) throws AbortSerializationException {
159         reportEvent(
160             source,
161             new ValidationEventImpl( ValidationEvent.ERROR,
162                                      formattedMessage,
163                                      new ValidationEventLocatorImpl(source) ) );
164     }
165     
166     /**
167      * Reports an error to the client.
168      * This version should be used when an exception is thrown from sub-modules.
169      */

170     public void reportEvent(ValidatableObject source, Exception JavaDoc nestedException ) throws AbortSerializationException {
171         reportEvent(
172             source,
173             new ValidationEventImpl( ValidationEvent.ERROR,
174                                      nestedException.toString(),
175                                      new ValidationEventLocatorImpl(source),
176                                      nestedException ) );
177     }
178     
179     public void reportEvent( ValidatableObject source, ValidationEvent event ) throws AbortSerializationException {
180         boolean r;
181         
182         try {
183             r = eventHandler.handleEvent( event );
184         } catch( RuntimeException JavaDoc re ) {
185             // if the client event handler causes a RuntimeException, then
186
// we have to return false.
187
r = false;
188         }
189         
190         if(!r) {
191             // throw an exception to abort validation
192
throw new AbortSerializationException( event.getMessage() );
193         }
194     }
195         
196     
197
198 }
199
Popular Tags