Ejemplo n.º 1
0
        // Load a single file
        private DitaFile LoadFile(string filePath)
        {
            // Look for known extensions
            // Is this an image?
            if (Path.HasExtension(filePath))
            {
                string extension = Path.GetExtension(filePath)?.ToLower();
                if (DitaFileImage.Extensions.Contains(extension))
                {
                    DitaFileImage image = new DitaFileImage(filePath);
                    Trace.TraceInformation($"{Path.GetFileName(filePath)} is a {typeof(DitaFileImage)}");
                    return(image);
                }
            }

            // Try to load the given file

            try {
                // Try to load as an XML document
                XmlDocument xmlDocument = DitaFile.LoadAndCheckType(filePath, out Type fileType);

                // Create a new object of the correct type
                if (DitaFile.DitaFileTypeCreation.ContainsKey(fileType))
                {
                    return(DitaFile.DitaFileTypeCreation[fileType](xmlDocument, filePath));
                }
            }
            catch (Exception ex) {
                Trace.TraceWarning($"Unable to load {filePath} as XML: {ex}");
            }

            throw new Exception($"{filePath} is an unknown file type.");
        }