📄 document.c
字号:
/////////////////////////////////////////////////////////////////////////////// Copyright (c) 2000 Intel Corporation// All rights reserved.//// Redistribution and use in source and binary forms, with or without// modification, are permitted provided that the following conditions are met://// * Redistributions of source code must retain the above copyright notice,// this list of conditions and the following disclaimer.// * Redistributions in binary form must reproduce the above copyright notice,// this list of conditions and the following disclaimer in the documentation// and/or other materials provided with the distribution.// * Neither name of Intel Corporation nor the names of its contributors// may be used to endorse or promote products derived from this software// without specific prior written permission.//// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE./////////////////////////////////////////////////////////////////////////////// $Revision: 1.1.2.12 $// $Date: 2004/08/16 03:17:47 $#include "../../inc/tools/config.h"#if EXCLUDE_DOM == 0#include "../../inc/upnpdom/all.h"#include "../../inc/upnpdom/Document.h"#include "../../inc/upnpdom/DOMException.h"#include <stdio.h>#include <stdlib.h>void Document_Document_0(Document *pDocument, struct strNode *pNode){ pDocument->ownerDocument=(Document *)pNode; pNode->nact=NULL;}void Document_Document_free(Document *pDocument){}void Document_createDocument(Document *returnDoc){ struct strNode *p; returnDoc = (Document *) malloc (sizeof(Document) + 1); if(!returnDoc) {DBGONLY(UpnpPrintf(UPNP_CRITICAL,DOM,__FILE__,__LINE__,"Insuffecient memory\n");)} p = (struct strNode *)returnDoc; p->nact= (struct strNodeAct *) malloc (sizeof(struct strNodeAct) + 1); NodeAct_NodeAct_4(p->nact, DOCUMENT_NODE, "#document",NULL, (struct strNode *)returnDoc); //new NodeAct(DOCUMENT_NODE, "#document",NULL, (struct strNode *)returnDoc); p->nact->OwnerNode=p->nact; p->nact->RefCount++; returnDoc = (Document *)p;}Document *Document_Document_createDocument(Document *pDocument){ Document *returnDoc; struct strNode *p; returnDoc = (Document *) malloc (sizeof(Document) + 1); if(!returnDoc) {DBGONLY(UpnpPrintf(UPNP_CRITICAL,DOM,__FILE__,__LINE__,"Insuffecient memory\n");)} p = (struct strNode *)returnDoc; p->nact= (struct strNodeAct *) malloc (sizeof(struct strNodeAct) + 1); NodeAct_NodeAct_4(p->nact, DOCUMENT_NODE, "#document",NULL, (struct strNode*)returnDoc); //returnDoc->nact= new NodeAct(DOCUMENT_NODE, "#document",NULL, (Node*)returnDoc); if(!(p->nact)) {DBGONLY(UpnpPrintf(UPNP_CRITICAL,DOM,__FILE__,__LINE__,"Insuffecient memory\n");)} p->nact->OwnerNode=p->nact; p->nact->RefCount++; returnDoc = (Document *) p; return returnDoc;} struct strAttr *Document_createAttribute_name(char *name){ Attr *returnAttr; returnAttr = (struct strAttr *) malloc(sizeof(struct strAttr) + 1); Attr_Attr_1(returnAttr, name); if(!returnAttr) {DBGONLY(UpnpPrintf(UPNP_CRITICAL,DOM,__FILE__,__LINE__,"Insuffecient memory\n");)} return returnAttr;}struct strAttr *Document_createAttribute_name_value(char *name, char *value){ Attr *returnAttr; returnAttr = (struct strAttr *) malloc(sizeof(struct strAttr) + 1); Attr_Attr_2(returnAttr, name, value); //returnAttr = new Attr(name, value); if(!returnAttr) {DBGONLY(UpnpPrintf(UPNP_CRITICAL,DOM,__FILE__,__LINE__,"Insuffecient memory\n");)} return returnAttr;}struct strElement *Document_createElement(char *tagName){ struct strElement *returnElement; struct strNode *pNode; returnElement = (struct strElement *) malloc (sizeof(struct strElement) + 1); Element_Element_1(returnElement, pNode, tagName); //returnElement = new Element(tagName); if(!returnElement) {DBGONLY(UpnpPrintf(UPNP_CRITICAL,DOM,__FILE__,__LINE__,"Insuffecient memory\n");)} return returnElement;}struct strNode *Document_createTextNode(Document *pDocument, char *data){ struct strNode *returnNode; Node_createNode(returnNode,TEXT_NODE,"#text",data); pDocument = (Document *) returnNode; return returnNode;} struct strDocument *Document_ReadDocumentFileOrBuffer(char * xmlFile, short int file){ //to do: try handling this in a better fashin than allocating a static length.. char *fileBuffer; FILE *XML; Document *RootDoc; enum NODE_TYPE NodeType; char *NodeName=NULL; char *NodeValue=NULL; short int IsEnd; short int IgnoreWhiteSpace=1; struct strNode *a, *b; //struct strElement *back; int exceptionFlag = 0; DBGONLY(UpnpPrintf(UPNP_ALL,DOM,__FILE__,__LINE__,"Inside ReadDocumentFileOrBuffer function\n");) if(file) { XML=fopen(xmlFile, "r"); if(!XML) { DBGONLY(UpnpPrintf(UPNP_INFO,DOM,__FILE__,__LINE__,"%s - No Such File Or Directory \n", xmlFile);) exceptionFlag = 1; //struct strDOMException *pDOMException = malloc (sizeof (struct strDOMException) + 1); //pDOMException->code = NO_SUCH_FILE; Handle_DOMException(NO_SUCH_FILE); //pDOMException); //free(pDOMException); //throw DOMException(DOMException::NO_SUCH_FILE, NULL); } else{ fseek(XML,0, SEEK_END); int fz = ftell(XML); fileBuffer = (char *)malloc(fz+2); if(fileBuffer == NULL) { DBGONLY(UpnpPrintf(UPNP_CRITICAL,DOM,__FILE__,__LINE__,"Insuffecient memory\n");) exceptionFlag = 1; Handle_DOMException(INSUFFICIENT_MEMORY); //throw DOMException(DOMException::INSUFFICIENT_MEMORY, NULL); } fseek(XML,0, SEEK_SET); int sizetBytesRead = fread (fileBuffer, 1, fz, XML); fileBuffer[sizetBytesRead] = '\0'; // append null fclose (XML); } } else { fileBuffer=(char *)malloc(strlen(xmlFile)+1); if(fileBuffer == NULL) { DBGONLY(UpnpPrintf(UPNP_CRITICAL,DOM,__FILE__,__LINE__,"Insuffecient memory\n");) exceptionFlag = 1; Handle_DOMException(INSUFFICIENT_MEMORY); //throw DOMException(DOMException::INSUFFICIENT_MEMORY, NULL); } strcpy(fileBuffer, xmlFile); } //try //{ //Parser myparser(fileBuffer); struct strParser *myparser; myparser = (struct strParser *) malloc (sizeof(struct strParser) + 1); Parser_Parser_1(myparser, fileBuffer); free(fileBuffer); Document_createDocument(RootDoc); RootDoc->CurrentNodePtr=((struct strNode *)RootDoc)->nact; while(1) { struct strElement *ele; struct strAttr *attr; //try //{ if(Parser_getNextNode(myparser, NodeType, &NodeName, &NodeValue, IsEnd, IgnoreWhiteSpace)==0) { DBGONLY(UpnpPrintf(UPNP_ALL,DOM,__FILE__,__LINE__,"NextNode while parsing Nodetype %d, Nodename %s, Nodevalue %s\n", NodeType, NodeName, NodeValue);) if(!IsEnd) { switch(NodeType) { case ELEMENT_NODE: ele=Document_createElement(NodeName); a=(struct strNode*)ele; NodeAct_appendChild(RootDoc->CurrentNodePtr, a->nact); RootDoc->CurrentNodePtr=RootDoc->CurrentNodePtr->LastChild; free(ele->ownerElement); break; case TEXT_NODE: Node_createNode(b,NodeType,NodeName,NodeValue); NodeAct_appendChild(RootDoc->CurrentNodePtr, b->nact); free(b); break; case ATTRIBUTE_NODE: attr=Document_createAttribute_name_value(NodeName, NodeValue); a=(struct strNode*)attr; NodeAct_appendChild(RootDoc->CurrentNodePtr, a->nact); free(attr->ownerAttr); break; case INVALID_NODE: break; default: break; } } //Throw exception if it is an invalid document else { if(!strcmp(NodeName, RootDoc->CurrentNodePtr->NA_NodeName)) { RootDoc->CurrentNodePtr = RootDoc->CurrentNodePtr->ParentNode; } else { Document_deleteDocumentTree(RootDoc); DBGONLY(UpnpPrintf(UPNP_CRITICAL,DOM,__FILE__,__LINE__,"fatal error during parsing\n");) exceptionFlag = 1; Handle_DOMException(FATAL_ERROR_DURING_PARSING); //throw DOMException(DOMException::FATAL_ERROR_DURING_PARSING); } } if(NodeType==INVALID_NODE) break; else NodeType=INVALID_NODE; if(NodeName) { Upnpfree(NodeName); NodeName=NULL; } if(NodeValue) { Upnpfree(NodeValue); NodeValue=NULL; } IsEnd=1; } else { Document_deleteDocumentTree(RootDoc); exceptionFlag = 1; Handle_DOMException(FATAL_ERROR_DURING_PARSING); //throw DOMException(DOMException::FATAL_ERROR_DURING_PARSING); } if ( exceptionFlag == 1) { Document_deleteDocumentTree(RootDoc); free(RootDoc); Handle_DOMException(NOT_FOUND_ERR_DOM); } //} //catch(DOMException& toCatch) //{ // RootDoc->deleteDocumentTree(); // delete RootDoc; // throw DOMException(toCatch.code); //} } if ( exceptionFlag == 1) { RootDoc=NULL; Handle_DOMException(NOT_FOUND_ERR_DOM); } //}catch(DOMException& toCatch) //{ // RootDoc=NULL; // throw DOMException(toCatch.code); //} RootDoc->ownerDocument=RootDoc; DBGONLY(UpnpPrintf(UPNP_ALL,DOM,__FILE__,__LINE__,"**EndParse**\n");) return RootDoc;}void Document_deleteDocumentTree(Document *pDocument){ struct strNode *p; p = (struct strNode *)(pDocument->ownerDocument); if(p->nact == NULL) return; NodeAct_deleteNodeAct((struct strNodeAct *)p, p->nact); free(p->nact); p->nact=NULL; }void Document_deleteDocument(Document *pDocument, struct strNode *pNode){ free(pDocument->ownerDocument); pNode->nact=NULL;}/******Document& Document::operator = (const Document &other){ if (this->nact != other.nact) { this->nact = other.nact; this->nact->RefCount++; } this->ownerDocument=other.ownerDocument; return *this;};********/Document *Document_Document_operator_eq(Document *pDocument, const Document *other){ struct strNode *p = (struct strNode *) other; struct strNode *q = (struct strNode *) pDocument; if (q->nact != p->nact) { q->nact = p->nact; q->nact->RefCount++; } pDocument->ownerDocument=other->ownerDocument; return pDocument;}struct strNodeList *Document_getElementsByTagName( struct strNode *pNode, char * tagName){ struct strNodeList *returnNodeList; returnNodeList= (struct strNodeList *) malloc (sizeof(struct strNodeList) + 1); if(!returnNodeList) {DBGONLY(UpnpPrintf(UPNP_CRITICAL,DOM,__FILE__,__LINE__,"Insuffecient memory\n");)} returnNodeList->ownerNodeList=returnNodeList; DBGONLY(UpnpPrintf(UPNP_ALL,DOM,__FILE__,__LINE__,"Calling makeNodeList\n");) Node_SearchList(pNode, tagName, returnNodeList, strchr(tagName,':')==NULL); return returnNodeList;}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -