/** * Validates the given Object * * @param object the Object to validate * @param resolver the ClassDescriptorResolver to load ClassDescriptors for use during validation. */ public void validate(Object object, ClassDescriptorResolver resolver) throws ValidationException { if (object == null) { throw new ValidationException("Cannot validate a null Object."); } if (resolver == null) { resolver = new ClassDescriptorResolverImpl(); } XMLClassDescriptor classDesc = resolver.resolve(object.getClass()); // -- we cannot validate an object if ClassDescriptor is null if (classDesc == null) return; TypeValidator validator = classDesc.getValidator(); XMLFieldDescriptor fieldDesc = null; try { if (validator != null) { if (validator instanceof ClassValidator) ((ClassValidator) validator).validate(object, resolver); else validator.validate(object); } // -- default validation else { // -- just validate each field FieldDescriptor[] fields = classDesc.getFields(); if (fields != null) { for (int i = 0; i < fields.length; i++) { fieldDesc = (XMLFieldDescriptor) fields[i]; if (fieldDesc == null) continue; FieldValidator fieldValidator = fieldDesc.getValidator(); if (fieldValidator != null) fieldValidator.validate(object, resolver); } } } } catch (ValidationException vx) { // -- add location information XPathLocation loc = (XPathLocation) vx.getLocation(); if (loc == null) { loc = new XPathLocation(); vx.setLocation(loc); if (fieldDesc != null) { if (fieldDesc.getNodeType() == NodeType.Attribute) loc.addAttribute(fieldDesc.getXMLName()); else loc.addChild(fieldDesc.getXMLName()); } } loc.addParent(classDesc.getXMLName()); throw vx; } } // -- validate
/** * {@inheritDoc} * * @see * org.exolab.castor.xml.XMLClassDescriptor#checkDescriptorForCorrectOrderWithinSequence(org.exolab.castor.xml.XMLFieldDescriptor, * org.exolab.castor.xml.UnmarshalState, java.lang.String) */ public void checkDescriptorForCorrectOrderWithinSequence( final XMLFieldDescriptor elementDescriptor, final UnmarshalState parentState, final String xmlName) throws ValidationException { _classDesc.checkDescriptorForCorrectOrderWithinSequence( elementDescriptor, parentState, xmlName); }
/** * Returns true if any of the fields associated with the given XMLClassDescriptor are located at, * or beneath, the given location. * * @param location the location to compare against * @param classDesc the XMLClassDescriptor in which to check the field locations * @return true if any of the fields has a location associated */ static final boolean hasFieldsAtLocation( final String location, final XMLClassDescriptor classDesc) { // -- check elements XMLFieldDescriptor[] descriptors = classDesc.getElementDescriptors(); for (int i = 0; i < descriptors.length; i++) { if (descriptors[i] == null) { continue; } String tmpLocation = descriptors[i].getLocationPath(); if ((tmpLocation != null) && (tmpLocation.startsWith(location))) { return true; } } // -- check attributes descriptors = classDesc.getAttributeDescriptors(); for (int i = 0; i < descriptors.length; i++) { if (descriptors[i] == null) { continue; } String tmpLocation = descriptors[i].getLocationPath(); if ((tmpLocation != null) && (tmpLocation.startsWith(location))) { return true; } } // -- check content/text XMLFieldDescriptor content = classDesc.getContentDescriptor(); if (content != null) { String tmpLocation = content.getLocationPath(); if ((tmpLocation != null) && (tmpLocation.startsWith(location))) { return true; } } return false; } // -- hasFieldsAtLocation
/** * Returns a list of fields represented by this descriptor. * * @return A list of fields */ public FieldDescriptor[] getFields() { if (_fields == null) { _fields = _classDesc.getFields(); } return _fields; } // -- getFields
/** * Returns the Java class represented by this descriptor. * * @return The Java class */ public Class getJavaClass() { return _classDesc.getJavaClass(); } // -- getJavaClass
/** * @see org.exolab.castor.xml.XMLClassDescriptor#canAccept( java.lang.String, java.lang.String, * java.lang.Object) */ public boolean canAccept(final String name, final String namespace, final Object object) { return _classDesc.canAccept(name, namespace, object); } // -- canAccept
/** * Returns the XML Name for the Class being described. * * @return the XML name. */ public String getXMLName() { return _classDesc.getXMLName(); } // -- getXMLName
/** * Returns a specific validator for the class described by this ClassDescriptor. A null value * may be returned if no specific validator exists. * * @return the type validator for the class described by this ClassDescriptor. */ public TypeValidator getValidator() { return _classDesc.getValidator(); } // -- getValidator
/** @return the namespace prefix to use when marshalling as XML. */ public String getNameSpacePrefix() { return _classDesc.getNameSpacePrefix(); } // -- getNameSpacePrefix
/** * Returns the set of XMLFieldDescriptors for all members that should be marshalled as XML * elements. * * @return an array of XMLFieldDescriptors for all members that should be marshalled as XML * elements. */ public XMLFieldDescriptor[] getElementDescriptors() { if (_elements == null) { _elements = _classDesc.getElementDescriptors(); } return _elements; } // -- getElementDescriptors
/** * Returns the XML field descriptor matching the given xml name and nodeType. If NodeType is * null, then either an AttributeDescriptor, or ElementDescriptor may be returned. Null is * returned if no matching descriptor is available. * * @param name the xml name to match against * @param namespace the xml namespace to match * @param nodeType the NodeType to match against, or null if the node type is not known. * @return the matching descriptor, or null if no matching descriptor is available. */ public XMLFieldDescriptor getFieldDescriptor( final String name, final String namespace, final NodeType nodeType) { return _classDesc.getFieldDescriptor(name, namespace, nodeType); } // -- getFieldDescriptor
/** * Returns the XMLFieldDescriptor for the member that should be marshalled as text content. * * @return the XMLFieldDescriptor for the member that should be marshalled as text content. */ public XMLFieldDescriptor getContentDescriptor() { return _classDesc.getContentDescriptor(); } // -- getContentDescriptor
/** * Returns the set of XMLFieldDescriptors for all members that should be marshalled as XML * attributes. This includes namespace nodes. * * @return an array of XMLFieldDescriptors for all members that should be marshalled as XML * attributes. */ public XMLFieldDescriptor[] getAttributeDescriptors() { if (_attributes == null) { _attributes = _classDesc.getAttributeDescriptors(); } return _attributes; } // -- getAttributeDescriptors
/** * Search there is a field descriptor which can accept one of the class descriptor which match the * given name and namespace. * * @param name XML name of the field * @param namespace namespace of the field * @param classDesc the class descriptor to match against * @param cdResolver the class descriptor resolver to use * @return An array of InheritanceMatch. * @throws MarshalException if the resolver called fails fatally */ protected InheritanceMatch[] searchInheritance( final String name, final String namespace, final XMLClassDescriptor classDesc) throws MarshalException { Iterator classDescriptorIterator = null; try { // -- A little required logic for finding Not-Yet-Loaded // -- descriptors String className = getJavaNaming().toJavaClassName(name); // -- should use namespace-to-prefix mappings, but // -- just create package for now. Class clazz = classDesc.getJavaClass(); String pkg = null; if (clazz != null) { while (clazz.getDeclaringClass() != null) { clazz = clazz.getDeclaringClass(); } pkg = clazz.getName(); int idx = pkg.lastIndexOf('.'); if (idx >= 0) { pkg = pkg.substring(0, idx + 1); className = pkg + className; } } getInternalContext() .getXMLClassDescriptorResolver() .resolve(className, classDesc.getClass().getClassLoader()); // -- end Not-Yet-Loaded descriptor logic // -- resolve all by XML name + namespace URI classDescriptorIterator = getInternalContext() .getXMLClassDescriptorResolver() .resolveAllByXMLName(name, namespace, null); } catch (ResolverException rx) { Throwable actual = rx.getCause(); if (actual instanceof MarshalException) { throw (MarshalException) actual; } if (actual != null) { throw new MarshalException(actual); } throw new MarshalException(rx); } Vector inheritanceList = null; XMLFieldDescriptor descriptor = null; XMLFieldDescriptor[] descriptors = classDesc.getElementDescriptors(); XMLClassDescriptor cdInherited = null; if (classDescriptorIterator.hasNext()) { while (classDescriptorIterator.hasNext() && (descriptor == null)) { cdInherited = (XMLClassDescriptor) classDescriptorIterator.next(); Class subclass = cdInherited.getJavaClass(); for (int i = 0; i < descriptors.length; i++) { if (descriptors[i] == null) { continue; } // -- skip descriptors with special internal name if (INTERNAL_XML_NAME.equals(descriptors[i].getXMLName())) { continue; } // -- check for inheritence Class superclass = descriptors[i].getFieldType(); // It is possible that the superclass is of type object if we use any node. if (superclass.isAssignableFrom(subclass) && (superclass != Object.class)) { descriptor = descriptors[i]; if (inheritanceList == null) { inheritanceList = new Vector(3); } inheritanceList.addElement(new InheritanceMatch(descriptor, cdInherited)); } } } // -- reset inherited class descriptor, if necessary if (descriptor == null) { cdInherited = null; } } if (inheritanceList != null) { InheritanceMatch[] result = new InheritanceMatch[inheritanceList.size()]; inheritanceList.toArray(result); return result; } return NO_MATCH_ARRAY; }
/** * Returns the class descriptor of the class extended by this class. * * @return The extended class descriptor */ public ClassDescriptor getExtends() { return _classDesc.getExtends(); } // -- getExtends
/** * Returns the identity field, null if this class has no identity. * * @return The identity field */ public FieldDescriptor getIdentity() { return _classDesc.getIdentity(); } // -- getIdentity
/** @return the namespace URI used when marshalling and unmarshalling as XML. */ public String getNameSpaceURI() { return _classDesc.getNameSpaceURI(); } // -- getNameSpaceURI