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

com.github.jscancella.writer.internal.RelativePathWriter Maven / Gradle / Ivy

Go to download

This is a software library intended to support the creation, manipulation, and validation of "bags" from the bagit specification. It currently supports version 0.93 through 1.0.

There is a newer version: 5.2
Show newest version
package com.github.jscancella.writer.internal;

import java.nio.charset.Charset;
import java.nio.file.Path;
import java.util.ResourceBundle;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.jscancella.domain.Version;

/**
 * util class to format strings correctly
 */
public enum RelativePathWriter { ; // enforce singleton
  private static final Logger logger = LoggerFactory.getLogger(RelativePathWriter.class);
  private static final ResourceBundle messages = ResourceBundle.getBundle("MessageBundle");

  /**
   * format the relative path to ensure it conforms to bagit spec
   * @param path the path to format
   * @param version the version of the bag
   * @param charset the charset you which wish to use to write the path
   * @return a string formated correctly according to the bagit specification
   */
  public static String formatRelativePathString(final Path path, final Version version, final Charset charset) {
    return encodeFilename(path, version, charset).replace("\\", "/") + System.lineSeparator();
  }
  
  /**
   * as per https://github.com/jkunze/bagitspec/commit/152d42f6298b31a4916ea3f8f644ca4490494070 encode any new lines or carriage returns
   * 
   * @param path the path that you want to encode
   * @param version the version of the bag
   * @param charset the charset you which wish to use to write the path
   * @return percent encoded path
   */
  public static String encodeFilename(final Path path, final Version version, final Charset charset) {
    String encodedPath = path.toString();
    if(version.isSameOrNewer(Version.VERSION_1_0())) {
      encodedPath = encodedPath.replaceAll("%", "%25");
    }
    encodedPath = encodedPath.replaceAll("\n", "%0A").replaceAll("\r", "%0D");
    logger.debug(messages.getString("encoded_path"), path, encodedPath);
    return encodedPath;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy