示例#1
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);
        }
示例#2
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);
        }