/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2014, Red Hat, Inc., and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.jboss.as.test.shared.integration.ejb.security;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.security.Permission;

import nu.xom.Document;
import nu.xom.Element;
import nu.xom.Serializer;
import org.jboss.shrinkwrap.api.asset.Asset;
import org.jboss.shrinkwrap.api.asset.StringAsset;

/**
 * @author <a href="mailto:david.lloyd@redhat.com">David M. Lloyd</a>
 */
public final class PermissionUtils {
    public static Asset createPermissionsXmlAsset(Permission... permissions) {
        final Element permissionsElement = new Element("permissions");
        for (Permission permission : permissions) {
            final Element permissionElement = new Element("permission");

            final Element classNameElement = new Element("classname");
            final Element nameElement = new Element("name");
            classNameElement.appendChild(permission.getClass().getName());
            classNameElement.appendChild(permission.getName());
            permissionElement.appendChild(classNameElement);
            permissionElement.appendChild(nameElement);

            final String actions = permission.getActions();
            if (actions != null && ! actions.isEmpty()) {
                final Element actionsElement = new Element("actions");
                actionsElement.appendChild(actions);
                permissionElement.appendChild(actionsElement);
            }
        }
        Document document = new Document(permissionsElement);
        try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
            final NiceSerializer serializer = new NiceSerializer(stream);
            serializer.setIndent(4);
            serializer.setLineSeparator("\n");
            serializer.write(document);
            serializer.flush();
            return new StringAsset(stream.toString("UTF-8"));
        } catch (IOException e) {
            throw new IllegalStateException("Generating permissions.xml failed", e);
        }
    }

    static class NiceSerializer extends Serializer {

        public NiceSerializer(OutputStream out) throws UnsupportedEncodingException {
            super(out, "UTF-8");
        }

        protected void writeXMLDeclaration() throws IOException {
            super.writeXMLDeclaration();
            super.breakLine();
        }
    }
}

@@ -1251,16 +1251,41 @@
 ssion;%0A%0A
+import nu.xom.Attribute;%0A
 import n
@@ -1296,24 +1296,24 @@
 m.Document;%0A
-
 import nu.xo
@@ -1703,32 +1703,186 @@
 %22permissions%22);%0A
+        permissionsElement.setNamespaceURI(%22http://xmlns.jcp.org/xml/ns/javaee%22);%0A        permissionsElement.addAttribute(new Attribute(%22version%22, %227%22));%0A
         for (Per
@@ -2054,16 +2054,17 @@
 t(%22class
+-
 name%22);%0A
@@ -2203,38 +2203,33 @@
 ));%0A            
-classN
+n
 ameElement.appen
@@ -2673,32 +2673,32 @@
 ctionsElement);%0A
-
             %7D%0A  
@@ -2687,32 +2687,95 @@
 ;%0A            %7D%0A
+            permissionsElement.appendChild(permissionElement);%0A
         %7D%0A      

Fix up the generated permissions.xml generator.