diff options
Diffstat (limited to 'common/ixml.cpp')
-rw-r--r-- | common/ixml.cpp | 330 |
1 files changed, 330 insertions, 0 deletions
diff --git a/common/ixml.cpp b/common/ixml.cpp new file mode 100644 index 0000000..f463124 --- /dev/null +++ b/common/ixml.cpp @@ -0,0 +1,330 @@ +/* + * ixml.cpp + * + * Created on: 03.10.2012 + * Author: savop + */ + +#include "../include/tools/ixml.h" + +namespace upnp { + +namespace ixml { + +void XmlEscapeSpecialChars(string& doc){ + std::string buffer; + + buffer.reserve(doc.size()*1.1); + for(unsigned int i = 0; i < doc.size(); i++){ + switch((long)doc[i]){ + case L'€': buffer.append("€"); break; + case L'"': buffer.append("""); break; + case L'&': buffer.append("&"); break; + case L'<': buffer.append("<"); break; + case L'>': buffer.append(">"); break; + case L'¡': buffer.append("¡"); break; + case L'¢': buffer.append("¢"); break; + case L'£': buffer.append("£"); break; + case L'¤': buffer.append("¤"); break; + case L'¥': buffer.append("¥"); break; + case L'¦': buffer.append("¦"); break; + case L'§': buffer.append("§"); break; + case L'¨': buffer.append("¨"); break; + case L'©': buffer.append("©"); break; + case L'ª': buffer.append("ª"); break; + case L'¬': buffer.append("¬"); break; + case L'': buffer.append("­"); break; + case L'®': buffer.append("®"); break; + case L'¯': buffer.append("¯"); break; + case L'°': buffer.append("°"); break; + case L'±': buffer.append("±"); break; + case L'²': buffer.append("²"); break; + case L'³': buffer.append("³"); break; + case L'´': buffer.append("´"); break; + case L'µ': buffer.append("µ"); break; + case L'¶': buffer.append("¶"); break; + case L'·': buffer.append("·"); break; + case L'¸': buffer.append("¸"); break; + case L'¹': buffer.append("¹"); break; + case L'º': buffer.append("º"); break; + case L'»': buffer.append("»"); break; + case L'«': buffer.append("«"); break; + case L'¼': buffer.append("¼"); break; + case L'½': buffer.append("½"); break; + case L'¾': buffer.append("¾"); break; + case L'¿': buffer.append("¿"); break; + case L'À': buffer.append("À"); break; + case L'Á': buffer.append("Á"); break; + case L'Â': buffer.append("Â"); break; + case L'Ã': buffer.append("Ã"); break; + case L'Ä': buffer.append("Ä"); break; + case L'Å': buffer.append("Å"); break; + case L'Æ': buffer.append("Æ"); break; + case L'Ç': buffer.append("Ç"); break; + case L'È': buffer.append("È"); break; + case L'É': buffer.append("É"); break; + case L'Ê': buffer.append("Ê"); break; + case L'Ë': buffer.append("Ë"); break; + case L'Ì': buffer.append("Ì"); break; + case L'Í': buffer.append("Í"); break; + case L'Î': buffer.append("Î"); break; + case L'Ï': buffer.append("Ï"); break; + case L'Ð': buffer.append("Ð"); break; + case L'Ñ': buffer.append("Ñ"); break; + case L'Ò': buffer.append("Ò"); break; + case L'Ó': buffer.append("Ó"); break; + case L'Ô': buffer.append("Ô"); break; + case L'Õ': buffer.append("Õ"); break; + case L'Ö': buffer.append("Ö"); break; + case L'×': buffer.append("×"); break; + case L'Ø': buffer.append("Ø"); break; + case L'Ù': buffer.append("Ù"); break; + case L'Ú': buffer.append("Ú"); break; + case L'Û': buffer.append("Û"); break; + case L'Ü': buffer.append("Ü"); break; + case L'Ý': buffer.append("Ý"); break; + case L'Þ': buffer.append("Þ"); break; + case L'ß': buffer.append("ß"); break; + case L'à': buffer.append("à"); break; + case L'á': buffer.append("á"); break; + case L'â': buffer.append("â"); break; + case L'ã': buffer.append("ã"); break; + case L'ä': buffer.append("ä"); break; + case L'å': buffer.append("å"); break; + case L'æ': buffer.append("æ"); break; + case L'ç': buffer.append("ç"); break; + case L'è': buffer.append("è"); break; + case L'é': buffer.append("é"); break; + case L'ê': buffer.append("ê"); break; + case L'ë': buffer.append("ë"); break; + case L'ì': buffer.append("ì"); break; + case L'í': buffer.append("í"); break; + case L'î': buffer.append("î"); break; + case L'ï': buffer.append("ï"); break; + case L'ð': buffer.append("ð"); break; + case L'ñ': buffer.append("ñ"); break; + case L'ò': buffer.append("ò"); break; + case L'ó': buffer.append("ó"); break; + case L'ô': buffer.append("ô"); break; + case L'õ': buffer.append("õ"); break; + case L'ö': buffer.append("ö"); break; + case L'÷': buffer.append("÷"); break; + case L'ø': buffer.append("ø"); break; + case L'ù': buffer.append("ù"); break; + case L'ú': buffer.append("ú"); break; + case L'û': buffer.append("û"); break; + case L'ü': buffer.append("ü"); break; + case L'ý': buffer.append("ý"); break; + case L'þ': buffer.append("þ"); break; + default: buffer.append(1, doc[i]); break; + } + } + + doc.swap(buffer); +} + +//Function copied from Intel SDK +/////////////////////////////////////////////////////////////////////////// +// +// Copyright (c) 2000-2003 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. +// +/////////////////////////////////////////////////////////////////////////// +/******************************************************************************** + * SampleUtil_GetFirstDocumentItem + * + * Description: + * Given a document node, this routine searches for the first element + * named by the input string item, and returns its value as a string. + * String must be freed by caller using free. + * Parameters: + * doc -- The DOM document from which to extract the value + * item -- The item to search for + * + * + ********************************************************************************/ + +int IxmlGetFirstDocumentItem( IN IXML_Document * doc, IN std::string item, std::string& value ) { + IXML_NodeList *nodeList = NULL; + IXML_Node *textNode = NULL; + IXML_Node *tmpNode = NULL; + + int error = 0; + + nodeList = ixmlDocument_getElementsByTagName( doc, item.c_str() ); + + if( nodeList != NULL ) { + if( ( tmpNode = ixmlNodeList_item( nodeList, 0 ) ) ) { + + textNode = ixmlNode_getFirstChild( tmpNode ); + + if(textNode != NULL){ + value = ixmlNode_getNodeValue( textNode ); + } + } + } else { + error = -1; + } + + if( nodeList != NULL) { + ixmlNodeList_free( nodeList ); + } + + + return error; +} + +IXML_Element* IxmlAddProperty(IXML_Document* document, IXML_Element* node, const string& upnpproperty, const string& value){ + if(!node) return NULL; + IXML_Element* PropertyNode = NULL; + + string tvalue = value.substr(0,MAX_METADATA_LENGTH_L); + + string::size_type pos = upnpproperty.find('@'); + string attribute = pos!=string::npos ? upnpproperty.substr(pos+1) : string(); + string property = pos!=string::npos ? upnpproperty.substr(0, pos) : upnpproperty; + + if(!attribute.empty()){ + if(property.empty()){ + if(ixmlElement_setAttribute(node, attribute.c_str(), tvalue.c_str())!=IXML_SUCCESS){ + return NULL; + } + } + else { + IXML_NodeList* NodeList = ixmlElement_getElementsByTagName(node, property.c_str()); + if(NodeList!=NULL){ + PropertyNode = (IXML_Element*) ixmlNodeList_item(NodeList, 0); + if(PropertyNode){ + if(ixmlElement_setAttribute(PropertyNode, attribute.c_str(), tvalue.c_str())!=IXML_SUCCESS){ + return NULL; + } + } + else { + ixmlNodeList_free(NodeList); + return NULL; + } + } + else { + return NULL; + } + } + } + else { + PropertyNode = ixmlDocument_createElement(document, property.c_str()); + IXML_Node* PropertyText = ixmlDocument_createTextNode(document, tvalue.c_str()); + ixmlNode_appendChild((IXML_Node*) PropertyNode, PropertyText); + ixmlNode_appendChild((IXML_Node*) node, (IXML_Node*) PropertyNode); + } + return PropertyNode; +} + +IXML_Element* IxmlAddFilteredProperty(const StringList& Filter, IXML_Document* document, IXML_Element* node, const string& upnpproperty, const string& value){ + // leave out empty values. + if(value.empty() || !value.compare("0")){ + return NULL; + } + + if((*Filter.begin()).compare("*") == 0) return IxmlAddProperty(document, node, upnpproperty, value); + + for(StringList::const_iterator it = Filter.begin(); it != Filter.end(); ++it){ + if((*it).compare(upnpproperty) == 0) return IxmlAddProperty(document, node, upnpproperty, value); + } + + return NULL; +} + +IXML_Element* IxmlReplaceProperty(IXML_Document* document, IXML_Element* node, const string& upnpproperty, const string& newValue){ + if(!node) return NULL; + IXML_Element* PropertyNode = NULL; + + string tvalue = newValue.substr(0, MAX_METADATA_LENGTH_L); + + string::size_type pos = upnpproperty.find('@'); + string attribute = pos!=string::npos ? upnpproperty.substr(pos+1) : string(); + string property = pos!=string::npos ? upnpproperty.substr(0, pos) : upnpproperty; + + if(!attribute.empty()){ + if(property.empty()){ + if(tvalue.empty()){ + if(ixmlElement_setAttribute(node, attribute.c_str(), tvalue.c_str())!=IXML_SUCCESS){ + return NULL; + } + } + else { + ixmlElement_removeAttribute(node, attribute.c_str()); + } + } + else { + IXML_NodeList* NodeList = ixmlElement_getElementsByTagName(node, property.c_str()); + if(NodeList!=NULL){ + PropertyNode = (IXML_Element*) ixmlNodeList_item(NodeList, 0); + if(PropertyNode){ + if(!tvalue.empty()){ + if(ixmlElement_setAttribute(PropertyNode, attribute.c_str(), tvalue.c_str())!=IXML_SUCCESS){ + return NULL; + } + } + else { + ixmlElement_removeAttribute(PropertyNode, attribute.c_str()); + } + } + else { + ixmlNodeList_free(NodeList); + return NULL; + } + } + else { + return NULL; + } + } + } + else { + IXML_NodeList* NodeList = ixmlElement_getElementsByTagName(node, property.c_str()); + if(NodeList!=NULL){ + PropertyNode = (IXML_Element*) ixmlNodeList_item(NodeList, 0); + IXML_Node* PropertyText = ixmlNode_getFirstChild((IXML_Node*) PropertyNode); + + if(ixmlNode_removeChild((IXML_Node*) PropertyNode, PropertyText, NULL)!=IXML_SUCCESS){ + return NULL; + } + if(!tvalue.empty()){ + PropertyText = ixmlDocument_createTextNode(document, tvalue.c_str()); + } + ixmlNode_appendChild((IXML_Node*) PropertyNode, PropertyText); + } + else { + ixmlNodeList_free(NodeList); + return NULL; + } + } + return PropertyNode; +} + +} // namespace ixml + +} // namespace upnp + |