Package com.skaringa.javaxml.serializers

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

package com.skaringa.javaxml.serializers;

import java.io.PrintStream;
import java.util.HashMap;
import java.util.Locale;
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.DocumentOutputHandlerInterface;

/**
* Implementation of ComponentSerializer for java.util.Locale (which
* doesn't have a no-arg constructor).
*/
public final class LocaleSerializer extends AbstractSerializer {
  private ObjectSerializer _delegate;

  private static final String LANGUAGE_FIELD_NAME = "language";
  private static final String COUNTRY_FIELD_NAME = "country";
  private static final String VARIANT_FIELD_NAME = "variant";

  /**
   * Construct an LocaleSerializer for a concrete type.
   * @param xmlTypeName The XML type name.
   */
  LocaleSerializer(String xmlTypeName) {
    _delegate = new ObjectSerializer(xmlTypeName);
  }

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

    Object obj = null;
    String nullAttr = attrs.getValue("xsi:nil");
    if (nullAttr == null || nullAttr.equals("false")) {
      obj = new HashMap();
      // defer creation of correct value until endDeserialize()
    }
    return obj;
  }

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

    if (obj != null) {
      HashMap members = (HashMap) obj;
      if (members.containsKey(VARIANT_FIELD_NAME)) {
        return new Locale(
          (String) members.get(LANGUAGE_FIELD_NAME),
          (String) members.get(COUNTRY_FIELD_NAME),
          (String) members.get(VARIANT_FIELD_NAME));
      }
      else if (members.containsKey(COUNTRY_FIELD_NAME)) {
        return new Locale(
          (String) members.get(LANGUAGE_FIELD_NAME),
          (String) members.get(COUNTRY_FIELD_NAME));
      }
//      else if (members.containsKey(LANGUAGE_FIELD_NAME)) {
//        return new Locale((String) members.get(LANGUAGE_FIELD_NAME));
//      }
// keep 1.3 compatible!     
      throw new DeserializerException(
        "Locale have no language and country fields - can't deserialize.");
    }

    return obj;
  }

  /**
   * @see com.skaringa.javaxml.serializers.ComponentSerializer#addUsedClasses(java.lang.Class, java.util.Set)
   */
  public void addUsedClasses(Class base, Set usedClasses)
    throws SerializerException {
    _delegate.addUsedClasses(base, usedClasses);
  }

  /**
   * @see com.skaringa.javaxml.serializers.ComponentSerializer#getXMLTypeName()
   */
  public String getXMLTypeName() {
    return _delegate.getXMLTypeName();
  }

  /**
   * @see com.skaringa.javaxml.serializers.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 {
    _delegate.serialize(obj, type, name, propertyMap, objectIdMap, output);
  }

  /**
   * @see com.skaringa.javaxml.serializers.ComponentSerializer#setMember(java.lang.Object, java.lang.String, java.lang.Object)
   */
  public void setMember(Object parent, String name, Object value)
    throws DeserializerException {
    ((HashMap) parent).put(name, value);
  }

  /**
   * @see com.skaringa.javaxml.serializers.ComponentSerializer#writeXMLTypeDefinition(Class, Map, DocumentOutputHandlerInterface)
   */
  public void writeXMLTypeDefinition(
    Class type,
    Map propertyMap,
    DocumentOutputHandlerInterface output)
    throws SerializerException {
    _delegate.writeXMLTypeDefinition(type, propertyMap, output);
  }

  /**
   * @see CollectionSerializer#toJson(Object, Class, Map, PrintStream)
   */
  public void toJson(Object obj, Class type, Map propertyMap,
      PrintStream output) throws SerializerException {
    _delegate.toJson(obj, type, propertyMap, output);
  }

}
TOP

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

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.