@@ -662,3 +662,79 @@ set_abstract <- function(doc, text) {
662662}
663663
664664
665+ # ' Validate an EML attributeList attribute-by-attribute
666+ # '
667+ # ' The attributes passed into this function are validated one-by-one and the
668+ # ' progress of going through each attribute is printed to the screen along
669+ # ' with any and all validation issues.
670+ # '
671+ # ' This is done by, for each attribute in the list, creating a minimum valid
672+ # ' EML document and adding a new otherEntity with a new attributeList containing
673+ # ' the single attribute to be validated.
674+ # '
675+ # ' @param attributes (attributeList) An attributeList
676+ # '
677+ # ' @return (boolean) Named vector of TRUE/FALSE indicating which attributes
678+ # ' are valid
679+ # ' @export
680+ # '
681+ # ' @examples
682+ # ' \dontrun{
683+ # ' atts_df <- read.csv('attributes_table.csv', stringsAsFactors = F)
684+ # ' enum_domain <- read.csv('enumerated_domain.csv') # optional
685+ # ' attributes <- EML::set_attributes(atts_df, factor = enum_domain)
686+ # ' eml_validate_attributes(attributes)
687+ # ' }
688+ eml_validate_attributes <- function (attributes ) {
689+ stopifnot(is(attributes , " attributeList" ))
690+
691+ # Define an interal applyable function to validate each attribute
692+ eml_validate_attribute <- function (attribute ) {
693+ stopifnot(is(attribute , " attribute" ))
694+
695+ doc @ dataset @ otherEntity [[1 ]]@ attributeList @ attribute [[1 ]] <- attribute
696+
697+ # Validate!
698+ eml_validate(doc )
699+ }
700+
701+
702+ # Create a minimum valid EML doc we'll re-use each time we validate a single
703+ # attribute
704+ doc <- new(" eml" , packageId = " test" , system = " test" )
705+ doc @ dataset @ title <- c(new(" title" , .Data = " test" ))
706+ doc @ dataset @ creator <- new(" ListOfcreator" , list (eml_creator(" Test" , " test" )))
707+ doc @ dataset @ contact <- new(" ListOfcontact" , list (eml_contact(" Test" , " test" )))
708+
709+ # Create a dummy otherEntity with our attributeList
710+ entity <- new(" otherEntity" ,
711+ entityName = " name" ,
712+ entityType = " type" )
713+ entity @ attributeList <- new(" attributeList" )
714+ doc @ dataset @ otherEntity <- new(" ListOfotherEntity" , list (entity ))
715+
716+ results <- sapply(attributes @ attribute , function (attribute ) {
717+ cat(paste0(" Validating single attribute '" , attribute @ attributeName @ .Data , " ': " ))
718+
719+ result <- NULL
720+ result <- tryCatch({
721+ eml_validate_attribute(attribute )
722+ },
723+ message = function (m ) { m }
724+ )
725+
726+ if (is(result , " simpleMessage" )) {
727+ cat(" FALSE\n " )
728+ message(trimws(result $ message ))
729+ return (FALSE )
730+ } else {
731+ cat(" TRUE\n " )
732+ return (TRUE )
733+ }
734+ })
735+
736+ names(results ) <- sapply(attributes @ attribute , function (x ) x @ attributeName )
737+
738+ results
739+ }
740+
0 commit comments