Package com.skaringa.javaxml.serializers

Source Code of com.skaringa.javaxml.serializers.MapEntrySerializer

package com.skaringa.javaxml.serializers;

import java.io.PrintStream;
import java.util.Map;
import java.util.Set;
import java.util.Stack;

import org.xml.sax.Attributes;

import com.skaringa.javaxml.DeserializerException;
import com.skaringa.javaxml.SerializerException;
import com.skaringa.javaxml.handler.AttrImpl;
import com.skaringa.javaxml.handler.DocumentOutputHandlerInterface;

/**
* Implementation of ComponentSerializer for Map.Entry
*/
public final class MapEntrySerializer extends AbstractSerializer {

  /**
   * @see ComponentSerializer#serialize(Object, Class, String, Map, Map, DocumentOutputHandlerInterface)
   */
  public void serialize(
    Object obj,
    Class type,
    String name,
    Map propertyMap,
    Map objectIdMap,
    DocumentOutputHandlerInterface output)
    throws SerializerException {

    java.util.Map.Entry comp;
    try {
      comp = (java.util.Map.Entry) obj;
    }
    catch (ClassCastException e) {
      throw new SerializerException(
        "wrong context: obj needs to be of type java.util.Map.Entry, but is: "
          + obj.getClass().getName());
    }

    startElement(obj, getXMLTypeName(), name, propertyMap, output);

    if (null != comp) {
      Object key = comp.getKey();
      Object value = comp.getValue();

      Class clazz = Object.class;
      if (null != key) {
        clazz = key.getClass();
      }
      ComponentSerializer ser =
        SerializerRegistry.getInstance().getSerializer(clazz);
      ser.serialize(key, clazz, "key", propertyMap, objectIdMap, output);

      clazz = Object.class;
      if (null != value) {
        clazz = value.getClass();
      }
      ser = SerializerRegistry.getInstance().getSerializer(clazz);
      ser.serialize(value, clazz, "value", propertyMap, objectIdMap, output);
    }

    output.endElement(name);
  }

  /**
   * @see ComponentSerializer#startDeserialize(String, Attributes, Object, Stack, ClassLoader)
   */
  public Object startDeserialize(
    String name,
    Attributes attrs,
    Object parent,
    Stack objStack,
    ClassLoader classLoader)
    throws DeserializerException {

    String nullAttr = attrs.getValue("xsi:nil");
    if (nullAttr == null || nullAttr.equals("false")) {
      return new MapEntryHelper();
    }
    return null;
  }

  /**
   * @see ComponentSerializer#endDeserialize(Object, String)
   */
  public Object endDeserialize(Object obj, String text)
    throws DeserializerException {

    return obj;
  }

  /**
   * @see ComponentSerializer#setMember(Object, String, Object)
   */
  public void setMember(Object parent, String name, Object value)
    throws DeserializerException {

    try {

      MapEntryHelper mapHelper = (MapEntryHelper) parent;

      if (name.equals("key")) {
        mapHelper.setKey(value);
      }
      else if (name.equals("value")) {
        mapHelper.setValue(value);
      }
      else {
        throw new DeserializerException(
          "element key or value expected as child of mapentry");
      }
    }
    catch (ClassCastException e) {
      throw new DeserializerException(
        "invalid sequence: Map.Entry expected, but was: "
          + parent.getClass().getName());
    }

  }

  /**
   * @see ComponentSerializer#getXMLTypeName()
   */
  public String getXMLTypeName() {
    return "MapEntry";
  }

  /**
   * @see ComponentSerializer#writeXMLTypeDefinition(Class, Map, DocumentOutputHandlerInterface)
   */
  public void writeXMLTypeDefinition(
    Class type,
    Map propertyMap,
    DocumentOutputHandlerInterface output)
    throws SerializerException {

    AttrImpl attrs = new AttrImpl();
    attrs.addAttribute("name", getXMLTypeName());
    output.startElement("xsd:complexType", attrs);

    output.startElement("xsd:sequence");

    attrs = new AttrImpl();
    attrs.addAttribute("name", "key");
    attrs.addAttribute("type", "xsd:anyType");
    attrs.addAttribute("nillable", "true");
    output.startElement("xsd:element", attrs);
    output.endElement("xsd:element");

    attrs = new AttrImpl();
    attrs.addAttribute("name", "value");
    attrs.addAttribute("type", "xsd:anyType");
    attrs.addAttribute("nillable", "true");
    output.startElement("xsd:element", attrs);
    output.endElement("xsd:element");

    output.endElement("xsd:sequence");

    output.endElement("xsd:complexType");
  }

  /**
   * @see ComponentSerializer#addUsedClasses(Class, Set)
   */
  public void addUsedClasses(Class base, Set usedClasses)
    throws SerializerException {
    // no other classes used
  }

  /**
   * @see CollectionSerializer#toJson(Object, Class, Map, PrintStream)
   */
  public void toJson(Object obj, Class type, Map propertyMap,
      PrintStream output) throws SerializerException {
    java.util.Map.Entry comp;
    try {
      comp = (java.util.Map.Entry) obj;
    }
    catch (ClassCastException e) {
      throw new SerializerException(
        "wrong context: obj needs to be of type java.util.Map.Entry, but is: "
          + obj.getClass().getName());
    }

    if (null != comp) {
      Object key = comp.getKey();
      Object value = comp.getValue();

      if (null != key) {
        output.print("\"");
        printEncodedStr(key.toString(), output);
        output.print("\":");

        Class clazz = Object.class;
        if (null != value) {
          clazz = value.getClass();
          ComponentSerializer ser = SerializerRegistry.getInstance().getSerializer(clazz);
          ser.toJson(value, clazz, propertyMap, output);
        } else {
          output.print("null");
        }
      }
    }
  }
 

}
TOP

Related Classes of com.skaringa.javaxml.serializers.MapEntrySerializer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.