Example usage for org.w3c.dom Node getLocalName

List of usage examples for org.w3c.dom Node getLocalName

Introduction

In this page you can find the example usage for org.w3c.dom Node getLocalName.

Prototype

public String getLocalName();

Source Link

Document

Returns the local part of the qualified name of this node.

Usage

From source file:org.apache.openaz.xacml.std.dom.DOMRequestAttributes.java

/**
 * Creates a new <code>DOMRequestAttributes</code> from the given root <code>Node</code> of a XACML
 * Attributes element.//w  w  w  . ja v  a2 s. c  o m
 *
 * @param nodeRequestAttributes the <code>Node</code> representing the Attributes element
 * @return a new <code>DOMRequestAttributes</code> from the given <code>Node</code>
 * @throws DOMStructureException if the conversion cannot be made
 */
public static RequestAttributes newInstance(Node nodeRequestAttributes) throws DOMStructureException {
    Element elementRequestAttributes = DOMUtil.getElement(nodeRequestAttributes);
    boolean bLenient = DOMProperties.isLenient();

    Identifier identifierCategory = DOMUtil.getIdentifierAttribute(elementRequestAttributes,
            XACML3.ATTRIBUTE_CATEGORY, !bLenient);
    String xmlId = DOMUtil.getXmlId(elementRequestAttributes);
    Node nodeContentRoot = null;
    List<Attribute> listAttributes = new ArrayList<Attribute>();
    boolean sawContent = false;

    NodeList children = elementRequestAttributes.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)) {
                    String childName = child.getLocalName();
                    if (XACML3.ELEMENT_CONTENT.equals(childName)) {
                        if (sawContent && !bLenient) {
                            throw DOMUtil.newUnexpectedElementException(child, elementRequestAttributes);
                        }
                        sawContent = true;
                        /*
                         * Get the single root element node
                         */
                        NodeList grandchildren = child.getChildNodes();
                        int numGrandchildren;
                        if (grandchildren != null && (numGrandchildren = grandchildren.getLength()) > 0) {
                            for (int j = 0; j < numGrandchildren; j++) {
                                Node grandchild = grandchildren.item(j);
                                if (DOMUtil.isElement(grandchild)) {
                                    if (nodeContentRoot != null) {
                                        if (!bLenient) {
                                            throw DOMUtil.newUnexpectedElementException(grandchild, child);
                                        }
                                    } else {
                                        nodeContentRoot = DOMUtil.getDirectDocumentChild(grandchild);
                                    }
                                }
                            }
                        }
                        if (nodeContentRoot == null && !bLenient) {
                            throw DOMUtil.newMissingContentException(child);
                        }
                    } else if (XACML3.ELEMENT_ATTRIBUTE.equals(childName)) {
                        listAttributes.add(DOMAttribute.newInstance(identifierCategory, child));
                    } else {
                        if (!bLenient) {
                            throw DOMUtil.newUnexpectedElementException(child, nodeRequestAttributes);
                        }
                    }
                } else {
                    if (!bLenient) {
                        throw DOMUtil.newUnexpectedElementException(child, nodeRequestAttributes);
                    }
                }
            }
        }
    }

    return new StdRequestAttributes(identifierCategory, listAttributes, nodeContentRoot, xmlId);
}

From source file:org.apache.openaz.xacml.std.dom.DOMRequestAttributes.java

public static boolean repair(Node nodeRequestAttributes) throws DOMStructureException {
    Element elementRequestAttributes = DOMUtil.getElement(nodeRequestAttributes);
    boolean result = false;

    result = DOMUtil.repairIdentifierAttribute(elementRequestAttributes, XACML3.ATTRIBUTE_CATEGORY, logger)
            || result;//from  w  ww.  ja  v  a  2 s.  c  om
    NodeList children = elementRequestAttributes.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)) {
                    String childName = child.getLocalName();
                    if (XACML3.ELEMENT_ATTRIBUTE.equals(childName)) {
                        result = DOMAttribute.repair(child) || result;
                        //} else if (XACML3.ELEMENT_CONTENT.equals(childName)) {
                    } else {
                        logger.warn("Unexpected element " + child.getNodeName());
                        elementRequestAttributes.removeChild(child);
                        result = true;
                    }
                } else {
                    logger.warn("Unexpected element " + child.getNodeName());
                    elementRequestAttributes.removeChild(child);
                    result = true;
                }
            }
        }
    }

    return result;
}

From source file:org.apache.openaz.xacml.std.dom.DOMRequestDefaults.java

/**
 * Creates a new <code>DOMRequestDefaults</code> by parsing the given <code>Node</code> representing a
 * XACML RequestDefaults element./*from w ww. jav  a  2 s. c om*/
 *
 * @param nodeRequestDefaults the <code>Node</code> representing the XACML RequestDefaults element
 * @return a new <code>DOMRequestDefaults</code> parsed from the given <code>Node</code>
 * @throws DOMStructureException if the conversion cannot be made
 */
public static RequestDefaults newInstance(Node nodeRequestDefaults) throws DOMStructureException {
    Element elementRequestDefaults = DOMUtil.getElement(nodeRequestDefaults);
    boolean bLenient = DOMProperties.isLenient();

    URI uriXPathVersion = null;

    NodeList children = elementRequestDefaults.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)
                        && XACML3.ELEMENT_XPATHVERSION.equals(child.getLocalName())) {
                    uriXPathVersion = DOMUtil.getURIContent(child);
                } else {
                    if (!bLenient) {
                        throw DOMUtil.newUnexpectedElementException(child, nodeRequestDefaults);
                    }
                }
            }
        }
    }
    return new StdRequestDefaults(uriXPathVersion);
}

From source file:org.apache.openaz.xacml.std.dom.DOMRequestDefaults.java

public static boolean repair(Node nodeRequestDefaults) throws DOMStructureException {
    Element elementRequestDefaults = DOMUtil.getElement(nodeRequestDefaults);
    boolean result = false;

    NodeList children = elementRequestDefaults.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)
                        && XACML3.ELEMENT_XPATHVERSION.equals(child.getLocalName())) {
                    try {
                        DOMUtil.getURIContent(child);
                    } catch (DOMStructureException ex) {
                        logger.warn("Deleting invalid XPathVersion " + child.getTextContent());
                        elementRequestDefaults.removeChild(child);
                        result = true;// w ww .j av a  2s  .  c om
                    }
                } else {
                    logger.warn("Unexpected element " + child.getNodeName());
                    elementRequestDefaults.removeChild(child);
                    result = true;
                }
            }
        }
    }

    return result;
}

From source file:org.apache.openaz.xacml.std.dom.DOMRequestReference.java

/**
 * Creates a new <code>DOMRequestReference</code> by parsing the given <code>Node</code> representing a
 * XACML RequestReference element./*from   w  ww .jav  a2s. co m*/
 *
 * @param nodeRequestReference the <code>Node</code> representing the XACML RequestReference element
 * @return a new <code>DOMRequestReference</code> parsed from the given <code>Node</code>
 * @throws DOMStructureException if the conversion cannot be made
 */
public static RequestReference newInstance(Node nodeRequestReference) throws DOMStructureException {
    Element elementRequestReference = DOMUtil.getElement(nodeRequestReference);
    boolean bLenient = DOMProperties.isLenient();

    StdMutableRequestReference stdRequestReference = new StdMutableRequestReference();

    NodeList children = elementRequestReference.getChildNodes();
    int numChildren;
    boolean sawAttributesReference = false;
    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)
                        && XACML3.ELEMENT_ATTRIBUTESREFERENCE.equals(child.getLocalName())) {
                    stdRequestReference.add(DOMRequestAttributesReference.newInstance(child));
                    sawAttributesReference = true;
                } else {
                    if (!bLenient) {
                        throw DOMUtil.newUnexpectedElementException(child, nodeRequestReference);
                    }
                }
            }
        }
    }
    if (!sawAttributesReference && !bLenient) {
        throw DOMUtil.newMissingElementException(nodeRequestReference, XACML3.XMLNS,
                XACML3.ELEMENT_ATTRIBUTESREFERENCE);
    }
    return new StdRequestReference(stdRequestReference);
}

From source file:org.apache.openaz.xacml.std.dom.DOMRequestReference.java

public static boolean repair(Node nodeRequestReference) throws DOMStructureException {
    Element elementRequestReference = DOMUtil.getElement(nodeRequestReference);
    boolean result = false;

    NodeList children = elementRequestReference.getChildNodes();
    int numChildren;
    boolean sawAttributesReference = false;
    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)
                        && XACML3.ELEMENT_ATTRIBUTESREFERENCE.equals(child.getLocalName())) {
                    result = DOMRequestAttributesReference.repair(child) || result;
                    sawAttributesReference = true;
                } else {
                    logger.warn("Unexpected element " + child.getNodeName());
                    elementRequestReference.removeChild(child);
                    result = true;/*from  w w  w . j  a  va  2  s .  com*/
                }
            }
        }
    }
    if (!sawAttributesReference) {
        throw DOMUtil.newMissingAttributeException(nodeRequestReference, XACML3.XMLNS,
                XACML3.ELEMENT_ATTRIBUTESREFERENCE);
    }

    return result;
}

From source file:org.apache.openaz.xacml.std.dom.DOMResponse.java

/**
 * Creates a new <code>DOMResponse</code> by parsing the given <code>Node</code> representing a XACML
 * Response element.//w w w.j ava2s.com
 *
 * @param nodeResponse the <code>Node</code> representing the XACML Response element
 * @return a new <code>DOMResponse</code> parsed from the given <code>Node</code>
 * @throws DOMStructureException if the conversion cannot be made
 */
public static Response newInstance(Node nodeResponse) throws DOMStructureException {
    Element elementResponse = DOMUtil.getElement(nodeResponse);
    boolean bLenient = DOMProperties.isLenient();

    StdMutableResponse mutableResponse = new StdMutableResponse();

    NodeList children = elementResponse.getChildNodes();
    int numChildren;
    boolean sawResult = false;
    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)
                        && XACML3.ELEMENT_RESULT.equals(child.getLocalName())) {
                    mutableResponse.add(DOMResult.newInstance(child));
                    sawResult = true;
                } else {
                    if (!bLenient) {
                        throw DOMUtil.newUnexpectedElementException(child, nodeResponse);
                    }
                }
            }
        }
    }
    if (!sawResult && !bLenient) {
        throw DOMUtil.newMissingElementException(nodeResponse, XACML3.XMLNS, XACML3.ELEMENT_RESULT);
    }

    return new StdResponse(mutableResponse);
}

From source file:org.apache.openaz.xacml.std.dom.DOMResponse.java

/**
 * Change XACML2 into XACML3/*  w  w  w .j  a  va 2  s  . c  o  m*/
 *
 * @param nodeResponse
 * @return
 * @throws DOMStructureException
 */
public static boolean repair(Node nodeResponse) throws DOMStructureException {
    Element elementResponse = DOMUtil.getElement(nodeResponse);
    boolean result = false;

    NodeList children = elementResponse.getChildNodes();
    int numChildren;
    boolean sawResult = false;
    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)
                        && XACML3.ELEMENT_RESULT.equals(child.getLocalName())) {
                    result = DOMResult.repair(child) || result;
                    sawResult = true;
                } else {
                    logger.warn("Unexpected element " + child.getNodeName());
                    elementResponse.removeChild(child);
                    result = true;
                }
            }
        }
    }

    if (!sawResult) {
        throw DOMUtil.newMissingElementException(nodeResponse, XACML3.XMLNS, XACML3.ELEMENT_RESULT);
    }
    return result;
}

From source file:org.apache.openaz.xacml.std.dom.DOMResponse.java

/**
 * Read a file containing an XML representation of a Response and parse it into a
 * {@link org.apache.openaz.xacml.api.Response} Object. This is used only for testing since Responses in
 * the normal environment are generated by the PDP code.
 *
 * @param fileResponse/*www.  jav a  2  s  .  com*/
 * @return
 * @throws DOMStructureException
 */
public static Response load(InputStream is) throws DOMStructureException {
    Response request = null;
    try {
        Document document = DOMUtil.loadDocument(is);
        if (document == null) {
            throw new DOMStructureException("Null document returned");
        }

        Node rootNode = DOMUtil.getFirstChildElement(document);
        if (rootNode == null) {
            throw new DOMStructureException("No child in document");
        }

        if (DOMUtil.isInNamespace(rootNode, XACML3.XMLNS)) {
            if (XACML3.ELEMENT_RESPONSE.equals(rootNode.getLocalName())) {
                request = DOMResponse.newInstance(rootNode);
                if (request == null) {
                    throw new DOMStructureException("Failed to parse Response");
                }
            } else {
                throw DOMUtil.newUnexpectedElementException(rootNode);
            }
        } else {
            throw DOMUtil.newUnexpectedElementException(rootNode);
        }
    } catch (Exception ex) {
        throw new DOMStructureException("Exception loading Response from InputStream: " + ex.getMessage(), ex);
    }
    return request;
}

From source file:org.apache.openaz.xacml.std.dom.DOMResponse.java

/**
 * Unit test program to load an XML file containing a XACML Response document.
 *
 * @param args the list of Response files to load and parse
 *///from  w w  w . j  a va2s  .  c  om
public static void main(String[] args) {
    if (args.length > 0) {
        for (String xmlFileName : args) {
            File fileXml = new File(xmlFileName);
            if (!fileXml.exists()) {
                System.err.println("Input file \"" + fileXml.getAbsolutePath() + "\" does not exist.");
                continue;
            } else if (!fileXml.canRead()) {
                System.err
                        .println("Permission denied reading input file \"" + fileXml.getAbsolutePath() + "\"");
                continue;
            }
            System.out.println(fileXml.getAbsolutePath() + ":");
            try {
                Document documentResponse = DOMUtil.loadDocument(fileXml);
                assert documentResponse != null;

                NodeList children = documentResponse.getChildNodes();
                if (children == null || children.getLength() == 0) {
                    System.err.println("No Responses found in \"" + fileXml.getAbsolutePath() + "\"");
                    continue;
                } else if (children.getLength() > 1) {
                    System.err.println("Multiple Responses found in \"" + fileXml.getAbsolutePath() + "\"");
                }
                Node nodeResponse = children.item(0);
                if (!nodeResponse.getLocalName().equals(XACML3.ELEMENT_RESPONSE)) {
                    System.err.println("\"" + fileXml.getAbsolutePath() + "\" is not a Response");
                    continue;
                }

                Response domResponse = DOMResponse.newInstance(nodeResponse);
                System.out.println(domResponse.toString());
                System.out.println();
            } catch (Exception ex) {
                ex.printStackTrace(System.err);
            }
        }
    }
}