ArraySerializer.java :  » J2EE » Pustefix » de » schlund » pfixcore » oxm » impl » serializers » Java Open Source

Java Open Source » J2EE » Pustefix 
Pustefix » de » schlund » pfixcore » oxm » impl » serializers » ArraySerializer.java
/*
 * This file is part of PFIXCORE.
 *
 * PFIXCORE 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 of the License, or
 * (at your option) any later version.
 *
 * PFIXCORE 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 PFIXCORE; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
package de.schlund.pfixcore.oxm.impl.serializers;

import java.lang.reflect.Array;

import de.schlund.pfixcore.oxm.impl.ComplexTypeSerializer;
import de.schlund.pfixcore.oxm.impl.SerializationContext;
import de.schlund.pfixcore.oxm.impl.SerializationException;
import de.schlund.pfixcore.oxm.impl.XMLWriter;

/**
 * @author mleidig@schlund.de
 */
public class ArraySerializer implements ComplexTypeSerializer {

    public void serialize(Object obj, SerializationContext context, XMLWriter writer) throws SerializationException {
        if(obj.getClass().isArray()) {
            int len = Array.getLength(obj);
            for (int i = 0 ; i<len; i++) {
                Object item = Array.get(obj,i);
                String elementName = context.mapClassName(item);
                writer.writeStartElement(elementName);
                if(item != null) {
                    context.serialize(item,writer);
                }
                writer.writeEndElement();
            }
        } else throw new SerializationException("Illegal type: "+obj.getClass().getName());
    } 
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.