/*---------------------------------------------------------------------- | PLT_StateVariable::Serialize +---------------------------------------------------------------------*/ NPT_Result PLT_StateVariable::Serialize(NPT_XmlElementNode& node) { NPT_List<NPT_Map<NPT_String, NPT_String>::Entry*>::Iterator entry = m_ExtraAttributes.GetEntries().GetFirstItem(); while (entry) { const NPT_String& key = (*entry)->GetKey(); const NPT_String& value = (*entry)->GetValue(); node.SetAttribute(key, value); ++entry; } return node.SetAttribute("val", GetValue()); }
/*---------------------------------------------------------------------- | TestRegression +---------------------------------------------------------------------*/ static void TestRegression() { // test for a bug found when the XML parser would try // to compare a null prefix NPT_XmlElementNode* element = new NPT_XmlElementNode("hello"); element->SetAttribute("ns", "foo", "6"); element->SetAttribute("foo", "5"); element->SetAttribute("ns", "foo", "7"); element->SetAttribute("foo", "8"); element->SetNamespaceUri("ns", "blabla"); CHECK(*element->GetAttribute("foo") == "8"); CHECK(*element->GetAttribute("foo", "blabla") == "7"); delete element; }
/*---------------------------------------------------------------------- | PLT_DeviceData::GetDescription +---------------------------------------------------------------------*/ NPT_Result PLT_DeviceData::GetDescription(NPT_String& desc) { NPT_Result res; NPT_XmlElementNode* spec = NULL; NPT_XmlElementNode* root = new NPT_XmlElementNode("root"); NPT_CHECK_LABEL_SEVERE(res = root->SetNamespaceUri("", "urn:schemas-upnp-org:device-1-0"), cleanup); NPT_CHECK_LABEL_SEVERE(res = root->SetNamespaceUri("dlna", "urn:schemas-dlna-org:device-1-0"), cleanup); NPT_CHECK_LABEL_SEVERE(res = root->SetAttribute("", "configId", NPT_String::FromInteger(m_ConfigId)), cleanup); // add spec version spec = new NPT_XmlElementNode("specVersion"); NPT_CHECK_LABEL_SEVERE(res = root->AddChild(spec), cleanup); NPT_CHECK_LABEL_SEVERE(res = PLT_XmlHelper::AddChildText(spec, "major", "1"), cleanup); NPT_CHECK_LABEL_SEVERE(res = PLT_XmlHelper::AddChildText(spec, "minor", "1"), cleanup); // get device xml NPT_CHECK_LABEL_SEVERE(res = GetDescription(root), cleanup); // serialize node NPT_CHECK_LABEL_SEVERE(res = PLT_XmlHelper::Serialize(*root, desc, true, 2), cleanup); cleanup: delete root; return res; }
/*---------------------------------------------------------------------- | PLT_StateVariable::GetSCPDXML +---------------------------------------------------------------------*/ NPT_Result PLT_StateVariable::GetSCPDXML(NPT_XmlElementNode* node) { NPT_XmlElementNode* variable = new NPT_XmlElementNode("stateVariable"); NPT_CHECK_SEVERE(node->AddChild(variable)); NPT_CHECK_SEVERE(variable->SetAttribute("sendEvents", m_IsSendingEvents?"yes":"no")); NPT_CHECK_SEVERE(PLT_XmlHelper::AddChildText(variable, "name", m_Name)); NPT_CHECK_SEVERE(PLT_XmlHelper::AddChildText(variable, "dataType", m_DataType)); if (m_DefaultValue.GetLength()) { NPT_CHECK_SEVERE(PLT_XmlHelper::AddChildText(variable, "defaultValue", m_DefaultValue)); } if (m_AllowedValues.GetItemCount()) { NPT_XmlElementNode* allowedValueList = new NPT_XmlElementNode("allowedValueList"); NPT_CHECK_SEVERE(variable->AddChild(allowedValueList)); for( int l = 0 ; l < (int)m_AllowedValues.GetItemCount(); l++) { NPT_CHECK_SEVERE(PLT_XmlHelper::AddChildText(allowedValueList, "allowedValue", (*m_AllowedValues[l]))); } } else if (m_AllowedValueRange) { NPT_XmlElementNode* range = new NPT_XmlElementNode("allowedValueRange"); NPT_CHECK_SEVERE(variable->AddChild(range)); NPT_CHECK_SEVERE(PLT_XmlHelper::AddChildText(range, "minimum", NPT_String::FromInteger(m_AllowedValueRange->min_value))); NPT_CHECK_SEVERE(PLT_XmlHelper::AddChildText(range, "maximum", NPT_String::FromInteger(m_AllowedValueRange->max_value))); if (m_AllowedValueRange->step != -1) { NPT_CHECK_SEVERE(PLT_XmlHelper::AddChildText(range, "step", NPT_String::FromInteger(m_AllowedValueRange->step))); } } return NPT_SUCCESS; }
/*---------------------------------------------------------------------- | TestSerializer +---------------------------------------------------------------------*/ static void TestSerializer() { NPT_XmlWriter writer; NPT_MemoryStream output; NPT_String check; NPT_LargeSize size; // // test without namespaces // // simple element with no prefix and no namespace NPT_XmlElementNode* top = new NPT_XmlElementNode("top"); writer.Serialize(*top, output); output.GetSize(size); check.Assign((const char*)output.GetData(), (NPT_Size)size); CHECK(check == "<top/>"); // with one attribute output.SetSize(0); top->SetAttribute("attr1", "b&w"); writer.Serialize(*top, output); output.GetSize(size); check.Assign((const char*)output.GetData(), (NPT_Size)size); CHECK(check == "<top attr1=\"b&w\"/>"); // add one child output.SetSize(0); delete top; top = new NPT_XmlElementNode("top"); NPT_XmlElementNode* child1 = new NPT_XmlElementNode("child1"); top->AddChild(child1); writer.Serialize(*top, output); output.GetSize(size); check.Assign((const char*)output.GetData(), (NPT_Size)size); CHECK(check == "<top><child1/></top>"); // // test with namespaces // // test default namespaces output.SetSize(0); delete top; top = new NPT_XmlElementNode("top"); top->SetNamespaceUri("", "http://namespace.com"); writer.Serialize(*top, output); output.GetSize(size); check.Assign((const char*)output.GetData(), (NPT_Size)size); CHECK(check == "<top xmlns=\"http://namespace.com\"/>"); // test attribute prefixes output.SetSize(0); delete top; top = new NPT_XmlElementNode("top"); top->SetAttribute(NULL, "foo", "6"); top->SetAttribute("ns1", "foo", "3"); top->SetAttribute("ns2", "foo", "4"); top->SetAttribute("ns1", "foo", "5"); writer.Serialize(*top, output); output.GetSize(size); check.Assign((const char*)output.GetData(), (NPT_Size)size); CHECK(check == "<top foo=\"6\" ns1:foo=\"5\" ns2:foo=\"4\"/>"); delete top; }