Example usage for org.w3c.dom Element getOwnerDocument

List of usage examples for org.w3c.dom Element getOwnerDocument

Introduction

In this page you can find the example usage for org.w3c.dom Element getOwnerDocument.

Prototype

public Document getOwnerDocument();

Source Link

Document

The Document object associated with this node.

Usage

From source file:org.apache.xml.security.utils.ElementProxy.java

/**
 * Method setElement/*from ww w . jav a2 s  . c om*/
 *
 * @param element
 * @param BaseURI
 * @throws XMLSecurityException
 */
public void setElement(Element element, String BaseURI) throws XMLSecurityException {
    if (element == null) {
        throw new XMLSecurityException("ElementProxy.nullElement");
    }

    if (log.isDebugEnabled()) {
        log.debug("setElement(" + element.getTagName() + ", \"" + BaseURI + "\"");
    }

    this.doc = element.getOwnerDocument();
    this.constructionElement = element;
    this.baseURI = BaseURI;
}

From source file:org.apache.xml.security.utils.IdResolver.java

/**
 * Method registerElementById/*from  w w w  . j  a  va  2  s  .  c  o m*/
 *
 * @param element the element to register
 * @param idValue the value of the ID attribute
 */
public static void registerElementById(Element element, String idValue) {
    Document doc = element.getOwnerDocument();
    Map<String, WeakReference<Element>> elementMap;
    synchronized (docMap) {
        elementMap = docMap.get(doc);
        if (elementMap == null) {
            elementMap = new WeakHashMap<String, WeakReference<Element>>();
            docMap.put(doc, elementMap);
        }
    }
    elementMap.put(idValue, new WeakReference<Element>(element));
}

From source file:org.apache.xml.security.utils.XMLUtils.java

/**
 * Method addReturnToElement/* w  w  w. j av  a2  s .c  o  m*/
 *
 * @param e
 */
public static void addReturnToElement(Element e) {
    if (!ignoreLineBreaks) {
        Document doc = e.getOwnerDocument();
        e.appendChild(doc.createTextNode("\n"));
    }
}

From source file:org.apache.xml.security.utils.XMLUtils.java

public static void addReturnBeforeChild(Element e, Node child) {
    if (!ignoreLineBreaks) {
        Document doc = e.getOwnerDocument();
        e.insertBefore(doc.createTextNode("\n"), child);
    }/*  w  w  w. j  a  va2  s  . c  o  m*/
}

From source file:org.apereo.portal.layout.dlm.EditManager.java

/**
   Evaluate whether attribute changes exist in the ilfChild and if so
   apply them. Returns true if some changes existed. If changes existed
   but matched those in the original node then they are not applicable,
   are removed from the editSet, and false is returned.
*//* ww w. j a  v  a 2 s . c om*/
public static boolean applyEditSet(Element plfChild, Element original) {
    // first get edit set if it exists
    Element editSet = null;
    try {
        editSet = getEditSet(plfChild, null, null, false);
    } catch (Exception e) {
        // should never occur unless problem during create in getEditSet
        // and we are telling it not to create.
        return false;
    }

    if (editSet == null || editSet.getChildNodes().getLength() == 0)
        return false;

    if (original.getAttribute(Constants.ATT_EDIT_ALLOWED).equals("false")) {
        // can't change anymore so discard changes
        plfChild.removeChild(editSet);
        return false;
    }

    Document ilf = original.getOwnerDocument();
    boolean attributeChanged = false;
    Element edit = (Element) editSet.getFirstChild();

    while (edit != null) {
        String attribName = edit.getAttribute(Constants.ATT_NAME);
        Attr attr = plfChild.getAttributeNode(attribName);

        // preferences are only updated at preference storage time so
        // if a preference change exists in the edit set assume it is
        // still valid so that the node being edited will persist in
        // the PLF.
        if (edit.getNodeName().equals(Constants.ELM_PREF))
            attributeChanged = true;
        else if (attr == null) {
            // attribute removed. See if needs removing in original.
            attr = original.getAttributeNode(attribName);
            if (attr == null) // edit irrelevant,
                editSet.removeChild(edit);
            else {
                // edit differs, apply to original
                original.removeAttribute(attribName);
                attributeChanged = true;
            }
        } else {
            // attribute there, see if original is also there
            Attr origAttr = original.getAttributeNode(attribName);
            if (origAttr == null) {
                // original attribute isn't defined so need to add
                origAttr = (Attr) ilf.importNode(attr, true);
                original.setAttributeNode(origAttr);
                attributeChanged = true;
            } else {
                // original attrib found, see if different
                if (attr.getValue().equals(origAttr.getValue())) {
                    // they are the same, edit irrelevant
                    editSet.removeChild(edit);
                } else {
                    // edit differs, apply to original
                    origAttr.setValue(attr.getValue());
                    attributeChanged = true;
                }
            }
        }
        edit = (Element) edit.getNextSibling();
    }
    return attributeChanged;
}

From source file:org.apereo.portal.layout.dlm.ILFBuilder.java

/**
 * @param source parent of children/*from w  w w  .  j a v  a  2s . c om*/
 * @param dest receiver of children
 * @param ap User's authorization principal for determining if they can view a channel
 * @param visitedNodes A Set of nodes from the source tree that have been visited to get to this node, used to ensure a loop doesn't exist in the source tree.
 * @throws AuthorizationException
 */
private static void mergeChildren(Element source, Element dest, IAuthorizationPrincipal ap, Set visitedNodes)
        throws AuthorizationException {
    //Record this node in the visited nodes set. If add returns false a loop has been detected
    if (!visitedNodes.add(source)) {
        final String msg = "mergeChildren has encountered a loop in the source DOM. currentNode='" + source
                + "', currentDepth='" + visitedNodes.size() + "', visitedNodes='" + visitedNodes + "'";
        final IllegalStateException ise = new IllegalStateException(msg);
        LOG.error(msg, ise);

        printNodeToDebug(source, "Source");
        printNodeToDebug(dest, "Dest");

        throw ise;
    }

    Document destDoc = dest.getOwnerDocument();

    Node item = source.getFirstChild();
    while (item != null) {
        if (item instanceof Element) {

            Element child = (Element) item;
            Element newChild = null;

            if (null != child && mergeAllowed(child, ap)) {
                newChild = (Element) destDoc.importNode(child, false);
                dest.appendChild(newChild);
                String id = newChild.getAttribute(Constants.ATT_ID);
                if (id != null && !id.equals(""))
                    newChild.setIdAttribute(Constants.ATT_ID, true);
                mergeChildren(child, newChild, ap, visitedNodes);
            }
        }

        item = item.getNextSibling();
    }

    //Remove this node from the visited nodes set
    visitedNodes.remove(source);
}

From source file:org.apereo.portal.layout.dlm.PLFIntegrator.java

private static void mergeChannel(Element plfChild, Element plfParent, Element ilfParent,
        IntegrationResult result, NodeInfoTracker tracker) {

    String id = plfChild.getAttribute(Constants.ATT_ID);

    if (id.startsWith(Constants.FRAGMENT_ID_USER_PREFIX)) {
        // incorporated channel - if a copy of an inc'd channel is in the
        // plf it is because either it has attribute edits. It does not
        // imply movement.
        // That is accomplished by the position set. So see if it still
        // exists in the ilf for applying changes

        Document ilf = ilfParent.getOwnerDocument();
        Element original = ilf.getElementById(id);

        if (original == null) {
            // not there anymore, discard from plf
            plfParent.removeChild(plfChild);
            result.setChangedPLF(true);/*from  w w w .  ja v  a 2s  .com*/
            return;
        }

        // found it, apply changes and see if they had any affect
        boolean attributeChanged = false;
        IntegrationResult childChanges = new IntegrationResult();

        attributeChanged = EditManager.applyEditSet(plfChild, original);
        applyChildChanges(plfChild, original, childChanges, tracker);

        if (attributeChanged == false && !childChanges.isChangedILF()) {
            // no changes were used so remove this guy from plf.
            plfParent.removeChild(plfChild);
            result.setChangedPLF(true);
        } else
            result.setChangedILF(true);
        // need to pass on up whether PLF changed in called methods
        if (childChanges.isChangedPLF())
            result.setChangedPLF(true);
    } else // plf channel
    {
        if (LOG.isInfoEnabled())
            LOG.info("merging into ilf channel " + id);

        if (ilfParent.getAttribute(Constants.ATT_ADD_CHILD_ALLOWED).equals("false")) {
            if (LOG.isInfoEnabled())
                LOG.info("removing from plf disallowed add of channel " + id);
            plfParent.removeChild(plfChild);
            result.setChangedPLF(true);
        } else {
            appendChild(plfChild, ilfParent, true);
            result.setChangedILF(true);
        }
    }
}

From source file:org.apereo.portal.layout.dlm.PLFIntegrator.java

private static void mergeFolder(Element plfChild, Element plfParent, Element ilfParent,
        IntegrationResult result, NodeInfoTracker tracker) throws PortalException {

    String id = plfChild.getAttribute(Constants.ATT_ID);

    if (id.startsWith(Constants.FRAGMENT_ID_USER_PREFIX)) {
        // incorporated folder - if a copy of an inc'd folder is in the
        // plf it is because either it has attribute edits or there are
        // nested child changes to be applied. It does not imply movement.
        // That is accomplished by the position set. So see if it still
        // exists in the ilf for applying changes

        Document ilf = ilfParent.getOwnerDocument();
        Element original = ilf.getElementById(id);

        if (original == null) {
            // not there anymore, discard from plf
            plfParent.removeChild(plfChild);
            result.setChangedPLF(true);/*from ww w  .  j  a v a 2 s  .c om*/
            return;
        }

        // found it, apply changes and see if they had any affect
        boolean attributeChanged = false;
        IntegrationResult childChanges = new IntegrationResult();

        attributeChanged = EditManager.applyEditSet(plfChild, original);
        applyChildChanges(plfChild, original, childChanges, tracker);

        if (attributeChanged == false && !childChanges.isChangedILF()) {
            // no changes were used so remove this guy from plf.
            plfParent.removeChild(plfChild);
            result.setChangedPLF(true);
        } else
            result.setChangedILF(true);
        // need to pass on up whether PLF changed in called methods
        if (childChanges.isChangedPLF())
            result.setChangedPLF(true);
    } else {
        // plf folder - the real node. What is being portrayed in this
        // case is a plf node that is supposed to be added into the ilf
        // parent

        if (ilfParent.getAttribute(Constants.ATT_ADD_CHILD_ALLOWED).equals("false")) {
            // nope, delete directive from plf
            if (LOG.isInfoEnabled())
                LOG.info("removing folder from plf " + id);
            plfParent.removeChild(plfChild);
            result.setChangedPLF(true);
            return;
        }
        Element ilfChild = appendChild(plfChild, ilfParent, false);
        result.setChangedILF(true);

        IntegrationResult childChanges = new IntegrationResult();
        applyChildChanges(plfChild, ilfChild, childChanges, tracker);

        if (childChanges.isChangedPLF())
            result.setChangedPLF(true);
    }
}

From source file:org.apereo.portal.layout.dlm.PLFIntegrator.java

/**
   This method copies a plf node and any of its children into the passed
   in compViewParent./*  w  w  w .j a v a  2s.  c  o  m*/
 */
static Element appendChild(Element plfChild, Element parent, boolean copyChildren) {
    Document document = parent.getOwnerDocument();
    Element copy = (Element) document.importNode(plfChild, false);
    parent.appendChild(copy);

    // set the identifier for the doc if warrented
    String id = copy.getAttribute(Constants.ATT_ID);
    if (id != null && !id.equals(""))
        copy.setIdAttribute(Constants.ATT_ID, true);

    if (copyChildren) {
        NodeList children = plfChild.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            if (children.item(i) instanceof Element)
                appendChild((Element) children.item(i), copy, true);
        }
    }
    return copy;
}

From source file:org.apereo.portal.layout.dlm.PositionManager.java

/**
   This method trims down the position set to the position directives on
   the node info elements still having a position directive. Any directives
   that violated restrictions were removed from the node info objects so
   the position set should be made to match the order of those still
   having one.//from  w w  w  . j  a v a  2s. c o  m
 */
static void adjustPositionSet(List<NodeInfo> order, Element positionSet, IntegrationResult result) {
    Node nodeToMatch = positionSet.getFirstChild();
    Element nodeToInsertBefore = positionSet.getOwnerDocument().createElement("INSERT_POINT");
    positionSet.insertBefore(nodeToInsertBefore, nodeToMatch);

    for (Iterator<NodeInfo> iter = order.iterator(); iter.hasNext();) {
        NodeInfo ni = iter.next();

        if (ni.getPositionDirective() != null) {
            // found one check it against the current one in the position
            // set to see if it is different. If so then indicate that
            // something (the position set) has changed in the plf
            if (ni.getPositionDirective() != nodeToMatch)
                result.setChangedPLF(true);
            ;

            // now bump the insertion point forward prior to
            // moving on to the next position node to be evaluated
            if (nodeToMatch != null)
                nodeToMatch = nodeToMatch.getNextSibling();

            // now insert it prior to insertion point
            positionSet.insertBefore(ni.getPositionDirective(), nodeToInsertBefore);
        }
    }

    // now for any left over after the insert point remove them.

    while (nodeToInsertBefore.getNextSibling() != null)
        positionSet.removeChild(nodeToInsertBefore.getNextSibling());

    // now remove the insertion point
    positionSet.removeChild(nodeToInsertBefore);
}