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.gvnix.dynamic.configuration.roo.addon.ConfigurationsImpl.java

/**
 * Add new property element containing key and value elements on component.
 * /*from   w w  w .  ja va  2 s.  c o  m*/
 * @param comp Component where add property
 * @param key Property key
 * @param value Property value
 */
private void addProperty(Element comp, String key, String value) {

    // Get document and create property element
    Document doc = comp.getOwnerDocument();
    Element propElem = doc.createElement(PROPERTY_ELEMENT_NAME);

    // Create key
    Element keyElem = doc.createElement(KEY_ELEMENT_NAME);
    keyElem.setTextContent(key);
    propElem.appendChild(keyElem);

    // Create value element
    Element valueElem = doc.createElement(VALUE_ELEMENT_NAME);
    valueElem.setTextContent(value);
    propElem.appendChild(valueElem);

    // Add property on component
    comp.appendChild(propElem);
}

From source file:eu.eidas.encryption.SAMLAuthnResponseEncrypter.java

@Nonnull
private Response performEncryption(@Nonnull Response samlResponseEncryptee, @Nonnull Credential credential)
        throws EncryptionException {
    try {//  w  ww .  j a  va2  s  .c o  m
        // Set Data Encryption parameters
        EncryptionParameters encParams = new EncryptionParameters();
        encParams.setAlgorithm(getDataEncAlgorithm());
        // Set Key Encryption parameters
        KeyEncryptionParameters kekParams = new KeyEncryptionParameters();
        kekParams.setEncryptionCredential(credential);
        kekParams.setAlgorithm(getKeyEncAlgorithm());
        KeyInfoGeneratorFactory kigf = Configuration.getGlobalSecurityConfiguration()
                .getKeyInfoGeneratorManager().getDefaultManager().getFactory(credential);
        kekParams.setKeyInfoGenerator(kigf.newInstance());
        // Setup Open SAML Encrypter
        Encrypter encrypter = new Encrypter(encParams, kekParams);
        encrypter.setKeyPlacement(Encrypter.KeyPlacement.INLINE);
        if (getJcaProviderName() != null) {
            encrypter.setJCAProviderName(getJcaProviderName());
        }

        for (Assertion assertion : samlResponseEncryptee.getAssertions()) {
            if (assertion.getDOM() == null) {
                OpenSamlHelper.marshallToDom(assertion);
            }
            manageNamespaces(assertion);
        }
        List<EncryptedAssertion> encryptedAssertions = new ArrayList<>();
        for (Assertion assertion : samlResponseEncryptee.getAssertions()) {
            EncryptedAssertion encryptedAssertion = encrypter.encrypt(assertion);
            encryptedAssertions.add(encryptedAssertion);
        }

        Element previousDom = samlResponseEncryptee.getDOM();
        if (null == previousDom) {
            previousDom = OpenSamlHelper.marshallToDom(samlResponseEncryptee);
        }
        Document ownerDocument = previousDom.getOwnerDocument();

        // Deep copy the previous DOM into a new one using importNode()
        Document newDocument = DocumentBuilderFactoryUtil.newDocument();
        Node copiedRoot = newDocument.importNode(ownerDocument.getDocumentElement(), true);
        newDocument.appendChild(copiedRoot);

        Element newRootElement = newDocument.getDocumentElement();
        NodeList assertionList = newRootElement.getElementsByTagNameNS(
                Assertion.DEFAULT_ELEMENT_NAME.getNamespaceURI(),
                Assertion.DEFAULT_ELEMENT_NAME.getLocalPart());

        // Replace the encrypted assertions by the decrypted assertions in the new DOM tree:
        for (int i = 0, n = assertionList.getLength(); i < n; i++) {
            Node assertion = assertionList.item(i);
            EncryptedAssertion encryptedAssertion = encryptedAssertions.get(i);
            Element encryptedAssertionDOM = encryptedAssertion.getDOM();
            Node copiedEncryptedAssertion;
            if (null == encryptedAssertionDOM) {
                encryptedAssertionDOM = OpenSamlHelper.marshallToDom(encryptedAssertion);
            }
            // we may use adoptNode() instead of importNode() because the unmarshaller rectifies the ID-ness:
            copiedEncryptedAssertion = newDocument.adoptNode(encryptedAssertionDOM);
            newRootElement.replaceChild(copiedEncryptedAssertion, assertion);
        }

        // Finally unmarshall the updated DOM into a new XMLObject graph:
        // The unmarshaller rectifies the ID-ness:
        // See org.opensaml.saml1.core.impl.AssertionUnmarshaller.unmarshall()
        // See org.opensaml.saml2.core.impl.AssertionUnmarshaller.processAttribute()
        // And org.opensaml.saml1.core.impl.ResponseAbstractTypeUnmarshaller.unmarshall()
        // And org.opensaml.saml2.core.impl.StatusResponseTypeUnmarshaller.processAttribute()
        Response encryptedResponse = (Response) OpenSamlHelper.unmarshallFromDom(newDocument);

        if (LOGGER.isTraceEnabled()) {
            try {
                LOGGER.trace("SAML Response XMLObject encrypted: "
                        + EidasStringUtil.toString(DocumentBuilderFactoryUtil.marshall(newDocument, true)));
            } catch (TransformerException e) {
                LOGGER.error(e.getMessage(), e);
            }
        }

        return encryptedResponse;

    } catch (org.opensaml.xml.encryption.EncryptionException | ParserConfigurationException | MarshallException
            | UnmarshallException e) {
        throw new EncryptionException(e);
    }
}

From source file:com.bstek.dorado.view.config.XmlDocumentPreprocessor.java

@SuppressWarnings("unchecked")
private void postProcessNodeReplacement(Element element) {
    Document ownerDocument = element.getOwnerDocument();
    Node child = element.getFirstChild();
    while (child != null) {
        Node nextChild = child.getNextSibling();
        if (child instanceof Element) {
            if (child.getUserData("dorado.delete") != null) {
                List<Element> replaceContent = (List<Element>) child.getUserData("dorado.replace");
                if (replaceContent != null) {
                    for (Element el : replaceContent) {
                        Element clonedElement = (Element) ownerDocument.importNode(el, true);
                        element.insertBefore(clonedElement, child);
                    }/*ww  w .  ja v  a 2  s. c  o m*/
                    child.setUserData("dorado.replace", null, null);
                }
                element.removeChild(child);
                child.setUserData("dorado.delete", null, null);
            }
        }
        child = nextChild;
    }
}

From source file:com.rest4j.generator.Generator.java

private void cleanupBeforePostprocess(Element element) {
    if ("http://www.w3.org/1999/xhtml".equals(element.getNamespaceURI())) {
        element.getOwnerDocument().renameNode(element, null, element.getLocalName());
    }/*from w w  w.java  2s  .  c  o  m*/
    for (Node child : Util.it(element.getChildNodes())) {
        if (child instanceof Element) {
            cleanupBeforePostprocess((Element) child);
        }
    }
}

From source file:org.gvnix.dynamic.configuration.roo.addon.ConfigurationsImpl.java

/**
 * Check if a configuration is the active.
 * /*from www. ja  v a 2  s  .c  o m*/
 * @param conf Configuration element
 * @return Active element or null if is not the active
 */
private Element isActiveConfiguration(Element conf) {

    // Get active configuration element
    Element activeConf = XmlUtils.findFirstElement(ACTIVE_CONFIGURATION_XPATH,
            conf.getOwnerDocument().getDocumentElement());

    // Mark the configuration as active if same than this
    String name = conf.getAttribute(NAME_ATTRIBUTE_NAME);

    if (activeConf != null && activeConf.getTextContent().equals(name)) {

        return activeConf;
    }

    return null;
}

From source file:org.gvnix.dynamic.configuration.roo.addon.ConfigurationsImpl.java

/**
 * {@inheritDoc}//from   ww  w  .ja  v  a 2 s.c o  m
 */
public void saveConfiguration(Element elem) {

    String path = getConfigurationFilePath();
    MutableFile file = fileManager.updateFile(path);
    XmlUtils.writeXml(file.getOutputStream(), elem.getOwnerDocument());
}

From source file:com.rest4j.generator.Generator.java

private void cleanupFinal(Element element) {
    if ("http://www.w3.org/1999/xhtml".equals(element.getNamespaceURI())) {
        element.getOwnerDocument().renameNode(element, null, element.getLocalName());
    }// w  ww .j  av  a2s  .  com
    NamedNodeMap attrs = element.getAttributes();
    if (attrs.getNamedItem("xmlns") != null) {
        attrs.removeNamedItem("xmlns");
    }
    if (attrs.getNamedItem("xmlns:html") != null) {
        attrs.removeNamedItem("xmlns:html");
    }
    for (Node child : Util.it(element.getChildNodes())) {
        if (child instanceof Element) {
            cleanupFinal((Element) child);
        }
    }
}

From source file:org.codehaus.grepo.core.config.GenericRepositoryScanBeanDefinitionParser.java

/**
 * {@inheritDoc}/* w w w .j ava 2 s  .c o m*/
 */
public BeanDefinition parse(Element element, ParserContext parserContext) {
    Object source = parserContext.extractSource(element);
    GenericRepositoryConfigContext configContext = new GenericRepositoryConfigContext(element);

    // init bean defintion parse delegate...
    BeanDefinitionParserDelegate delegate = new BeanDefinitionParserDelegate(parserContext.getReaderContext());
    delegate.initDefaults(element.getOwnerDocument().getDocumentElement());

    GenericRepositoryBeanDefinitionScanner scanner = configureScanner(configContext, parserContext);

    parseBeanNameGenerator(configContext, parserContext);

    String[] basePackages = StringUtils.commaDelimitedListToStringArray(configContext.getBasePackage());
    for (String basePackage : basePackages) {
        Set<BeanDefinition> candidates = scanner.findCandidateComponents(basePackage);
        for (BeanDefinition candidate : candidates) {
            BeanDefinitionBuilder builder = BeanDefinitionParserHelper
                    .createBuilderFromConfigContext(configContext, source, defaultGenericRepositoryFactoryType);

            delegate.parsePropertyElements(configContext.getElement(), builder.getRawBeanDefinition());

            builder.addPropertyValue(GenericRepositoryConfigContext.PROXY_CLASS_PROPERTY,
                    candidate.getBeanClassName());

            String beanName = beanNameGenerator.generateBeanName(candidate, parserContext.getRegistry());
            parserContext
                    .registerBeanComponent(new BeanComponentDefinition(builder.getBeanDefinition(), beanName));
            logger.debug("Registered generic repository bean '{}'", beanName);
        }
    }

    return null;
}

From source file:figs.treeVisualization.gui.TimeAxisTree2DPanel.java

/**
 * This retrieves the leaf nodes and their dates and coordinates.
 * <P>/*from  w w  w  .  ja  v a2s.co m*/
 * The tree area needs to be set before this is called.
 */
private void fetchLeafNodes() {
    // TODO: sensitive to document changes!

    /** Blank everything */
    Calendar oldD = null, youngD = null;
    Element oldE = null, youngE = null;
    fLeafNodes.clear();

    Phylogeny phylo = this.getPhylogeny();
    Element rootClade = phylo.getRootClade();
    DocumentImpl rootDocument = (DocumentImpl) rootClade.getOwnerDocument();

    NodeIterator postIter = new PostOrderNodeIteratorImpl(rootDocument, rootClade, NodeFilter.SHOW_ELEMENT,
            cladeNodeFilter, true);
    Element cladeElem;
    while ((cladeElem = (Element) postIter.nextNode()) != null) {
        if (phylo.isCladeLeaf(cladeElem)) {
            fLeafNodes.add(cladeElem);

            /**
             * Get their dates, keeping track of the oldest and youngest.
             */
            Calendar cal = this.getPhylogeny().getCladeDate(cladeElem);
            if (cal != null) {
                fLeafDates.put(cladeElem, cal);
                if (oldD == null) {
                    oldD = cal;
                    oldE = cladeElem;
                } else if (oldD.compareTo(cal) > 0) {
                    oldD = cal;
                    oldE = cladeElem;
                }
                if (youngD == null) {
                    youngD = cal;
                    youngE = cladeElem;
                } else if (youngD.compareTo(cal) < 0) {
                    youngD = cal;
                    youngE = cladeElem;
                }
            }
        }
    }

    this.fTopLeafDate = youngE;
    this.fBottomLeafDate = oldE;
}

From source file:hoot.services.geo.BoundingBox.java

/**
 * Returns an XML representation of the bounds
 * /*  ww w.ja va 2  s  .  c om*/
 * @param parentXml parent XML node
 * @return an XML bounds node
 * @throws Exception
 */
public Element toXml(final Element parentXml) throws Exception {
    if (!isInitialized()) {
        throw new Exception("Bounds not initialized.");
    }
    Document doc = parentXml.getOwnerDocument();
    Element element = doc.createElement("bounds");
    element.setAttribute("minlat", String.valueOf(minLat));
    element.setAttribute("minlon", String.valueOf(minLon));
    element.setAttribute("maxlat", String.valueOf(maxLat));
    element.setAttribute("maxlon", String.valueOf(maxLon));
    return element;
}