예제 #1
0
        private ILiteralNode CreateLiteralNode(string cellValue, DatatypeDescription datatypeDescription, string language)
        {
            var datatypeIri = GetAnnotatedDatatypeIri(datatypeDescription);


            // C# library ignores the fragment part of the Iri so we need to also explicitly compare that.
            if (datatypeIri.Equals(DatatypeAnnotation.String.Iri) &&
                datatypeIri.Fragment.Equals(DatatypeAnnotation.String.Iri.Fragment))
            {
                if (!string.IsNullOrEmpty(language))
                {
                    // Generate language tagged literal
                    return(_rdfHandler.CreateLiteralNode(cellValue, language));
                }

                if (_suppressStringDatatype)
                {
                    // In RDF 1.1 string is the default literal datatype, so we don't need to specify it
                    // DNR doesn't handle this internally at the moment and this can cause problems with verifying test results
                    return(_rdfHandler.CreateLiteralNode(cellValue));
                }
            }

            // Generate a data-typed literal
            cellValue = NormalizeLiteral(cellValue, datatypeDescription, datatypeIri.ToString());
            return(_rdfHandler.CreateLiteralNode(cellValue, datatypeIri));
        }
예제 #2
0
        private static Uri GetAnnotatedDatatypeIri(DatatypeDescription datatypeDescription)
        {
            if (datatypeDescription == null)
            {
                return(DatatypeAnnotation.String.Iri);
            }
            if (datatypeDescription.Id != null)
            {
                return(datatypeDescription.Id);
            }
            var annotation = DatatypeAnnotation.GetAnnotationById(datatypeDescription.Base);

            if (annotation == null)
            {
                throw new ConversionError(
                          $"Could not determine the correct IRI for the datatype annotation {datatypeDescription.Base}");
            }
            return(annotation.Iri);
        }
예제 #3
0
        private static string NormalizeLiteral(string lit, DatatypeDescription datatype, string datatypeIri)
        {
            if (datatype?.Format != null)
            {
                return(datatype.Format.Normalize(lit));
            }

            // TODO: Better handling for default datatype normalization
            switch (datatypeIri)
            {
            case XmlSpecsHelper.XmlSchemaDataTypeDate:
                return(DateTime.Parse(lit).ToString(XmlSpecsHelper.XmlSchemaDateFormat));

            case XmlSpecsHelper.XmlSchemaDataTypeDateTime:
                return(DateTime.Parse(lit).ToString(XmlSpecsHelper.XmlSchemaDateTimeFormat));
                // TODO: Implement numeric type normalization
            }

            return(lit);
        }
예제 #4
0
 private bool ValidateCellValue(string cellValue, DatatypeDescription datatypeDescription, string language)
 {
     // TODO: Implement me
     return(true);
 }
예제 #5
0
        public static CellValue NormalizeCellValue(string rawValue, ColumnDescription column,
                                                   DatatypeDescription cellDatatype)
        {
            var baseDatatype = cellDatatype == null
                ? DatatypeAnnotation.String
                : DatatypeAnnotation.GetAnnotationById(cellDatatype.Base);

            if (baseDatatype == null)
            {
                throw new Converter.ConversionError($"Unrecognized cell base datatype ID: {cellDatatype.Base}");
            }
            var cellValue = new CellValue {
                RawString = rawValue
            };

            if (rawValue == null)
            {
                return(cellValue);
            }

            var normalizedString = rawValue;

            if (!RetainsLineEndings.Contains(baseDatatype))
            {
                normalizedString = normalizedString.Replace('\u000d', ' ').Replace('\u000a', ' ')
                                   .Replace('\u0009', ' ');
            }

            if (!RetainsLeadingAndTrailingWhitespace.Contains(baseDatatype))
            {
                normalizedString = normalizedString.Trim();
                normalizedString = Regex.Replace(normalizedString, @"\s+", " ");
            }

            if (normalizedString.Equals(string.Empty))
            {
                normalizedString = column.Default;
            }

            cellValue.NormalizedString = normalizedString;

            // 5. if the column separator annotation is not null, the cell value is a list of values; set the list annotation on the cell to true, and create the cell value created by:
            if (column.Separator != null)
            {
                cellValue.IsList = true;
                // 5.1 if the normalized string is the same as any one of the values of the column null annotation, then the resulting value is null.
                if (column.Null.Contains(normalizedString) || normalizedString == null)
                {
                    cellValue.ValueList = null;
                }
                else
                {
                    cellValue.ValueList = new List <string>();
                    // 5.2 split the normalized string at the character specified by the column separator annotation.
                    foreach (var tok in normalizedString.Split(new[] { column.Separator }, StringSplitOptions.None))
                    {
                        // 5.3 unless the datatype base is string or anyAtomicType, strip leading and trailing whitespace from these strings.
                        var normalizedToken = tok;
                        if (!RetainsLeadingAndTrailingWhitespaceInList.Contains(baseDatatype))
                        {
                            normalizedToken = normalizedToken.Trim();
                        }
                        // 5.4 applying the remaining steps to each of the strings in turn.
                        NormalizeCellValue(cellValue, normalizedToken, column, cellDatatype);
                    }
                }
            }
            else
            {
                NormalizeCellValue(cellValue, normalizedString, column, cellDatatype);
            }

            return(cellValue);
        }
예제 #6
0
        private static void NormalizeCellValue(CellValue cellValue, string str, ColumnDescription column, DatatypeDescription datatype)
        {
            // 6. if the string is an empty string, apply the remaining steps to the string given by the column default annotation.
            if (string.Empty.Equals(str))
            {
                str = column.Default;
            }

            // 7. if the string is the same as any one of the values of the column null annotation, then the resulting value is null.
            // If the column separator annotation is null and the column required annotation is true, add an error to the list of errors for the cell.
            if (column.Null.Contains(str))
            {
                str = null;
                if (column.Separator == null && column.Required)
                {
                    cellValue.Errors.Add("Got NULL value for a required cell");
                }
            }

            /* Still TODO:
             * 8. parse the string using the datatype format if one is specified, as described below to give a value with an associated datatype. If the datatype base is string, or there is no datatype, the value has an associated language from the column lang annotation. If there are any errors, add them to the list of errors for the cell; in this case the value has a datatype of string; if the datatype base is string, or there is no datatype, the value has an associated language from the column lang annotation.
             * 9. validate the value based on the length constraints described in section 4.6.1 Length Constraints, the value constraints described in section 4.6.2 Value Constraints and the datatype format annotation if one is specified, as described below. If there are any errors, add them to the list of errors for the cell.
             */
            if (cellValue.IsList)
            {
                cellValue.ValueList.Add(str);
            }
            else
            {
                cellValue.Value = str;
            }
        }