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

com.github.jscancella.conformance.profile.deserialize.BagInfoNodeDeserializer 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.conformance.profile.deserialize;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ResourceBundle;
import java.util.Map.Entry;

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

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.NullNode;
import com.github.jscancella.conformance.profile.BagInfoRequirement;
import com.github.jscancella.conformance.profile.BagitProfileBuilder;

/**
 * responsible for deserializing "Bag-Info" node from bagit profile
 */
public enum BagInfoNodeDeserializer {
  ;// using enum to ensure singleton
  private static final ResourceBundle messages = ResourceBundle.getBundle("MessageBundle");
  private static final Logger logger = LoggerFactory.getLogger(BagInfoNodeDeserializer.class);
  
  /**
   * @param rootNode the json node to parse
   * @param builder the bagit profile builder
   */
  @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
  public static void parseBagInfo(final JsonNode rootNode, final BagitProfileBuilder builder){
    final JsonNode bagInfoNode = rootNode.get("Bag-Info");
    if(bagInfoNode != null && !(bagInfoNode instanceof NullNode)) {
      logger.debug(messages.getString("parsing_bag_info"));
      
      final Iterator> nodes = bagInfoNode.fields(); //stuck in java 6...
      
      while(nodes.hasNext()){
        final Entry node = nodes.next();
        final boolean isRequired = determineIsRequired(node);
        final boolean isRepeatable = determineIsRepeatable(node);
        final List acceptableValues = determineAcceptableValues(node);
        final String description = determineDescription(node);
        
        final BagInfoRequirement entry = new BagInfoRequirement(isRequired, acceptableValues, isRepeatable, description);
        
        logger.debug(messages.getString("parsed_key_value"), node.getKey(), entry);
        builder.addBagInfoRequirement(node.getKey(), entry);
      }
    }
  }
  
  private static boolean determineIsRequired(final Entry node) {
    final JsonNode requiredNode = node.getValue().get("required");
    boolean isRequired = false;
    
    if (requiredNode != null) {
      isRequired = requiredNode.asBoolean();
    }
    
    return isRequired;
  }
  
  private static boolean determineIsRepeatable(final Entry node) {
    final JsonNode repeatableNode = node.getValue().get("repeatable");
    boolean isRepeatable = true;
    
    if (repeatableNode != null) {
      isRepeatable = repeatableNode.asBoolean();
    }
    
    return isRepeatable;
  }
  
  private static List determineAcceptableValues(final Entry node) {
    final JsonNode valuesNode = node.getValue().get("values");
    final List acceptableValues = new ArrayList<>();
    
    if(valuesNode != null){
      for(final JsonNode value : valuesNode){
        acceptableValues.add(value.asText());
      }
    }
    
    return acceptableValues;
  }
  
  private static String determineDescription(final Entry node) {
    final JsonNode descriptionNode = node.getValue().get("description");
    String description = "";
    
    if (descriptionNode != null) {
      description = descriptionNode.asText();
    }
    
    return description;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy