Created
January 11, 2010 20:21
-
-
Save billywhizz/274570 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#include "./xml_syntax_error.h" | |
#include "./xsl_transform.h" | |
#include "./xml_sax_parser.h" | |
#include "./xml_document.h" | |
namespace libxmljs { | |
inline v8::Handle<v8::Value> | |
BuildDoc(xmlDoc *doc, JsObj *errors) { | |
if (doc == NULL) { | |
xmlFreeDoc(doc); | |
xmlError *error = xmlGetLastError(); | |
if (error) { | |
return THROW_SYNTAX_ERROR(error); | |
} else { | |
return v8::ThrowException(v8::Exception::Error( | |
v8::String::New("Could not parse XML string"))); | |
} | |
return v8::Null(); | |
} | |
v8::Handle<v8::Object> jsDoc = | |
LXJS_GET_MAYBE_BUILD(XmlDocument, xmlDoc, doc); | |
XmlDocument *document = LibXmlObj::Unwrap<XmlDocument>(jsDoc); | |
document->errors = v8::Persistent<v8::Array>::Cast( | |
JsObj::UnwrapNonXmlObj(errors)); | |
return jsDoc; | |
} | |
v8::Handle<v8::Value> | |
Transform(const v8::Arguments& args) { | |
v8::HandleScope scope; | |
if (!(args[0]->IsString() && args[1]->IsString())) | |
return v8::ThrowException(v8::Exception::Error( | |
v8::String::New("Must supply Transform with 2 strings"))); | |
v8::Local<v8::Array> jsErrArray = v8::Array::New(); | |
JsObj *errArray = JsObj::WrapNonXmlObj(jsErrArray); | |
xmlResetLastError(); | |
xmlSetStructuredErrorFunc(reinterpret_cast<void *>(errArray), | |
XmlSyntaxError::PushToArray); | |
v8::String::Utf8Value strxsl(args[0]->ToString()); | |
xmlDoc *style = xmlReadMemory(*strxsl, strxsl.length(), NULL, "UTF-8", 0); | |
//xsltStylesheetPtr xslt = xsltParseStylesheetFile((const xmlChar *)*strxsl); | |
xsltStylesheetPtr cur = xsltLoadStylesheetPI(style); | |
cur = xsltParseStylesheetDoc(style); | |
v8::String::Utf8Value strxml(args[1]->ToString()); | |
xmlDoc *xmldoc = xmlReadMemory(*strxml, strxml.length(), NULL, "UTF-8", 0); | |
const char *params[0]; | |
params[0] = NULL; | |
xmlDoc *xmlres = xsltApplyStylesheet(cur, xmldoc, params); | |
//xsltSaveResultToFile(stdout, res, cur); | |
xmlSetStructuredErrorFunc(NULL, NULL); | |
xsltFreeStylesheet(cur); | |
xmlFreeDoc(xmldoc); | |
return BuildDoc(xmlres, errArray); | |
} | |
void | |
XslTransform::Initialize(v8::Handle<v8::Object> target) { | |
v8::HandleScope scope; | |
LIBXMLJS_SET_METHOD(target, | |
"transform", | |
Transform); | |
XmlSaxParser::Initialize(target); | |
} | |
} // namespace libxmljs |
This file contains hidden or 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
#ifndef SRC_XSL_TRANSFORM_H_ | |
#define SRC_XSL_TRANSFORM_H_ | |
#include "./libxmljs.h" | |
namespace libxmljs { | |
class XslTransform : public LibXmlObj { | |
public: | |
static void Initialize(v8::Handle<v8::Object> target); | |
private: | |
xsltStylesheetPtr *stylesheets; | |
}; | |
} // namespace libxmljs | |
#endif // SRC_XSL_TRANSFORM_H_ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment