All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.simpleframework.xml.core.PrimitiveInlineList Maven / Gradle / Ivy

Go to download

Simple is a high performance XML serialization and configuration framework for Java

There is a newer version: 2.7.1
Show newest version
/*
 * PrimitiveInlineList.java July 2006
 *
 * Copyright (C) 2006, Niall Gallagher 
 *
 * This library 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.
 *
 * This library 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 library; if not, write to the 
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
 * Boston, MA  02111-1307  USA
 */

package org.simpleframework.xml.core;

import org.simpleframework.xml.stream.InputNode;
import org.simpleframework.xml.stream.Mode;
import org.simpleframework.xml.stream.OutputNode;
import java.util.Collection;

/**
 * The PrimitiveInlineList object is used to convert a
 * group of elements in to a collection of element entries. This is
 * used when a containing element for a list is not required. It 
 * extracts the elements by matching elements to name of the type
 * that the annotated field or method requires. This enables these
 * element entries to exist as siblings to other objects within the
 * object.  One restriction is that the Root annotation
 * for each of the types within the list must be the same.
 * 
 
 *    
 *    <entry>example one</entry>
 *    <entry>example two</entry>
 *    <entry>example three</entry>
 *    <entry>example four</entry>      
 * 
 * 
* For the above XML element list the element entry is * used to wrap the primitive string value. This wrapping XML element * is configurable and defaults to the lower case string for the name * of the class it represents. So, for example, if the primitive type * is an int the enclosing element will be called int. * * @author Niall Gallagher * * @see org.simpleframework.xml.core.Primitive * @see org.simpleframework.xml.ElementList */ class PrimitiveInlineList implements Repeater { /** * This factory is used to create a suitable collection list. */ private final CollectionFactory factory; /** * This performs the traversal used for object serialization. */ private final Primitive root; /** * This is the name that each list element is wrapped with. */ private final String parent; /** * This is the type of object that will be held in the list. */ private final Class entry; /** * Constructor for the PrimitiveInlineList object. * This is given the list type and entry type to be used. The list * type is the Collection implementation that is used * to collect the deserialized entry objects from the XML source. * * @param context this is the context object used for serialization * @param type this is the collection type for the list used * @param entry the entry type to be stored within the list * @param parent this is the name to wrap the list element with */ public PrimitiveInlineList(Context context, Class type, Class entry, String parent) { this.factory = new CollectionFactory(context, type); this.root = new Primitive(context, entry, null); this.parent = parent; this.entry = entry; } /** * This read method wll read the XML element list from * the provided node and deserialize its children as entry types. * This will deserialize each entry type as a primitive value. In * order to do this the parent string provided forms the element. * * @param node this is the XML element that is to be deserialized * * @return this returns the item to attach to the object contact */ public Object read(InputNode node) throws Exception{ Object value = factory.getInstance(); Collection list = (Collection) value; if(list != null) { return read(node, list); } return null; } /** * This read method wll read the XML element list from * the provided node and deserialize its children as entry types. * This will deserialize each entry type as a primitive value. In * order to do this the parent string provided forms the element. * * @param node this is the XML element that is to be deserialized * * @return this returns the item to attach to the object contact */ public Object read(InputNode node, Object value) throws Exception { Collection list = (Collection) value; if(list != null) { return read(node, list); } return read(node); } /** * This read method wll read the XML element list from * the provided node and deserialize its children as entry types. * This will deserialize each entry type as a primitive value. In * order to do this the parent string provided forms the element. * * @param node this is the XML element that is to be deserialized * @param list this is the collection that is to be populated * * @return this returns the item to attach to the object contact */ private Object read(InputNode node, Collection list) throws Exception { InputNode from = node.getParent(); String name = node.getName(); while(node != null) { Object item = root.read(node); if(item != null) { list.add(item); } node = from.getNext(name); } return list; } /** * This read method wll read the XML element list from * the provided node and deserialize its children as entry types. * This will deserialize each entry type as a primitive value. In * order to do this the parent string provided forms the element. * * @param node this is the XML element that is to be deserialized * * @return this returns the item to attach to the object contact */ public boolean validate(InputNode node) throws Exception{ InputNode from = node.getParent(); String name = node.getName(); while(node != null) { boolean valid = root.validate(node); if(valid == false) { return false; } node = from.getNext(name); } return true; } /** * This write method will write the specified object * to the given XML element as as list entries. Each entry within * the given list must be assignable to the given primitive type. * This will deserialize each entry type as a primitive value. In * order to do this the parent string provided forms the element. * * @param source this is the source collection to be serialized * @param node this is the XML element container to be populated */ public void write(OutputNode node, Object source) throws Exception { OutputNode parent = node.getParent(); Mode mode = node.getMode(); if(!node.isCommitted()) { node.remove(); } write(parent, source, mode); } /** * This write method will write the specified object * to the given XML element as as list entries. Each entry within * the given list must be assignable to the given primitive type. * This will deserialize each entry type as a primitive value. In * order to do this the parent string provided forms the element. * * @param node this is the parent output node to write values to * @param source this is the source collection to be serialized * @param mode this is used to determine whether to output CDATA */ public void write(OutputNode node, Object source, Mode mode) throws Exception { Collection list = (Collection) source; for(Object item : list) { if(item != null) { OutputNode child = node.getChild(parent); if(!isOverridden(child, item)) { child.setMode(mode); root.write(child, item); } } } } /** * This is used to determine whether the specified value has been * overrideen by the strategy. If the item has been overridden * then no more serialization is require for that value, this is * effectivly telling the serialization process to stop writing. * * @param node the node that a potential override is written to * @param value this is the object instance to be serialized * * @return returns true if the strategy overrides the object */ private boolean isOverridden(OutputNode node, Object value) throws Exception{ return factory.setOverride(entry, value, node); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy