<commit_before>/*
 * Copyright [2006] [University Corporation for Advanced Internet Development, Inc.]
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.opensaml.xml.signature;

import java.security.GeneralSecurityException;
import java.security.cert.X509CRL;
import java.security.cert.X509Certificate;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.log4j.Logger;
import org.apache.xml.security.Init;
import org.apache.xml.security.exceptions.XMLSecurityException;
import org.apache.xml.security.keys.content.KeyName;
import org.apache.xml.security.keys.content.X509Data;
import org.opensaml.xml.XMLObject;
import org.opensaml.xml.io.Marshaller;
import org.opensaml.xml.io.MarshallingException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * A marshaller for {@link org.opensaml.xml.signature.impl.KeyInfoImpl} objects. This class, along with it's
 * respective builder and unmarshaller use the Apache XMLSec 1.3 APIs to perform signing and verification.
 */
public class KeyInfoMarshaller implements Marshaller {

    /** Logger */
    private static final Logger LOG = Logger.getLogger(KeyInfoMarshaller.class);

    /**
     * Constructor
     */
    public KeyInfoMarshaller() {
        if (!Init.isInitialized()) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Initializing XML security library");
            }
            Init.init();
        }
    }

    /**
     * {@inheritDoc}
     */
    public Element marshall(XMLObject xmlObject) throws MarshallingException {
        try {
            Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
            return marshall(xmlObject, document);
        } catch (ParserConfigurationException e) {
            throw new MarshallingException("Unable to create Document to place marshalled elements in", e);
        }
    }

    /**
     * {@inheritDoc}
     */
    public Element marshall(XMLObject xmlObject, Element parentElement) throws MarshallingException {
        return marshall(xmlObject, parentElement.getOwnerDocument());
    }

    /**
     * {@inheritDoc}
     */
    public Element marshall(XMLObject xmlObject, Document document) throws MarshallingException {
        KeyInfo keyInfoObj = (KeyInfo) xmlObject;

        if (LOG.isDebugEnabled()) {
            LOG.debug("Starting to marshall " + xmlObject.getElementQName());
        }

        org.apache.xml.security.keys.KeyInfo keyInfoElem = new org.apache.xml.security.keys.KeyInfo(document);

        for (String keyName : keyInfoObj.getKeyNames()) {
            keyInfoElem.add(new KeyName(document, keyName));
        }

        if(keyInfoObj.getPublicKey() != null){
            keyInfoElem.add(keyInfoObj.getPublicKey());
        }

        // Add X509 Certificates to X509Data
        X509Data x509Data = null;
        for (X509Certificate cert : keyInfoObj.getCertificates()) {
            try {<commit_msg>Fix bug where marshalled keyinfo elements were not being added to their parent element<commit_after>/*
 * Copyright [2006] [University Corporation for Advanced Internet Development, Inc.]
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.opensaml.xml.signature;

import java.security.GeneralSecurityException;
import java.security.cert.X509CRL;
import java.security.cert.X509Certificate;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.log4j.Logger;
import org.apache.xml.security.Init;
import org.apache.xml.security.exceptions.XMLSecurityException;
import org.apache.xml.security.keys.content.KeyName;
import org.apache.xml.security.keys.content.X509Data;
import org.opensaml.xml.XMLObject;
import org.opensaml.xml.io.Marshaller;
import org.opensaml.xml.io.MarshallingException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * A marshaller for {@link org.opensaml.xml.signature.impl.KeyInfoImpl} objects. This class, along with it's
 * respective builder and unmarshaller use the Apache XMLSec 1.3 APIs to perform signing and verification.
 */
public class KeyInfoMarshaller implements Marshaller {

    /** Logger */
    private static final Logger LOG = Logger.getLogger(KeyInfoMarshaller.class);

    /**
     * Constructor
     */
    public KeyInfoMarshaller() {
        if (!Init.isInitialized()) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Initializing XML security library");
            }
            Init.init();
        }
    }

    /**
     * {@inheritDoc}
     */
    public Element marshall(XMLObject xmlObject) throws MarshallingException {
        try {
            Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
            return marshall(xmlObject, document);
        } catch (ParserConfigurationException e) {
            throw new MarshallingException("Unable to create Document to place marshalled elements in", e);
        }
    }

    /**
     * {@inheritDoc}
     */
    public Element marshall(XMLObject xmlObject, Element parentElement) throws MarshallingException {
        Element marshalledElement = marshall(xmlObject, parentElement.getOwnerDocument());
        parentElement.appendChild(marshalledElement);
        xmlObject.setDOM(marshalledElement);
        return marshalledElement;
    }

    /**
     * {@inheritDoc}
     */
    public Element marshall(XMLObject xmlObject, Document document) throws MarshallingException {
        KeyInfo keyInfoObj = (KeyInfo) xmlObject;

        if (LOG.isDebugEnabled()) {
            LOG.debug("Starting to marshall " + xmlObject.getElementQName());
        }

        org.apache.xml.security.keys.KeyInfo keyInfoElem = new org.apache.xml.security.keys.KeyInfo(document);

        for (String keyName : keyInfoObj.getKeyNames()) {
            keyInfoElem.add(new KeyName(document, keyName));
        }

        if(keyInfoObj.getPublicKey() != null){
            keyInfoElem.add(keyInfoObj.getPublicKey());
        }

        // Add X509 Certificates to X509Data
        X509Data x509Data = null;
        for (X509Certificate cert : keyInfoObj.getCertificates()) {
            try {
{'fraction_non_alphanumeric': 0.09588097534095605, 'fraction_numerical': 0.010194241631078661, 'mean_word_length': 3.1965317919075145, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 41}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5624321', 'n_tokens_mistral': 1977, 'n_tokens_neox': 1913, 'n_words': 532}