Example #1
0
File: xmlMS.c Project: UPPMAX/irods
int 
msiLoadMetadataFromXml(msParam_t *targetObj, msParam_t *xmlObj, ruleExecInfo_t *rei) 
{
	/* for parsing msParams and to open iRODS objects */
	dataObjInp_t xmlDataObjInp, *myXmlDataObjInp;
	dataObjInp_t targetObjInp, *myTargetObjInp;
	int xmlObjID;

	/* for getting size of objects to read from */
	rodsObjStat_t *rodsObjStatOut = NULL;

	/* for reading from iRODS objects */
	openedDataObjInp_t openedDataObjInp;
	bytesBuf_t *xmlBuf;

	/* misc. to avoid repeating rei->rsComm */
	rsComm_t *rsComm;

	/* for xml parsing */
	xmlDocPtr doc;

	/* for XPath evaluation */
	xmlXPathContextPtr xpathCtx;
	xmlXPathObjectPtr xpathObj;
	xmlChar xpathExpr[] = "//AVU";
	xmlNodeSetPtr nodes;
	int avuNbr, i;
	
	/* for new AVU creation */
	modAVUMetadataInp_t modAVUMetadataInp;
	int max_attr_len = 2700;
	char attrStr[max_attr_len];



	/*********************************  USUAL INIT PROCEDURE **********************************/
	
	/* For testing mode when used with irule --test */
	RE_TEST_MACRO ("    Calling msiLoadMetadataFromXml")


	/* Sanity checks */
	if (rei == NULL || rei->rsComm == NULL)
	{
		rodsLog (LOG_ERROR, "msiLoadMetadataFromXml: input rei or rsComm is NULL.");
		return (SYS_INTERNAL_NULL_INPUT_ERR);
	}

	rsComm = rei->rsComm;


	
	/********************************** RETRIEVE INPUT PARAMS **************************************/

	/* Get path of target object */
	rei->status = parseMspForDataObjInp (targetObj, &targetObjInp, &myTargetObjInp, 0);
	if (rei->status < 0)
	{
		rodsLog (LOG_ERROR, "msiLoadMetadataFromXml: input targetObj error. status = %d", rei->status);
		return (rei->status);
	}


	/* Get path of XML document */
	rei->status = parseMspForDataObjInp (xmlObj, &xmlDataObjInp, &myXmlDataObjInp, 0);
	if (rei->status < 0)
	{
		rodsLog (LOG_ERROR, "msiLoadMetadataFromXml: input xmlObj error. status = %d", rei->status);
		return (rei->status);
	}



	/******************************** OPEN AND READ FROM XML OBJECT ********************************/

	/* Open XML file */
	if ((xmlObjID = rsDataObjOpen(rsComm, &xmlDataObjInp)) < 0) 
	{
		rodsLog (LOG_ERROR, "msiLoadMetadataFromXml: Cannot open XML data object. status = %d", xmlObjID);
		return (xmlObjID);
	}


	/* Get size of XML file */
	rei->status = rsObjStat (rsComm, &xmlDataObjInp, &rodsObjStatOut);
	if (rei->status < 0 || !rodsObjStatOut)
	{
		rodsLog (LOG_ERROR, "msiLoadMetadataFromXml: Cannot stat XML data object. status = %d", rei->status);
		return (rei->status);
	}


	/* xmlBuf init */
	/* memory for xmlBuf->buf is allocated in rsFileRead() */
	xmlBuf = (bytesBuf_t *) malloc (sizeof (bytesBuf_t));
	memset (xmlBuf, 0, sizeof (bytesBuf_t));


	/* Read XML file */
	memset (&openedDataObjInp, 0, sizeof (openedDataObjInp_t));
	openedDataObjInp.l1descInx = xmlObjID;
	openedDataObjInp.len = (int)rodsObjStatOut->objSize;

	rei->status = rsDataObjRead (rsComm, &openedDataObjInp, xmlBuf);
	
	/* Make sure that the result is null terminated */
	if (strlen((char*)xmlBuf->buf) > (size_t)openedDataObjInp.len)
	{
		((char*)xmlBuf->buf)[openedDataObjInp.len-1]='\0';
	}


	/* Close XML file */
	rei->status = rsDataObjClose (rsComm, &openedDataObjInp);

	/* cleanup */
	freeRodsObjStat (rodsObjStatOut);



	/******************************** PARSE XML DOCUMENT ********************************/

	xmlSubstituteEntitiesDefault(1);
	xmlLoadExtDtdDefaultValue = 1;


	/* Parse xmlBuf.buf into an xmlDocPtr */
	doc = xmlParseDoc((xmlChar*)xmlBuf->buf);


	/* Create xpath evaluation context */
	xpathCtx = xmlXPathNewContext(doc);
	if(xpathCtx == NULL) 
	{
		rodsLog (LOG_ERROR, "msiLoadMetadataFromXml: Unable to create new XPath context.");
		xmlFreeDoc(doc); 
		return(-1);
	}


	/* Evaluate xpath expression */
	xpathObj = xmlXPathEvalExpression(xpathExpr, xpathCtx);
	if(xpathObj == NULL) 
	{
		rodsLog (LOG_ERROR, "msiLoadMetadataFromXml: Unable to evaluate XPath expression \"%s\".", xpathExpr);
		xmlXPathFreeContext(xpathCtx); 
		xmlFreeDoc(doc); 
		return(-1);
	}

	/* How many AVU nodes did we get? */
	if ((nodes = xpathObj->nodesetval) != NULL)
	{
		avuNbr = nodes->nodeNr;
	}
	else 
	{
		avuNbr = 0;
	}



	/******************************** CREATE AVU TRIPLETS ********************************/

	/* Add a new AVU for each node. It's ok to process the nodes in forward order since we're not modifying them */
	for(i = 0; i < avuNbr; i++) 
	{
		if (nodes->nodeTab[i])
		{
//			/* temporary: Add index number to avoid duplicating attribute names */
//			memset(attrStr, '\0', MAX_NAME_LEN);
//			snprintf(attrStr, MAX_NAME_LEN - 1, "%04d: %s", i+1, (char*)xmlNodeGetContent(getChildNodeByName(nodes->nodeTab[i], "Attribute")) );

			/* Truncate if needed. No prefix. */
			memset(attrStr, '\0', max_attr_len);
			snprintf(attrStr, max_attr_len - 1, "%s", (char*)xmlNodeGetContent(getChildNodeByName(nodes->nodeTab[i], "Attribute")) );

			/* init modAVU input */
			memset (&modAVUMetadataInp, 0, sizeof(modAVUMetadataInp_t));
			modAVUMetadataInp.arg0 = "add";
			modAVUMetadataInp.arg1 = "-d";

			/* Use target object if one was provided, otherwise look for it in the XML doc */
			if (myTargetObjInp->objPath != NULL && strlen(myTargetObjInp->objPath) > 0)
			{
				modAVUMetadataInp.arg2 = myTargetObjInp->objPath;
			}
			else
			{
				modAVUMetadataInp.arg2 = xmlURIUnescapeString((char*)xmlNodeGetContent(getChildNodeByName(nodes->nodeTab[i], "Target")),
						MAX_NAME_LEN, NULL);
			}

			modAVUMetadataInp.arg3 = attrStr;
			modAVUMetadataInp.arg4 = (char*)xmlNodeGetContent(getChildNodeByName(nodes->nodeTab[i], "Value"));
			modAVUMetadataInp.arg5 = (char*)xmlNodeGetContent(getChildNodeByName(nodes->nodeTab[i], "Unit"));
			
			/* invoke rsModAVUMetadata() */
			rei->status = rsModAVUMetadata (rsComm, &modAVUMetadataInp);
		}
	}



	/************************************** WE'RE DONE **************************************/

	/* cleanup of all xml parsing stuff */
	xmlFreeDoc(doc);
        xmlCleanupParser();

	return (rei->status);
}
Example #2
0
BOOL parseXML (char* path, char* file)
{
	char *fullFile = (char*)malloc(sizeof(char)*(strlen(path)+strlen(file)+2));
	xmlDocPtr doc;
	xmlNodePtr tPtr1, movPtr;
	xmlChar* tmpStr;
	BOOL parseRes = FALSE;

	sprintf (fullFile, "%s/%s", path, file);
	printD ("Starting Parse of %s\n", fullFile);

	if ((doc = xmlParseFile(fullFile)) == NULL)
		return (FALSE);

	/* check if first entry is NOT library */
	tPtr1=doc->children;
	if (strcasecmp("details", (char*)tPtr1->name) != 0) {
		printD ("First Element is NOT \"detail\", it was: \"%s\"\n", (char*)tPtr1->name);
		xmlFreeDoc(doc);
		free (fullFile);
		return (FALSE);		/* it's ok that this happens, but FALSE there where no data written to the DB */
	}
	printD ("Found tag \"details\", this is no library XML...good..\n");

	/* child of "detail", find element-tag "movie" */
	if ( (movPtr = getChildNodeByName(tPtr1, "movie")) == NULL) {
		printD ("getChildNodebyName() returned NULL while looking for movie Tag\n");
		xmlFreeDoc(doc);
		free (fullFile);
		return (FALSE);
	}

	/* mhh can be wiped out ? */
	if (strcasecmp("movie", (char*)movPtr->name) != 0) {
		printD ("First Child of \"detail\" is NOT \"movie\" it was \"%s\"\n", (char*)movPtr->name);
		xmlFreeDoc(doc);
		free (fullFile);
		return (FALSE);		/* should not happen :/ */
	}

	printD("Found \"movie\" Tag ...good...\n");

	/* movPtr holds now the "movie" Element Tag, which holds ALL childs */
	if (xmlHasProp(movPtr, (unsigned char*)"isTV") == NULL) {
		printD ("Property \"isTV\" not found in Node:%s\n", movPtr->name);
		xmlFreeDoc(doc);
		free (fullFile);
		return (FALSE);
	}

	/* everything ok in here, we know now what data is in this XML file */

	tmpStr=xmlGetProp(movPtr,(unsigned char*)"isTV");

	if (strcasecmp((char*)tmpStr, "true") == 0)
		parseRes = fillTVStruct (movPtr);
	else
		parseRes = fillMovieStruct (movPtr);

	xmlFree (tmpStr);

	xmlFreeDoc(doc);
	free (fullFile);
	return (parseRes);
}
Example #3
0
BOOL fillTVStruct (xmlNodePtr parentTag) {
	xmlNode *seekChild, *curNode, *airNode;
	char *tmpString;
	char *toFree;
	/* clear Structs */
	if (curTV->actors) free (curTV->actors);
	if (curTV->plot) free (curTV->plot);
	if (curTV->tagline) free (curTV->tagline);
	bzero (curTV, sizeof(struct ST_tv));

	if ( (seekChild = getChildNodeByName(parentTag, "title")) != NULL ) {
		if ( ( tmpString = encNodeGetContent(seekChild)) != NULL ) {
			strncat(curTV->seriesname, tmpString, 255);
			free (tmpString);
			printD ("Stored Title:%s\n", curTV->seriesname);
		}
	}
	else {
		printD("Something went wrong while reading title...\n");
		return (FALSE);
	}

	if ( (seekChild = getChildNodeByName(parentTag, "language")) != NULL ) {
		if ( ( tmpString = encNodeGetContent(seekChild)) != NULL ) {
			strncat(curTV->language, tmpString, 255);
			free (tmpString);
			printD ("Stored Language:%s\n", curTV->language);
		}
	}
	else {
		printD("Something went wrong while reading language...\n");
		return (FALSE);
	}

	if ( (seekChild = getChildNodeByName(parentTag, "container")) != NULL ) {
		if ( ( tmpString = encNodeGetContent(seekChild)) != NULL ) {
			strncat(curTV->mediatype, tmpString, 255);
			xmlFree (tmpString);
			printD ("Stored MediaType:%s\n", curTV->mediatype);
		}
	}
	else {
		printD("Something went wrong while reading container(mediatype)...\n");
		return (FALSE);
	}

	if ( (toFree = getIDs(parentTag)) != NULL ) {
		sprintf (curTV->imdblink, "http://www.imdb.com/title/%s\n", toFree);
		free (toFree);
	}
	else
		sprintf (curTV->imdblink, "http://www.imdb.com/find?s=all&q=%s", curMovie->moviename);
	printD ("Stored IMDBLink:%s\n", curTV->imdblink);

	if ( (seekChild = getChildNodeByName(parentTag, "videoOutput")) != NULL) {
		if ( (tmpString = (char*)xmlNodeGetContent (seekChild)) != NULL) {
			curMovie->isHD = isHD(tmpString);
			xmlFree ((xmlChar*)tmpString);
		}
	}
	else
		curMovie->isHD = FALSE;


	if ( (seekChild = getChildNodeByName(parentTag, "directors")) != NULL ) {
		if ( childTextToString(seekChild, curTV->directors, "director") == 0 )
			strcat (curTV->directors, "UNKNOWN");
		printD ("Stored Directors:%s\n", curTV->directors);
	}
	else {
		printD("Something went wrong while reading directors...\n");
	}

	if ( (seekChild = getChildNodeByName(parentTag, "writers")) != NULL ) {
		if ( childTextToString(seekChild, curTV->writers, "writer") == 0 )
			strcat (curTV->writers, "UNKNOWN");
		printD ("Stored Writers:%s\n", curTV->writers);
	}

	if ( (seekChild = getChildNodeByName(parentTag, "genres")) != NULL ) {
		if ( childTextToString(seekChild, curTV->genres, "genre") == 0 )
			strcat (curTV->genres, "UNKNOWN");
		printD ("Stored Genres:%s\n", curTV->genres);
	}

	if ( (seekChild = getChildNodeByName(parentTag, "cast")) != NULL ) {
		if ( childTextToString_m(seekChild, &curTV->actors, "actor") == 0 )
			curTV->actors = strdup ("UNKNOWN");
		printD ("Stored Actors:%s\n", curTV->actors);
	}
	else
		curTV->actors = strdup ("UNKNOWN");

	if ( (seekChild = getChildNodeByName(parentTag, "tagline")) != NULL ) {
		if ( ( tmpString = encNodeGetContent(seekChild)) != NULL ) {
			curTV->tagline=tmpString;
			printD ("Stored Outline:%s\n", curTV->tagline);
		}
	}

	if ( (seekChild = getChildNodeByName(parentTag, "plot")) != NULL ) {
		if ( ( tmpString = encNodeGetContent(seekChild)) != NULL ) {
			curTV->plot=tmpString;
			printD ("Stored Plot:%s\n", curTV->plot);
		}
	}

	if ( (seekChild = getChildNodeByName(parentTag, "releaseDate")) != NULL ) {
		if ( ( tmpString = encNodeGetContent(seekChild)) != NULL ) {
			strncat (curTV->year, tmpString, 63);
			xmlFree (tmpString);
			printD ("Stored Releasedate:%s\n", curTV->year);
		}
	}

	if ( (seekChild = getChildNodeByName(parentTag, "posterFile")) != NULL ) {
		if ( ( tmpString = encNodeGetContent(seekChild)) != NULL ) {
			strncat (curTV->imglink, tmpString, 255);
			free (tmpString);
			printD ("Stored Imagelink:%s\n", curTV->imglink);
		}
	}

	if ( (seekChild = getChildNodeByName(parentTag, "rating")) != NULL ) {
		if ( ( tmpString = (char*)xmlNodeGetContent(seekChild)) != NULL ) {
			if (atoi((char*)tmpString) < 0)
				curTV->rating=0;
			else
				curTV->rating=atoi((char*)tmpString);
			xmlFree (tmpString);
			printD ("Stored Rating:%i\n", curTV->rating);
		}
	}

	if ( (seekChild = getChildNodeByName(parentTag, "season")) != NULL ) {
		if ( ( tmpString = (char*)xmlNodeGetContent(seekChild)) != NULL ) {
			if (atoi((char*)tmpString) < 0)
				curTV->season=0;
			else
				curTV->season=atoi((char*)tmpString);
			xmlFree (tmpString);
			printD ("Stored Season:%i\n", curTV->season);
		}
	}

	if ( (seekChild = getChildNodeByName(parentTag, "files")) != NULL) {
		if (seekChild->children) {
			curNode = seekChild->children;
			while (curNode) {
				if (curNode->type == XML_ELEMENT_NODE) {
					if (strcasecmp((char*)curNode->name, "file") == 0) {
						if ( (airNode = getChildNodeByName(curNode, "fileTitle")) != NULL) {
							if ( (tmpString = (char*)xmlGetProp(airNode, (xmlChar*)"part")) != NULL ) {
								if (atoi((char*)tmpString) < 0 )
									curTV->episode=0;
								else
									curTV->episode=atoi((char*)tmpString);
								xmlFree (tmpString);
							}
							if ( (tmpString = encNodeGetContent(airNode)) != NULL ) {
								strncat (curTV->episodename, tmpString, 255);
								free(tmpString);
							}
						}
						if ( (airNode = getChildNodeByName(curNode, "fileLocation")) != NULL) {
							tmpString = (char*)xmlNodeGetContent(airNode);
							strncat (curTV->filename, tmpString ? tmpString : "*UNAVAILABLE*", 255);
							if (tmpString) free (tmpString);
						}
					}
					if (!writeToDB(TRUE))
						fprintf (stderr, "%s could not be stored in DB:%s\n", curTV->filename, mysql_error(mysql_conn));
					bzero(curTV->episodename, 255);
					bzero(curTV->filename, 255);
				}
				/* should all be filled by now, pump it in the DB */
				curNode=curNode->next;
			}
		}
	}

	return (TRUE);

}
Example #4
0
void createNews(void) {
	xmlDocPtr doc;
	xmlNodePtr seekPtr, parentPtr;
	xmlChar *mediaTitle, *season;
	char *filePath, *SQLexec=NULL;
	MYSQL_ROW idrow;
	MYSQL_RES *idres;
	int seas_check;

	printf ("\nGenerating new movies...\n");

	filePath=(char*)malloc(sizeof(char)* (strlen(input_dir)+strlen(yamj_newmovies)+2 ) );
	sprintf (filePath, "%s/%s", input_dir, yamj_newmovies);

	if ((doc = xmlParseFile(filePath)) != NULL) {
		parentPtr = doc->children;
		if ( (seekPtr = getChildNodeByName(parentPtr, "movies")) != NULL ) {
			parentPtr = seekPtr->children;
			while (parentPtr) {
				if (parentPtr->type == XML_ELEMENT_NODE) {
					if ( strcasecmp((char*)parentPtr->name, "movie") == 0 ) {
						if ( (seekPtr = getChildNodeByName(parentPtr, "title")) != NULL ) {
							if ( (mediaTitle = xmlNodeGetContent(seekPtr))  != NULL ) {
								SQLexec = (char*)malloc(sizeof(char)* (strlen(db_FIND_MOVIENEWS) + strlen(db_movietable) + strlen((char*)mediaTitle) +2 ));
								sprintf (SQLexec, db_FIND_MOVIENEWS, db_movietable, mediaTitle);
								if (mysql_real_query(mysql_conn, SQLexec, strlen(SQLexec)) == 0) {
									if ( (idres = mysql_store_result(mysql_conn)) != NULL ) {
										idrow = mysql_fetch_row(idres);
										free (SQLexec);
										SQLexec = (char*)malloc(sizeof(char) * (strlen(db_INSERT_MOVIENEWS) + strlen(idrow[0])+2));
										sprintf (SQLexec, db_INSERT_MOVIENEWS, db_newstable, idrow[0]);
										if ( mysql_real_query (mysql_conn, SQLexec, strlen(SQLexec)) != 0)
											fprintf (stderr, "Error while storing newmovies into newstable\n");
										mysql_free_result(idres);
									}
								}
							}
						}
					}
				}
				parentPtr = parentPtr->next;
			}
		}
		xmlFreeDoc(doc);
	}
	free (filePath);

	printf ("Generating new tv...\n");

	filePath=(char*)malloc(sizeof(char)* (strlen(input_dir)+strlen(yamj_newtv)+2 ) );
	sprintf (filePath, "%s/%s", input_dir, yamj_newtv);

	if ((doc = xmlParseFile(filePath)) != NULL) {
		parentPtr = doc->children;
		if ( (seekPtr = getChildNodeByName(parentPtr, "movies")) != NULL ) {
			parentPtr = seekPtr->children;
			while (parentPtr) {
				if (parentPtr->type == XML_ELEMENT_NODE) {
					if ( !strcasecmp((char*)parentPtr->name, "movie") && parentPtr->name ) {
						if ( (seekPtr = getChildNodeByName(parentPtr, "title")) != NULL ) {
							if ( (mediaTitle = xmlNodeGetContent(seekPtr))  != NULL ) {
								if ( (seekPtr = getChildNodeByName(parentPtr, "season")) != NULL ) {
									if ( (season = xmlNodeGetContent(seekPtr)) != NULL ) {
										seas_check = atoi ((char*)season);
										if (seas_check < 1) {
											fprintf (stderr, "WARNING:%s(season) < 1, seeking in DB for ID.\n", (char*)mediaTitle);
											SQLexec = (char*)malloc( sizeof(char)*( strlen(db_FIND_TVNEWS_FAILED)+strlen(db_movietable)+strlen((char*)mediaTitle) ) );
											sprintf (SQLexec, db_FIND_TVNEWS_FAILED, db_tvtable, mediaTitle);
										}
										else {
											SQLexec = (char*)malloc(sizeof(char)* (strlen(db_FIND_TVNEWS) + strlen(db_movietable) + strlen((char*)mediaTitle) + strlen((char*)season)+2 ));
											sprintf (SQLexec, db_FIND_TVNEWS, db_tvtable, mediaTitle, season);
										}
										if (mysql_real_query(mysql_conn, SQLexec, strlen(SQLexec)) == 0) {
											if ( (idres = mysql_store_result(mysql_conn)) != NULL ) {
												idrow = mysql_fetch_row(idres);
												if (SQLexec) free (SQLexec);
												SQLexec = (char*)malloc(sizeof(char) * (strlen(db_INSERT_TVNEWS) + strlen(idrow[0])+2));
												sprintf (SQLexec, db_INSERT_TVNEWS, db_newstable, idrow[0]);
												if ( mysql_real_query (mysql_conn, SQLexec, strlen(SQLexec)) != 0)
													fprintf (stderr, "Error while storing newmovies into newstable\n");
												mysql_free_result(idres);
											}
										}
									}
								}
							}
						}
					}
				}
				parentPtr = parentPtr->next;
			}
		}
		xmlFreeDoc(doc);
	}
	free (filePath);
}