Просмотр исходного кода

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 15 лет назад
Родитель
Сommit
b97394809d
5 измененных файлов: 80 добавлений и 7 удалений
  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 Просмотреть файл

return list; 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) 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. // ReplaceText in Headers of the document.

+ 2
- 1
DocX/DocX.csproj Просмотреть файл

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

+ 19
- 0
DocX/Paragraph.cs Просмотреть файл

return query; 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> /// <summary>
/// Insert a PageNumber place holder into a Paragraph. /// Insert a PageNumber place holder into a Paragraph.
/// This place holder should only be inserted into a Header or Footer Paragraph. /// This place holder should only be inserted into a Header or Footer Paragraph.

+ 22
- 0
UnitTests/UnitTest1.cs Просмотреть файл

using System.Drawing.Imaging; using System.Drawing.Imaging;
using System.Xml.Linq; using System.Xml.Linq;
using System.IO.Packaging; using System.IO.Packaging;
using System.Text.RegularExpressions;
namespace UnitTests namespace UnitTests
{ {
directory_documents = String.Join("\\", steps) + "\\documents\\"; 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] [TestMethod]
public void Test_EverybodyHasAHome_Loaded() public void Test_EverybodyHasAHome_Loaded()
{ {

+ 6
- 6
UnitTests/UnitTests.csproj Просмотреть файл

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

Загрузка…
Отмена
Сохранить