Skip to content
This repository was archived by the owner on Oct 28, 2021. It is now read-only.

Commit 131e2bb

Browse files
authored
Merge pull request #10 from jforsell/develop
Adding plain text parsing of messageML.
2 parents db24aa2 + 65c57b4 commit 131e2bb

10 files changed

+392
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Licensed to the Symphony Software Foundation (SSF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SSF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
namespace SymphonyOSS.RestApiClient.MessageML
19+
{
20+
using System.Xml;
21+
22+
/// <summary>
23+
/// Renders a cash tag (<cash tag="CASH"/>) as $CASH.
24+
/// </summary>
25+
public class DefaultCashTagRenderer : IXmlRenderer
26+
{
27+
public string Render(XmlReader reader)
28+
{
29+
return "$" + reader.GetAttribute("tag");
30+
}
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Licensed to the Symphony Software Foundation (SSF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SSF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
namespace SymphonyOSS.RestApiClient.MessageML
19+
{
20+
using System.Xml;
21+
22+
/// <summary>
23+
/// Renders a hash tag (<hash tag="hash"/>) as #hash.
24+
/// </summary>
25+
public class DefaultHashTagRenderer : IXmlRenderer
26+
{
27+
public string Render(XmlReader reader)
28+
{
29+
return "#" + reader.GetAttribute("tag");
30+
}
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Licensed to the Symphony Software Foundation (SSF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SSF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
namespace SymphonyOSS.RestApiClient.MessageML
19+
{
20+
using System.Xml;
21+
22+
/// <summary>
23+
/// Renders a link (<a href="https://link"/>) as https://link.
24+
/// </summary>
25+
public class DefaultLinkRenderer : IXmlRenderer
26+
{
27+
public string Render(XmlReader reader)
28+
{
29+
return reader.GetAttribute("href");
30+
}
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Licensed to the Symphony Software Foundation (SSF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SSF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
namespace SymphonyOSS.RestApiClient.MessageML
19+
{
20+
using System.Text;
21+
using System.Xml;
22+
23+
/// <summary>
24+
/// Renders a user mention (<mention uid="12345"/>) as @12345.
25+
/// </summary>
26+
public class DefaultMentionRenderer : IXmlRenderer
27+
{
28+
public string Render(XmlReader reader)
29+
{
30+
var result = new StringBuilder();
31+
result.Append("@");
32+
var uid = long.Parse(reader.GetAttribute("uid"));
33+
result.Append(uid);
34+
return result.ToString();
35+
}
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Licensed to the Symphony Software Foundation (SSF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SSF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
namespace SymphonyOSS.RestApiClient.MessageML
19+
{
20+
using System;
21+
using System.Xml;
22+
23+
/// <summary>
24+
/// Renders a new line (<br/>).
25+
/// </summary>
26+
public class DefaultNewLineRenderer : IXmlRenderer
27+
{
28+
public string Render(XmlReader reader)
29+
{
30+
return Environment.NewLine;
31+
}
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Licensed to the Symphony Software Foundation (SSF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SSF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
namespace SymphonyOSS.RestApiClient.MessageML
19+
{
20+
using System.Xml;
21+
22+
/// <summary>
23+
/// Renders an XML element to a string.
24+
/// </summary>
25+
public interface IXmlRenderer
26+
{
27+
string Render(XmlReader reader);
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
// Licensed to the Symphony Software Foundation (SSF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SSF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
namespace SymphonyOSS.RestApiClient.MessageML
19+
{
20+
using System.IO;
21+
using System.Text;
22+
using System.Xml;
23+
24+
/// <summary>
25+
/// Provides a method for parsing messageML to plain text.
26+
/// </summary>
27+
public class MessageParser
28+
{
29+
private static readonly IXmlRenderer DefaultCashTagRenderer = new DefaultCashTagRenderer();
30+
31+
private static readonly IXmlRenderer DefaultHashTagRenderer = new DefaultHashTagRenderer();
32+
33+
private static readonly IXmlRenderer DefaultLinkRenderer = new DefaultLinkRenderer();
34+
35+
private static readonly IXmlRenderer DefaultMentionRenderer = new DefaultMentionRenderer();
36+
37+
private static readonly IXmlRenderer DefaultNewLineRenderer = new DefaultNewLineRenderer();
38+
39+
public MessageParser()
40+
{
41+
CashTagRenderer = DefaultCashTagRenderer;
42+
HashTagRenderer = DefaultHashTagRenderer;
43+
LinkRenderer = DefaultLinkRenderer;
44+
MentionRenderer = DefaultMentionRenderer;
45+
NewLineRenderer = DefaultNewLineRenderer;
46+
}
47+
48+
/// <summary>
49+
/// The renderer to use for cash tags (<cash tag="CASH"/>).
50+
/// </summary>
51+
public IXmlRenderer CashTagRenderer { get; set; }
52+
53+
/// <summary>
54+
/// The renderer to use for hash tags (<hash tag="hash"/>).
55+
/// </summary>
56+
public IXmlRenderer HashTagRenderer { get; set; }
57+
58+
/// <summary>
59+
/// The renderer to use for links (<a href="https://link"/>).
60+
/// </summary>
61+
public IXmlRenderer LinkRenderer { get; set; }
62+
63+
/// <summary>
64+
/// The renderer to use for @mentions (<mention uid="12345"/>).
65+
/// </summary>
66+
public IXmlRenderer MentionRenderer { get; set; }
67+
68+
/// <summary>
69+
/// The renderer to use for new lines (<br/>).
70+
/// </summary>
71+
public IXmlRenderer NewLineRenderer { get; set; }
72+
73+
/// <summary>
74+
/// Parses a messageML message to plain text.
75+
/// </summary>
76+
/// <param name="messageMl">The messageML as a string.</param>
77+
/// <returns>The plain text.</returns>
78+
public string GetPlainText(string messageMl)
79+
{
80+
var reader = XmlReader.Create(new StringReader(messageMl));
81+
var result = new StringBuilder();
82+
while (reader.Read())
83+
{
84+
if (reader.IsEmptyElement)
85+
{
86+
switch (reader.Name)
87+
{
88+
case "cash":
89+
result.Append(CashTagRenderer.Render(reader));
90+
break;
91+
case "hash":
92+
result.Append(HashTagRenderer.Render(reader));
93+
break;
94+
case "a":
95+
result.Append(LinkRenderer.Render(reader));
96+
break;
97+
case "mention":
98+
result.Append(MentionRenderer.Render(reader));
99+
break;
100+
case "br":
101+
result.Append(NewLineRenderer.Render(reader));
102+
break;
103+
}
104+
}
105+
else
106+
{
107+
result.Append(reader.Value);
108+
}
109+
}
110+
111+
return result.ToString();
112+
}
113+
}
114+
}

src/SymphonyOSS.RestApiClient/SymphonyOSS.RestApiClient.csproj

+7
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,14 @@
7070
<Compile Include="Factories\AuthenticatorApiFactory.cs" />
7171
<Compile Include="Factories\PodApiFactory.cs" />
7272
<Compile Include="Api\IApiExecutor.cs" />
73+
<Compile Include="MessageML\DefaultNewLineRenderer.cs" />
74+
<Compile Include="MessageML\DefaultMentionRenderer.cs" />
75+
<Compile Include="MessageML\DefaultLinkRenderer.cs" />
76+
<Compile Include="MessageML\DefaultHashTagRenderer.cs" />
77+
<Compile Include="MessageML\DefaultCashTagRenderer.cs" />
78+
<Compile Include="MessageML\IXmlRenderer.cs" />
7379
<Compile Include="MessageML\MessageBuilder.cs" />
80+
<Compile Include="MessageML\MessageParser.cs" />
7481
<Compile Include="Properties\AssemblyInfo.cs" />
7582
<Compile Include="Api\RetryStrategyApiExecutor.cs" />
7683
<Compile Include="Api\PodApi\PresenceApi.cs" />

0 commit comments

Comments
 (0)