/*
 * Copyright (c) 2015, the IRMA Team
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 *
 *  Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 *
 *  Neither the name of the IRMA project nor the names of its
 *   contributors may be used to endorse or promote products derived from
 *   this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.irmacard.credentials.info;

import org.w3c.dom.Document;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.Serializable;
import java.io.StringWriter;

public class SchemeManager extends ConfigurationParser implements Serializable {
	private static final long serialVersionUID = -6892307557305396448L;

	private transient Document d;
	private String name;
	private String url;
	private String hrName;
	private String description;
	private String contact;
	private String keyshareServer = "";

	public SchemeManager(InputStream stream) throws InfoException {
		super();
		Document d = parse(stream);
		init(d);
	}

	public SchemeManager(String xml) throws InfoException {
		this(new ByteArrayInputStream(xml.getBytes()));
	}

	private void init(Document d) throws InfoException {
		this.d = d;
		name = getFirstTagText(d, "Id");
		url = getFirstTagText(d, "Url");

		if (getSchemaVersion() >= 3) {
			hrName = getFirstTagText(d, "Name");
			description = getFirstTagText(d, "Description");
			contact = getFirstTagText(d, "Contact");
		} else {
			hrName = name;
			description = "";
			contact = "";
		}

		if (getSchemaVersion() >= 4) {
			keyshareServer = getFirstTagText(d, "KeyshareServer");
		}
	}

	public String getName() {
		return name;
	}

	public String getUrl() {
		return url;
	}

	@SuppressWarnings("unused")
	public String getHumanReadableName() {
		return hrName;
	}

	@SuppressWarnings("unused")
	public String getDescription() {
		return description;
	}

	@SuppressWarnings("unused")
	public String getContactInfo() {
		return contact;
	}

	public String getKeyshareServer() {
		return keyshareServer;
	}

	public boolean hasKeyshareServer() {
		return keyshareServer != null && keyshareServer.length() > 0;
	}

	public void setUrl(String url) {
		this.url = url;
		d.getElementsByTagName("Url").item(0).setTextContent(url);
	}

	public String getXml() {
		try {
			Transformer trans = TransformerFactory.newInstance().newTransformer();
			trans.setOutputProperty(OutputKeys.METHOD, "xml");
			trans.setOutputProperty(OutputKeys.INDENT, "yes");
			trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", Integer.toString(4));

			StringWriter sw = new StringWriter();
			StreamResult result = new StreamResult(sw);
			DOMSource source = new DOMSource(d.getDocumentElement());

			trans.transform(source, result);
			return sw.toString();
		} catch (TransformerException e) {
			throw new RuntimeException(e);
		}
	}
}

@@ -2359,16 +2359,54 @@
 er = %22%22;
+%0A%09private String keyshareWebsite = %22%22;
 %0A%0A%09publi
@@ -3106,16 +3106,161 @@
 %22);%0A%09%09%7D%0A
+%09%09if (getSchemaVersion() %3E= 5) %7B%0A%09%09%09keyshareWebsite = getFirstTagText(d, %22KeyshareWebsite%22);%0A%09%09%7D else %7B%0A%09%09%09keyshareWebsite = keyshareServer;%0A%09%09%7D%0A
 %09%7D%0A%0A%09pub
@@ -3671,24 +3671,92 @@
 Server;%0A%09%7D%0A%0A
+%09public String getKeyshareWebsite() %7B%0A%09%09return keyshareWebsite;%0A%09%7D%0A%0A
 %09public bool

Add keyshare website to SchemeManager and xml files