zorba-coders team mailing list archive
-
zorba-coders team
-
Mailing list archive
-
Message #24768
[Merge] lp:~zorba-coders/zorba/mz-htlm-module into lp:zorba/html-module
Markos Zaharioudakis has proposed merging lp:~zorba-coders/zorba/mz-htlm-module into lp:zorba/html-module.
Commit message:
XmlDataManager is not a singleton any more
Requested reviews:
Markos Zaharioudakis (markos-za)
For more details, see:
https://code.launchpad.net/~zorba-coders/zorba/mz-htlm-module/+merge/178585
XmlDataManager is not a singleton any more
--
https://code.launchpad.net/~zorba-coders/zorba/mz-htlm-module/+merge/178585
Your team Zorba Coders is subscribed to branch lp:zorba/html-module.
=== modified file 'src/html.xq.src/html.cpp'
--- src/html.xq.src/html.cpp 2011-10-06 07:40:17 +0000
+++ src/html.xq.src/html.cpp 2013-08-05 15:23:31 +0000
@@ -25,8 +25,8 @@
namespace zorba
{
- namespace htmlmodule
- {
+namespace htmlmodule
+{
//*****************************************************************************
//*****************************************************************************
@@ -36,72 +36,80 @@
{
}
+
ItemSequence_t
ParseFunction::evaluate(
- const ExternalFunction::Arguments_t& aArgs,
- const StaticContext* aSctxCtx,
- const DynamicContext* aDynCtx) const
- {
- std::auto_ptr<std::istringstream> iss;
- std::istream *is;
- String docString;
- Item lStringItem, lOptionsItem;
-
- if (aArgs.size() >= 1)
- {
- Iterator_t lArg0Iter = aArgs[0]->getIterator();
- lArg0Iter->open();
- lArg0Iter->next(lStringItem);
- lArg0Iter->close();
- }
-
- if ( lStringItem.isStreamable() )
- {
- //
- // The "iss" auto_ptr can NOT be used since it will delete the stream that,
- // in this case, is a data member inside another object and not dynamically
- // allocated.
- //
- // We can't replace "iss" with "is" since we still need the auto_ptr for
- // the case when the result is not streamable.
- //
- is = &lStringItem.getStream();
- }
- else
- {
- docString = lStringItem.getStringValue();
- iss.reset (new std::istringstream(docString.c_str()));
- is = iss.get();
- }
-
- if (aArgs.size() == 2)
- {
- Iterator_t lArg1Iter = aArgs[1]->getIterator();
- lArg1Iter->open();
- lArg1Iter->next(lOptionsItem);
- lArg1Iter->close();
- }
-
- return ItemSequence_t(new SingletonItemSequence(
- createHtmlItem( *is , lOptionsItem )));
- }
-
-//*****************************************************************************
-//*****************************************************************************
-
-ItemFactory* HtmlModule::theFactory = 0;
+ const ExternalFunction::Arguments_t& aArgs,
+ const StaticContext* aSctxCtx,
+ const DynamicContext* aDynCtx) const
+{
+ std::auto_ptr<std::istringstream> iss;
+ std::istream *is;
+ String docString;
+ Item lStringItem, lOptionsItem;
+
+ if (aArgs.size() >= 1)
+ {
+ Iterator_t lArg0Iter = aArgs[0]->getIterator();
+ lArg0Iter->open();
+ lArg0Iter->next(lStringItem);
+ lArg0Iter->close();
+ }
+
+ if ( lStringItem.isStreamable() )
+ {
+ //
+ // The "iss" auto_ptr can NOT be used since it will delete the stream that,
+ // in this case, is a data member inside another object and not dynamically
+ // allocated.
+ //
+ // We can't replace "iss" with "is" since we still need the auto_ptr for
+ // the case when the result is not streamable.
+ //
+ is = &lStringItem.getStream();
+ }
+ else
+ {
+ docString = lStringItem.getStringValue();
+ iss.reset (new std::istringstream(docString.c_str()));
+ is = iss.get();
+ }
+
+ if (aArgs.size() == 2)
+ {
+ Iterator_t lArg1Iter = aArgs[1]->getIterator();
+ lArg1Iter->open();
+ lArg1Iter->next(lOptionsItem);
+ lArg1Iter->close();
+ }
+
+ return ItemSequence_t(
+ new SingletonItemSequence(createHtmlItem(*is, lOptionsItem)));
+}
+
+
+//*****************************************************************************
+//*****************************************************************************
+HtmlModule::HtmlModule()
+{
+ Zorba* engine = Zorba::getInstance(0);
+
+ theFactory = engine->getItemFactory();
+}
+
HtmlModule::~HtmlModule()
{
for ( FuncMap_t::const_iterator lIter = theFunctions.begin();
lIter != theFunctions.end();
++lIter)
- {
- delete lIter->second;
- }
- theFunctions.clear();
+ {
+ delete lIter->second;
+ }
+ theFunctions.clear();
}
+
ExternalFunction*
HtmlModule::getExternalFunction(const String& aLocalname)
{
@@ -127,6 +135,8 @@
}
delete this;
}
+
+
//*****************************************************************************
//*****************************************************************************
=== modified file 'src/html.xq.src/html.h'
--- src/html.xq.src/html.h 2011-10-06 07:40:17 +0000
+++ src/html.xq.src/html.h 2013-08-05 15:23:31 +0000
@@ -26,87 +26,85 @@
{
namespace htmlmodule
{
-//*****************************************************************************
-//*****************************************************************************
- class HtmlModule : public ExternalModule
- {
- private:
- static ItemFactory* theFactory;
-
- protected:
- class ltstr
- {
- public:
- bool operator()(const String& s1, const String& s2) const
- {
- return s1.compare(s2) < 0;
- }
- };
-
- typedef std::map<String, ExternalFunction*, ltstr> FuncMap_t;
-
- FuncMap_t theFunctions;
-
- public:
- virtual ~HtmlModule();
-
- virtual String
- getURI() const { return "http://www.zorba-xquery.com/modules/converters/html"; }
-
- virtual ExternalFunction*
- getExternalFunction(const String& aLocalname);
-
- virtual void
- destroy();
-
- static ItemFactory*
- getItemFactory()
- {
- if(!theFactory)
- {
- theFactory = Zorba::getInstance(0)->getItemFactory();
- }
- return theFactory;
- }
- };
-
-//*****************************************************************************
-//*****************************************************************************
- class HtmlFunction : public ContextualExternalFunction
- {
- protected:
- const HtmlModule* theModule;
- public:
- HtmlFunction(const HtmlModule* aModule)
- : theModule(aModule) {};
-
- ~HtmlFunction() {};
-
- virtual String
- getURI() const { return theModule->getURI(); }
-
- };
-
-//*****************************************************************************
-//*****************************************************************************
- class ParseFunction : public HtmlFunction
- {
- public:
- ParseFunction(const HtmlModule* aModule);
-
- virtual String
- getLocalName() const { return "parse-internal"; }
-
- virtual ItemSequence_t
- evaluate(const ExternalFunction::Arguments_t& args,
- const StaticContext* aSctxCtx,
- const DynamicContext* aDynCtx) const;
- };
-
-
-
-
- } /* namespace htmlmodule */
+
+//*****************************************************************************
+//*****************************************************************************
+class HtmlModule : public ExternalModule
+{
+protected:
+ class ltstr
+ {
+ public:
+ bool operator()(const String& s1, const String& s2) const
+ {
+ return s1.compare(s2) < 0;
+ }
+ };
+
+ typedef std::map<String, ExternalFunction*, ltstr> FuncMap_t;
+
+protected:
+ ItemFactory * theFactory;
+ XmlDataManager_t theDataMgr;
+ FuncMap_t theFunctions;
+
+public:
+ HtmlModule();
+
+ virtual ~HtmlModule();
+
+ virtual String
+ getURI() const { return "http://www.zorba-xquery.com/modules/converters/html"; }
+
+ virtual ExternalFunction*
+ getExternalFunction(const String& aLocalname);
+
+ virtual void
+ destroy();
+
+ ItemFactory* getItemFactory() const { return theFactory; }
+
+ XmlDataManager* getDataMgr() const { return theDataMgr.get(); }
+};
+
+
+//*****************************************************************************
+//*****************************************************************************
+class HtmlFunction : public ContextualExternalFunction
+{
+protected:
+ const HtmlModule* theModule;
+
+public:
+ HtmlFunction(const HtmlModule* aModule) : theModule(aModule) {};
+
+ ~HtmlFunction() {};
+
+ virtual String
+ getURI() const { return theModule->getURI(); }
+};
+
+
+//*****************************************************************************
+//*****************************************************************************
+class ParseFunction : public HtmlFunction
+{
+public:
+ ParseFunction(const HtmlModule* aModule);
+
+ virtual String
+ getLocalName() const { return "parse-internal"; }
+
+ virtual ItemSequence_t
+ evaluate(const ExternalFunction::Arguments_t& args,
+ const StaticContext* aSctxCtx,
+ const DynamicContext* aDynCtx) const;
+};
+
+
+
+
+} /* namespace htmlmodule */
} /* namespace zorba */
#endif /* ZORBA_HTMLMODULE_HTML_H */
=== modified file 'src/html.xq.src/tidy_wrapper.h'
--- src/html.xq.src/tidy_wrapper.h 2011-08-18 23:42:49 +0000
+++ src/html.xq.src/tidy_wrapper.h 2013-08-05 15:23:31 +0000
@@ -35,166 +35,176 @@
namespace zorba
{
- namespace htmlmodule
- {
- class TidyReader {
- private:
- std::istream* theStream;
- // We need a buffer to support the unget function
- std::vector<unsigned int> theBuffer;
- public:
- TidyReader(std::istream* aStream) : theStream(aStream) {}
- TidyInputSource getInputSource()
- {
- TidyInputSource lResult;
- lResult.sourceData = this;
- lResult.getByte = &getByte;
- lResult.ungetByte = &ungetByte;
- lResult.eof = &isEof;
- return lResult;
- }
-
- public: // callback functions
- static int TIDY_CALL getByte(void* aData)
- {
- TidyReader* lReader = static_cast<TidyReader*>(aData);
- if (lReader->theBuffer.empty())
- return lReader->theStream->get();
- else
- {
- int lResult = lReader->theBuffer.back();
- lReader->theBuffer.pop_back();
- return lResult;
- }
- }
-
- static void TIDY_CALL ungetByte(void* aData, byte aByte)
- {
- TidyReader* lReader = static_cast<TidyReader*>(aData);
- lReader->theBuffer.push_back(aByte);
- }
-
- static Bool TIDY_CALL isEof(void* aData)
- {
- TidyReader* lReader = static_cast<TidyReader*>(aData);
- return lReader->theStream->eof() ? yes : no;
- }
- };
-
- static void checkRC(int rc, const char* errMsg)
- {
- if (rc > 1)
- {
- zorba::Item lError = Zorba::getInstance(0)->getItemFactory()
- ->createQName(
- "http://www.zorba-xquery.com/modules/converters/html",
- "InternalError");
- throw USER_EXCEPTION(lError, errMsg );
- }
- }
-
- static Bool setTidyOption(TidyDoc doc, const char* option, const char* value)
- {
- Bool ok = yes;
- TidyOptionId toID = tidyOptGetIdForName(option);
- if(toID < N_TIDY_OPTIONS)
- {
- ok = tidyOptSetValue(doc, toID, value);
- if (ok != yes)
- {
- zorba::Item lError = Zorba::getInstance(0)->getItemFactory()
- ->createQName(
- "http://www.zorba-xquery.com/modules/converters/html",
- "TidyOption");
- std::ostringstream lErrorMsg;
- lErrorMsg << "Error setting tidy option '" << option
- << "' with value '" << value << "'";
- throw USER_EXCEPTION(lError, lErrorMsg.str());
- }
- }
- else
- {
- return no;
- }
- return ok;
- }
-
- static Bool applyOptions(TidyDoc aDoc, zorba::Item &aOptions)
- {
- zorba::Iterator_t lAttributes, lElements;
- zorba::Item lAttr, lElementItem, lAttrName;
- zorba::String lStrName, lStrValue;
- Bool lRet = yes;
-
- if(!aOptions.isNull())
- {
- lElements = aOptions.getChildren();
- lElements->open();
- while (lElements->next(lElementItem)
- && lElementItem.getNodeKind () == store::StoreConsts::elementNode)
- {
- lAttributes = lElementItem.getAttributes();
- lAttributes->open();
- while (lAttributes->next(lAttr))
- {
- lAttr.getNodeName(lAttrName);
- if(lAttrName.getLocalName() == "name")
- lStrName = lAttr.getStringValue();
- else if(lAttrName.getLocalName() == "value")
- lStrValue = lAttr.getStringValue();
- }
- setTidyOption(aDoc, lStrName.c_str(), lStrValue.c_str());
- lAttributes->close();
- }
- lElements->close();
- }
- return lRet;
- }
-
- static zorba::Item createHtmlItem( std::istream& aStream , zorba::Item &aOptions)
- {
- TidyReader lReader(&aStream);
- TidyInputSource lInputSource = lReader.getInputSource();
-
- TidyBuffer output;
- tidyBufInit(&output);
- TidyBuffer errbuf;
- tidyBufInit(&errbuf);
- TidyDoc tDoc = tidyCreate();
-
- applyOptions(tDoc, aOptions);
-
- int rc = -1;
- rc = tidySetErrorBuffer(tDoc, &errbuf);
- checkRC(rc, "Could not set error buffer");
- rc = tidyParseSource(tDoc, &lInputSource);
- checkRC(rc, "Could not parse the source");
- rc = tidyCleanAndRepair(tDoc);
- checkRC(rc, "Could not clean and repair");
- rc = tidyRunDiagnostics(tDoc);
- if ( rc > 1 )
- rc = ( tidyOptSetBool(tDoc, TidyForceOutput, yes) ? rc : -1 );
-
- // Tidy does not support streaming for output, it only supports
- // something they call a "sink". Therefore we buffer it in a string.
- rc = tidySaveBuffer(tDoc, &output);
- checkRC(rc, "Could not save the buffer");
- std::string lResult((char*) output.bp, output.size);
- std::istringstream lStream(lResult);
-
- tidyBufFree(&output);
- tidyBufFree(&errbuf);
- tidyRelease(tDoc);
- XmlDataManager* lDM = Zorba::getInstance(0)->getXmlDataManager();
- try
- {
- return lDM->parseXML(lStream);
- } catch (ZorbaException&)
- {
- return NULL;//Zorba::getInstance(0)->getItemFactory()->createString(lResult);
- }
- }
- } /* namespace htmlmodule */
+namespace htmlmodule
+{
+
+class TidyReader
+{
+private:
+ std::istream* theStream;
+ // We need a buffer to support the unget function
+ std::vector<unsigned int> theBuffer;
+
+public:
+ TidyReader(std::istream* aStream) : theStream(aStream) {}
+
+ TidyInputSource getInputSource()
+ {
+ TidyInputSource lResult;
+ lResult.sourceData = this;
+ lResult.getByte = &getByte;
+ lResult.ungetByte = &ungetByte;
+ lResult.eof = &isEof;
+ return lResult;
+ }
+
+public: // callback functions
+ static int TIDY_CALL getByte(void* aData)
+ {
+ TidyReader* lReader = static_cast<TidyReader*>(aData);
+ if (lReader->theBuffer.empty())
+ return lReader->theStream->get();
+ else
+ {
+ int lResult = lReader->theBuffer.back();
+ lReader->theBuffer.pop_back();
+ return lResult;
+ }
+ }
+
+ static void TIDY_CALL ungetByte(void* aData, byte aByte)
+ {
+ TidyReader* lReader = static_cast<TidyReader*>(aData);
+ lReader->theBuffer.push_back(aByte);
+ }
+
+ static Bool TIDY_CALL isEof(void* aData)
+ {
+ TidyReader* lReader = static_cast<TidyReader*>(aData);
+ return lReader->theStream->eof() ? yes : no;
+ }
+};
+
+
+static void checkRC(int rc, const char* errMsg)
+{
+ if (rc > 1)
+ {
+ zorba::Item lError = Zorba::getInstance(0)->getItemFactory()->
+ createQName("http://www.zorba-xquery.com/modules/converters/html",
+ "InternalError");
+
+ throw USER_EXCEPTION(lError, errMsg );
+ }
+}
+
+
+static Bool setTidyOption(TidyDoc doc, const char* option, const char* value)
+{
+ Bool ok = yes;
+ TidyOptionId toID = tidyOptGetIdForName(option);
+ if(toID < N_TIDY_OPTIONS)
+ {
+ ok = tidyOptSetValue(doc, toID, value);
+ if (ok != yes)
+ {
+ zorba::Item lError = Zorba::getInstance(0)->getItemFactory()->
+ createQName("http://www.zorba-xquery.com/modules/converters/html",
+ "TidyOption");
+
+ std::ostringstream lErrorMsg;
+ lErrorMsg << "Error setting tidy option '" << option
+ << "' with value '" << value << "'";
+ throw USER_EXCEPTION(lError, lErrorMsg.str());
+ }
+ }
+ else
+ {
+ return no;
+ }
+ return ok;
+}
+
+
+static Bool applyOptions(TidyDoc aDoc, zorba::Item &aOptions)
+{
+ zorba::Iterator_t lAttributes, lElements;
+ zorba::Item lAttr, lElementItem, lAttrName;
+ zorba::String lStrName, lStrValue;
+ Bool lRet = yes;
+
+ if(!aOptions.isNull())
+ {
+ lElements = aOptions.getChildren();
+ lElements->open();
+ while (lElements->next(lElementItem)
+ && lElementItem.getNodeKind () == store::StoreConsts::elementNode)
+ {
+ lAttributes = lElementItem.getAttributes();
+ lAttributes->open();
+ while (lAttributes->next(lAttr))
+ {
+ lAttr.getNodeName(lAttrName);
+ if(lAttrName.getLocalName() == "name")
+ lStrName = lAttr.getStringValue();
+ else if(lAttrName.getLocalName() == "value")
+ lStrValue = lAttr.getStringValue();
+ }
+ setTidyOption(aDoc, lStrName.c_str(), lStrValue.c_str());
+ lAttributes->close();
+ }
+ lElements->close();
+ }
+ return lRet;
+}
+
+
+static zorba::Item createHtmlItem(std::istream& aStream , zorba::Item& aOptions)
+{
+ TidyReader lReader(&aStream);
+ TidyInputSource lInputSource = lReader.getInputSource();
+
+ TidyBuffer output;
+ tidyBufInit(&output);
+ TidyBuffer errbuf;
+ tidyBufInit(&errbuf);
+ TidyDoc tDoc = tidyCreate();
+
+ applyOptions(tDoc, aOptions);
+
+ int rc = -1;
+ rc = tidySetErrorBuffer(tDoc, &errbuf);
+ checkRC(rc, "Could not set error buffer");
+ rc = tidyParseSource(tDoc, &lInputSource);
+ checkRC(rc, "Could not parse the source");
+ rc = tidyCleanAndRepair(tDoc);
+ checkRC(rc, "Could not clean and repair");
+ rc = tidyRunDiagnostics(tDoc);
+ if ( rc > 1 )
+ rc = ( tidyOptSetBool(tDoc, TidyForceOutput, yes) ? rc : -1 );
+
+ // Tidy does not support streaming for output, it only supports
+ // something they call a "sink". Therefore we buffer it in a string.
+ rc = tidySaveBuffer(tDoc, &output);
+ checkRC(rc, "Could not save the buffer");
+ std::string lResult((char*) output.bp, output.size);
+ std::istringstream lStream(lResult);
+
+ tidyBufFree(&output);
+ tidyBufFree(&errbuf);
+ tidyRelease(tDoc);
+ XmlDataManager_t lDM = Zorba::getInstance(0)->getXmlDataManager();
+ try
+ {
+ return lDM->parseXML(lStream);
+ }
+ catch (ZorbaException&)
+ {
+ return NULL;//Zorba::getInstance(0)->getItemFactory()->createString(lResult);
+ }
+}
+
+} /* namespace htmlmodule */
} /* namespace zorba */
#endif //ZORBA_HTMLMODULE_TIDY_WRAPPER_H
Follow ups