This repository has been archived by the owner on Oct 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from jforsell/develop
Adding plain text parsing of messageML.
- Loading branch information
Showing
10 changed files
with
392 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
src/SymphonyOSS.RestApiClient/MessageML/DefaultCashTagRenderer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Licensed to the Symphony Software Foundation (SSF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The SSF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
namespace SymphonyOSS.RestApiClient.MessageML | ||
{ | ||
using System.Xml; | ||
|
||
/// <summary> | ||
/// Renders a cash tag (<cash tag="CASH"/>) as $CASH. | ||
/// </summary> | ||
public class DefaultCashTagRenderer : IXmlRenderer | ||
{ | ||
public string Render(XmlReader reader) | ||
{ | ||
return "$" + reader.GetAttribute("tag"); | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/SymphonyOSS.RestApiClient/MessageML/DefaultHashTagRenderer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Licensed to the Symphony Software Foundation (SSF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The SSF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
namespace SymphonyOSS.RestApiClient.MessageML | ||
{ | ||
using System.Xml; | ||
|
||
/// <summary> | ||
/// Renders a hash tag (<hash tag="hash"/>) as #hash. | ||
/// </summary> | ||
public class DefaultHashTagRenderer : IXmlRenderer | ||
{ | ||
public string Render(XmlReader reader) | ||
{ | ||
return "#" + reader.GetAttribute("tag"); | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/SymphonyOSS.RestApiClient/MessageML/DefaultLinkRenderer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Licensed to the Symphony Software Foundation (SSF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The SSF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
namespace SymphonyOSS.RestApiClient.MessageML | ||
{ | ||
using System.Xml; | ||
|
||
/// <summary> | ||
/// Renders a link (<a href="https://link"/>) as https://link. | ||
/// </summary> | ||
public class DefaultLinkRenderer : IXmlRenderer | ||
{ | ||
public string Render(XmlReader reader) | ||
{ | ||
return reader.GetAttribute("href"); | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/SymphonyOSS.RestApiClient/MessageML/DefaultMentionRenderer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Licensed to the Symphony Software Foundation (SSF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The SSF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
namespace SymphonyOSS.RestApiClient.MessageML | ||
{ | ||
using System.Text; | ||
using System.Xml; | ||
|
||
/// <summary> | ||
/// Renders a user mention (<mention uid="12345"/>) as @12345. | ||
/// </summary> | ||
public class DefaultMentionRenderer : IXmlRenderer | ||
{ | ||
public string Render(XmlReader reader) | ||
{ | ||
var result = new StringBuilder(); | ||
result.Append("@"); | ||
var uid = long.Parse(reader.GetAttribute("uid")); | ||
result.Append(uid); | ||
return result.ToString(); | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/SymphonyOSS.RestApiClient/MessageML/DefaultNewLineRenderer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Licensed to the Symphony Software Foundation (SSF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The SSF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
namespace SymphonyOSS.RestApiClient.MessageML | ||
{ | ||
using System; | ||
using System.Xml; | ||
|
||
/// <summary> | ||
/// Renders a new line (<br/>). | ||
/// </summary> | ||
public class DefaultNewLineRenderer : IXmlRenderer | ||
{ | ||
public string Render(XmlReader reader) | ||
{ | ||
return Environment.NewLine; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Licensed to the Symphony Software Foundation (SSF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The SSF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
namespace SymphonyOSS.RestApiClient.MessageML | ||
{ | ||
using System.Xml; | ||
|
||
/// <summary> | ||
/// Renders an XML element to a string. | ||
/// </summary> | ||
public interface IXmlRenderer | ||
{ | ||
string Render(XmlReader reader); | ||
} | ||
} |
114 changes: 114 additions & 0 deletions
114
src/SymphonyOSS.RestApiClient/MessageML/MessageParser.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
// Licensed to the Symphony Software Foundation (SSF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The SSF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
namespace SymphonyOSS.RestApiClient.MessageML | ||
{ | ||
using System.IO; | ||
using System.Text; | ||
using System.Xml; | ||
|
||
/// <summary> | ||
/// Provides a method for parsing messageML to plain text. | ||
/// </summary> | ||
public class MessageParser | ||
{ | ||
private static readonly IXmlRenderer DefaultCashTagRenderer = new DefaultCashTagRenderer(); | ||
|
||
private static readonly IXmlRenderer DefaultHashTagRenderer = new DefaultHashTagRenderer(); | ||
|
||
private static readonly IXmlRenderer DefaultLinkRenderer = new DefaultLinkRenderer(); | ||
|
||
private static readonly IXmlRenderer DefaultMentionRenderer = new DefaultMentionRenderer(); | ||
|
||
private static readonly IXmlRenderer DefaultNewLineRenderer = new DefaultNewLineRenderer(); | ||
|
||
public MessageParser() | ||
{ | ||
CashTagRenderer = DefaultCashTagRenderer; | ||
HashTagRenderer = DefaultHashTagRenderer; | ||
LinkRenderer = DefaultLinkRenderer; | ||
MentionRenderer = DefaultMentionRenderer; | ||
NewLineRenderer = DefaultNewLineRenderer; | ||
} | ||
|
||
/// <summary> | ||
/// The renderer to use for cash tags (<cash tag="CASH"/>). | ||
/// </summary> | ||
public IXmlRenderer CashTagRenderer { get; set; } | ||
|
||
/// <summary> | ||
/// The renderer to use for hash tags (<hash tag="hash"/>). | ||
/// </summary> | ||
public IXmlRenderer HashTagRenderer { get; set; } | ||
|
||
/// <summary> | ||
/// The renderer to use for links (<a href="https://link"/>). | ||
/// </summary> | ||
public IXmlRenderer LinkRenderer { get; set; } | ||
|
||
/// <summary> | ||
/// The renderer to use for @mentions (<mention uid="12345"/>). | ||
/// </summary> | ||
public IXmlRenderer MentionRenderer { get; set; } | ||
|
||
/// <summary> | ||
/// The renderer to use for new lines (<br/>). | ||
/// </summary> | ||
public IXmlRenderer NewLineRenderer { get; set; } | ||
|
||
/// <summary> | ||
/// Parses a messageML message to plain text. | ||
/// </summary> | ||
/// <param name="messageMl">The messageML as a string.</param> | ||
/// <returns>The plain text.</returns> | ||
public string GetPlainText(string messageMl) | ||
{ | ||
var reader = XmlReader.Create(new StringReader(messageMl)); | ||
var result = new StringBuilder(); | ||
while (reader.Read()) | ||
{ | ||
if (reader.IsEmptyElement) | ||
{ | ||
switch (reader.Name) | ||
{ | ||
case "cash": | ||
result.Append(CashTagRenderer.Render(reader)); | ||
break; | ||
case "hash": | ||
result.Append(HashTagRenderer.Render(reader)); | ||
break; | ||
case "a": | ||
result.Append(LinkRenderer.Render(reader)); | ||
break; | ||
case "mention": | ||
result.Append(MentionRenderer.Render(reader)); | ||
break; | ||
case "br": | ||
result.Append(NewLineRenderer.Render(reader)); | ||
break; | ||
} | ||
} | ||
else | ||
{ | ||
result.Append(reader.Value); | ||
} | ||
} | ||
|
||
return result.ToString(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.