/****************************************************************************
 * Copyright (C) 2012 HS Coburg.
 * All rights reserved.
 * Contact: ecsec GmbH (info@ecsec.de)
 *
 * This file is part of the Open eCard App.
 *
 * GNU General Public License Usage
 * This file may be used under the terms of the GNU General Public
 * License version 3.0 as published by the Free Software Foundation
 * and appearing in the file LICENSE.GPL included in the packaging of
 * this file. Please review the following information to ensure the
 * GNU General Public License version 3.0 requirements will be met:
 * http://www.gnu.org/copyleft/gpl.html.
 *
 * Other Usage
 * Alternatively, this file may be used in accordance with the terms
 * and conditions contained in a signed written agreement between
 * you and ecsec GmbH.
 *
 ***************************************************************************/

package org.openecard.sal.protocol.eac.anytype;

import iso.std.iso_iec._24727.tech.schema.DIDAuthenticationDataType;
import javax.xml.parsers.ParserConfigurationException;
import org.openecard.common.anytype.AuthDataMap;
import org.w3c.dom.Element;
import static org.openecard.binding.tctoken.ex.ErrorTranslations.*;


/**
 * Implements the EACAdditionalInputType data structure.
 * See BSI-TR-03112, version 1.1.2, part 7, section 4.6.7.
 *
 * @author Dirk Petrautzki
 * @author Moritz Horsch
 */
public class EACAdditionalInputType {

    public static final String SIGNATURE = "Signature";

    private final AuthDataMap authMap;
    private final byte[] signature;

    /**
     * Creates a new EACAdditionalInputType.
     *
     * @param baseType DIDAuthenticationDataType
     * @throws ParserConfigurationException
     * @throws ElementParsingException
     */
    public EACAdditionalInputType(DIDAuthenticationDataType baseType) throws ParserConfigurationException, 
	    ElementParsingException {
	parseSignatureElement(baseType);
	authMap = new AuthDataMap(baseType);
	signature = authMap.getContentAsBytes(SIGNATURE);
    }

    /**
     * Returns the signature.
     *
     * @return Signature
     */
    public byte[] getSignature() {
	return signature;
    }

    /**
     * Returns EAC1OutputType.
     *
     * @return EAC1OutputType
     */
    public EAC2OutputType getOutputType() {
	return new EAC2OutputType(authMap);
    }

    /**
     * Parses the number of Signature elements.
     *
     * @param baseType The {@link DIDAuthenticationDataType} which contains the Signature elements in the Any element.
     * @throws ElementParsingException Thrown if the is more or less than 1 occurrences of the Signature element.
     */
    private void parseSignatureElement(DIDAuthenticationDataType baseType) throws ElementParsingException {
	int counter = 0;
	for (Element element : baseType.getAny()) {
	    if (element.getLocalName().equals(SIGNATURE)) {
		counter++;
		if (counter > 1) {    
		    throw new ElementParsingException(INVALID_SIGNATURE_NUMBER);
		}
	    }
	}

	if (counter == 0) {
	    throw new ElementParsingException(INVALID_SIGNATURE_NUMBER);
	}
    }

}

@@ -1119,104 +1119,8 @@
 ap;%0A
-import org.w3c.dom.Element;%0Aimport static org.openecard.binding.tctoken.ex.ErrorTranslations.*;%0A
 %0A%0A/*
@@ -1811,42 +1811,8 @@
 n %7B%0A
-%09parseSignatureElement(baseType);%0A
 %09aut
@@ -2166,24 +2166,24 @@
 putType() %7B%0A
+
 %09return new 
@@ -2218,751 +2218,6 @@
  %7D%0A%0A
-    /**%0A     * Parses the number of Signature elements.%0A     *%0A     * @param baseType The %7B@link DIDAuthenticationDataType%7D which contains the Signature elements in the Any element.%0A     * @throws ElementParsingException Thrown if the is more or less than 1 occurrences of the Signature element.%0A     */%0A    private void parseSignatureElement(DIDAuthenticationDataType baseType) throws ElementParsingException %7B%0A%09int counter = 0;%0A%09for (Element element : baseType.getAny()) %7B%0A%09    if (element.getLocalName().equals(SIGNATURE)) %7B%0A%09%09counter++;%0A%09%09if (counter %3E 1) %7B    %0A%09%09    throw new ElementParsingException(INVALID_SIGNATURE_NUMBER);%0A%09%09%7D%0A%09    %7D%0A%09%7D%0A%0A%09if (counter == 0) %7B%0A%09    throw new ElementParsingException(INVALID_SIGNATURE_NUMBER);%0A%09%7D%0A    %7D%0A%0A
 %7D%0A

Remove parsing method. The DIDAuthenticate message is now validated in ChipAuthenticationStep.