Skip to content

Instantly share code, notes, and snippets.

@Dheer-Rajpoot
Created March 31, 2017 07:27
Show Gist options
  • Save Dheer-Rajpoot/f258c85debc7429f7abed80c74d2c8af to your computer and use it in GitHub Desktop.
Save Dheer-Rajpoot/f258c85debc7429f7abed80c74d2c8af to your computer and use it in GitHub Desktop.
[RequiredEndpointPlugins(typeof(XMLSystemSettings))]
public class ReadXMLDataStepProcessor : BaseReadDataStepProcessor
{
public ReadXMLDataStepProcessor()
{
}
protected override void ReadData(
Endpoint endpoint,
PipelineStep pipelineStep,
PipelineContext pipelineContext)
{
if (endpoint == null)
{
throw new ArgumentNullException(nameof(endpoint));
}
if (pipelineStep == null)
{
throw new ArgumentNullException(nameof(pipelineStep));
}
if (pipelineContext == null)
{
throw new ArgumentNullException(nameof(pipelineContext));
}
var logger = pipelineContext.PipelineBatchContext.Logger;
//
//get the file path from the plugin on the endpoint
var settings = endpoint.GetXMLSystemSettings();
if (settings == null)
{
return;
}
if (string.IsNullOrWhiteSpace(settings.XMLPath))
{
logger.Error(
"No path is specified on the endpoint. " +
"(pipeline step: {0}, endpoint: {1})",
pipelineStep.Name, endpoint.Name);
return;
}
//
//if the path is relative, the base directory is used to build an
//absolute path so that when this code runs on the Sitecore server,
//relative paths will be based on the webroot
var path = settings.XMLPath;
Uri uriResult;
bool result = Uri.TryCreate(path, UriKind.Absolute, out uriResult)
&& (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);
if (!result)
{
if (!Path.IsPathRooted(path))
{
path = string.Format("{0}{1}", AppDomain.CurrentDomain.BaseDirectory, path);
}
//
if (!File.Exists(path))
{
logger.Error(
"The path specified on the endpoint does not exist. " +
"(pipeline step: {0}, endpoint: {1}, path: {2})",
pipelineStep.Name, endpoint.Name, path);
return;
}
}
var lines = new List<string[]>();
XmlDocument document = new XmlDocument();
document.Load(path);
XmlNodeList xmlNodeList = document.GetElementsByTagName(settings.XMLNodeName);
XmlNode[] nodeArray = xmlNodeList.Cast<XmlNode>().ToArray();
for (int i = 0; i < xmlNodeList.Count; i++)
{
List<string> strs = new List<string>();
for (int j = 0; j < xmlNodeList[i].ChildNodes.Count; j++)
{
strs.Add(xmlNodeList[i].ChildNodes[j].InnerText);
}
lines.Add(strs.ToArray());
}
//
//add the data that was read from the xml file to a plugin
var dataSettings = new IterableDataSettings(nodeArray);
logger.Info(
"{0} rows were read from the file. (pipeline step: {1}, endpoint: {2})",
lines.Count, pipelineStep.Name, endpoint.Name);
//
//add the plugin to the pipeline context
pipelineContext.Plugins.Add(dataSettings);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment