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); }
private void WriteListChange(string docxToXmlOutput, IEnumerable <VersionInfoSkeleton> previousVersions, VersionInfoSkeleton currentVersionInfoSkeleton) { XNamespace tei = XNamespace.Get("http://www.tei-c.org/ns/1.0"); //"http://www.tei-c.org/ns/1.0"; XDocument document = XDocument.Load(docxToXmlOutput); //document.Root.Attribute("xmlns:tei") XElement teiHeader = document.Element(tei + "TEI").Element(tei + "teiHeader"); XElement revisionElement = teiHeader.Element(tei + "revisionDesc"); if (revisionElement == null) { revisionElement = new XElement(tei + "revisionDesc"); foreach (VersionInfoSkeleton version in previousVersions) { revisionElement.Add(new XElement(tei + "change", new XAttribute("n", version.Id), new XAttribute("when", version.Creation), new XText(version.Message ?? ""))); } teiHeader.Add(revisionElement); } var teiN = document.Root.Attribute("n"); if (teiN == null) { var fileDesc = teiHeader.Element(tei + "fileDesc"); document.Root.Add(new XAttribute("n", fileDesc.Attribute("n").Value)); } document.Root.Add(new XAttribute("change", "#" + currentVersionInfoSkeleton.Id)); document.Save(docxToXmlOutput); }