Package com.cedarsoft.file.io

Source Code of com.cedarsoft.file.io.FileNameSerializer

package com.cedarsoft.file.io;

import com.cedarsoft.Version;
import com.cedarsoft.VersionRange;
import com.cedarsoft.file.BaseName;
import com.cedarsoft.file.Extension;
import com.cedarsoft.file.ExtensionSerializer;
import com.cedarsoft.file.FileName;
import com.cedarsoft.serialization.stax.AbstractStaxMateSerializer;
import com.google.inject.Inject;
import org.codehaus.staxmate.out.SMOutputElement;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

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

/**
* Serializer for file names
*/
public class FileNameSerializer extends AbstractStaxMateSerializer<FileName, Object> {
  @NotNull
  @NonNls
  public static final String ELEMENT_EXTENSION = "extension";
  @NotNull
  @NonNls
  public static final String ELEMENT_BASE_NAME = "baseName";

  @NotNull
  private final ExtensionSerializer extensionSerializer;
  @NotNull
  private final BaseNameSerializer baseNameSerializer;

  @Inject
  public FileNameSerializer( @NotNull BaseNameSerializer baseNameSerializer, @NotNull ExtensionSerializer extensionSerializer ) {
    super( "fileName", new VersionRange( new Version( 1, 0, 0 ), new Version( 1, 0, 0 ) ) );
    this.extensionSerializer = extensionSerializer;
    this.baseNameSerializer = baseNameSerializer;

    verifyDelegatingSerializerVersion( extensionSerializer, new Version( 1, 0, 0 ) );
    verifyDelegatingSerializerVersion( baseNameSerializer, new Version( 1, 0, 0 ) );
  }

  @NotNull
  @Override
  public SMOutputElement serialize( @NotNull SMOutputElement serializeTo, @NotNull FileName object, @Nullable Object context ) throws IOException, XMLStreamException {
    baseNameSerializer.serialize( serializeTo.addElement( ELEMENT_BASE_NAME ), object.getBaseName(), context );
    extensionSerializer.serialize( serializeTo.addElement( ELEMENT_EXTENSION ), object.getExtension(), context );
    return serializeTo;
  }

  @NotNull
  @Override
  public FileName deserialize( @NotNull XMLStreamReader deserializeFrom, @Nullable Object context ) throws IOException, XMLStreamException {
    nextTag( deserializeFrom, ELEMENT_BASE_NAME );
    BaseName baseName = baseNameSerializer.deserialize( deserializeFrom, context );

    nextTag( deserializeFrom, ELEMENT_EXTENSION );
    Extension extension = extensionSerializer.deserialize( deserializeFrom, context );

    closeTag( deserializeFrom );

    return new FileName( baseName, extension );
  }
}
TOP

Related Classes of com.cedarsoft.file.io.FileNameSerializer

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.