001: //
002: // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v1.0.5-b16-fcs
003: // See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
004: // Any modifications to this file will be lost upon recompilation of the source schema.
005: // Generated on: 2005.12.17 at 09:43:27 AM GMT+07:00
006: //
007:
008: package com.mvnforum.jaxb.db.impl.runtime;
009:
010: import java.util.HashMap;
011: import java.util.HashSet;
012: import java.util.Iterator;
013: import java.util.Map;
014:
015: import javax.xml.bind.ValidationEvent;
016: import javax.xml.bind.ValidationEventHandler;
017: import javax.xml.bind.helpers.NotIdentifiableEventImpl;
018: import javax.xml.bind.helpers.ValidationEventImpl;
019: import javax.xml.bind.helpers.ValidationEventLocatorImpl;
020:
021: import org.xml.sax.SAXException;
022:
023: import com.sun.xml.bind.ProxyGroup;
024: import com.sun.xml.bind.serializer.AbortSerializationException;
025: import com.sun.xml.bind.validator.Messages;
026:
027: /**
028: * Maintains information that needs to be stored across
029: * validations of multiple objects.
030: *
031: * Specifically, this object is responsible for:
032: *
033: * <ol>
034: * <li>detecting a cycle in a content tree by keeping track of
035: * objects that were validated.
036: *
037: * <li>keeping an instance of NamespaceContextImpl, which is
038: * shared by all MSVValidators.
039: *
040: * <li>keeping a reference to {@link ValidationErrorHandler}.
041: * MSVValidators should use this error handler to report any error.
042: * </ol>
043: */
044: class ValidationContext {
045: final DefaultJAXBContextImpl jaxbContext;
046:
047: /**
048: * @param validateID
049: * if true, ID/IDREF validation will be performed.
050: */
051: ValidationContext(DefaultJAXBContextImpl _context,
052: ValidationEventHandler _eventHandler, boolean validateID) {
053: this .jaxbContext = _context;
054: this .eventHandler = _eventHandler;
055: this .validateID = validateID;
056: }
057:
058: //
059: //
060: // detecting cycles.
061: //
062: //
063:
064: /** Set of all validated objects. Used to detect a cycle. */
065: private final IdentityHashSet validatedObjects = new IdentityHashSet();
066:
067: /**
068: * Validates the sub-tree rooted at <code>vo</code> and reports
069: * any errors/warnings to the error handler.
070: */
071: public void validate(ValidatableObject vo) throws SAXException {
072: if (validatedObjects.add(ProxyGroup.unwrap(vo))) {
073: // setup a new validator for this object and validate it.
074: MSVValidator.validate(jaxbContext, this , vo);
075: } else {
076: // this object has already been validated once.
077: reportEvent(vo, Messages.format(Messages.CYCLE_DETECTED));
078: }
079: }
080:
081: //
082: //
083: // Keeping track of namespace bindings.
084: //
085: //
086:
087: /** namespace context. */
088: private final NamespaceContextImpl nsContext = new NamespaceContextImpl(
089: null);
090:
091: public NamespaceContextImpl getNamespaceContext() {
092: return nsContext;
093: }
094:
095: //
096: //
097: // ID/IDREF validation
098: //
099: //
100: /** ID/IDREF validation is done only when this flag is true. */
101: private final boolean validateID;
102:
103: private final HashSet IDs = new HashSet();
104: private final HashMap IDREFs = new HashMap();
105:
106: public String onID(XMLSerializable owner, String value)
107: throws SAXException {
108:
109: if (!validateID)
110: return value;
111:
112: if (!IDs.add(value)) {
113: // this ID value has already been used.
114: //reportEvent(Util.toValidatableObject(owner),
115: // Messages.format(Messages.DUPLICATE_ID,value));
116: reportEvent(jaxbContext.getGrammarInfo()
117: .castToValidatableObject(owner), Messages.format(
118: Messages.DUPLICATE_ID, value));
119: }
120:
121: return value;
122: }
123:
124: public String onIDREF(XMLSerializable referer, String value)
125: throws SAXException {
126: if (!validateID)
127: return value;
128:
129: if (IDs.contains(value))
130: return value; // this IDREF has the corresponding ID.
131:
132: // remember the value to check the value later.
133: IDREFs.put(value, referer);
134:
135: return value;
136: }
137:
138: /** Tests if all IDREFs have corresponding IDs. */
139: protected void reconcileIDs() throws SAXException {
140: if (!validateID)
141: return;
142:
143: for (Iterator itr = IDREFs.entrySet().iterator(); itr.hasNext();) {
144: Map.Entry e = (Map.Entry) itr.next();
145:
146: if (IDs.contains(e.getKey()))
147: continue; // OK.
148:
149: // ID was not found.
150: ValidatableObject source = (ValidatableObject) e.getValue();
151: reportEvent(source, new NotIdentifiableEventImpl(
152: ValidationEvent.ERROR, Messages.format(
153: Messages.ID_NOT_FOUND, e.getKey()),
154: new ValidationEventLocatorImpl(source)));
155: }
156:
157: IDREFs.clear();
158: }
159:
160: //
161: //
162: // Keeping track of ValidationErrorHandler
163: //
164: //
165: private final ValidationEventHandler eventHandler;
166:
167: /**
168: * Reports an error to the application.
169: */
170: public void reportEvent(ValidatableObject source,
171: String formattedMessage) throws AbortSerializationException {
172: reportEvent(source, new ValidationEventImpl(
173: ValidationEvent.ERROR, formattedMessage,
174: new ValidationEventLocatorImpl(source)));
175: }
176:
177: /**
178: * Reports an error to the client.
179: * This version should be used when an exception is thrown from sub-modules.
180: */
181: public void reportEvent(ValidatableObject source,
182: Exception nestedException)
183: throws AbortSerializationException {
184: reportEvent(source,
185: new ValidationEventImpl(ValidationEvent.ERROR,
186: nestedException.toString(),
187: new ValidationEventLocatorImpl(source),
188: nestedException));
189: }
190:
191: public void reportEvent(ValidatableObject source,
192: ValidationEvent event) throws AbortSerializationException {
193: boolean r;
194:
195: try {
196: r = eventHandler.handleEvent(event);
197: } catch (RuntimeException re) {
198: // if the client event handler causes a RuntimeException, then
199: // we have to return false.
200: r = false;
201: }
202:
203: if (!r) {
204: // throw an exception to abort validation
205: throw new AbortSerializationException(event.getMessage());
206: }
207: }
208:
209: }
|