public static string ToLocalSchemas(this string xml, string xsdFolder, QtiVersion newVersion)
        {
            var itemXsdLocation     = GetBaseSchemaLocation(QtiResourceType.AssessmentItem, newVersion);
            var manifestXsdLocation = GetBaseSchemaLocation(QtiResourceType.Manifest, newVersion);

            xml = xml.Replace($"{itemXsdLocation}", $"{xsdFolder}/{Path.GetFileName(itemXsdLocation)}");
            xml = xml.Replace($"{manifestXsdLocation}", $"{xsdFolder}/{Path.GetFileName(manifestXsdLocation)}");
            return(xml);
        }
Beispiel #2
0
 protected BaseConverter(DirectoryInfo extractedPackageLocation,
                         QtiVersion currentVersion,
                         Func <string, string> beforeItem,
                         Func <string, string> beforeTest,
                         Func <string, string> beforeManifest)
 {
     Version        = currentVersion;
     _packageFolder = extractedPackageLocation;
     Init(beforeItem, beforeTest, beforeManifest);
 }
        public static bool Validate(this XDocument xDoc, QtiVersion version)
        {
            var result = true;

            // Set the validation settings.
            using var sr = new StringReader(xDoc.ToString());
            var reader = version == QtiVersion.Qti22
                ?
                         XmlReader.Create(sr, XsdHelper.GetXmlReaderSettingsQti22(ValidationEventHandler))
                : version == QtiVersion.Qti30
                    ? XmlReader.Create(sr, XsdHelper.GetXmlReaderSettingsQti30(ValidationEventHandler))
                    :
                         XmlReader.Create(sr, XsdHelper.GetXmlReaderSettingsQti21(ValidationEventHandler));

            void ValidationEventHandler(object sender, ValidationEventArgs e)
            {
                result = false;
                var orgColor = Console.ForegroundColor;
                var type     = e.Severity;

                if (type == XmlSeverityType.Warning)
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine(e.Message);
                    Console.ForegroundColor = orgColor;
                }
                if (type == XmlSeverityType.Error)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(e.Message);
                    Console.ForegroundColor = orgColor;
                }
            }

            // Parse the file.
            while (reader.Read())
            {
                ;
            }
            return(result);
        }
        public static void AddLocalSchemasToPackage(this Manifest manifest, string packageLocation, QtiVersion newVersion)
        {
            var controlResources = manifest
                                   .FindElementsByElementAndAttributeStartValue("resource", "type", "controlfile/xmlv1p0")
                                   .FirstOrDefault();

            if (controlResources == null)
            {
                var resources = manifest.FindElementByName("resources");
                controlResources = XElement.Parse("<resource identifier=\"I_00001_CF\" type=\"controlfile/xmlv1p0\"></resource>");
                resources?.Add(controlResources);
            }
            var xsdFolder = Path.Combine(packageLocation, "controlxsds");

            if (!Directory.Exists(xsdFolder))
            {
                Directory.CreateDirectory(xsdFolder);
            }
            var applicationPath = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
            var version         = newVersion == QtiVersion.Qti21 ? "21" : newVersion == QtiVersion.Qti22 ? "22" : "30";
            var schemaLocation  = $"Xsds/schema_qti{version}.zip";

            ZipFile.ExtractToDirectory(Path.Combine(applicationPath, schemaLocation), xsdFolder, Encoding.Default, true);
            Directory.GetFiles(xsdFolder, "*.xsd").ToList().ForEach(xsdFile =>
            {
                controlResources?.Add(XElement.Parse($"<file href=\"controlxsds/{Path.GetFileName(xsdFile)}\" />"));
            });
        }
        public static string ReplaceSchemas(this string xml, QtiResourceType resourceType, QtiVersion newVersion, QtiVersion oldVersion, bool localSchema)
        {
            var tagName = resourceType == QtiResourceType.AssessmentItem ? "assessmentItem" :
                          resourceType == QtiResourceType.AssessmentTest ? "assessmentTest" :
                          "manifest";
            var baseScheme         = GetBaseSchema(resourceType, newVersion);
            var baseSchemeLocation = GetBaseSchemaLocation(resourceType, newVersion);
            var qtiParentTag       = Regex.Match(xml, $"<{tagName}(.*?)>").Value;
            var qtiParentOrg       = qtiParentTag;
            var schemaPrefix       = Regex.Match(qtiParentTag, " (.*?)schemaLocation")
                                     .Value.Replace(":schemaLocation", "").Trim();

            schemaPrefix = schemaPrefix.Substring(schemaPrefix.LastIndexOf(" ", StringComparison.Ordinal), schemaPrefix.Length - schemaPrefix.LastIndexOf(" ", StringComparison.Ordinal)).Trim();

            var schemaLocations  = Regex.Match(qtiParentTag, @$ "{schemaPrefix}:schemaLocation=" "(.*?)" "").Value;
            var extensionSchemas = RemoveSchemaFromLocation(schemaLocations, GetBaseSchema(resourceType, oldVersion));

            if (resourceType == QtiResourceType.Manifest)
            {
                extensionSchemas = RemoveSchemaFromLocation(extensionSchemas, GetBaseSchema(QtiResourceType.AssessmentItem, oldVersion));
                extensionSchemas = extensionSchemas +
                                   $" {GetBaseSchema(QtiResourceType.AssessmentItem, newVersion)} {GetBaseSchemaLocation(QtiResourceType.AssessmentItem, newVersion)}";
            }

            qtiParentTag = Regex.Replace(qtiParentTag, @$ "{schemaPrefix}:schemaLocation=" "(.*?)" "", "");
            qtiParentTag = Regex.Replace(qtiParentTag, @$ "xmlns:{schemaPrefix}=" "(.*?)" "", "");
            qtiParentTag = Regex.Replace(qtiParentTag, @$ "xmlns:xsi=" "(.*?)" "", "");
            qtiParentTag = Regex.Replace(qtiParentTag, @"xmlns=""(.*?)""", "");

            if (localSchema)
            {
                var prefix = resourceType == QtiResourceType.Manifest ? "/" : "../";
                baseSchemeLocation = $"{prefix}controlxsds/{Path.GetFileName(baseSchemeLocation)}";
            }
            var schemaLocation = $"xsi:schemaLocation=\"{baseScheme}  {baseSchemeLocation} ";

            qtiParentTag = qtiParentTag.Replace($"{tagName} ",
                                                $@"{tagName} xmlns=""{baseScheme}"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" " +
                                                schemaLocation + "  " + extensionSchemas + @""" "
                                                );

            xml = xml.Replace(qtiParentOrg, qtiParentTag);
            if (schemaPrefix != "xsi")
            {
                xml = xml.Replace($":{schemaPrefix}", ":xsi");
                xml = xml.Replace($"{schemaPrefix}:", "xsi:");
            }
            return(xml);
        }
 public static string GetBaseSchemaLocation(QtiResourceType resourceType, QtiVersion version)
 => XsdHelper.BaseSchemaLocations[$"{resourceType.ToString()}-{version.ToString()}"];
 private static string GetBaseSchema(QtiResourceType resourceType, QtiVersion version)
 => XsdHelper.BaseSchemas[$"{resourceType}-{version}"];
Beispiel #8
0
 public Manifest(DirectoryInfo packageLocation, QtiVersion version, Func <string, string> beforeManifest) :
     base(Parse(beforeManifest(File.ReadAllText(Path.Combine(packageLocation.FullName, "imsmanifest.xml")))))
 {
     _packageLocation = packageLocation;
     _version         = version;
 }
Beispiel #9
0
 protected BaseConverter(DirectoryInfo extractedPackageLocation, QtiVersion currentVersion) :  this(extractedPackageLocation, currentVersion, x => x, x => x, x => x)
 {
 }
        public Qti30Converter(DirectoryInfo extractedPackageLocation, bool localSchema, QtiVersion fromVersion) : base(
                extractedPackageLocation, xml => xml
                .ReplaceRunsTabsAndLineBraks()
                .ReplaceSchemas(QtiResourceType.AssessmentItem, QtiVersion.Qti30, fromVersion, localSchema),

                testXml => testXml
                .ReplaceRunsTabsAndLineBraks()
                .ReplaceSchemas(QtiResourceType.AssessmentTest, QtiVersion.Qti30, fromVersion, localSchema),
                manifestXml => manifestXml
                .ReplaceRunsTabsAndLineBraks()
                .ReplaceSchemas(QtiResourceType.Manifest, QtiVersion.Qti30, fromVersion, localSchema)
                )
        {
        }