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

Commit

Permalink
Merge pull request #10 from jforsell/develop
Browse files Browse the repository at this point in the history
Adding plain text parsing of messageML.
  • Loading branch information
jforsell authored Jun 28, 2016
2 parents db24aa2 + 65c57b4 commit 131e2bb
Show file tree
Hide file tree
Showing 10 changed files with 392 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/SymphonyOSS.RestApiClient/MessageML/DefaultCashTagRenderer.cs
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 src/SymphonyOSS.RestApiClient/MessageML/DefaultHashTagRenderer.cs
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 src/SymphonyOSS.RestApiClient/MessageML/DefaultLinkRenderer.cs
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 src/SymphonyOSS.RestApiClient/MessageML/DefaultMentionRenderer.cs
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 src/SymphonyOSS.RestApiClient/MessageML/DefaultNewLineRenderer.cs
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;
}
}
}
29 changes: 29 additions & 0 deletions src/SymphonyOSS.RestApiClient/MessageML/IXmlRenderer.cs
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 src/SymphonyOSS.RestApiClient/MessageML/MessageParser.cs
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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,14 @@
<Compile Include="Factories\AuthenticatorApiFactory.cs" />
<Compile Include="Factories\PodApiFactory.cs" />
<Compile Include="Api\IApiExecutor.cs" />
<Compile Include="MessageML\DefaultNewLineRenderer.cs" />
<Compile Include="MessageML\DefaultMentionRenderer.cs" />
<Compile Include="MessageML\DefaultLinkRenderer.cs" />
<Compile Include="MessageML\DefaultHashTagRenderer.cs" />
<Compile Include="MessageML\DefaultCashTagRenderer.cs" />
<Compile Include="MessageML\IXmlRenderer.cs" />
<Compile Include="MessageML\MessageBuilder.cs" />
<Compile Include="MessageML\MessageParser.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Api\RetryStrategyApiExecutor.cs" />
<Compile Include="Api\PodApi\PresenceApi.cs" />
Expand Down
Loading

0 comments on commit 131e2bb

Please sign in to comment.