Package com.cedarsoft.serialization

Source Code of com.cedarsoft.serialization.HashSerializer

package com.cedarsoft.serialization;

import com.cedarsoft.Version;
import com.cedarsoft.VersionRange;
import com.cedarsoft.serialization.stax.AbstractStaxMateSerializer;
import com.cedarsoft.crypt.Algorithm;
import com.cedarsoft.crypt.Hash;
import org.codehaus.staxmate.out.SMOutputElement;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;

import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import java.io.IOException;

/**
*
*/
public class HashSerializer extends AbstractStaxMateSerializer<Hash> {
  @NotNull
  @NonNls
  private static final String ATTRIBUTE_ALGORITHM = "algorithm";

  public HashSerializer() {
    super( "hash", new VersionRange( new Version( 1, 0, 0 ), new Version( 1, 0, 0 ) ) );
  }

  @Override
  public void serialize( @NotNull SMOutputElement serializeTo, @NotNull Hash object ) throws IOException, XMLStreamException {
    serializeTo.addAttribute( ATTRIBUTE_ALGORITHM, object.getAlgorithm().name() );
    serializeTo.addCharacters( object.getValueAsHex() );
  }

  @NotNull
  @Override
  public Hash deserialize( @NotNull XMLStreamReader deserializeFrom, @NotNull Version formatVersion ) throws IOException, XMLStreamException {
    String algorithm = deserializeFrom.getAttributeValue( null, ATTRIBUTE_ALGORITHM );
    String valueAsHex = getText( deserializeFrom );

    return Hash.fromHex( Algorithm.valueOf( algorithm ), valueAsHex );
  }
}
TOP

Related Classes of com.cedarsoft.serialization.HashSerializer

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.