use of org.w3c.dom.Attr in project midpoint by Evolveum.
the class XPathTest method xpathTest.
/**
* This is now a proper test yet.
* It does some operations with XPath. If it does not die, then the
* code some somehow consistent.
*
* It should be improved later.
*/
@Test
public void xpathTest() throws JAXBException, FileNotFoundException, IOException, ParserConfigurationException, SchemaException {
ObjectModificationType objectModification = PrismTestUtil.parseAtomicValue(new File(FILENAME_CHANGETYPE), ObjectModificationType.COMPLEX_TYPE);
for (ItemDeltaType change : objectModification.getItemDelta()) {
ItemPathType pathType = change.getPath();
System.out.println(" path=" + pathType + " (" + pathType.getClass().getName() + ") " + pathType.toString());
// NamedNodeMap attributes = path.getAttributes();
// for (int i = 0; i < attributes.getLength(); i++) {
// Node n = attributes.item(i);
// System.out.println(" A: " + n.getClass().getName() + " " + n.getNodeName() + "(" + n.getPrefix() + " : " + n.getLocalName() + ") = " + n.getNodeValue());
// }
// List<Object> any = change.getValue().getAny();
// for (Object e : any) {
// if (e instanceof Element) {
// System.out.println(" E: " + ((Element) e).getLocalName());
// }
// }
ItemPath path = pathType.getItemPath();
XPathHolder xpath = new XPathHolder(path);
AssertJUnit.assertEquals("c:extension/piracy:ship[2]/c:name", xpath.getXPathWithoutDeclarations());
System.out.println("XPATH: " + xpath);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder loader = factory.newDocumentBuilder();
Document doc = loader.newDocument();
Element xpathElement = xpath.toElement("http://elelel/", "path", doc);
Attr nsC = xpathElement.getAttributeNodeNS("http://www.w3.org/2000/xmlns/", "c");
Attr nsPiracy = xpathElement.getAttributeNodeNS("http://www.w3.org/2000/xmlns/", "piracy");
System.out.println("c: " + nsC);
System.out.println("piracy: " + nsPiracy);
// AssertJUnit.assertEquals("http://midpoint.evolveum.com/xml/ns/public/common/common-3", nsC.getValue());
// AssertJUnit.assertEquals("http://midpoint.evolveum.com/xml/ns/samples/piracy", nsPiracy.getValue());
System.out.println("XPATH Element: " + xpathElement);
XPathHolder xpathFromElement = new XPathHolder(xpathElement);
AssertJUnit.assertEquals(xpath, xpathFromElement);
// attributes = xpathElement.getAttributes();
// for (int i = 0; i < attributes.getLength(); i++) {
// Node n = attributes.item(i);
// System.out.println(" A: " + n.getNodeName() + "(" + n.getPrefix() + " : " + n.getLocalName() + ") = " + n.getNodeValue());
// }
List<XPathSegment> segments = xpath.toSegments();
System.out.println("XPATH segments: " + segments);
XPathHolder xpathFromSegments = new XPathHolder(segments);
System.out.println("XPath from segments: " + xpathFromSegments);
AssertJUnit.assertEquals("c:extension/piracy:ship[2]/c:name", xpathFromSegments.getXPathWithoutDeclarations());
}
}
use of org.w3c.dom.Attr in project midpoint by Evolveum.
the class DOMUtil method compareAttributesIsSubset.
private static boolean compareAttributesIsSubset(NamedNodeMap subset, NamedNodeMap superset, boolean considerNamespacePrefixes) {
for (int i = 0; i < subset.getLength(); i++) {
Node aItem = subset.item(i);
Attr aAttr = (Attr) aItem;
if (!considerNamespacePrefixes && isNamespaceDefinition(aAttr)) {
continue;
}
if (StringUtils.isBlank(aAttr.getLocalName())) {
// this is strange, but it can obviously happen
continue;
}
QName aQname = new QName(aAttr.getNamespaceURI(), aAttr.getLocalName());
Attr bAttr = findAttributeByQName(superset, aQname);
if (bAttr == null) {
return false;
}
if (!StringUtils.equals(aAttr.getTextContent(), bAttr.getTextContent())) {
return false;
}
}
return true;
}
use of org.w3c.dom.Attr in project OpenAM by OpenRock.
the class ValidationErrorHandler method printAttributeValue.
/**
* Print SAML Attribute Element and replace its prefix with the input
* prefix.
*
* @param node
* A DOM tree Node
* @param prefix
* A String representing the new prefix
* @return An xml String representation of the DOM tree.
*/
public static String printAttributeValue(Element node, String prefix) {
if (node == null) {
return null;
}
StringBuffer xml = new StringBuffer(100);
xml.append('<');
xml.append(prefix).append(node.getLocalName());
NamedNodeMap attrs = node.getAttributes();
int length = attrs.getLength();
for (int i = 0; i < length; i++) {
Attr attr = (Attr) attrs.item(i);
xml.append(' ');
xml.append(attr.getNodeName());
xml.append("=\"");
// xml.append(normalize(attr.getNodeValue()));
xml.append(attr.getNodeValue());
xml.append('"');
}
xml.append('>');
NodeList children = node.getChildNodes();
if (children != null) {
int len = children.getLength();
for (int i = 0; i < len; i++) {
xml.append(print(children.item(i)));
}
}
xml.append("</");
xml.append(prefix).append(node.getLocalName());
xml.append('>');
return xml.toString();
}
use of org.w3c.dom.Attr in project OpenAM by OpenRock.
the class ResponseImpl method parseElement.
private void parseElement(Element element) throws SAML2Exception {
// make sure that the input xml block is not null
if (element == null) {
if (SAML2SDKUtils.debug.messageEnabled()) {
SAML2SDKUtils.debug.message("ResponseImpl.parseElement: " + "element input is null.");
}
throw new SAML2Exception(SAML2SDKUtils.bundle.getString("nullInput"));
}
// Make sure this is an Response.
String tag = null;
tag = element.getLocalName();
if ((tag == null) || (!tag.equals("Response"))) {
if (SAML2SDKUtils.debug.messageEnabled()) {
SAML2SDKUtils.debug.message("ResponseImpl.parseElement: " + "not Response.");
}
throw new SAML2Exception(SAML2SDKUtils.bundle.getString("wrongInput"));
}
// handle the attributes of <Response> element
NamedNodeMap atts = ((Node) element).getAttributes();
if (atts != null) {
int length = atts.getLength();
for (int i = 0; i < length; i++) {
Attr attr = (Attr) atts.item(i);
String attrName = attr.getName();
String attrValue = attr.getValue().trim();
if (attrName.equals("ID")) {
responseId = attrValue;
} else if (attrName.equals("InResponseTo")) {
inResponseTo = attrValue;
} else if (attrName.equals("Version")) {
version = attrValue;
} else if (attrName.equals("IssueInstant")) {
try {
issueInstant = DateUtils.stringToDate(attrValue);
} catch (ParseException pe) {
throw new SAML2Exception(pe.getMessage());
}
} else if (attrName.equals("Destination")) {
destination = attrValue;
} else if (attrName.equals("Consent")) {
consent = attrValue;
}
}
}
// handle child elements
NodeList nl = element.getChildNodes();
Node child;
String childName;
int length = nl.getLength();
for (int i = 0; i < length; i++) {
child = nl.item(i);
if ((childName = child.getLocalName()) != null) {
if (childName.equals("Issuer")) {
if (issuer != null) {
if (SAML2SDKUtils.debug.messageEnabled()) {
SAML2SDKUtils.debug.message("ResponseImpl.parse" + "Element: included more than one Issuer.");
}
throw new SAML2Exception(SAML2SDKUtils.bundle.getString("moreElement"));
}
if (signatureString != null || extensions != null || status != null || assertions != null || encAssertions != null) {
if (SAML2SDKUtils.debug.messageEnabled()) {
SAML2SDKUtils.debug.message("ResponseImpl.parse" + "Element:wrong sequence.");
}
throw new SAML2Exception(SAML2SDKUtils.bundle.getString("schemaViolation"));
}
issuer = AssertionFactory.getInstance().createIssuer((Element) child);
} else if (childName.equals("Signature")) {
if (signatureString != null) {
if (SAML2SDKUtils.debug.messageEnabled()) {
SAML2SDKUtils.debug.message("ResponseImpl.parse" + "Element:included more than one Signature.");
}
throw new SAML2Exception(SAML2SDKUtils.bundle.getString("moreElement"));
}
if (extensions != null || status != null || assertions != null || encAssertions != null) {
if (SAML2SDKUtils.debug.messageEnabled()) {
SAML2SDKUtils.debug.message("ResponseImpl.parse" + "Element:wrong sequence.");
}
throw new SAML2Exception(SAML2SDKUtils.bundle.getString("schemaViolation"));
}
signatureString = XMLUtils.print((Element) child, "UTF-8");
isSigned = true;
} else if (childName.equals("Extensions")) {
if (extensions != null) {
if (SAML2SDKUtils.debug.messageEnabled()) {
SAML2SDKUtils.debug.message("ResponseImpl.parse" + "Element:included more than one Extensions.");
}
throw new SAML2Exception(SAML2SDKUtils.bundle.getString("moreElement"));
}
if (status != null || assertions != null || encAssertions != null) {
if (SAML2SDKUtils.debug.messageEnabled()) {
SAML2SDKUtils.debug.message("ResponseImpl.parse" + "Element:wrong sequence.");
}
throw new SAML2Exception(SAML2SDKUtils.bundle.getString("schemaViolation"));
}
extensions = ProtocolFactory.getInstance().createExtensions((Element) child);
} else if (childName.equals("Status")) {
if (status != null) {
if (SAML2SDKUtils.debug.messageEnabled()) {
SAML2SDKUtils.debug.message("ResponseImpl.parse" + "Element: included more than one Status.");
}
throw new SAML2Exception(SAML2SDKUtils.bundle.getString("moreElement"));
}
if (assertions != null || encAssertions != null) {
if (SAML2SDKUtils.debug.messageEnabled()) {
SAML2SDKUtils.debug.message("ResponseImpl.parse" + "Element:wrong sequence.");
}
throw new SAML2Exception(SAML2SDKUtils.bundle.getString("schemaViolation"));
}
status = ProtocolFactory.getInstance().createStatus((Element) child);
} else if (childName.equals("Assertion")) {
if (assertions == null) {
assertions = new ArrayList();
}
Element canoEle = SAMLUtils.getCanonicalElement(child);
if (canoEle == null) {
throw new SAML2Exception(SAML2SDKUtils.bundle.getString("errorCanonical"));
}
assertions.add(AssertionFactory.getInstance().createAssertion(canoEle));
} else if (childName.equals("EncryptedAssertion")) {
if (encAssertions == null) {
encAssertions = new ArrayList();
}
encAssertions.add(AssertionFactory.getInstance().createEncryptedAssertion((Element) child));
} else {
if (SAML2SDKUtils.debug.messageEnabled()) {
SAML2SDKUtils.debug.message("ResponseImpl.parse" + "Element: Invalid element:" + childName);
}
throw new SAML2Exception(SAML2SDKUtils.bundle.getString("invalidElement"));
}
}
}
super.validateData();
if (assertions != null) {
Iterator iter = assertions.iterator();
while (iter.hasNext()) {
((Assertion) iter.next()).makeImmutable();
}
assertions = Collections.unmodifiableList(assertions);
}
if (encAssertions != null) {
encAssertions = Collections.unmodifiableList(encAssertions);
}
isMutable = false;
}
use of org.w3c.dom.Attr in project OpenAM by OpenRock.
the class FSRequest method parseQuery.
/**
* Parses the Query or <code>SubjectQuery</code> represented by
* a DOM tree Node. It then checks and sets data members if it is a
* supported query, such as <code>AuthenticationQuery</code>,
* <code>AttributeQeury</code>, or <code>AuthorizationDecisionQuery</code>.
*
* @param child a <code>DOM</code> Node.
* @throws <code>SAMLException</code> if the <code>Query</code> is invalid.
*/
private void parseQuery(Node child) throws SAMLException {
NamedNodeMap nm = child.getAttributes();
int len = nm.getLength();
String attrName;
String attrValue;
Attr attr;
boolean found = false;
for (int j = 0; j < len; j++) {
attr = (Attr) nm.item(j);
attrName = attr.getLocalName();
if ((attrName != null) && (attrName.equals("type"))) {
attrValue = attr.getNodeValue();
if (attrValue.equals("AuthenticationQueryType")) {
if (contentType != NOT_SUPPORTED) {
if (FSUtils.debug.messageEnabled()) {
FSUtils.debug.message("Request(Element): should" + " contain only one AuthenticationQuery.");
}
throw new SAMLRequesterException(FSUtils.BUNDLE_NAME, "wrongInput", null);
}
contentType = AUTHENTICATION_QUERY;
query = new AuthenticationQuery((Element) child);
} else if (attrValue.equals("AuthorizationDecisionQueryType")) {
if (contentType != NOT_SUPPORTED) {
if (FSUtils.debug.messageEnabled()) {
FSUtils.debug.message("Request(Element): should " + "contain one " + "AuthorizationDecisionQuery.");
}
throw new SAMLRequesterException(FSUtils.BUNDLE_NAME, "wrongInput", null);
}
contentType = AUTHORIZATION_DECISION_QUERY;
query = new AuthorizationDecisionQuery((Element) child);
} else if (attrValue.equals("AttributeQueryType")) {
if (contentType != NOT_SUPPORTED) {
if (FSUtils.debug.messageEnabled()) {
FSUtils.debug.message("Request(Element): should " + "contain one AttributeQuery.");
}
throw new SAMLRequesterException(FSUtils.BUNDLE_NAME, "wrongInput", null);
}
contentType = ATTRIBUTE_QUERY;
query = new AttributeQuery((Element) child);
} else {
if (FSUtils.debug.messageEnabled()) {
FSUtils.debug.message("Request(Element): This type of" + " " + attrName + " is not supported.");
}
throw new SAMLResponderException(FSUtils.BUNDLE_NAME, "queryNotSupported", null);
}
// check typevalue
found = true;
break;
}
// if found type attribute
}
// if not found type
if (!found) {
if (FSUtils.debug.messageEnabled()) {
FSUtils.debug.message("Request(Element): missing" + " xsi:type definition in " + child.getLocalName());
}
throw new SAMLRequesterException(FSUtils.BUNDLE_NAME, "wrongInput", null);
}
}
Aggregations