All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.att.research.xacml.std.dom.DOMStatusCode Maven / Gradle / Ivy

/*
 *                        AT&T - PROPRIETARY
 *          THIS FILE CONTAINS PROPRIETARY INFORMATION OF
 *        AT&T AND IS NOT TO BE DISCLOSED OR USED EXCEPT IN
 *             ACCORDANCE WITH APPLICABLE AGREEMENTS.
 *
 *          Copyright (c) 2013 AT&T Knowledge Ventures
 *              Unpublished and Not for Publication
 *                     All Rights Reserved
 */
package com.att.research.xacml.std.dom;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.att.research.xacml.api.Identifier;
import com.att.research.xacml.api.StatusCode;
import com.att.research.xacml.api.XACML3;
import com.att.research.xacml.std.StdStatusCode;

/**
 * DOMStatusCode extends {@link com.att.research.xacml.comomon.std.StdStatusCode} with methods for creation from
 * DOM {@link org.w3c.dom.Node}s.
 * 
 * @author car
 * @version $Revision: 1.2 $
 */
public class DOMStatusCode {
	private static final Log logger	= LogFactory.getLog(DOMStatusCode.class);
	
	protected DOMStatusCode() {
	}
	
	/**
	 * Creates a new DOMStatusCode by parsing the given Node representing a XACML StatusCode element.
	 * 
	 * @param nodeStatusCode the Node representing a StatusCode element
	 * @return a new DOMStatusCode parsed from the given Node
	 * @throws DOMStructureException if the conversion cannot be made
	 */
	public static StatusCode newInstance(Node nodeStatusCode) throws DOMStructureException {
		Element elementStatusCode	= DOMUtil.getElement(nodeStatusCode);
		boolean bLenient			= DOMProperties.isLenient();
		
		Identifier identifierStatusCode	= DOMUtil.getIdentifierAttribute(elementStatusCode, XACML3.ATTRIBUTE_VALUE, !bLenient);
		StatusCode statusCodeChild		= null;
		
		NodeList children	= elementStatusCode.getChildNodes();
		int numChildren;
		if (children != null && (numChildren = children.getLength()) > 0) {
			for (int i = 0 ; i < numChildren ; i++) {
				Node child	= children.item(i);
				if (DOMUtil.isElement(child)) {
					if (DOMUtil.isInNamespace(child, XACML3.XMLNS)) {
						if (child.getLocalName().equals(XACML3.ELEMENT_STATUSCODE)) {
							if (statusCodeChild != null) {
								if (!bLenient) {
									throw DOMUtil.newUnexpectedElementException(child, nodeStatusCode);
								}
							} else {
								statusCodeChild	= DOMStatusCode.newInstance(child);
							}
						} else {
							if (!bLenient) {
								throw DOMUtil.newUnexpectedElementException(child, nodeStatusCode);
							}							
						}
					} else {
						if (!bLenient) {
							throw DOMUtil.newUnexpectedElementException(child, nodeStatusCode);
						}
					}
				}
			}
		}
		return new StdStatusCode(identifierStatusCode, statusCodeChild);
	}
	
	public static boolean repair(Node nodeStatusCode) throws DOMStructureException {
		Element elementStatusCode	= DOMUtil.getElement(nodeStatusCode);
		boolean result				= false;
		
		result						= DOMUtil.repairIdentifierAttribute(elementStatusCode, XACML3.ATTRIBUTE_VALUE, logger) || result;
		
		NodeList children	= elementStatusCode.getChildNodes();
		int numChildren;
		if (children != null && (numChildren = children.getLength()) > 0) {
			for (int i = 0 ; i < numChildren ; i++) {
				Node child	= children.item(i);
				if (DOMUtil.isElement(child)) {
					if (DOMUtil.isInNamespace(child, XACML3.XMLNS)) {
						result		= DOMStatusCode.repair(child) || result;
					} else {
						logger.warn("Unexpected element " + child.getNodeName());
						elementStatusCode.removeChild(child);
						result	= true;
					}
				}
			}
		}
		return result;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy