/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2013, 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.clustering.controller;

import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

import org.jboss.as.controller.AttributeDefinition;
import org.jboss.as.controller.AttributeMarshaller;
import org.jboss.dmr.ModelNode;
import org.jboss.dmr.Property;

/**
 * {@link AttributeMarshaller} utilities.
 * @author Paul Ferraro
 */
public class AttributeMarshallerFactory {

    public static AttributeMarshaller createSimpleListAttributeMarshaller() {
        return new AttributeMarshaller() {
            @Override
            public void marshallAsElement(AttributeDefinition attribute, ModelNode resourceModel, boolean marshallDefault, XMLStreamWriter writer) throws XMLStreamException {
                // Hack to workaround the fact that SimpleListAttributeDefinition lacks a marshallAsAttribute(...) method!
                this.marshallAsAttribute(attribute, resourceModel, marshallDefault, writer);
            }

            @Override
            public void marshallAsAttribute(AttributeDefinition attribute, ModelNode resourceModel, boolean marshallDefault, XMLStreamWriter writer) throws XMLStreamException {
                ModelNode aliases = resourceModel.get(attribute.getName());
                if (aliases.isDefined()) {
                    StringBuilder builder = new StringBuilder();
                    for (ModelNode alias : aliases.asList()) {
                        if (builder.length() > 0) {
                            builder.append(' ');
                        }
                        builder.append(alias.asString());
                    }
                    writer.writeAttribute(attribute.getXmlName(), builder.toString());
                }
            }
        };
    }

    public static AttributeMarshaller createPropertyListAttributeMarshaller() {
        return new AttributeMarshaller() {
            @Override
            public void marshallAsElement(AttributeDefinition attribute, ModelNode resourceModel, boolean marshallDefault, XMLStreamWriter writer) throws XMLStreamException {
                ModelNode properties = resourceModel.get(attribute.getName());
                if (properties.isDefined()) {
                    for (Property property : properties.asPropertyList()) {
                        writer.writeStartElement(org.jboss.as.controller.parsing.Element.PROPERTY.getLocalName());
                        writer.writeAttribute(org.jboss.as.controller.parsing.Element.NAME.getLocalName(), property.getName());
                        writer.writeCharacters(property.getValue().asString());
                        writer.writeEndElement();
                    }
                }
            }
        };
    }

    private AttributeMarshallerFactory() {
        // Hide
    }
}
