//
// Created by tkawata on 1/6/2012.
//
+#include "DNContainerBuilder.h"
#include "TKLock.h"
#include "TKContainer.h"
#include "DNUtils.h"
#include "TKCell.h"
#include "TKDebug.h"
-
-#include "DNContainerBuilder.h"
+#include "DNGlobal.h"
DNContainerBuilder::DNContainerBuilder(TKContainer *container) : mContainer(container)
{
bool DNContainerBuilder::buildContainerFromXHTML(const char* docRoot)
{
-
mLock.lock();
mContainer->setContentPath(std::string(docRoot));
DNDirectory directory(docRoot);
- const DNFileList *flist = directory.getFileList("html|htm|xml");
-
+ const DNFileList *flist = directory.getFileList("html|htm|xml|xhtml");
+
//parse XHTML files
while(flist)
{
- parseXHTML(docRoot, flist->filePath.c_str());
+ std::string p = flist->getFilePath();
+ parseXHTML(docRoot, p.c_str());
flist = flist->next;
}
-
+
//build cells
while(!mPendingCellDefinitions.empty())
{
TKCell *newCell = NULL;
if (cellCode)
{
- newCell = mContainer->createCell(cellInfo->location, cellInfo->cellName, cellCode, cellInfo->startupScript);
+ newCell = mContainer->createCell(cellInfo->location, cellInfo->cellName, cellCode, cellInfo->customScript);
+ }
+ else
+ {
+ std::string message = std::string("Failed to construct cell: ").append(cellInfo->location).append("#").append(cellInfo->cellName);
+ message.append("\nCellCode '").append(cellInfo->cellCodeName).append("' couldn't be found.");
+ TKLog::printf("%s", message.c_str());
+ if (dnGlobal()->updateErrorStatus(DNGlobal::ERROR))
+ {
+ dnGlobal()->setMessage1("Initialization failed");
+ dnGlobal()->setMessage2(message);
+ }
}
if (newCell == NULL)
{
- TKLog::printf("Failed to construct cell:%s.",cellInfo->cellName.c_str());
+ std::string message = std::string("Failed to construct cell: ").append(cellInfo->location).append("#").append(cellInfo->cellName);
+ TKLog::printf("%s", message.c_str());
+ if (dnGlobal()->updateErrorStatus(DNGlobal::ERROR))
+ {
+ dnGlobal()->setMessage1("Initialization failed");
+ dnGlobal()->setMessage2(message);
+ }
}
mPendingCellDefinitions.pop();
}
}
else
{
- TKLog::printf("Failed to make connection (%s) cell:%s -> cell:%s", connInfo->connectionName.c_str(),connInfo->originCellName.c_str(), connInfo->targetCellName.c_str());
+ std::string message = std::string("Failed to make connection (").append(connInfo->connectionName).append(" cell:").append(connInfo->originCellName).append(" - > ").append(connInfo->targetCellName);
+ TKLog::printf("%s", message.c_str());
+ if (dnGlobal()->updateErrorStatus(DNGlobal::ERROR))
+ {
+ dnGlobal()->setMessage1("Initialization failed");
+ dnGlobal()->setMessage2(message);
+ }
}
mPendingConnections.pop();
}
-
mLock.unlock();
-
+
return true;
}
+void DNContainerBuilder::addCell(const char *path, std::string name, std::string cellcode, std::string customScript)
+{
+ DEBUG_TRACE("\n===== Define cell ================ \nName:%s/%s\nClass:%s\nCustom script:\n%s\n", path,name.c_str(), cellcode.c_str(),customScript.c_str());
+
+ PendingCellDefinition *newCellDefine = new PendingCellDefinition();
+ newCellDefine->location = path;
+ newCellDefine->cellName = name;
+ newCellDefine->cellCodeName = getFQNString(path, cellcode.c_str());
+ newCellDefine->customScript = customScript;
+
+ mPendingCellDefinitions.push(newCellDefine);
+}
+
+void DNContainerBuilder::addCellCode(const char *path, std::string name, std::string type, std::string cellScript)
+{
+ std::string fqnName = getFQNString(path, name.c_str());
+
+ DEBUG_TRACE("\n===== Define cell code class ===== \nClass:%s\nAPI:%s\n%s\n", fqnName.c_str(), type.c_str(), cellScript.c_str());
+
+ mContainer->createCellCode(fqnName, type, cellScript);
+}
+
+void DNContainerBuilder::addConnection(const char *path, std::string origine, std::string destination, std::string name)
+{
+ PendingCellConnection *newConnection = new PendingCellConnection();
+ newConnection->connectionName = name;
+ newConnection->originCellName = getFQNString(path, origine.c_str());
+ newConnection->targetCellName = getFQNString(path, destination.c_str());
+ mPendingConnections.push(newConnection);
+}
+
void DNContainerBuilder::parseXHTML(const char *docRoot, const char *path)
{
-
DNXML *xml = DNXML::createXMLFromFile(docRoot, path);
DNXMLElement *element = xml->getRoot();
- DNXMLElement *classElement = NULL;
- DNXMLElement *classScriptElement = NULL;
- DNXMLElement *classAPIElement = NULL;
- std::string className;
-
- DNXMLElement *cellElement = NULL;
- DNXMLElement *cellScriptElement = NULL;
std::string cellName;
- std::string cellCodeClassName;
-
+ std::string cellCodeName;
+ std::string cellAPIType;
+ std::string script;
+
+ int defineDepth = -1;
+
while(element)
{
- std::string eclass = element->getAttributeValue("class");
- if (eclass == "dennco-script")
+ std::string define = element->getAttributeValue("define");
+ if (define.length() > 0)
{
- DNXMLElement *te = element->outer;
- while(te)
+ if (defineDepth > 0)
{
- std::string teclass = te->getAttributeValue("class");
- if (teclass == "dennco-classdef")
- {
- if (classElement && classElement == te)
- {
- classScriptElement = element;
- }
- else
- {
- TKLog::printf("A syntax error is found in %s. dennco-script isn't properly defined.",path);
- }
-
- break;
- }
- else if (teclass == "dennco-cell")
+ std::string message = std::string("Syntax error while parsing ").append(path).append(". A defnie element seems to be nested.");
+ TKLog::printf("%s", message.c_str());
+ if (dnGlobal()->updateErrorStatus(DNGlobal::ERROR))
{
- if (cellElement && cellElement == te)
- {
- std::string s = element->getAttributeValue("href");
- if (s.length() > 0)
- {
- cellCodeClassName = s;
- }
- else
- {
- cellScriptElement = element;
- }
- }
- else
- {
- TKLog::printf("A syntax error is found in %s. dennco-script isn't properly defined.",path);
- }
-
- break;
+ dnGlobal()->setMessage1("Initialization failed");
+ dnGlobal()->setMessage2(message);
}
- te = te->outer;
- }
- if (!te)
- {
- TKLog::printf("A syntax error is found in %s. dennco-script doesn't belong to any of dennco-classdef or dennco-cell.", path);
}
-
- }
- else if (eclass == "dennco-classdef")
- {
- className = element->getAttributeValue("name");
- if (className.length() > 0)
+ else
{
- if (!classElement)
+ if (define == "cell")
{
- classElement = element;
+ cellName = element->getAttributeValue("name");
+ defineDepth = element->depth;
+ }
+ else if (define == "cellcode")
+ {
+ cellCodeName = element->getAttributeValue("name");
+ cellAPIType = element->getAttributeValue("type");
+ defineDepth = element->depth;
}
else
{
- TKLog::printf("A syntax error is found in %s. Nested dennco-classdef?", path);
+ std::string message = std::string("Syntax error while parsing ").append(path).append(". defnie='").append("' isn't correct.");
+ TKLog::printf("%s", message.c_str());
+ if (dnGlobal()->updateErrorStatus(DNGlobal::ERROR))
+ {
+ dnGlobal()->setMessage1("Initialization failed");
+ dnGlobal()->setMessage2(message);
+ }
}
}
- else
- {
- TKLog::printf("A syntax error is found in %s. dennco-classdef doesn't have name attribute", path);
- }
}
- else if (eclass == "dennco-cell")
+
+ std::string parameter = element->getAttributeValue("parameter");
+ if (parameter.length() > 0)
{
- cellName = element->getAttributeValue("name");
- trimString(cellName);
- if (cellName.length() > 0)
+ if (parameter == "script")
{
- if (!cellElement)
- {
- cellElement = element;
- }
- else
- {
- TKLog::printf("A syntax error is found in %s. Nested dennco-cell?", path);
- }
+ script = element->text;
}
- else
+ else if (parameter == "cellcode")
{
- TKLog::printf("A syntax error is found in %s. dennco-cell doesn't have name attribute", path);
+ cellCodeName = element->getAttributeValue("href");
}
- }
- else if (eclass == "dennco-connect")
- {
- if (cellElement)
+ else if (parameter== "connection")
{
- //connection
std::string target = element->getAttributeValue("href");
- trimString(target);
- if (target.length() > 0)
- {
- PendingCellConnection *newConnection = new PendingCellConnection();
- newConnection->connectionName = getFQNString(path, cellName.c_str());
- newConnection->originCellName = getFQNString(path, cellName.c_str());
- newConnection->targetCellName = getFQNString(path, target.c_str());
- mPendingConnections.push(newConnection);
- }
- else
+ std::string rname = element->getAttributeValue("receptor");
+ if (rname.length()==0)
{
- TKLog::printf("A syntax error is found in %s. dennco-connect doesn't have link target (href) attribute.", path);
+ rname = cellName;
}
+ addConnection(path, cellName, target, rname);
}
else
{
- TKLog::printf("A syntax error is found in %s. dennco-connect doesn't belong to any of dennco-cell.", path);
- }
- }
- else if (eclass == "dennco-api")
- {
- DNXMLElement *te = element->outer;
- while(te)
- {
- std::string teclass = te->getAttributeValue("class");
- if (teclass == "dennco-classdef")
+ std::string message = std::string("Syntax error while parsing ").append(path).append(". parameter='").append(parameter).append("' isn't correct.");
+ TKLog::printf("%s", message.c_str());
+ if (dnGlobal()->updateErrorStatus(DNGlobal::ERROR))
{
- if (classElement && classElement == te)
- {
- classAPIElement = element;
- }
- else
- {
- TKLog::printf("A syntax error is found in %s. dennco-api isn't properly defined.",path);
- }
-
- break;
+ dnGlobal()->setMessage1("Initialization failed");
+ dnGlobal()->setMessage2(message);
}
- te = te->outer;
- }
- if (!te)
- {
- TKLog::printf("A syntax error is found in %s. dennco-api doesn't belong to any of dennco-cell.", path);
}
}
-
+
//go to next element
//
if (element->inner)
{
element = element->outer;
}
+ } while(element && element->next == NULL && element != xml->getRoot());
+
+ if (!element)
+ break;
+ element = element->next;
- // the processed element was cell code class definition.
- // define it now.
- if (classElement && (element == NULL || element->depth <= classElement->depth))
+ if (defineDepth >= 0 && (element == NULL || element->depth <= defineDepth))
+ {
+ defineDepth = -1;
+ if (cellName.length()>0)
{
- std::string _cellCodeName = getFQNString(path,className.c_str());
- std::string _cellCodeAPIType = classAPIElement ? classAPIElement->text : "";
- std::string _cellCodeScript = classScriptElement ? classScriptElement->text : "";
-
- DEBUG_TRACE("\n===== Define cell code class ===== \nClass:%s\nAPI:%s\n%s\n", _cellCodeName.c_str(), _cellCodeAPIType.c_str(), _cellCodeScript.c_str());
-
- mContainer->createCellCode(_cellCodeName, _cellCodeAPIType, _cellCodeScript);
-
-
- classElement = NULL;
- classScriptElement = NULL;
- classAPIElement = NULL;
- className = "";
+ addCell(path, cellName, cellCodeName, script);
+ cellName = "";
+ cellCodeName = "";
+ script = "";
}
-
- // the processed element was cell definition.
- // define it now.
- if (cellElement && (element == NULL || element->depth <= cellElement->depth))
+ else
{
- //scriptless celldef
- std::string _cellName = cellName;
- std::string _cellNameFQN = getFQNString(path, cellName.c_str());
- std::string _cellCodeName = getFQNString(path, cellCodeClassName.c_str());
- std::string _cellScript = cellScriptElement->text;
-
- DEBUG_TRACE("\n===== Define cell ================ \nName:%s\nClass:%s\nStartup script:\n%s\n", _cellNameFQN.c_str(), _cellCodeName.c_str(),_cellScript.c_str());
-
-
- PendingCellDefinition *newCellDefine = new PendingCellDefinition();
- newCellDefine->location = path;
- newCellDefine->cellName = _cellName;
- newCellDefine->cellCodeName = _cellCodeName;
- newCellDefine->startupScript = _cellScript;
-
- mPendingCellDefinitions.push(newCellDefine);
-
- cellElement = NULL;
- cellScriptElement = NULL;
- cellName = "";
- cellCodeClassName = "";
+ addCellCode(path, cellCodeName, cellAPIType, script);
+ cellCodeName = "";
+ cellAPIType = "";
+ script = "";
}
-
- } while(element && !element->next && element != xml->getRoot());
-
- if (!element)
- break;
- element = element->next;
+ }
}
}
+ if (xml)
+ delete xml;
}