Skip to content

Commit b5870a0

Browse files
authored
Separate attribute formatters and translators to own files (#339)
1 parent e9565be commit b5870a0

15 files changed

+762
-699
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System.Diagnostics;
5+
6+
namespace DocumentFormat.OpenXml.Attributes.Formatter
7+
{
8+
/// <summary>
9+
/// The base class of the Attribute formatter. (abstract)
10+
/// </summary>
11+
abstract internal class AttributeFormatter
12+
{
13+
protected int length = 0;
14+
15+
/// <summary>
16+
/// The constructor to set the length of characters to express.
17+
/// </summary>
18+
/// <param name="length"></param>
19+
internal AttributeFormatter(int length)
20+
{
21+
Debug.Assert(length > 0);
22+
23+
this.length = length;
24+
}
25+
26+
/// <summary>
27+
/// Convert string to long. (abstract)
28+
/// </summary>
29+
/// <param name="strValue"></param>
30+
/// <returns>the converted long value</returns>
31+
abstract internal long StringToValue(string strValue);
32+
33+
/// <summary>
34+
/// Convert long to string. (abstract)
35+
/// </summary>
36+
/// <param name="longValue"></param>
37+
/// <returns>the converted string</returns>
38+
abstract internal string ValueToString(long longValue);
39+
}
40+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System.Diagnostics;
5+
using System.Text;
6+
7+
namespace DocumentFormat.OpenXml.Attributes.Formatter
8+
{
9+
/// <summary>
10+
/// Convert format between long and bin.
11+
/// </summary>
12+
internal class BinAttributeFormatter : AttributeFormatter
13+
{
14+
/// <summary>
15+
/// The constructor to set the length of characters to express.
16+
/// </summary>
17+
/// <param name="length"></param>
18+
internal BinAttributeFormatter(int length) : base(length)
19+
{
20+
}
21+
22+
/// <summary>
23+
/// Convert string to long.
24+
/// </summary>
25+
/// <param name="strValue"></param>
26+
/// <returns>the converted long value</returns>
27+
internal override long StringToValue(string strValue)
28+
{
29+
long longValue = 0;
30+
int length;
31+
32+
Debug.Assert(strValue != null);
33+
34+
length = strValue.Length;
35+
36+
Debug.Assert(length > 0);
37+
Debug.Assert(length <= 64); // The length of strValue changes depending on the attribute representation, but it at least needs to be less-than-equal 64(bit).
38+
39+
for (int count = 0; count < length; count++)
40+
{
41+
long bit;
42+
if (strValue[count] == '1')
43+
{
44+
bit = 1;
45+
}
46+
else if (strValue[count] == '0')
47+
{
48+
bit = 0;
49+
}
50+
else
51+
{
52+
// if strValue[count] is neither '1' nor '0', we ignore this value by setting the bit zero.
53+
bit = 0;
54+
}
55+
56+
longValue = (bit == 0) ? (longValue & ~(bit << (length - 1 - count))) : (longValue | (bit << (length - 1 - count)));
57+
}
58+
59+
return longValue;
60+
}
61+
62+
/// <summary>
63+
/// Convert long to string.
64+
/// </summary>
65+
/// <param name="longValue"></param>
66+
/// <returns>the converted string</returns>
67+
internal override string ValueToString(long longValue)
68+
{
69+
StringBuilder strBin = new StringBuilder();
70+
71+
for (int count = this.length - 1; count >= 0; count--)
72+
{
73+
strBin.Append((longValue & (1 << count)) > 0 ? '1' : '0');
74+
}
75+
76+
return strBin.ToString();
77+
}
78+
}
79+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
using System.Diagnostics;
6+
using System.Globalization;
7+
8+
namespace DocumentFormat.OpenXml.Attributes.Formatter
9+
{
10+
/// <summary>
11+
/// Convert format between long and hex.
12+
/// </summary>
13+
internal class HexAttributeFormatter : AttributeFormatter
14+
{
15+
/// <summary>
16+
/// The constructor to set the length of characters to express.
17+
/// </summary>
18+
/// <param name="length"></param>
19+
internal HexAttributeFormatter(int length) : base(length)
20+
{
21+
}
22+
23+
/// <summary>
24+
/// Convert string to long.
25+
/// </summary>
26+
/// <param name="strValue"></param>
27+
/// <returns>the converted long value</returns>
28+
internal override long StringToValue(string strValue)
29+
{
30+
long longValue = 0;
31+
32+
Debug.Assert(strValue != null);
33+
34+
try
35+
{
36+
Int64.TryParse(strValue, NumberStyles.HexNumber, new CultureInfo("en-US"), out longValue);
37+
}
38+
catch
39+
{
40+
Debug.Assert(true, "Int64.TryParse failed.");
41+
}
42+
43+
return longValue;
44+
}
45+
46+
/// <summary>
47+
/// Convert long to string.
48+
/// </summary>
49+
/// <param name="longValue"></param>
50+
/// <returns>the converted string</returns>
51+
internal override string ValueToString(long longValue)
52+
{
53+
return longValue.ToString("X4");
54+
}
55+
}
56+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System.Collections.Generic;
5+
using System.Diagnostics;
6+
7+
namespace DocumentFormat.OpenXml.Attributes.Translator
8+
{
9+
/// <summary>
10+
/// Translate Strict attributes to Transitional
11+
/// </summary>
12+
internal static class AttributeTranslator
13+
{
14+
private static TagAttributeTranslator translator;
15+
private static Dictionary<string, TagAttributeTranslator> dicOfTranslators = null;
16+
17+
private static Dictionary<string, TagAttributeTranslator> DicOfTranslators
18+
{
19+
get
20+
{
21+
if (dicOfTranslators == null)
22+
{
23+
dicOfTranslators = new Dictionary<string, TagAttributeTranslator>();
24+
25+
// Add all the tags we handle.
26+
dicOfTranslators["document"] = null;
27+
dicOfTranslators["cnfStyle"] = null;
28+
dicOfTranslators["tblLook"] = null;
29+
dicOfTranslators["ind"] = null;
30+
dicOfTranslators["tab"] = null; // "tab" and "jc"
31+
dicOfTranslators["stylePaneSortMethod"] = null;
32+
dicOfTranslators["textDirection"] = null;
33+
}
34+
35+
return dicOfTranslators;
36+
}
37+
}
38+
39+
/// <summary>
40+
/// Translate Strict attribute to Transitional
41+
/// </summary>
42+
/// <param name="strTag"></param>
43+
/// <param name="strLocalName"></param>
44+
/// <param name="strValue"></param>
45+
/// <returns>The translator</returns>
46+
internal static TagAttributeTranslator Translate(string strTag, string strLocalName, string strValue)
47+
{
48+
translator = null;
49+
50+
if (strTag != null && strLocalName != null && strValue != null)
51+
{
52+
try
53+
{
54+
if (DicOfTranslators.ContainsKey(strTag))
55+
{
56+
translator = DicOfTranslators[strTag];
57+
58+
if (translator == null)
59+
{
60+
switch (strTag)
61+
{
62+
case "document":
63+
translator = new DocumentTagAttributeTranslator();
64+
break;
65+
66+
case "cnfStyle":
67+
translator = new CnfStyleTagAttributeTranslator();
68+
break;
69+
70+
case "tblLook":
71+
translator = new TblLookTagAttributeTranslator();
72+
break;
73+
74+
case "ind":
75+
translator = new IndTagAttributeTranslator();
76+
break;
77+
78+
case "jc": // falls through...
79+
case "tab":
80+
translator = new JcTabTagAttributeTranslator();
81+
break;
82+
83+
case "stylePaneSortMethod":
84+
translator = new StylePaneSortMethodTagAttributeTranslator();
85+
break;
86+
87+
case "textDirection":
88+
translator = new TextDirectionTagAttributeTranslator();
89+
break;
90+
91+
default:
92+
break;
93+
}
94+
95+
Debug.Assert(translator != null, "AttributeTranslator.Translate() can't initialize a translator.");
96+
97+
DicOfTranslators[strTag] = translator;
98+
}
99+
}
100+
101+
if (translator != null)
102+
{
103+
translator.SetLocalNameAndValue(strLocalName, strValue);
104+
105+
if (translator.Index == (int)TagAttributeTranslator.State.NotInitialized)
106+
{
107+
translator = null;
108+
}
109+
}
110+
}
111+
catch
112+
{
113+
Debug.Assert(true, "AttributeTranslator.Translate() failed.");
114+
115+
translator = null;
116+
}
117+
}
118+
119+
return translator;
120+
}
121+
}
122+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using DocumentFormat.OpenXml.Attributes.Formatter;
5+
6+
namespace DocumentFormat.OpenXml.Attributes.Translator
7+
{
8+
internal class CnfStyleTagAttributeTranslator : TagAttributeTranslator
9+
{
10+
private static string[] arrayOfOrigTagAttrNames = { "firstRow", "lastRow", "firstColumn", "lastColumn", "oddVBand", "evenVBand", "oddHBand", "evenHBand", "firstRowLastColumn", "firstRowFirstColumn", "lastRowFirstColumn", "lastRowLastColumn" };
11+
private static string[] arrayOfNewTagAttrNames = { "val", "val", "val", "val", "val", "val", "val", "val", "val", "val", "val", "val" };
12+
private static long[] arrayOfTagAttrTraits = { 0x800, 0x400, 0x200, 0x100, 0x080, 0x040, 0x020, 0x010, 0x008, 0x004, 0x002, 0x001 };
13+
14+
internal CnfStyleTagAttributeTranslator()
15+
{
16+
arrayOfNewAttrNames = arrayOfNewTagAttrNames;
17+
arrayOfOrigAttrNames = arrayOfOrigTagAttrNames;
18+
arrayOfOrigAttrValues = null;
19+
arrayOfNewAttrValues = null;
20+
arrayOfAttrTraits = arrayOfTagAttrTraits;
21+
formatter = new BinAttributeFormatter(arrayOfTagAttrTraits.Length);
22+
}
23+
24+
/// <summary>
25+
/// Set the index to specify the attribute name to translate.
26+
/// </summary>
27+
/// <returns>The index</returns>
28+
protected override int SetIndex()
29+
{
30+
return this.GetIndexByAttributeName();
31+
}
32+
33+
/// <summary>
34+
/// Get the attribute value.
35+
/// </summary>
36+
/// <returns>The attribute value</returns>
37+
internal override string Value
38+
{
39+
get { return this.strAttrValue; }
40+
}
41+
}
42+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
namespace DocumentFormat.OpenXml.Attributes.Translator
5+
{
6+
internal class DocumentTagAttributeTranslator : TagAttributeTranslator
7+
{
8+
private static string[] arrayOfOrigTagAttrNames = { "conformance" };
9+
private static string[] arrayOfNewTagAttrNames = { "conformance" };
10+
private static string[] arrayOfOrigTagAttrValues = { "strict" };
11+
private static string[] arrayOfNewTagAttrValues = { "" }; // Must be "".
12+
private static long[] arrayOfTagAttrTraits = { 0 };
13+
14+
internal DocumentTagAttributeTranslator()
15+
{
16+
arrayOfOrigAttrNames = arrayOfOrigTagAttrNames;
17+
arrayOfNewAttrNames = arrayOfNewTagAttrNames;
18+
arrayOfOrigAttrValues = arrayOfOrigTagAttrValues;
19+
arrayOfNewAttrValues = arrayOfNewTagAttrValues;
20+
arrayOfAttrTraits = arrayOfTagAttrTraits;
21+
formatter = null;
22+
}
23+
24+
/// <summary>
25+
/// Set the index to specify the value to translate.
26+
/// </summary>
27+
/// <returns>The index</returns>
28+
protected override int SetIndex()
29+
{
30+
return this.GetIndexByValue();
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)