001: //
002: // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v1.0.4-b18-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.03.04 at 10:20:40 PST
006: //
007:
008: package com.nabhinc.portal.config.impl.runtime;
009:
010: import javax.xml.bind.DatatypeConverter;
011: import javax.xml.bind.PropertyException;
012: import javax.xml.bind.ValidationEvent;
013: import javax.xml.bind.ValidationEventHandler;
014: import javax.xml.bind.ValidationException;
015: import javax.xml.bind.Validator;
016: import javax.xml.bind.helpers.DefaultValidationEventHandler;
017:
018: import org.xml.sax.SAXException;
019:
020: import com.sun.xml.bind.DatatypeConverterImpl;
021: import com.sun.xml.bind.validator.Messages;
022:
023: /*
024: TODO:
025: reorganize classes into appropriate packages.
026: to reflect the fact that some of the classes in
027: the marshaller package are used for both marshalling
028: and validation.
029:
030: In particular, the MarshallingContext interface should be
031: renamed. It is not only for marshalling.
032: (something like "Serializer", maybe).
033: */
034:
035: /**
036: * Validator implementation of JAXB RI.
037: */
038: public class ValidatorImpl implements Validator {
039: /** Validation errors will be reported to this object. */
040: private ValidationEventHandler eventHandler = new DefaultValidationEventHandler();
041:
042: final DefaultJAXBContextImpl jaxbContext;
043:
044: public ValidatorImpl(DefaultJAXBContextImpl c) {
045: // initialize datatype converter with ours
046: DatatypeConverter
047: .setDatatypeConverter(DatatypeConverterImpl.theInstance);
048:
049: jaxbContext = c;
050: }
051:
052: /**
053: * We need to know whether an validation error was detected or not.
054: * For this purpose, we set up the validation so that this interceptor
055: * will "intercept" errors before the application receives it.
056: */
057: private static class EventInterceptor implements
058: ValidationEventHandler {
059: EventInterceptor(ValidationEventHandler _next) {
060: this .next = _next;
061: }
062:
063: private boolean hadError = false;
064:
065: public boolean hadError() {
066: return hadError;
067: }
068:
069: /** event will be passed to this component. */
070: private final ValidationEventHandler next;
071:
072: public boolean handleEvent(ValidationEvent e) {
073: hadError = true;
074: boolean result;
075: if (next != null) {
076: // pass it to the application
077: try {
078: result = next.handleEvent(e);
079: } catch (RuntimeException re) {
080: // if the client event handler causes a RuntimeException,
081: // then we have to return false
082: result = false;
083: }
084: } else {
085: // if no error handler was specified, there is no point
086: // in continuing the validation.
087: result = false;
088: }
089: return result;
090: }
091: };
092:
093: public boolean validateRoot(Object o) throws ValidationException {
094: if (o == null) {
095: throw new IllegalArgumentException(Messages.format(
096: Messages.MUST_NOT_BE_NULL, "rootObj"));
097: }
098:
099: return validate(o, true);
100: }
101:
102: public boolean validate(Object o) throws ValidationException {
103: if (o == null) {
104: throw new IllegalArgumentException(Messages.format(
105: Messages.MUST_NOT_BE_NULL, "subrootObj"));
106: }
107:
108: return validate(o, false);
109: }
110:
111: private boolean validate(Object o, boolean validateId)
112: throws ValidationException {
113:
114: try {
115:
116: //ValidatableObject vo = Util.toValidatableObject(o);
117: ValidatableObject vo = jaxbContext.getGrammarInfo()
118: .castToValidatableObject(o);
119:
120: if (vo == null)
121: throw new ValidationException(Messages
122: .format(Messages.NOT_VALIDATABLE));
123:
124: EventInterceptor ei = new EventInterceptor(eventHandler);
125: ValidationContext context = new ValidationContext(
126: jaxbContext, ei, validateId);
127: context.validate(vo);
128: context.reconcileIDs();
129:
130: return !ei.hadError();
131: } catch (SAXException e) {
132: // we need a consistent mechanism to convert SAXException into JAXBException
133: Exception nested = e.getException();
134: if (nested != null) {
135: throw new ValidationException(nested);
136: } else {
137: throw new ValidationException(e);
138: }
139: //return false;
140: }
141: }
142:
143: public ValidationEventHandler getEventHandler() {
144: return eventHandler;
145: }
146:
147: public void setEventHandler(ValidationEventHandler handler) {
148: if (handler == null) {
149: eventHandler = new DefaultValidationEventHandler();
150: } else {
151: eventHandler = handler;
152: }
153: }
154:
155: /**
156: * There are no required properties, so simply throw an exception. Other
157: * providers may have support for properties on Validator, but the RI doesn't
158: */
159: public void setProperty(String name, Object value)
160: throws PropertyException {
161:
162: if (name == null) {
163: throw new IllegalArgumentException(Messages.format(
164: Messages.MUST_NOT_BE_NULL, "name"));
165: }
166:
167: throw new PropertyException(name, value);
168: }
169:
170: /**
171: * There are no required properties, so simply throw an exception. Other
172: * providers may have support for properties on Validator, but the RI doesn't
173: */
174: public Object getProperty(String name) throws PropertyException {
175:
176: if (name == null) {
177: throw new IllegalArgumentException(Messages.format(
178: Messages.MUST_NOT_BE_NULL, "name"));
179: }
180:
181: throw new PropertyException(name);
182: }
183:
184: }
|