Explorar el Código

Added FindUniqueByPattern and FindAllbyPattern.

Added a new unit test to test ReplaceText, based on ReplaceTests.docx
The Test fails.  This functionality worked in the previous release.
master
bdm_cp hace 15 años
padre
commit
b97394809d
Se han modificado 5 ficheros con 80 adiciones y 7 borrados
  1. 31
    0
      DocX/Container.cs
  2. 2
    1
      DocX/DocX.csproj
  3. 19
    0
      DocX/Paragraph.cs
  4. 22
    0
      UnitTests/UnitTest1.cs
  5. 6
    6
      UnitTests/UnitTests.csproj

+ 31
- 0
DocX/Container.cs Ver fichero

@@ -207,6 +207,37 @@ namespace Novacode
return list;
}
/// <summary>
/// Find all unique instances of the given Regex Pattern,
/// returning the list of the unique strings found
/// </summary>
/// <param name="str"></param>
/// <param name="options"></param>
/// <returns></returns>
public virtual List<string> FindUniqueByPattern(string pattern, RegexOptions options)
{
List<string> rawResults = new List<string>();
foreach (Paragraph p in Paragraphs)
{ // accumulate the search results from all paragraphs
List<string> partials = p.FindAllByPattern(pattern, options);
rawResults.AddRange(partials);
}
// this dictionary is used to collect results and test for uniqueness
Dictionary<string, int> uniqueResults = new Dictionary<string, int>();
foreach (string currValue in rawResults)
{
if (!uniqueResults.ContainsKey(currValue))
{ // if the dictionary doesn't have it, add it
uniqueResults.Add(currValue, 0);
}
}
return uniqueResults.Keys.ToList(); // return the unique list of results
}
public virtual void ReplaceText(string oldValue, string newValue, bool trackChanges = false, RegexOptions options = RegexOptions.None, Formatting newFormatting = null, Formatting matchFormatting = null, MatchFormattingOptions fo = MatchFormattingOptions.SubsetMatch)
{
// ReplaceText in Headers of the document.

+ 2
- 1
DocX/DocX.csproj Ver fichero

@@ -47,7 +47,8 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Debug\DocX.xml</DocumentationFile>
<DocumentationFile>
</DocumentationFile>
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">

+ 19
- 0
DocX/Paragraph.cs Ver fichero

@@ -3186,6 +3186,25 @@ namespace Novacode
return query;
}
/// <summary>
/// Find all unique instances of the given Regex Pattern
/// </summary>
/// <param name="str"></param>
/// <param name="options"></param>
/// <returns></returns>
public List<string> FindAllByPattern(string str, RegexOptions options)
{
MatchCollection mc = Regex.Matches(this.Text, str, options);
var query =
(
from m in mc.Cast<Match>()
select m.Value
).ToList();
return query;
}
/// <summary>
/// Insert a PageNumber place holder into a Paragraph.
/// This place holder should only be inserted into a Header or Footer Paragraph.

+ 22
- 0
UnitTests/UnitTest1.cs Ver fichero

@@ -10,6 +10,7 @@ using System.Drawing;
using System.Drawing.Imaging;
using System.Xml.Linq;
using System.IO.Packaging;
using System.Text.RegularExpressions;
namespace UnitTests
{
@@ -36,6 +37,27 @@ namespace UnitTests
directory_documents = String.Join("\\", steps) + "\\documents\\";
}
[TestMethod]
public void PatternReplacement()
{
Dictionary<string, string> testPatterns = new Dictionary<string, string>()
{
{"COURT NAME","Fred Frump"},
{"Case Number","cr-md-2011-1234567"}
};
using (DocX replaceDoc = DocX.Load(directory_documents + "ReplaceTests.docx"))
{
foreach (var p in testPatterns)
{
replaceDoc.ReplaceText("<" + p.Key + ">", p.Value, false, RegexOptions.IgnoreCase);
}
replaceDoc.SaveAs(directory_documents + "ReplaceResults.docx");
}
}
[TestMethod]
public void Test_EverybodyHasAHome_Loaded()
{

+ 6
- 6
UnitTests/UnitTests.csproj Ver fichero

@@ -67,12 +67,6 @@
<None Include="documents\Tables.docx" />
<None Include="documents\Template.dotx" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DocX\DocX.csproj">
<Project>{E863D072-AA8B-4108-B5F1-785241B37F67}</Project>
<Name>DocX</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Content Include="documents\green.jpg" />
<Content Include="documents\orange.gif" />
@@ -80,6 +74,12 @@
<Content Include="documents\red.bmp" />
<Content Include="documents\yellow.tif" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DocX\DocX.csproj">
<Project>{E863D072-AA8B-4108-B5F1-785241B37F67}</Project>
<Name>DocX</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.

Cargando…
Cancelar
Guardar