Skip to content

Commit

Permalink
XML Export Type API updates
Browse files Browse the repository at this point in the history
  • Loading branch information
benkeen committed Jan 29, 2015
1 parent 54e13d1 commit 63aa811
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 12 deletions.
3 changes: 2 additions & 1 deletion plugins/exportTypes/SQL/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ Post the following JSON content to the following API path:
"export": {
"type": "SQL",
"settings" {

"tableName": "myTable",
"databaseType": "MySQL"
}
}
}
Expand Down
85 changes: 85 additions & 0 deletions plugins/exportTypes/XML/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
## XML Export Type

This Export Type lets you generate your random data in XML format. You can use this plugin in one of two ways:

1. Rely on the plugin generating the XML structure by just passing in `rootNodeName` and `recordNodeName` settings,
which govern the key node names, or
2. Pass in a `useCustomExportFormat` (true) and a `customTemplate` setting that contains a Smarty template to
control the generated markup.

See below for an example of each.


### Example API Usage

Here's a simple example that uses the `rootNodeName` and `recordNodeName` setting to generate the XM content. Just
POST the following JSON content to the following API path:
`http://[your site]/[generate data folder]/api/v1/data`

```javascript
{
"numRows": 15,
"rows": [
{
"type": "AlphaNumeric",
"title": "RandomPassword",
"settings": {
"placeholder": "LLLxxLLLxLL"
}
},
{
"type": "AlphaNumeric",
"title": "USZipcode",
"settings": {
"placeholder": "xxxxx"
}
}
],
"export": {
"type": "XML",
"settings": {
"rootNodeName": "rows",
"recordNodeName": "row"
}
}
}
```

Here's a second example. Like with the HTML Export Type, you can provide your own custom Smarty template to generate
the XML with. It's ungainly and a pain, since you need to serialize your template into a JSON string, but the option is
there if you want it.

```javascript
{
"numRows": 15,
"rows": [
{
"type": "AlphaNumeric",
"title": "RandomPassword",
"settings": {
"placeholder": "LLLxxLLLxLL"
}
},
{
"type": "AlphaNumeric",
"title": "USZipcode",
"settings": {
"placeholder": "xxxxx"
}
}
],
"export": {
"type": "XML",
"settings": {
"useCustomExportFormat": true,
"customTemplate": "{if $isFirstBatch}<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n<records>\n{/if}{foreach $rowData as $row} <record>{foreach from=$colData item=col name=c} <{$col}>{$row[$smarty.foreach.c.index]}</{$col}>{/foreach} </record>{/foreach}{if $isLastBatch}</records>{/if}"
}
}
}
```


### API help

For more information about the API, check out:
[http://benkeen.github.io/generatedata/api.html](http://benkeen.github.io/generatedata/api.html)
63 changes: 55 additions & 8 deletions plugins/exportTypes/XML/XML.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class XML extends ExportTypePlugin {
protected $exportTypeName = "XML";
protected $jsModules = array("XML.js");
protected $codeMirrorModes = array("xml", "smarty", "smartymixed");
protected $contentTypeHeader = "text/xml";
public $L = array();


Expand All @@ -23,19 +24,20 @@ public function __construct($runtimeContext) {

/**
* Generates the XML data.
* @see ExportTypePlugin::generate()
* @param $generator
* @return array
*/
function generate($generator) {
$postData = $generator->getUserSettings();
$useCustomXMLFormat = isset($postData["etXMLUseCustomExportFormat"]);
$this->genEnvironment = $generator->genEnvironment; // API / POST
$this->userSettings = $generator->getUserSettings();
$useCustomXMLFormat = $this->isUsingCustomXMLFormat();

$content = "";
if ($useCustomXMLFormat) {
$smartyTemplate = (get_magic_quotes_gpc()) ? stripslashes($postData["etXMLCustomHTMLSource"]) : $postData["etXMLCustomHTMLSource"];
$smartyTemplate = $this->getCustomTemplate();
$content = $this->generateCustomXML($generator, $smartyTemplate);
} else {
$content = $this->generateXML($generator, $postData);
$content = $this->generateXML($generator, $this->userSettings);
}

return array(
Expand Down Expand Up @@ -114,11 +116,12 @@ public function getAdditionalSettingsHTML() {
*
* @param object $generator the Generator object
* @param array $postData
* @return string
*/
private function generateXML($generator, $postData) {
$data = $generator->generateExportData();
$rootNodeName = $postData["etXMLRootNodeName"];
$recordNodeName = $postData["etXMLRecordNodeName"];
$rootNodeName = $this->getXMLRootNodeName();
$recordNodeName = $this->getXMLRecordNodeName();

$content = "";
if ($generator->isFirstBatch()) {
Expand Down Expand Up @@ -146,11 +149,55 @@ private function generateXML($generator, $postData) {
* This is used to generate custom XML formats.
*
* @param object $generator the Generator object
* @param string $smartyTemplate the Smarty content entered by
* @param string $smartyTemplate the Smarty content
* @return string
*/
private function generateCustomXML($generator, $smartyTemplate) {
$data = $generator->generateExportData();
return Templates::evalSmartyString($smartyTemplate, $data);
}

// TODO group these!

private function isUsingCustomXMLFormat() {
$usingXMLFormat = false;
if ($this->genEnvironment == GEN_ENVIRONMENT_API) {
$settings = $this->userSettings->export->settings;
$usingXMLFormat = property_exists($settings, "useCustomExportFormat") ? $settings->useCustomExportFormat : false;
} else {
$usingXMLFormat = $this->userSettings["etXMLUseCustomExportFormat"];
}
return $usingXMLFormat;
}


private function getCustomTemplate() {
$template = "";
if ($this->genEnvironment == GEN_ENVIRONMENT_API) {
$template = $this->userSettings->export->settings->customTemplate;
} else {
$template = (get_magic_quotes_gpc()) ? stripslashes($this->userSettings["etXMLCustomHTMLSource"]) : $this->userSettings["etXMLCustomHTMLSource"];
}
return $template;
}

private function getXMLRootNodeName() {
$name = "";
if ($this->genEnvironment == GEN_ENVIRONMENT_API) {
$name = $this->userSettings->export->settings->rootNodeName;
} else {
$name = $this->userSettings["etXMLRootNodeName"];
}
return $name;
}

private function getXMLRecordNodeName() {
$name = "";
if ($this->genEnvironment == GEN_ENVIRONMENT_API) {
$name = $this->userSettings->export->settings->recordNodeName;
} else {
$name = $this->userSettings["etXMLRecordNodeName"];
}
return $name;
}
}
5 changes: 2 additions & 3 deletions plugins/exportTypes/XML/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
"useCustomExportFormat": {
"type": "boolean"
},
"customFormat": {
"customTemplate": {
"type": "string"
}
},
"required": ["rootNodeName", "recordNodeName"]
}
}

0 comments on commit 63aa811

Please sign in to comment.