Skip to content

Commit

Permalink
Merge pull request #211 from melven/importElementFromStringAtIndex
Browse files Browse the repository at this point in the history
Import element from string at index
  • Loading branch information
joergbrech authored Apr 21, 2022
2 parents b7e4e0c + b0e2f84 commit 64d23e5
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 12 deletions.
34 changes: 34 additions & 0 deletions src/tixi.h
Original file line number Diff line number Diff line change
Expand Up @@ -2024,6 +2024,40 @@ DLL_EXPORT ReturnCode tixiExportElementAsString (const TixiDocumentHandle handle
DLL_EXPORT ReturnCode tixiImportElementFromString (const TixiDocumentHandle handle, const char *parentPath, const char *xmlImportString);


/**
@brief Imports a char-string as a child element into an existing tixi-document
Creates a new element with the content of the string and checks if it is well formed.
The element name is defined through the root element in the imported string.
Elements with the same name can be added multiple times.
<b>Fortran syntax:</b>
tixi_import_element_from_string( integer handle, character*n parent_path, character*n xmlImportString, integer error )
@param[in] handle file handle as returned by ::tixiCreateDocument
@param[in] parentPath an XPath compliant path to an element in the document
specified by handle (see section \ref XPathExamples above)
into which the new element is to be inserted. The parent
element has to exist already.
@param[in] index the position index where the new node should be created.
@param[in] xmlImportString the string with the xml-content
@return
- SUCCESS if successfully added the text element
- NOT_WELL_FORMED if importing of the string succeeds but test for well-formedness fails
- INVALID_HANDLE if the handle is not valid
- INVALID_XPATH if elementPath is not a well-formed XPath-expression
- ELEMENT_PATH_NOT_UNIQUE if parentPath resolves not to a single element but to a list of elements
- ELEMENT_NOT_FOUND if parentPath points to a non-existing element
- ALREADY_SAVED if element should be added to an already saved document
*/
DLL_EXPORT ReturnCode tixiImportElementFromStringAtIndex (const TixiDocumentHandle handle, const char *parentPath, int index, const char *xmlImportString);


/*@}*/

/**
Expand Down
43 changes: 31 additions & 12 deletions src/tixiImpl.c
Original file line number Diff line number Diff line change
Expand Up @@ -3177,6 +3177,11 @@ DLL_EXPORT ReturnCode tixiExportElementAsString(const TixiDocumentHandle handle,
}

DLL_EXPORT ReturnCode tixiImportElementFromString (const TixiDocumentHandle handle, const char *parentPath, const char *xmlImportString)
{
return tixiImportElementFromStringAtIndex(handle, parentPath, -1, xmlImportString);
}

DLL_EXPORT ReturnCode tixiImportElementFromStringAtIndex (const TixiDocumentHandle handle, const char *parentPath, int index, const char *xmlImportString)
{
TixiDocument *document = getDocument(handle);
xmlDocPtr xmlDocument = NULL;
Expand All @@ -3186,6 +3191,8 @@ DLL_EXPORT ReturnCode tixiImportElementFromString (const TixiDocumentHandle hand
xmlBufferPtr buffer;
int textLen;
xmlParserErrors parseErrors = 0;
int i = 1;
xmlNodePtr targetNode = NULL;

if (!document) {
printMsg(MESSAGETYPE_ERROR, "Error: Invalid document handle.\n");
Expand All @@ -3199,23 +3206,35 @@ DLL_EXPORT ReturnCode tixiImportElementFromString (const TixiDocumentHandle hand
}

error = checkElement(document->xpathContext, parentPath, &parentElement);
if (error) {
return error;
}

if (!error) {
parseErrors = xmlParseInNodeContext(parentElement, xmlImportString, strlen(xmlImportString), 0, &newElement);
parseErrors = xmlParseInNodeContext(parentElement, xmlImportString, strlen(xmlImportString), 0, &newElement);
if (parseErrors) {
printMsg(MESSAGETYPE_ERROR, "Error: XML-string to import is not wellformed!\n");
return NOT_WELL_FORMED;
}

if (!parseErrors) {

// structure change!, we have to empty the xpath cache
XPathClearCache(document->xpathCache);
/* find node where new node should be inserted before */
targetNode = parentElement->children;
while(targetNode != NULL && i++ < index)
targetNode = targetNode->next;

xmlAddChild(parentElement, newElement);
}
else {
printMsg(MESSAGETYPE_ERROR, "Error: XML-string to import is not wellformed!\n");
error = NOT_WELL_FORMED;
}
// structure change!, we have to empty the xpath cache
XPathClearCache(document->xpathCache);

if (targetNode != NULL && index > 0) {
/* insert at position index */
xmlAddPrevSibling(targetNode, newElement);
}
return error;
else {
/* insert at the end of the list */
xmlAddChild(parentElement, newElement);
}

return SUCCESS;
}

DLL_EXPORT ReturnCode tixiGetNumberOfChilds(const TixiDocumentHandle handle, const char *elementPath, int* nChilds)
Expand Down
27 changes: 27 additions & 0 deletions tests/import_element_check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,33 @@ TEST_F(ImportElementsCheck, importElem2)
EXPECT_STREQ("<?xml version=\"1.0\"?>\n<root><a/><b><x attr=\"hello\"><c/></x></b></root>\n", text);
}

TEST_F(ImportElementsCheck, importElemAtIndex1)
{
char* text = NULL;

ASSERT_EQ(SUCCESS, tixiImportElementFromStringAtIndex(handle, "/root", 1, "<x>text</x>"));
ASSERT_EQ(SUCCESS, tixiExportDocumentAsString(handle, &text));
EXPECT_STREQ("<?xml version=\"1.0\"?>\n<root><x>text</x><a/><b/></root>\n", text);
}

TEST_F(ImportElementsCheck, importElemAtIndex2)
{
char* text = NULL;

ASSERT_EQ(SUCCESS, tixiImportElementFromStringAtIndex(handle, "/root", 2, "<x>text</x>"));
ASSERT_EQ(SUCCESS, tixiExportDocumentAsString(handle, &text));
EXPECT_STREQ("<?xml version=\"1.0\"?>\n<root><a/><x>text</x><b/></root>\n", text);
}

TEST_F(ImportElementsCheck, importElemAtIndex3)
{
char* text = NULL;

ASSERT_EQ(SUCCESS, tixiImportElementFromStringAtIndex(handle, "/root", 3, "<x>text</x>"));
ASSERT_EQ(SUCCESS, tixiExportDocumentAsString(handle, &text));
EXPECT_STREQ("<?xml version=\"1.0\"?>\n<root><a/><b/><x>text</x></root>\n", text);
}

TEST_F(ImportElementsCheck, invalidHandle)
{
ASSERT_EQ(INVALID_HANDLE, tixiImportElementFromString(-999, "/root/b", "<x attr=\"hello\"><c/></x>"));
Expand Down

0 comments on commit 64d23e5

Please sign in to comment.