aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Kummerländer2014-04-29 19:24:58 +0200
committerAdrian Kummerländer2014-04-29 19:24:58 +0200
commitddfe25807ef90ddd3d10fd4532875ffdb5d5e9c5 (patch)
treefb8a7851cf2bf5fa4eac3420d218745cf0930756
parentd573e3160131e6776a9c148ca3a6754533378a76 (diff)
downloadInputXSLT-ddfe25807ef90ddd3d10fd4532875ffdb5d5e9c5.tar
InputXSLT-ddfe25807ef90ddd3d10fd4532875ffdb5d5e9c5.tar.gz
InputXSLT-ddfe25807ef90ddd3d10fd4532875ffdb5d5e9c5.tar.bz2
InputXSLT-ddfe25807ef90ddd3d10fd4532875ffdb5d5e9c5.tar.lz
InputXSLT-ddfe25807ef90ddd3d10fd4532875ffdb5d5e9c5.tar.xz
InputXSLT-ddfe25807ef90ddd3d10fd4532875ffdb5d5e9c5.tar.zst
InputXSLT-ddfe25807ef90ddd3d10fd4532875ffdb5d5e9c5.zip
Simplified external function result node set
* all functions return a single "error" node in the case that something went wrong * otherwise they return one or more "result" nodes that contain the function results and may be distinguished by one or more attributes * this makes handling the return values easier and more pleasing to the eye as not everything has to be prefixed by "self::*" * updated test transformation accordingly
-rw-r--r--dummy/transform.xsl28
-rw-r--r--src/function/read_directory.cc34
-rw-r--r--src/function/read_file.cc29
-rw-r--r--src/function/read_xml_file.cc35
4 files changed, 35 insertions, 91 deletions
diff --git a/dummy/transform.xsl b/dummy/transform.xsl
index 359db05..eab7a81 100644
--- a/dummy/transform.xsl
+++ b/dummy/transform.xsl
@@ -20,11 +20,11 @@
<div id="raw">
<xsl:variable name="testFile" select="external:read-file('test.txt')"/>
<xsl:choose>
- <xsl:when test="$testFile/self::status = 'successful'">
- <xsl:value-of select="$testFile/self::content"/>
+ <xsl:when test="$testFile/self::error">
+ Could not read text file.
</xsl:when>
<xsl:otherwise>
- Could not read text file.
+ <xsl:value-of select="$testFile[@name='test.txt']"/>
</xsl:otherwise>
</xsl:choose>
</div>
@@ -32,15 +32,15 @@
<div id="xml">
<xsl:variable name="testXml" select="external:read-xml-file('test.txt')"/>
<xsl:choose>
- <xsl:when test="$testXml/self::status = 'successful'">
+ <xsl:when test="$testXml/self::error">
+ Could not read XML file.
+ </xsl:when>
+ <xsl:otherwise>
<ul id="xml">
- <xsl:for-each select="$testXml/self::content/tester/eintrag">
+ <xsl:for-each select="$testXml[@name='test.txt']/tester/eintrag">
<li><xsl:value-of select="."/></li>
</xsl:for-each>
</ul>
- </xsl:when>
- <xsl:otherwise>
- Could not read XML file.
</xsl:otherwise>
</xsl:choose>
</div>
@@ -48,20 +48,20 @@
<div id="filelists">
<xsl:variable name="fileList" select="external:read-directory('../')"/>
<xsl:choose>
- <xsl:when test="$fileList/self::status = 'successful'">
+ <xsl:when test="$fileList/self::error">
+ Could not read directory.
+ </xsl:when>
+ <xsl:otherwise>
<ul id="filelist1">
- <xsl:for-each select="$fileList/self::content/item[@type='file']">
+ <xsl:for-each select="$fileList[@type='file']">
<li><xsl:value-of select="."/></li>
</xsl:for-each>
</ul>
<ul id="filelist2">
- <xsl:for-each select="$fileList/self::content/item[@type='directory']">
+ <xsl:for-each select="$fileList[@type='directory']">
<li><xsl:value-of select="."/></li>
</xsl:for-each>
</ul>
- </xsl:when>
- <xsl:otherwise>
- Could not read directory.
</xsl:otherwise>
</xsl:choose>
</div>
diff --git a/src/function/read_directory.cc b/src/function/read_directory.cc
index b0ae13f..686767d 100644
--- a/src/function/read_directory.cc
+++ b/src/function/read_directory.cc
@@ -37,15 +37,11 @@ xalan::XObjectPtr FunctionReadDirectory::execute(
);
if ( boost::filesystem::is_directory(directoryPath) ) {
- xercesc::DOMElement* const contentNode(
- domDocument->createElement(*XercesStringGuard("content"))
- );
-
this->fs_context_.iterate(
argument->str(),
- [&domDocument, &contentNode](const boost::filesystem::path& p) {
+ [&domDocument, &rootNode](const boost::filesystem::path& p) {
xercesc::DOMElement* const itemNode(
- domDocument->createElement(*XercesStringGuard("item"))
+ domDocument->createElement(*XercesStringGuard("result"))
);
switch ( boost::filesystem::status(p).type() ) {
@@ -77,35 +73,13 @@ xalan::XObjectPtr FunctionReadDirectory::execute(
);
itemNode->appendChild(textNode);
- contentNode->appendChild(itemNode);
+ rootNode->appendChild(itemNode);
});
-
- xercesc::DOMElement* const resultNode(
- domDocument->createElement(*XercesStringGuard("status"))
- );
-
- xercesc::DOMText* const resultTextNode(
- domDocument->createTextNode(
- *XercesStringGuard("successful")
- )
- );
-
- resultNode->appendChild(resultTextNode);
-
- rootNode->appendChild(contentNode);
- rootNode->appendChild(resultNode);
} else {
xercesc::DOMElement* const resultNode(
- domDocument->createElement(*XercesStringGuard("status"))
- );
-
- xercesc::DOMText* const resultTextNode(
- domDocument->createTextNode(
- *XercesStringGuard("error")
- )
+ domDocument->createElement(*XercesStringGuard("error"))
);
- resultNode->appendChild(resultTextNode);
rootNode->appendChild(resultNode);
}
}
diff --git a/src/function/read_file.cc b/src/function/read_file.cc
index cfbec59..83456c6 100644
--- a/src/function/read_file.cc
+++ b/src/function/read_file.cc
@@ -48,43 +48,28 @@ xalan::XObjectPtr FunctionReadFile::execute(
(std::istreambuf_iterator<char>())
);
- xercesc::DOMElement* const contentNode(
- domDocument->createElement(*XercesStringGuard("content"))
- );
-
- xercesc::DOMText* const contentTextNode(
- domDocument->createTextNode(
- *XercesStringGuard(fileContent)
- )
+ xercesc::DOMElement* const resultNode(
+ domDocument->createElement(*XercesStringGuard("result"))
);
- xercesc::DOMElement* const resultNode(
- domDocument->createElement(*XercesStringGuard("status"))
+ resultNode->setAttribute(
+ *XercesStringGuard("name"),
+ *XercesStringGuard(filePath.filename().string())
);
xercesc::DOMText* const resultTextNode(
domDocument->createTextNode(
- *XercesStringGuard("successful")
+ *XercesStringGuard(fileContent)
)
);
- contentNode->appendChild(contentTextNode);
resultNode->appendChild(resultTextNode);
-
- rootNode->appendChild(contentNode);
rootNode->appendChild(resultNode);
} else {
xercesc::DOMElement* const resultNode(
- domDocument->createElement(*XercesStringGuard("status"))
+ domDocument->createElement(*XercesStringGuard("error"))
);
- xercesc::DOMText* const resultTextNode(
- domDocument->createTextNode(
- *XercesStringGuard("error")
- )
- );
-
- resultNode->appendChild(resultTextNode);
rootNode->appendChild(resultNode);
}
}
diff --git a/src/function/read_xml_file.cc b/src/function/read_xml_file.cc
index 52f7735..7b54732 100644
--- a/src/function/read_xml_file.cc
+++ b/src/function/read_xml_file.cc
@@ -40,48 +40,33 @@ xalan::XObjectPtr FunctionReadXmlFile::execute(
);
if ( boost::filesystem::is_regular_file(filePath) ) {
- xercesc::DOMElement* const contentNode(
- domDocument->createElement(*XercesStringGuard("content"))
+ xercesc::DOMElement* const resultNode(
+ domDocument->createElement(*XercesStringGuard("result"))
+ );
+
+ resultNode->setAttribute(
+ *XercesStringGuard("name"),
+ *XercesStringGuard(filePath.filename().string())
);
xercesc::XercesDOMParser parser;
boost::filesystem::ifstream file(filePath);
parser.parse(xalan::XSLTInputSource(file));
- xercesc::DOMNode* const contentTreeNode(
+ xercesc::DOMNode* const resultTreeNode(
domDocument->importNode(
parser.getDocument()->getDocumentElement(),
true
)
);
- xercesc::DOMElement* const resultNode(
- domDocument->createElement(*XercesStringGuard("status"))
- );
-
- xercesc::DOMText* const resultTextNode(
- domDocument->createTextNode(
- *XercesStringGuard("successful")
- )
- );
-
- contentNode->appendChild(contentTreeNode);
- resultNode->appendChild(resultTextNode);
-
- rootNode->appendChild(contentNode);
+ resultNode->appendChild(resultTreeNode);
rootNode->appendChild(resultNode);
} else {
xercesc::DOMElement* const resultNode(
- domDocument->createElement(*XercesStringGuard("status"))
- );
-
- xercesc::DOMText* const resultTextNode(
- domDocument->createTextNode(
- *XercesStringGuard("error")
- )
+ domDocument->createElement(*XercesStringGuard("error"))
);
- resultNode->appendChild(resultTextNode);
rootNode->appendChild(resultNode);
}
}