Esempio n. 1
0
        private static void TestDocument(string file)
        {
            ContentInfoBuilder builder = new ContentInfoBuilder();

            builder.XmlFile         = file;
            builder.StartingElement = "body";
            TableOfContentResult result = builder.MakeTableOfContent();

            //GenerateTocXml(result);
            SaveToc(result, Path.Combine(@"V:\Projekty\BitBucket\itjakub\UJCSystem\ITJakub.Xml.Conversion\Ujc.Ovj.Ooxml.Conversion.Test\Data\Metadata",
                                         file.Substring(file.LastIndexOf('\\') + 1).Replace(".xml", ".cnt")));

            //GenerateTocText(result);
        }
Esempio n. 2
0
        public ConversionResult Convert(DocxToTeiConverterSettings settings)
        {
            ConverterSettings = settings;
            CheckIfDirectoryPathsExists(ConverterSettings);
            _result = new ConversionResult();
            string documentType = null;

            //get metadata only for first (by alphabet) uploaded file
            var inputFileName = ConverterSettings.InputFilesPath.Select(filePath => new FileInfo(filePath)).Select(fileInfo => fileInfo.Name).First();

            ResolveDefaultSettingsValues(ConverterSettings);

            var prepis = GetPrepisy(ConverterSettings, inputFileName);

            if (prepis == null)
            {
                //dokument v evidenci neexistuje, nabídnout zanesení dokumentu do evidence
                // mělo by stačit přiřazení typu dokumentu
                _result.Errors.Add(new DocumentNotInEvidenceException(String.Format("Dokument s uvedeným jménem souboru '{0}' neexistuje v evidenci.", inputFileName)));
                return(_result);
            }

            if (prepis.FazeZpracovani < FazeZpracovani.Exportovat)
            {
                _result.Errors.Add(new DocumentNotInRequredStateException("Dokument s uvedeným jménem souboru není připraven pro export."));
                return(_result);
            }

            documentType = GetDocumentType(prepis.TypPrepisu);
            _documentId  = prepis.GUID;

            if (documentType == null)
            {
                //dokument má v evidenci přiřazen typ dokumentu, který není podporován
                _result.Errors.Add(new NotSupportedFileFormatException("Dokument má v evidenci přiřazen typ dokumentu, který není podporován."));
                return(_result);
            }

            string tempDirectoryPath = ConverterSettings.TempDirectoryPath;
            //vytvoří se adresářová struktura, pokud neexistuje, pro ukládání výsledných a dočasných souborů
            AdresarovaStruktura ads = new AdresarovaStruktura(tempDirectoryPath, documentType);

            ads.VytvorStrukturu();


            string docxToXmlFilePath       = Path.Combine(GetDataDirectoryPath(), "AllStylesConvert.2xml");
            string xsltTemplatesPath       = GetXsltTemplatesPath();
            string xsltTransformationsPath = GetXsltTransformationsPath();

            string fileNameWithoutExtension = prepis.Soubor.NazevBezPripony;
            string xmlOutpuFileName         = fileNameWithoutExtension + XmlExtension;

            string finalOutputDirectory = ads.DejVystup;             // Path.Combine(ads.DejVystup, fileNameWithoutExtension);
            string finalOutputFileName  = Path.Combine(finalOutputDirectory, xmlOutpuFileName);

            //Zatím pouze konverze z DOCX do základního XML
            IList <string> xmlOutputFiles = new List <string>();

            try
            {
                var filePart = 0;
                foreach (var inputFilePath in ConverterSettings.InputFilesPath)
                {
                    xmlOutputFiles.Add(GetDocxToXmlOutput(ads, prepis.Soubor.NazevBezPripony, filePart, ConverterSettings.InputFilesPath.Length > 1));

                    ConvertDocxToXml(inputFilePath, docxToXmlFilePath, xmlOutputFiles.Last());
                    filePart++;
                }
            }
            catch (Exception exception)
            {
                _result.Errors.Add(exception);
                return(_result);
            }

            if (!Directory.Exists(finalOutputDirectory))
            {
                Directory.CreateDirectory(finalOutputDirectory);
            }

            IExportNastaveni exportSettings = GetExportSettings(documentType, ConverterSettings, xsltTransformationsPath, xsltTemplatesPath, ads, prepis);
            ExportBase       export         = GetExportModule(documentType, exportSettings);

            if (export == null || exportSettings == null)
            {
                //Objekt pro export se nepodažřilo vytvořit, není podporován.
                return(_result);
            }

            try
            {
                export.Exportuj(exportSettings.Prepis, xmlOutputFiles, ConverterSettings.UploadedFilesPath);
                _result.IsConverted = true;
            }
            catch (Exception exception)
            {
                _result.Errors.Add(exception);
                return(_result);
            }

            if (!settings.Debug)
            {
                foreach (var xmlOutputFile in xmlOutputFiles.Where(File.Exists))
                {
                    File.Delete(xmlOutputFile);
                }
            }

            var versions = settings.GetVersionList(_documentId);

            _currentVersionInfoSkeleton = versions.Last();


            WriteListChange(finalOutputFileName, versions, _currentVersionInfoSkeleton);
            var xmlFinalOutputPath = Path.Combine(settings.OutputDirectoryPath, xmlOutpuFileName);

            File.Copy(finalOutputFileName, xmlFinalOutputPath, true);
            _result.MetadataFilePath = settings.OutputMetadataFilePath;
            //GetConversionMetadataFileFullPath(settings.OutputFilePath);

            if (export.UsePersonalizedXmdGenerator)
            {
                export.GenerateConversionMetadataFile(documentType, xmlFinalOutputPath, xmlOutpuFileName, settings.OutputMetadataFilePath);
            }
            else
            {
                SplittingResult splittingResult = null;
                if (settings.SplitDocumentByPageBreaks)
                {
                    splittingResult = SplitDocumentByPageBreaks(xmlFinalOutputPath, fileNameWithoutExtension);
                    if (!splittingResult.IsSplitted)
                    {
                        _result.IsConverted = false;
                        _result.Errors.Add(new DocumentSplittingException("Vyskytla se chyba při rozdělení souboru podle hranice stran."));
                    }
                }

                TableOfContentResult tocResult  = null;
                ContentInfoBuilder   tocBuilder = new ContentInfoBuilder();
                tocResult = tocBuilder.MakeTableOfContent(xmlFinalOutputPath, "body");

                GenerateConversionMetadataFile(splittingResult, tocResult, documentType, xmlFinalOutputPath, xmlOutpuFileName, settings.OutputMetadataFilePath);
            }

            if (!settings.Debug)
            {
                try
                {
                    Directory.Delete(settings.TempDirectoryPath, true);
                }
                catch (IOException exception)
                {
                    Directory.Delete(settings.TempDirectoryPath, true);
                }
            }

            return(_result);
        }