Skip to content

Commit 84298bb

Browse files
oerodgervlabramov
andauthored
Release/1.5.4 (#10)
CONTMS-116 add syncwrapper (#8) CONTMS-120 Trados: fix bug with < > in Deepl Co-authored-by: Vladimir Abramov <mr.vovan@GMAIL.COM>
1 parent 5740f70 commit 84298bb

File tree

11 files changed

+416
-116
lines changed

11 files changed

+416
-116
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace IntentoSDK.Handlers
8+
{
9+
/// <summary>
10+
/// Base symbol handler
11+
/// </summary>
12+
public abstract class BaseSymbolHandler : ISymbolHandler
13+
{
14+
protected abstract IReadOnlyDictionary<string, string> SpecialCodesIn { get; }
15+
16+
protected abstract IReadOnlyDictionary<string, string> SpecialCodesOut { get; }
17+
18+
public abstract string Format { get; }
19+
20+
public virtual string OnResponsing(string text)
21+
{
22+
return PrepareResult(text);
23+
}
24+
25+
public virtual string OnSending(string text)
26+
{
27+
return PrepareText(text);
28+
}
29+
30+
protected virtual string PrepareText(string data)
31+
{
32+
// Remove parasite character for memoq
33+
data = new string(data.Where(c => (int)c != 9727).ToArray());
34+
35+
// Replacing some HTML codes with special tags
36+
foreach (KeyValuePair<string, string> pair in SpecialCodesIn)
37+
{
38+
data = data.Replace(pair.Key, pair.Value);
39+
}
40+
return data;
41+
}
42+
43+
protected virtual string PrepareResult(string text)
44+
{
45+
// Return HTML codes instead of special tags
46+
foreach (KeyValuePair<string, string> pair in SpecialCodesOut)
47+
{
48+
text = text.Replace(pair.Key, pair.Value);
49+
}
50+
51+
return text;
52+
}
53+
54+
}
55+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace IntentoSDK.Handlers
8+
{
9+
/// <summary>
10+
/// Prepare HTML text
11+
/// </summary>
12+
internal class HtmlSymbolHandler : BaseSymbolHandler
13+
{
14+
protected override IReadOnlyDictionary<string, string> SpecialCodesIn => new Dictionary<string, string>();
15+
16+
protected override IReadOnlyDictionary<string, string> SpecialCodesOut =>
17+
new Dictionary<string, string>
18+
{
19+
{ "&lt;", "<" },
20+
{ "&gt;", ">" }
21+
};
22+
23+
public override string Format => "html";
24+
25+
protected override string PrepareResult(string text)
26+
{
27+
text = base.PrepareResult(text);
28+
29+
// Remove <meta> and </meta> tags
30+
int n1 = text.IndexOf("<meta");
31+
string text2 = text;
32+
if (n1 != -1)
33+
{
34+
int n2 = text.IndexOf(">");
35+
text2 = text.Substring(n2 + 1);
36+
}
37+
return text2;
38+
}
39+
}
40+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace IntentoSDK.Handlers
8+
{
9+
/// <summary>
10+
/// Process text before and after sending
11+
/// </summary>
12+
public interface ISymbolHandler
13+
{
14+
/// <summary>
15+
/// Type of text format
16+
/// </summary>
17+
string Format { get; }
18+
19+
/// <summary>
20+
/// Prepare text before send
21+
/// </summary>
22+
/// <param name="text">Text</param>
23+
/// <returns></returns>
24+
string OnSending(string text);
25+
26+
/// <summary>
27+
/// Prepare text after send
28+
/// </summary>
29+
/// <param name="text"></param>
30+
/// <returns></returns>
31+
string OnResponsing(string text);
32+
}
33+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace IntentoSDK.Handlers
8+
{
9+
/// <summary>
10+
/// Factory for handlers
11+
/// </summary>
12+
public class SymbolHandlersFactory
13+
{
14+
private readonly ISymbolHandler[] symbolHandlers;
15+
16+
/// <summary>
17+
/// Ctor
18+
/// </summary>
19+
public SymbolHandlersFactory()
20+
{
21+
symbolHandlers = new ISymbolHandler[] {
22+
new XmlSymbolHandler(),
23+
new HtmlSymbolHandler()
24+
};
25+
}
26+
27+
/// <summary>
28+
/// Prepare source
29+
/// </summary>
30+
/// <param name="text"></param>
31+
/// <param name="format"></param>
32+
/// <returns></returns>
33+
public string HandleSource(string text, string format)
34+
{
35+
foreach (ISymbolHandler handler in symbolHandlers.Where(h => h.Format == format))
36+
{
37+
text = handler.OnSending(text);
38+
}
39+
return text;
40+
}
41+
42+
/// <summary>
43+
/// Prepare result
44+
/// </summary>
45+
/// <param name="text"></param>
46+
/// <param name="format"></param>
47+
/// <returns></returns>
48+
public string HandleResult(string text, string format)
49+
{
50+
foreach (ISymbolHandler handler in symbolHandlers.Where(h => h.Format == format))
51+
{
52+
text = handler.OnResponsing(text);
53+
}
54+
return text;
55+
}
56+
57+
private static readonly SymbolHandlersFactory current = new SymbolHandlersFactory();
58+
59+
/// <summary>
60+
/// Current instance of factory
61+
/// </summary>
62+
public static SymbolHandlersFactory Current => current;
63+
64+
}
65+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace IntentoSDK.Handlers
8+
{
9+
/// <summary>
10+
/// Xml symbols prepare
11+
/// </summary>
12+
internal class XmlSymbolHandler : BaseSymbolHandler
13+
{
14+
protected override IReadOnlyDictionary<string, string> SpecialCodesIn => new Dictionary<string, string>
15+
{
16+
{ "&amp;gt;" , "<tph1/>" },
17+
{ "&amp;lt;" , "<tph2/>" },
18+
{ "&lt;" , "<tph3/>" },
19+
{ "&gt;" , "<tph4/>" }
20+
};
21+
22+
protected override IReadOnlyDictionary<string, string> SpecialCodesOut => new Dictionary<string, string>
23+
{
24+
{ "<tph1>" , "&amp;gt;" },
25+
{ "<tph2>" , "&amp;lt;" },
26+
{ "<tph3>" , "&lt;" },
27+
{ "<tph4>" , "&gt;" },
28+
{ "<tph1/>", "&amp;gt;" },
29+
{ "<tph2/>", "&amp;lt;" },
30+
{ "<tph3/>", "&lt;" },
31+
{ "<tph4/>", "&gt;" },
32+
{ "<tph1 />", "&amp;gt;" },
33+
{ "<tph2 />", "&amp;lt;" },
34+
{ "<tph3 />", "&lt;" },
35+
{ "<tph4 />", "&gt;" },
36+
{ "</tph1>", "" },
37+
{ "</tph2>", "" },
38+
{ "</tph3>", "" },
39+
{ "</tph4>", "" }
40+
};
41+
42+
public override string Format => "xml";
43+
44+
protected override string PrepareResult(string text)
45+
{
46+
text = base.PrepareResult(text);
47+
48+
// Remove <? > tag
49+
int n1 = text.IndexOf("<?");
50+
string text2 = text;
51+
if (n1 != -1)
52+
{
53+
int n2 = text.IndexOf(">");
54+
text2 = text.Substring(n2 + 1);
55+
}
56+
57+
// Remove <root> and </root> tags
58+
string text3 = text2.Replace("<root>", "").Replace("</root>", "");
59+
return text3;
60+
}
61+
62+
protected override string PrepareText(string data)
63+
{
64+
data = base.PrepareText(data);
65+
return string.Format("<root>{0}</root>", data);
66+
}
67+
}
68+
}

IntentoSDK/HttpConnector.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,15 @@ public HttpConnector(Intento _intento)
4646
client.DefaultRequestHeaders.Add("User-Agent", userAgent);
4747
}
4848

49-
async public Task<dynamic> PostAsync(string path, dynamic json, Dictionary<string, string> special_headers = null, Dictionary<string, string> additionalParams = null)
49+
async public Task<dynamic> PostAsync(string path, dynamic json, Dictionary<string, string> special_headers = null,
50+
Dictionary<string, string> additionalParams = null, bool useSyncwrapper = false)
5051
{
5152
try
5253
{
5354
string jsonData = JsonConvert.SerializeObject(json);
5455
using (HttpContent httpContent = new StringContent(jsonData))
5556
{
56-
var url = MakeUrl(path, additionalParams);
57+
var url = MakeUrl(path, additionalParams, useSyncwrapper);
5758
if (special_headers != null && special_headers.Count != 0)
5859
{
5960
foreach (KeyValuePair<string, string> pair in special_headers)
@@ -112,12 +113,13 @@ async public Task<dynamic> GetAsync(string path, Dictionary<string, string> addi
112113
}
113114
}
114115

115-
private string MakeUrl(string path, Dictionary<string, string> additionalParams)
116+
private string MakeUrl(string path, Dictionary<string, string> additionalParams, bool useSyncwrapper = false)
116117
{
117-
if (additionalParams == null)
118-
return intento.serverUrl + path;
118+
string url = useSyncwrapper ? intento.syncwrapperUrl : intento.serverUrl;
119+
if (additionalParams == null)
120+
return url + path;
119121

120-
UriBuilder uri = new UriBuilder(intento.serverUrl + path);
122+
UriBuilder uri = new UriBuilder(url + path);
121123
System.Collections.Specialized.NameValueCollection query = HttpUtility.ParseQueryString(uri.Query);
122124
foreach (KeyValuePair<string, string> pair in additionalParams)
123125
query[pair.Key] = pair.Value;

IntentoSDK/Intento.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ public class Intento
5454
{
5555
internal string apiKey;
5656
internal Dictionary<string, object> auth;
57-
internal string serverUrl;
58-
internal string otherUserAgent;
57+
internal string serverUrl;
58+
internal string syncwrapperUrl;
59+
internal string otherUserAgent;
5960
internal string version;
6061
internal Action<string, string, Exception> loggingCallback;
6162
internal int waitAsyncDelay = 0;
@@ -81,7 +82,10 @@ private Intento(
8182
this.apiKey = apiKey;
8283
this.auth = auth != null ? new Dictionary<string, object>(auth) : null;
8384
this.serverUrl = string.IsNullOrEmpty(path) ? "https://api.inten.to/" : path;
84-
otherUserAgent = userAgent;
85+
// path can be https://api2.inten.to/
86+
// this.syncwrapperUrl = this.serverUrl.Replace("https://api", "https://syncwrapper-memoq");
87+
this.syncwrapperUrl = "https://syncwrapper-memoq.inten.to/";
88+
otherUserAgent = userAgent;
8589
System.Net.ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
8690
this.waitAsyncDelay = waitAsyncDelay;
8791

0 commit comments

Comments
 (0)