//private void GenerateConversionMetadataFile(SplittingResult splittingResult, // string documentType, // string finalOutputFileFullPath) //{ // GenerateConversionMetadataFile(splittingResult, new TableOfContentResult(), documentType, finalOutputFileFullPath); //} private void GenerateConversionMetadataFile(SplittingResult splittingResult, TableOfContentResult tableOfContentResult, string documentType, string finalOutputFileFullPath, string finalOutputFileName, string finalOutputMetadataFileName) { XDocument metada = new XDocument(); XDocument teiDocument = XDocument.Load(finalOutputFileFullPath); metada.Add(new XElement(nsItj + "document", new XAttribute("doctype", documentType), new XAttribute("versionId", _currentVersionInfoSkeleton.Id), new XAttribute(nsXml + "lang", "cs"), new XAttribute("n", _documentId), new XAttribute("xmlns", nsTei), new XAttribute(XNamespace.Xmlns + "itj", nsItj), new XAttribute(XNamespace.Xmlns + "nlp", nsNlp), new XAttribute(XNamespace.Xmlns + "xml", XNamespace.Xml) ) ); XElement header = teiDocument.Descendants(nsTei + "teiHeader").FirstOrDefault(); metada.Root.Add(header); XElement toc = GenerateToc(tableOfContentResult); XElement hws = GenerateHwList(tableOfContentResult); XElement hwt = GenerateHwTable(hws); XElement accessories = new XElement(nsItj + "accessories", new XElement(nsItj + "file", new XAttribute("type", "content"), new XAttribute("name", finalOutputFileName))); if (splittingResult != null) //generovat pouze v případě, že k rozdělení na strany došlo { XElement pages = new XElement(nsItj + "pages", from info in splittingResult.PageBreaksSplitInfo select new XElement(nsItj + "page", info.Number == null ? null : new XAttribute("n", info.Number), info.Id == null ? null : new XAttribute(nsXml + "id", info.Id), info.FileName == null ? null : new XAttribute("resource", info.FileName), info.Facsimile == null ? null : new XAttribute("facs", info.Facsimile) ) ); metada.Root.Add(pages); } metada.Root.Add(toc); metada.Root.Add(hwt); metada.Root.Add(hws); metada.Root.Add(accessories); metada.Save(finalOutputMetadataFileName); }
/* * private XElement GenerateList(TableOfContentItem item) * { * if (item == null) return null; * string corresp = item.FormXmlId ?? item.DivXmlId; * XElement it = * new XElement(nsTei + "item", new XAttribute("corresp", "#" + corresp), (item.Type == null) ? null : new XAttribute("type", item.Type), * new XElement(nsTei + "head", new XText(item.Head)), * item.PageBreak == null ? null : new XElement(nsTei + "ref", new XAttribute("target", "#" + item.PageBreakXmlId), new XText(item.PageBreak)), * GenerateList(item.Sections) * ); * return it; * } */ //private static string GetConversionMetadataFileFullPath(string outputFilePath) //{ // string result = outputFilePath.Replace(XmlExtension, ".xmd"); // return result; //} private SplittingResult SplitDocumentByPageBreaks(string fileFullPath, string fileNameWithoutExtension) { FileInfo file = new FileInfo(fileFullPath); //string outputDirectory = Path.Combine(file.DirectoryName, fileNameWithoutExtension); string outputDirectory = file.DirectoryName; if (!Directory.Exists(outputDirectory)) { Directory.CreateDirectory(outputDirectory); } Splitter splitter = new Splitter(fileFullPath, outputDirectory); splitter.StartingElement = "body"; SplittingResult splittingResult = splitter.SplitOnPageBreak(); return(splittingResult); }
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); }