private void doElementNamespaceValidation(Node received, Node source) { // validate element namespace if (log.isDebugEnabled()) { log.debug("Validating namespace for element: " + received.getLocalName()); } if (received.getNamespaceURI() != null) { Assert.isTrue( source.getNamespaceURI() != null, ValidationUtils.buildValueMismatchErrorMessage( "Element namespace not equal for element '" + received.getLocalName() + "'", null, received.getNamespaceURI())); Assert.isTrue( received.getNamespaceURI().equals(source.getNamespaceURI()), ValidationUtils.buildValueMismatchErrorMessage( "Element namespace not equal for element '" + received.getLocalName() + "'", source.getNamespaceURI(), received.getNamespaceURI())); } else { Assert.isTrue( source.getNamespaceURI() == null, ValidationUtils.buildValueMismatchErrorMessage( "Element namespace not equal for element '" + received.getLocalName() + "'", source.getNamespaceURI(), null)); } }
/** * Perform validation on namespace qualified attribute values if present. This includes the * validation of namespace presence and equality. * * @param receivedElement * @param receivedAttribute * @param sourceElement * @param sourceAttribute */ private void doNamespaceQualifiedAttributeValidation( Node receivedElement, Node receivedAttribute, Node sourceElement, Node sourceAttribute) { String receivedValue = receivedAttribute.getNodeValue(); String sourceValue = sourceAttribute.getNodeValue(); if (receivedValue.contains(":") && sourceValue.contains(":")) { // value has namespace prefix set, do special QName validation String receivedPrefix = receivedValue.substring(0, receivedValue.indexOf(':')); String sourcePrefix = sourceValue.substring(0, sourceValue.indexOf(':')); Map<String, String> receivedNamespaces = XMLUtils.lookupNamespaces(receivedAttribute.getOwnerDocument()); receivedNamespaces.putAll(XMLUtils.lookupNamespaces(receivedElement)); if (receivedNamespaces.containsKey(receivedPrefix)) { Map<String, String> sourceNamespaces = XMLUtils.lookupNamespaces(sourceAttribute.getOwnerDocument()); sourceNamespaces.putAll(XMLUtils.lookupNamespaces(sourceElement)); if (sourceNamespaces.containsKey(sourcePrefix)) { Assert.isTrue( sourceNamespaces.get(sourcePrefix).equals(receivedNamespaces.get(receivedPrefix)), ValidationUtils.buildValueMismatchErrorMessage( "Values not equal for attribute value namespace '" + receivedValue + "'", sourceNamespaces.get(sourcePrefix), receivedNamespaces.get(receivedPrefix))); // remove namespace prefixes as they must not form equality receivedValue = receivedValue.substring((receivedPrefix + ":").length()); sourceValue = sourceValue.substring((sourcePrefix + ":").length()); } else { throw new ValidationException( "Received attribute value '" + receivedAttribute.getLocalName() + "' describes namespace qualified attribute value," + " control value '" + sourceValue + "' does not"); } } } Assert.isTrue( receivedValue.equals(sourceValue), ValidationUtils.buildValueMismatchErrorMessage( "Values not equal for attribute '" + receivedAttribute.getLocalName() + "'", sourceValue, receivedValue)); }
/** * Converts an XML element into an <code>EppCommandRenewXriName</code> object. The caller of this * method must make sure that the root node is of an EPP Command Renew entity for EPP XRI I-Name * object * * @param root root node for an <code>EppCommandRenewXriName</code> object in XML format * @return an <code>EppCommandRenewXriName</code> object, or null if the node is invalid */ public static EppEntity fromXML(Node root) { EppCommandRenewXriName cmd = null; String iname = null; Calendar curExpDate = null; EppPeriod period = null; NodeList list = root.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { Node node = list.item(i); String name = node.getLocalName(); if (name == null) { continue; } if (name.equals("iname")) { iname = EppUtil.getText(node); } else if (name.equals("curExpDate")) { curExpDate = EppUtil.getDate(node, true); } else if (name.equals("period")) { period = (EppPeriod) EppPeriod.fromXML(node); } } if (iname != null) { cmd = new EppCommandRenewXriName(iname, curExpDate, period, null); } return cmd; }
private void doElementNameValidation(Node received, Node source) { // validate element name if (log.isDebugEnabled()) { log.debug( "Validating element: " + received.getLocalName() + " (" + received.getNamespaceURI() + ")"); } Assert.isTrue( received.getLocalName().equals(source.getLocalName()), ValidationUtils.buildValueMismatchErrorMessage( "Element names not equal", source.getLocalName(), received.getLocalName())); }
/** * Handle processing instruction during validation. * * @param received */ private void doPI(Node received) { log.info( "Ignored processing instruction (" + received.getLocalName() + "=" + received.getNodeValue() + ")"); }
/** Full dump for debugging */ public String dump() { return rdfNode.getLocalName() + siblings.dump() + " succ=" + dumpSet(succ) + ", succClose=" + dumpSet(succClosed) + ", pred=" + dumpSet(pred); }
public Object deserialize(String xml) throws Exception { DOMDocument domDocument = new DOMDocument(xml); root = domDocument.getRootNode(); if ((classPackage == null) || (rootPath == null)) classLoaderStrategy = getClassLoaderStrategy(root.lookupNamespaceURI(nsPrefix) + "." + root.getLocalName()); else { currentPackage = classPackage; classLoaderStrategy = ClassLoaderUtil.getClassLoader( ClassLoaderUtil.FILE_SYSTEM_CLASS_LOADER, new String[] {rootPath}); } return deserialize(root, true, true); }
/** * Converts an XML element into an <code>EppResponseDataRenewXriNumber</code> object. The caller * of this method must make sure that the root node is the resData element of EPP responseType for * creating an EPP XRI I-Number object * * @param root root node for an <code>EppResponseDataRenewXriNumber</code> object in XML format * @return an <code>EppResponseDataRenewXriNumber</code> object, or null if the node is invalid */ public static EppEntity fromXML(Node root) { String i_number = null; Calendar exDate = null; NodeList list = root.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { Node node = list.item(i); String name = node.getLocalName(); if (name == null) { continue; } else if (name.equals("inumber")) { i_number = EppUtil.getText(node); } else if (name.equals("exDate")) { exDate = EppUtil.getDate(node); } } return new EppResponseDataRenewXriNumber(i_number, exDate); }
/** * Constructs an attribute element from an existing XML block. * * @param element representing a DOM tree element. * @exception SAMLException if there is an error in the sender or in the element definition. */ public Attribute(Element element) throws SAMLException { // make sure that the input xml block is not null if (element == null) { SAMLUtilsCommon.debug.message("Attribute: Input is null."); throw new SAMLRequesterException(SAMLUtilsCommon.bundle.getString("nullInput")); } // Make sure this is an Attribute. String tag = null; tag = element.getLocalName(); if ((tag == null) || (!tag.equals("Attribute"))) { SAMLUtilsCommon.debug.message("Attribute: wrong input"); throw new SAMLRequesterException(SAMLUtilsCommon.bundle.getString("wrongInput")); } int i = 0; // handle attributes NamedNodeMap atts = element.getAttributes(); int attrCount = atts.getLength(); for (i = 0; i < attrCount; i++) { Node att = atts.item(i); if (att.getNodeType() == Node.ATTRIBUTE_NODE) { String attName = att.getLocalName(); if (attName == null) { attName = att.getNodeName(); } if (attName == null || attName.length() == 0) { if (SAMLUtilsCommon.debug.messageEnabled()) { SAMLUtilsCommon.debug.message("Attribute:" + "Attribute Name is either null or empty."); } continue; // throw new SAMLRequesterException( // SAMLUtilsCommon.bundle.getString("nullInput")); } if (attName.equals("AttributeName")) { this._attributeName = ((Attr) att).getValue().trim(); } else if (attName.equals("AttributeNamespace")) { this._attributeNameSpace = ((Attr) att).getValue().trim(); } } } // AttributeName is required if (_attributeName == null || _attributeName.length() == 0) { if (SAMLUtilsCommon.debug.messageEnabled()) { SAMLUtilsCommon.debug.message("Attribute: " + "AttributeName is required attribute"); } throw new SAMLRequesterException(SAMLUtilsCommon.bundle.getString("missingAttribute")); } // AttributeNamespace is required if (_attributeNameSpace == null || _attributeNameSpace.length() == 0) { if (SAMLUtilsCommon.debug.messageEnabled()) { SAMLUtilsCommon.debug.message("Attribute: " + "AttributeNamespace is required attribute"); } throw new SAMLRequesterException(SAMLUtilsCommon.bundle.getString("missingAttribute")); } // handle the children of Attribute element NodeList nodes = element.getChildNodes(); int nodeCount = nodes.getLength(); if (nodeCount > 0) { for (i = 0; i < nodeCount; i++) { Node currentNode = nodes.item(i); if (currentNode.getNodeType() == Node.ELEMENT_NODE) { String tagName = currentNode.getLocalName(); String tagNS = currentNode.getNamespaceURI(); if ((tagName == null) || tagName.length() == 0 || tagNS == null || tagNS.length() == 0) { if (SAMLUtilsCommon.debug.messageEnabled()) { SAMLUtilsCommon.debug.message( "Attribute:" + " The tag name or tag namespace of child" + " element is either null or empty."); } throw new SAMLRequesterException(SAMLUtilsCommon.bundle.getString("nullInput")); } if (tagName.equals("AttributeValue") && tagNS.equals(SAMLConstants.assertionSAMLNameSpaceURI)) { if (_attributeValue == null) { _attributeValue = new ArrayList(); } if (!(_attributeValue.add((Element) currentNode))) { if (SAMLUtilsCommon.debug.messageEnabled()) { SAMLUtilsCommon.debug.message( "Attribute: failed to " + "add to the attribute value list."); } throw new SAMLRequesterException(SAMLUtilsCommon.bundle.getString("addListError")); } } else { if (SAMLUtilsCommon.debug.messageEnabled()) { SAMLUtilsCommon.debug.message("Attribute:" + "wrong element:" + tagName); } throw new SAMLRequesterException(SAMLUtilsCommon.bundle.getString("wrongInput")); } } // end of if (currentNode.getNodeType() == Node.ELEMENT_NODE) } // end of for loop } // end of if (nodeCount > 0) if (_attributeValue == null || _attributeValue.isEmpty()) { if (SAMLUtilsCommon.debug.messageEnabled()) { SAMLUtilsCommon.debug.message( "Attribute: " + "should contain at least one AttributeValue."); } throw new SAMLRequesterException(SAMLUtilsCommon.bundle.getString("missingElement")); } }
/** Print node label to assist with debug. */ @Override public String toString() { return "[" + rdfNode.getLocalName() + "]"; }
private Object deserialize(Node node, boolean setProperty, boolean popBean) throws Exception { Object object = null; currentType = null; currentNode = node; currentName = node.getNodeName(); boolean isNull = false; NamedNodeMap attrs = node.getAttributes(); String arrayType = null; for (int i = 0; i < attrs.getLength(); i++) { String nodeName = attrs.item(i).getNodeName(); String nodeValue = attrs.item(i).getNodeValue(); if (nodeName.equals(NamespaceConstants.NSPREFIX_SCHEMA_XSI + ":" + Constants.ATTR_TYPE)) currentType = new StringBuffer(nodeValue).delete(0, nodeValue.indexOf(':') + 1).toString(); else if (nodeName.equals( NamespaceConstants.NSPREFIX_SOAP_ENCODING + ":" + Constants.ATTR_ARRAY_TYPE)) arrayType = new StringBuffer(nodeValue).delete(0, nodeValue.indexOf(':') + 1).toString(); else if (nodeName.equals(NamespaceConstants.NSPREFIX_SCHEMA_XSI + ":null")) isNull = nodeValue.equals("true"); } Class cls = null; if (currentType != null) cls = getXsdTypeClass(currentType); // Handle array, Vector, ArrayList, LinkedList, Hashtable, // Properties, and HashMap data types if ((cls != null) && ((cls == java.lang.reflect.Array.class) || (cls == Vector.class) || (cls == ArrayList.class) || (cls == LinkedList.class) || (cls == Hashtable.class) || (cls == Properties.class) || (cls == HashMap.class) || (cls == SortedMap.class))) { parentNode = currentNode; String name = node.getNodeName(); // Handle arrays if (cls == java.lang.reflect.Array.class) { int a = arrayType.indexOf("["); int b = arrayType.indexOf("]"); String s = arrayType.substring(a + 1, b); int arrayLen = Integer.valueOf(s).intValue(); arrayType = arrayType.substring(0, a); // check if the array element is a standard Java class Class arrayClass = getXsdTypeClass(arrayType); // otherwise try to get the class of the bean if (arrayClass == null) arrayClass = getClassOfBean((ClassLoader) classLoaderStrategy, arrayType); object = java.lang.reflect.Array.newInstance(arrayClass, arrayLen); } else { // Construct the list or map type Constructor ct = cls.getConstructor((Class[]) null); object = ct.newInstance((Object[]) null); } // deserialize the elements of the array, list, or map NodeList childNodes = node.getChildNodes(); int arrayIndex = -1; Node childNode = null; Object nodeObj = null; for (int i = 0; i < childNodes.getLength(); i++) { childNode = childNodes.item(i); if (childNode.getNodeType() == Node.ELEMENT_NODE) { if ((cls == java.lang.reflect.Array.class) || (cls == Vector.class) || (cls == ArrayList.class) || (cls == LinkedList.class)) { nodeObj = deserialize(childNode, false, true); if (nodeObj != null) { if (cls == java.lang.reflect.Array.class) java.lang.reflect.Array.set(object, ++arrayIndex, nodeObj); else ((List) object).add(nodeObj); } } else if ((cls == Hashtable.class) || (cls == Properties.class) || (cls == HashMap.class) || (cls == SortedMap.class)) { if (childNode.getLocalName().equals("item")) { NodeList htNodes = childNode.getChildNodes(); if (htNodes.getLength() == 2) { Object hashKey = deserialize(htNodes.item(0), false, false); Object hashValue = deserialize(htNodes.item(1), false, true); ((Map) object).put(hashKey, hashValue); } } } } } setBeanProperty(name, object); // Handle everything else (primitives & POJOs) } else { // recurse on each of the child nodes NodeList childNodes = node.getChildNodes(); if ((childNodes != null) && (childNodes.getLength() > 0)) { for (int i = 0; i < childNodes.getLength(); i++) { Node childNode = childNodes.item(i); if (childNode.getNodeType() == Node.ELEMENT_NODE) { if (currentType != null) createObject( node, currentName, currentPackage, currentType, childNode.getNodeValue(), setProperty); parentNode = node; object = deserialize(childNode, true, true); } else if ((childNode.getNodeType() == Node.TEXT_NODE) && (currentType != null)) { object = createObject( node, currentName, currentPackage, currentType, childNode.getNodeValue(), setProperty); } currentType = null; } } else { if (!isNull) object = createObject(node, currentName, currentPackage, currentType, null, setProperty); } if (node.getParentNode() != parentNode) { parentNode = node.getParentNode(); if (popBean) { Object bean = popBeanOffStack(); if (bean != null) object = bean; } } } return object; }
/** * Constructs an attribute designator element from an existing XML block. * * @param element representing a DOM tree element. * @exception SAMLException if that there is an error in the sender or in the element definition. */ public AttributeDesignator(Element element) throws SAMLException { // make sure that the input xml block is not null if (element == null) { SAMLUtilsCommon.debug.message("AttributeDesignator: Input is null."); throw new SAMLRequesterException(SAMLUtilsCommon.bundle.getString("nullInput")); } // Make sure this is an AttributeDesignator. String tag = null; tag = element.getLocalName(); if ((tag == null) || (!tag.equals("AttributeDesignator"))) { SAMLUtilsCommon.debug.message("AttributeDesignator: wrong input"); throw new SAMLRequesterException(SAMLUtilsCommon.bundle.getString("wrongInput")); } // handle attributes int i = 0; NamedNodeMap atts = ((Node) element).getAttributes(); int attrCount = atts.getLength(); for (i = 0; i < attrCount; i++) { Node att = atts.item(i); if (att.getNodeType() == Node.ATTRIBUTE_NODE) { String attName = att.getLocalName(); if (attName == null || attName.length() == 0) { if (SAMLUtilsCommon.debug.messageEnabled()) { SAMLUtilsCommon.debug.message( "AttributeDesignator:" + "Attribute Name is either null or empty."); } throw new SAMLRequesterException(SAMLUtilsCommon.bundle.getString("nullInput")); } if (attName.equals("AttributeName")) { _attributeName = ((Attr) att).getValue().trim(); } else if (attName.equals("AttributeNamespace")) { _attributeNameSpace = ((Attr) att).getValue().trim(); } } } // AttributeName is required if (_attributeName == null || _attributeName.length() == 0) { if (SAMLUtilsCommon.debug.messageEnabled()) { SAMLUtilsCommon.debug.message( "AttributeDesignator: " + "AttributeName is required attribute"); } throw new SAMLRequesterException(SAMLUtilsCommon.bundle.getString("missingAttribute")); } // AttributeNamespace is required if (_attributeNameSpace == null || _attributeNameSpace.length() == 0) { if (SAMLUtilsCommon.debug.messageEnabled()) { SAMLUtilsCommon.debug.message( "AttributeDesignator: " + "AttributeNamespace is required attribute"); } throw new SAMLRequesterException(SAMLUtilsCommon.bundle.getString("missingAttribute")); } // handle the children of AttributeDesignator element // Since AttributeDesignator does not have any child element_node, // we will throw exception if we found any such child. NodeList nodes = element.getChildNodes(); int nodeCount = nodes.getLength(); if (nodeCount > 0) { for (i = 0; i < nodeCount; i++) { Node currentNode = nodes.item(i); if (currentNode.getNodeType() == Node.ELEMENT_NODE) { if (SAMLUtilsCommon.debug.messageEnabled()) { SAMLUtilsCommon.debug.message("AttributeDesignator: illegal input!"); } throw new SAMLRequesterException(SAMLUtilsCommon.bundle.getString("wrongInput")); } } } }
/** * Converts an XML element into an <code>EppXriAuthority</code> object. The caller of this method * must make sure that the root node is of the EPP Authority type. * * @param root root node for an <code>EppXriAuthority</code> object in XML format * @return an <code>EppXriAuthority</code> object, or null if the node is invalid */ public static EppEntity fromXML(Node root) { EppXriAuthority authority = new EppXriAuthority(); NodeList list = root.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { Node node = list.item(i); String name = node.getLocalName(); if (name == null) { continue; } if (name.equals("authId")) { authority.setAuthorityId(EppUtil.getText(node)); } if (name.equals("isEscrow")) { authority.setIsEscrow(EppUtil.getTextAsBool(node)); } if (name.equals("isContact")) { authority.setIsContact(EppUtil.getTextAsBool(node)); } if (name.equals("escrowAgent")) { EppXriTrustee agent = (EppXriTrustee) EppXriTrustee.fromXML(node); if (agent != null) authority.setEscrowAgent(agent); } if (name.equals("contactAgent")) { EppXriTrustee agent = (EppXriTrustee) EppXriTrustee.fromXML(node); if (agent != null) authority.setContactAgent(agent); } else if (name.equals("socialData")) { EppXriSocialData socialData = (EppXriSocialData) EppXriSocialData.fromXML(node); if (socialData != null) { authority.setSocialData(socialData); } } else if (name.equals("trustee")) { EppXriTrustee trustee = (EppXriTrustee) EppXriTrustee.fromXML(node); if (trustee != null) { authority.addTrustee(trustee); } } else if (name.equals("contactId")) { EppXriContactData con = (EppXriContactData) EppXriContactData.fromXML(node); if (con != null) { authority.addContactHandle(con); } } else if (name.equals("ref")) { EppXriRef ref = (EppXriRef) EppXriRef.fromXML(node); if (ref != null) { authority.addRef(ref); } } else if (name.equals("redirect")) { EppXriURI redirect = (EppXriURI) EppXriURI.fromXML(node); if (redirect != null) { authority.addRedirect(redirect); } } else if (name.equals("equivID")) { EppXriSynonym equivID = (EppXriSynonym) EppXriSynonym.fromXML(node); if (equivID != null) { authority.addEquivID(equivID); } } if (name.equals("canonicalEquivID")) { authority.setCanonicalEquivID(EppUtil.getText(node)); } else if (name.equals("sep")) { EppXriServiceEndpoint sep = (EppXriServiceEndpoint) EppXriServiceEndpoint.fromXML(node); if (sep != null) { authority.addServiceEndpoint(sep); } } else if (name.equals("iname")) { authority.addIName(EppUtil.getText(node)); } else if (name.equals("inumber")) { EppXriNumberAttribute xin = (EppXriNumberAttribute) EppXriNumberAttribute.fromXML(node); if (xin != null) { authority.addINumber(xin); } } else if (name.equals("iservice")) { authority.addIService(EppUtil.getText(node)); } else if (name.equals("authInfo")) { EppAuthInfo authInfo = (EppAuthInfo) EppAuthInfo.fromXML(node); if (authInfo != null) { authority.setAuthInfo(authInfo); } } else if (name.equals("extension")) { authority.setExtension(EppUtil.getText(node)); } else { authority.fromXMLCommon(node, name); } } return authority; }
/** * @return <code>Document</code> based on <code>documentURL</code>. * @throws Exception if an error occurs during the process of building a <code>Document</code> */ private Document getDocument() throws Exception { Document returnDoc; DocumentBuilder db = getNonValidatingBuilder(); InputSource is = new InputSource(getInputStream(documentURL)); is.setSystemId(documentURL.toExternalForm()); Document doc = db.parse(is); String documentNS = doc.getDocumentElement().getNamespaceURI(); if (validating && documentNS != null) { DOMSource domSource = new DOMSource(doc, documentURL.toExternalForm()); /* * If the Document in question is 1.2 (i.e. it has a namespace matching * JAVAEE_SCHEMA_DEFAULT_NS, then perform validation using the cached schema * and return. Otherwise we assume a 1.0 or 1.1 faces-config in which case * we need to transform it to reference a special 1.1 schema before validating. */ Node documentElement = ((Document) domSource.getNode()).getDocumentElement(); if (JAVAEE_SCHEMA_DEFAULT_NS.equals(documentNS)) { Attr version = (Attr) documentElement.getAttributes().getNamedItem("version"); DbfFactory.FacesSchema schema; if (version != null) { String versionStr = version.getValue(); if ("2.0".equals(versionStr)) { if ("facelet-taglib".equals(documentElement.getLocalName())) { schema = DbfFactory.FacesSchema.FACELET_TAGLIB_20; } else { schema = DbfFactory.FacesSchema.FACES_20; } } else if ("1.2".equals(versionStr)) { schema = DbfFactory.FacesSchema.FACES_12; } else { throw new ConfigurationException("Unknown Schema version: " + versionStr); } DocumentBuilder builder = getBuilderForSchema(schema); builder.getSchema().newValidator().validate(domSource); returnDoc = ((Document) domSource.getNode()); } else { // this shouldn't happen, but... throw new ConfigurationException("No document version available."); } } else { DOMResult domResult = new DOMResult(); Transformer transformer = getTransformer(documentNS); transformer.transform(domSource, domResult); // copy the source document URI to the transformed result // so that processes that need to build URLs relative to the // document will work as expected. ((Document) domResult.getNode()) .setDocumentURI(((Document) domSource.getNode()).getDocumentURI()); DbfFactory.FacesSchema schemaToApply; if (FACES_CONFIG_1_X_DEFAULT_NS.equals(documentNS)) { schemaToApply = DbfFactory.FacesSchema.FACES_11; } else if (FACELETS_1_0_DEFAULT_NS.equals(documentNS)) { schemaToApply = DbfFactory.FacesSchema.FACELET_TAGLIB_20; } else { throw new IllegalStateException(); } DocumentBuilder builder = getBuilderForSchema(schemaToApply); builder.getSchema().newValidator().validate(new DOMSource(domResult.getNode())); returnDoc = (Document) domResult.getNode(); } } else { returnDoc = doc; } // mark this document as the parsed representation of the // WEB-INF/faces-config.xml. This is used later in the configuration // processing. if (documentURL.toExternalForm().contains("/WEB-INF/faces-config.xml")) { Attr webInf = returnDoc.createAttribute(WEB_INF_MARKER); webInf.setValue("true"); returnDoc.getDocumentElement().getAttributes().setNamedItem(webInf); } return returnDoc; }
/** * Handle attribute node during validation. * * @param receivedElement * @param receivedAttribute * @param sourceElement * @param validationContext */ private void doAttribute( Node receivedElement, Node receivedAttribute, Node sourceElement, XmlMessageValidationContext validationContext, NamespaceContext namespaceContext, TestContext context) { if (receivedAttribute.getNodeName().startsWith(XMLConstants.XMLNS_ATTRIBUTE)) { return; } String receivedAttributeName = receivedAttribute.getLocalName(); if (log.isDebugEnabled()) { log.debug( "Validating attribute: " + receivedAttributeName + " (" + receivedAttribute.getNamespaceURI() + ")"); } NamedNodeMap sourceAttributes = sourceElement.getAttributes(); Node sourceAttribute = sourceAttributes.getNamedItemNS(receivedAttribute.getNamespaceURI(), receivedAttributeName); Assert.isTrue( sourceAttribute != null, "Attribute validation failed for element '" + receivedElement.getLocalName() + "', unknown attribute " + receivedAttributeName + " (" + receivedAttribute.getNamespaceURI() + ")"); if (XmlValidationUtils.isAttributeIgnored( receivedElement, receivedAttribute, sourceAttribute, validationContext.getIgnoreExpressions(), namespaceContext)) { return; } String receivedValue = receivedAttribute.getNodeValue(); String sourceValue = sourceAttribute.getNodeValue(); if (isValidationMatcherExpression(sourceAttribute)) { ValidationMatcherUtils.resolveValidationMatcher( sourceAttribute.getNodeName(), receivedAttribute.getNodeValue().trim(), sourceAttribute.getNodeValue().trim(), context); } else if (receivedValue.contains(":") && sourceValue.contains(":")) { doNamespaceQualifiedAttributeValidation( receivedElement, receivedAttribute, sourceElement, sourceAttribute); } else { Assert.isTrue( receivedValue.equals(sourceValue), ValidationUtils.buildValueMismatchErrorMessage( "Values not equal for attribute '" + receivedAttributeName + "'", sourceValue, receivedValue)); } if (log.isDebugEnabled()) { log.debug("Attribute '" + receivedAttributeName + "'='" + receivedValue + "': OK"); } }
/** * Handle element node. * * @param received * @param source * @param validationContext */ private void doElement( Node received, Node source, XmlMessageValidationContext validationContext, NamespaceContext namespaceContext, TestContext context) { doElementNameValidation(received, source); doElementNamespaceValidation(received, source); // check if element is ignored either by xpath or by ignore placeholder in source message if (XmlValidationUtils.isElementIgnored( source, received, validationContext.getIgnoreExpressions(), namespaceContext)) { return; } // work on attributes if (log.isDebugEnabled()) { log.debug("Validating attributes for element: " + received.getLocalName()); } NamedNodeMap receivedAttr = received.getAttributes(); NamedNodeMap sourceAttr = source.getAttributes(); Assert.isTrue( countAttributes(receivedAttr) == countAttributes(sourceAttr), ValidationUtils.buildValueMismatchErrorMessage( "Number of attributes not equal for element '" + received.getLocalName() + "'", countAttributes(sourceAttr), countAttributes(receivedAttr))); for (int i = 0; i < receivedAttr.getLength(); i++) { doAttribute( received, receivedAttr.item(i), source, validationContext, namespaceContext, context); } // check if validation matcher on element is specified if (isValidationMatcherExpression(source)) { ValidationMatcherUtils.resolveValidationMatcher( source.getNodeName(), received.getFirstChild().getNodeValue().trim(), source.getFirstChild().getNodeValue().trim(), context); return; } // work on child nodes NodeList receivedChilds = received.getChildNodes(); NodeList sourceChilds = source.getChildNodes(); Assert.isTrue( receivedChilds.getLength() == sourceChilds.getLength(), ValidationUtils.buildValueMismatchErrorMessage( "Number of child elements not equal for element '" + received.getLocalName() + "'", sourceChilds.getLength(), receivedChilds.getLength())); for (int i = 0; i < receivedChilds.getLength(); i++) { this.validateXmlTree( receivedChilds.item(i), sourceChilds.item(i), validationContext, namespaceContext, context); } if (log.isDebugEnabled()) { log.debug( "Validation successful for element: " + received.getLocalName() + " (" + received.getNamespaceURI() + ")"); } }