protected static XmlDocument LoadXmlDocument(PublicationInformation projInfo) { var xml = new XmlDocument { XmlResolver = FileStreamXmlResolver.GetNullResolver() }; var streamReader = new StreamReader(projInfo.DefaultXhtmlFileWithPath); xml.Load(streamReader); streamReader.Close(); return(xml); }
/// <summary> /// Validating the xml file with xmldocument to avoid further processing. /// </summary> /// <param name="xml">Xml file Name for Validating</param> /// <exception cref="FileNotFoundException">if xml file missing</exception> /// <exception cref="XmlException">if xml file won't load</exception> protected static void ValidXmlFile(string xml) { if (!File.Exists(xml)) { throw new FileNotFoundException(); } XmlDocument xDoc = new XmlDocument(); using (var stream = new FileStream(xml, FileMode.Open)) { xDoc.XmlResolver = FileStreamXmlResolver.GetNullResolver(); // Null may not work on Mono; not trying to validate any URLs. xDoc.Load(stream); } }
public static void Execute(string inputFile, string output) { var xmlDoc = new XmlDocument(); xmlDoc.XmlResolver = FileStreamXmlResolver.GetNullResolver(); var nsmgr = new XmlNamespaceManager(xmlDoc.NameTable); const string xhtmlns = "http://www.w3.org/1999/xhtml"; nsmgr.AddNamespace("x", xhtmlns); xmlDoc.Load(inputFile); var changed = false; if (_insertSenseNumClass) { var senseNums = xmlDoc.SelectNodes("//x:span[@class='headref']/x:span", nsmgr); foreach (XmlElement senseNum in senseNums) { if (!senseNum.HasAttribute("class")) { senseNum.SetAttribute("class", "revsensenumber"); changed = true; } } } if (_insertHomographClass) { var nodes = xmlDoc.SelectNodes("//x:span[@class='headref']/text()", nsmgr); foreach (XmlNode node in nodes) { var match = Regex.Match(node.InnerText, "([^0-9]*)([0-9]*)"); if (match.Groups[2].Length > 0) { var homographNode = xmlDoc.CreateElement("span", xhtmlns); homographNode.SetAttribute("class", "revhomographnumber"); homographNode.InnerText = match.Groups[2].Value; node.InnerText = match.Groups[1].Value; node.ParentNode.InsertAfter(homographNode, node); changed = true; } } } if (changed) { var xmlWriter = XmlWriter.Create(output); xmlDoc.WriteTo(xmlWriter); xmlWriter.Close(); } xmlDoc.RemoveAll(); }
/// <summary> /// Load Xml data from part of ODT file (usually content.xml or styles.xml) /// </summary> /// <param name="odtPath">full path to odt</param> /// <param name="sectionName">section name (usually content.xml or styles.xml)</param> /// <returns>xmlDocument with <paramref name="sectionName">sectionName</paramref> loaded</returns> public static XmlDocument LoadXml(string odtPath, string sectionName) { if (File.Exists(odtPath)) { var odtFile = new ZipFile(odtPath); var reader = new StreamReader(odtFile.GetInputStream(odtFile.GetEntry(sectionName).ZipFileIndex)); var text = reader.ReadToEnd(); reader.Close(); odtFile.Close(); var xmlDocument = new XmlDocument(); xmlDocument.XmlResolver = FileStreamXmlResolver.GetNullResolver(); xmlDocument.LoadXml(text); return(xmlDocument); } else { return(null); } }
/// <summary> /// Compares all idml's in outputPath to make sure the content.xml and styles.xml are the same /// </summary> public static void AreEqual(string expectFullName, string outputFullName, string msg) { using (var expFl = new ZipFile(expectFullName)) { var outFl = new ZipFile(outputFullName); foreach (ZipEntry zipEntry in expFl) { //TODO: designmap.xml should be tested but \\MetadataPacketPreference should be ignored as it contains the creation date. if (!CheckFile(zipEntry.Name, "Stories,Spreads,Resources,MasterSpreads")) { continue; } if (Path.GetExtension(zipEntry.Name) != ".xml") { continue; } string outputEntry = new StreamReader(outFl.GetInputStream(outFl.GetEntry(zipEntry.Name).ZipFileIndex)).ReadToEnd(); string expectEntry = new StreamReader(expFl.GetInputStream(expFl.GetEntry(zipEntry.Name).ZipFileIndex)).ReadToEnd(); XmlDocument outputDocument = new XmlDocument(); outputDocument.XmlResolver = FileStreamXmlResolver.GetNullResolver(); outputDocument.LoadXml(outputEntry); XmlDocument expectDocument = new XmlDocument(); outputDocument.XmlResolver = FileStreamXmlResolver.GetNullResolver(); expectDocument.LoadXml(expectEntry); XmlDsigC14NTransform outputCanon = new XmlDsigC14NTransform(); outputCanon.Resolver = new XmlUrlResolver(); outputCanon.LoadInput(outputDocument); XmlDsigC14NTransform expectCanon = new XmlDsigC14NTransform(); expectCanon.Resolver = new XmlUrlResolver(); expectCanon.LoadInput(expectDocument); Stream outputStream = (Stream)outputCanon.GetOutput(typeof(Stream)); Stream expectStream = (Stream)expectCanon.GetOutput(typeof(Stream)); string errMessage = string.Format("{0}: {1} doesn't match", msg, zipEntry.Name); Assert.AreEqual(expectStream.Length, outputStream.Length, errMessage); FileAssert.AreEqual(expectStream, outputStream, errMessage); } } }
/// <summary> /// Compares all odt's in outputPath to make sure the content.xml and styles.xml are the same /// </summary> /// <param name="expectPath">expected output path</param> /// <param name="outputPath">output path</param> /// <param name="msg">message to display if mismatch</param> public static void AreEqual(string expectPath, string outputPath, string msg) { var outDi = new DirectoryInfo(outputPath); var expDi = new DirectoryInfo(expectPath); FileInfo[] outFi = outDi.GetFiles("*.od*"); FileInfo[] expFi = expDi.GetFiles("*.od*"); Assert.AreEqual(outFi.Length, expFi.Length, string.Format("{0} {1} odt found {2} expected", msg, outFi.Length, expFi.Length)); foreach (FileInfo fi in outFi) { var outFl = new ZipFile(fi.FullName); var expFl = new ZipFile(Common.PathCombine(expectPath, fi.Name)); foreach (string name in "content.xml,styles.xml".Split(',')) { string outputEntry = new StreamReader(outFl.GetInputStream(outFl.GetEntry(name).ZipFileIndex)).ReadToEnd(); string expectEntry = new StreamReader(expFl.GetInputStream(expFl.GetEntry(name).ZipFileIndex)).ReadToEnd(); XmlDocument outputDocument = new XmlDocument(); outputDocument.XmlResolver = FileStreamXmlResolver.GetNullResolver(); outputDocument.LoadXml(outputEntry); XmlDocument expectDocument = new XmlDocument(); expectDocument.XmlResolver = FileStreamXmlResolver.GetNullResolver(); expectDocument.LoadXml(expectEntry); XmlDsigC14NTransform outputCanon = new XmlDsigC14NTransform(); outputCanon.Resolver = new XmlUrlResolver(); outputCanon.LoadInput(outputDocument); XmlDsigC14NTransform expectCanon = new XmlDsigC14NTransform(); expectCanon.Resolver = new XmlUrlResolver(); expectCanon.LoadInput(expectDocument); Stream outputStream = (Stream)outputCanon.GetOutput(typeof(Stream)); Stream expectStream = (Stream)expectCanon.GetOutput(typeof(Stream)); string errMessage = string.Format("{0}: {1} {2} doesn't match", msg, fi.Name, name); Assert.AreEqual(expectStream.Length, outputStream.Length, errMessage); FileAssert.AreEqual(expectStream, outputStream, errMessage); } } }
/// <summary> /// Converts USX file(s) to a single XHTML file. /// </summary> /// <param name="projInfo"></param> /// <param name="files">List of files to convert. This can be a single file or wildcard (*).</param> private static void UsxToXhtml(PublicationInformation projInfo, List <string> files) { // try to find the stylesheet associated with this project (*.sty) var tmpFiles = Directory.GetFiles(projInfo.ProjectPath, "*.sty"); string styFile; if (tmpFiles.Length == 0) { // not here - check in the "gather" subdirectory tmpFiles = Directory.GetFiles(Common.PathCombine(projInfo.ProjectPath, "gather"), "*.sty"); styFile = Common.PathCombine(Common.PathCombine(projInfo.ProjectPath, "gather"), tmpFiles[0]); } else { styFile = Common.PathCombine(projInfo.ProjectPath, tmpFiles[0]); } try { string paratextSupportDLL = Common.PathCombine(Common.AssemblyPath, "ParatextSupport.dll"); if (!File.Exists(paratextSupportDLL)) { paratextSupportDLL = Common.PathCombine(Path.GetDirectoryName(Common.AssemblyPath), "ParatextSupport.dll"); } // load the ParatextSupport DLL dynamically Assembly asm = Assembly.LoadFrom(paratextSupportDLL); // Convert the .sty stylesheet to css // new ScrStylesheet(styFile) Type tStyToCSS = asm.GetType("SIL.PublishingSolution.StyToCSS"); Object oScrStylesheet = null; if (tStyToCSS != null) { // new styToCss oScrStylesheet = Activator.CreateInstance(tStyToCSS); // styToCss.StyFullPath = styFile PropertyInfo piStyFullPath = tStyToCSS.GetProperty("StyFullPath", BindingFlags.Public | BindingFlags.Instance); piStyFullPath.SetValue(oScrStylesheet, styFile, null); Object[] args = new object[1]; args[0] = Common.PathCombine(projInfo.ProjectPath, projInfo.ProjectName + ".css"); tStyToCSS.InvokeMember("ConvertStyToCSS", BindingFlags.Default | BindingFlags.InvokeMethod, null, oScrStylesheet, args); } projInfo.DefaultCssFileWithPath = Common.PathCombine(projInfo.ProjectPath, projInfo.ProjectName + ".css"); // collect the files and convert them to XmlDocuments if (files[0] == "*") { // wildcard - need to expand the file count to include all .sfm files in this directory var usxFiles = Directory.GetFiles(projInfo.ProjectPath, "*.sfm"); files.Clear(); // should only be 1 item, but just in case... foreach (var usxFile in usxFiles) { files.Add(Path.GetFileName(usxFile)); // relative file names (no path) } } if (files.Count == 0) { throw new Exception("No files found to convert."); } var docs = new List <XmlDocument>(); foreach (var file in files) { if (File.Exists(Common.PathCombine(projInfo.ProjectPath, file))) { var xmlDoc = new XmlDocument(); xmlDoc.XmlResolver = FileStreamXmlResolver.GetNullResolver(); xmlDoc.Load(Common.PathCombine(projInfo.ProjectPath, file)); docs.Add(xmlDoc); } } // now convert them to xhtml Type tPPL = asm.GetType("SIL.PublishingSolution.ParatextPathwayLink"); if (tPPL != null) { Object[] args = new object[5]; args[0] = projInfo.ProjectName; args[1] = projInfo.ProjectName; args[2] = "zxx"; args[3] = "zxx"; args[4] = "PathwayB"; Object oPPL = Activator.CreateInstance(tPPL, args); Object[] argsCombine = new object[2]; argsCombine[0] = docs; argsCombine[1] = Param.PrintVia; XmlDocument scrBooksDoc = (XmlDocument)tPPL.InvokeMember ("CombineUsxDocs", BindingFlags.Default | BindingFlags.InvokeMethod, null, oPPL, argsCombine); if (string.IsNullOrEmpty(scrBooksDoc.InnerText)) { throw new Exception("No content found to convert."); } Object[] argsConvert = new object[2]; argsConvert[0] = scrBooksDoc.InnerXml; argsConvert[1] = Common.PathCombine(projInfo.ProjectPath, projInfo.ProjectName.Replace(" ", "_") + ".xhtml"); tPPL.InvokeMember("ConvertUsxToPathwayXhtmlFile", BindingFlags.Default | BindingFlags.InvokeMethod, null, oPPL, argsConvert); } projInfo.DefaultXhtmlFileWithPath = Common.PathCombine(projInfo.ProjectPath, projInfo.ProjectName.Replace(" ", "_") + ".xhtml"); } catch (FileNotFoundException) { throw new ArgumentException("USFM output depends on ParatextSupport.DLL. Please make sure this library is in the Pathway installation directory."); } catch (FileLoadException ex) { throw new ArgumentException("Unable to load a needed library for USFM output. Details: " + ex.Message); } catch (Exception ex) { Console.WriteLine(ex); } }