/** * Internal function for creating the <device> element filled with values * @param w Pointer to XMLWriter * @param dev Device data * @param debug Flag - add debug mark * @param proto Communication protocol version * @param fw Firmware version of device */ void XMLTool::createDevice(XMLWriter* w, Device dev, bool debug, string proto, string fw) { AttributesImpl att; att.addAttribute("", "", "euid", "", toStringFromLongHex(dev.euid)); att.addAttribute("", "", "device_id", "", toStringFromLongHex(dev.device_id, 2)); if(dev.name != ""){ att.addAttribute("", "", "name", "", dev.name); } w->startElement("", "device", "", att); if (debug) w->dataElement("", "", "debug", "", "protocol_version", proto, "fw_version", fw); TT_Table tt = fillDeviceTable(); auto tt_device = tt.find(dev.device_id); if (tt_device == tt.end()) throw Poco::Exception("Missing device in types table"); // FIXME temporary "fix", do it properly auto tt_dev = tt_device->second; if (dev.values.size()) { // If there are some values AttributesImpl attVal; attVal.addAttribute("", "", "count", "", toStringFromInt(dev.values.size())); w->startElement("", "values", "", attVal); for (auto item : dev.values){ if(item.status == true){ w->dataElement("", "", "value", toStringFromFloat(item.value), "module_id", toStringFromHex(item.mid)); // convert to hex format (0x00) } else { w->dataElement("", "", "value", toStringFromFloat(item.value), "module_id", toStringFromHex(item.mid), "status", "unavailable"); } } w->endElement("", "values", ""); } w->endElement("", "device", ""); }
void AttributesImplTest::testCopy() { AttributesImpl attrs; attrs.addAttribute("urn:ns1", "a1", "p:a1", "CDATA", "v1"); attrs.addAttribute("urn:ns1", "a2", "p:a2", "CDATA", "v2"); attrs.addAttribute("urn:ns2", "a3", "q:a3", "CDATA", "v3"); AttributesImpl attrs2; attrs2.setAttributes(attrs); assert (attrs2.getLength() == 3); assert (attrs2.getQName(0) == "p:a1"); assert (attrs2.getQName(1) == "p:a2"); assert (attrs2.getQName(2) == "q:a3"); assert (attrs2.getLocalName(0) == "a1"); assert (attrs2.getLocalName(1) == "a2"); assert (attrs2.getLocalName(2) == "a3"); assert (attrs2.getURI(0) == "urn:ns1"); assert (attrs2.getURI(1) == "urn:ns1"); assert (attrs2.getURI(2) == "urn:ns2"); assert (attrs2.getValue(0) == "v1"); assert (attrs2.getValue(1) == "v2"); assert (attrs2.getValue(2) == "v3"); }
void AttributesImplTest::testNoNamespaces() { AttributesImpl attrs; assert (attrs.getLength() == 0); assert (attrs.getIndex("foo") == -1); assert (attrs.getValue("foo").empty()); attrs.addAttribute("", "", "a1", "CDATA", "v1"); assert (attrs.getLength() == 1); assert (attrs.getIndex("a1") == 0); assert (attrs.getQName(0) == "a1"); assert (attrs.getType(0) == "CDATA"); assert (attrs.getValue(0) == "v1"); assert (attrs.isSpecified(0)); assert (attrs.getType("a1") == "CDATA"); assert (attrs.getValue("a1") == "v1"); attrs.addAttribute("", "", "a2", "CDATA", "v2"); assert (attrs.getLength() == 2); assert (attrs.getIndex("a2") == 1); assert (attrs.getQName(1) == "a2"); assert (attrs.getType(1) == "CDATA"); assert (attrs.getValue(1) == "v2"); assert (attrs.isSpecified(1)); assert (attrs.getType("a2") == "CDATA"); assert (attrs.getValue("a2") == "v2"); attrs.addAttribute("", "", "a3", "CDATA", "v3"); assert (attrs.getLength() == 3); assert (attrs.getIndex("a3") == 2); assert (attrs.getValue("a3") == "v3"); attrs.removeAttribute(0); assert (attrs.getLength() == 2); assert (attrs.getIndex("a1") == -1); assert (attrs.getIndex("a2") == 0); assert (attrs.getIndex("a3") == 1); assert (attrs.getQName(0) == "a2"); assert (attrs.getQName(1) == "a3"); attrs.removeAttribute("a3"); assert (attrs.getLength() == 1); assert (attrs.getIndex("a1") == -1); assert (attrs.getIndex("a2") == 0); assert (attrs.getIndex("a3") == -1); assert (attrs.getQName(0) == "a2"); }
void XSDGenerator::structStart(const Poco::CppParser::Struct* pStruct, const CodeGenerator::Properties& properties) { _pStructIn = pStruct; if (!_documentStarted) { _xsdOut.startDocument(); _xsdOut.startPrefixMapping(WSDLGenerator::TYPE_PREFIX, _targetNamespace); } _xsdOut.startPrefixMapping(SCHEMA_PREFIX, SCHEMA_NS); AttributesImpl attr; attr.addAttribute(EMPTYSTRING, TARGETNAMESPACE, TARGETNAMESPACE, EMPTYSTRING, _targetNamespace); attr.addAttribute(EMPTYSTRING, ELEMENTFORMDEFAULT, ELEMENTFORMDEFAULT, EMPTYSTRING, "qualified"); _xsdOut.startElement(SCHEMA_NS, SCHEMA, SCHEMA, attr); }
void XMLTool::createParam(XMLWriter* w, CmdParam par, string state){ AttributesImpl att; att.addAttribute("", "", "param_id", "", toStringFromInt(par.param_id)); if (par.euid > 0) att.addAttribute("", "", "euid", "", toStringFromLongHex(par.euid)); w->startElement("", "parameter", "", att); if (state == "parameters" && par.value.size()) { for (auto item: par.value){ w->dataElement("", "", "value", item.first); } } w->endElement("", "parameter", ""); }
void AttributesImplTest::testAccessors() { AttributesImpl attrs; attrs.addAttribute("urn:ns1", "a1", "p:a1", "CDATA", "v1"); attrs.addAttribute("urn:ns1", "a2", "p:a2", "CDATA", "v2", false); attrs.addAttribute("urn:ns2", "a3", "q:a3", "CDATA", "v3", true); assert (attrs.getQName(0) == "p:a1"); assert (attrs.getQName(1) == "p:a2"); assert (attrs.getQName(2) == "q:a3"); assert (attrs.getLocalName(0) == "a1"); assert (attrs.getLocalName(1) == "a2"); assert (attrs.getLocalName(2) == "a3"); assert (attrs.getURI(0) == "urn:ns1"); assert (attrs.getURI(1) == "urn:ns1"); assert (attrs.getURI(2) == "urn:ns2"); assert (attrs.getValue(0) == "v1"); assert (attrs.getValue(1) == "v2"); assert (attrs.getValue(2) == "v3"); assert (attrs.isSpecified(0)); assert (!attrs.isSpecified(1)); assert (attrs.isSpecified(2)); attrs.setType(0, "NMTOKEN"); assert (attrs.getType(0) == "NMTOKEN"); assert (attrs.getType("urn:ns1", "a1") == "NMTOKEN"); attrs.setValue(1, "v2 v2"); assert (attrs.getValue(1) == "v2 v2"); assert (attrs.getValue("urn:ns1", "a2") == "v2 v2"); assert (attrs.isSpecified(1)); attrs.setLocalName(2, "A3"); assert (attrs.getLocalName(2) == "A3"); attrs.setQName(2, "p:A3"); assert (attrs.getQName(2) == "p:A3"); attrs.setURI(2, "urn:ns1"); assert (attrs.getURI(2) == "urn:ns1"); assert (attrs.getValue("urn:ns1", "A3") == "v3"); }
void XMLWriter::dataElement(const XMLString& namespaceURI, const XMLString& localName, const XMLString& qname, const XMLString& data, const XMLString& attr1, const XMLString& value1, const XMLString& attr2, const XMLString& value2, const XMLString& attr3, const XMLString& value3) { AttributesImpl attributes; if (!attr1.empty()) attributes.addAttribute(XMLString(), XMLString(), attr1, CDATA, value1); if (!attr2.empty()) attributes.addAttribute(XMLString(), XMLString(), attr2, CDATA, value2); if (!attr3.empty()) attributes.addAttribute(XMLString(), XMLString(), attr3, CDATA, value3); if (data.empty()) { emptyElement(namespaceURI, localName, qname, attributes); } else { startElement(namespaceURI, localName, qname, attributes); characters(data); endElement(namespaceURI, localName, qname); } }
/** * Create message which can be sent to server. * @param type Message type (0 - A_TO_S, 1 - INIT, 2 - PARAM) * @return Created message in string */ string XMLTool::createXML(int type) { stringstream stream; Poco::UTF8Encoding utf8; try { XMLWriter writer(stream, XMLWriter::WRITE_XML_DECLARATION | XMLWriter::PRETTY_PRINT, "UTF-8", &utf8); // Print XML header and add newline for each tag writer.setNewLine("\n"); AttributesImpl attrs; writer.startDocument(); attrs.addAttribute("", "", "adapter_id", "", msg.adapter_id); // TODO!! remove attrs.addAttribute("", "", "state", "", msg.state); attrs.addAttribute("", "", "protocol_version", "", msg.protocol_version); attrs.addAttribute("", "", "fw_version", "", msg.fw_version); attrs.addAttribute("", "", "time", "", toStringFromLongInt(msg.time)); if (type == INIT) { // TODO attrs.addAttribute("", "", "adapter_id", "", msg.adapter_id); // TODO send even if I don't have it yet? attrs.addAttribute("", "", "tt_version", "", toStringFromLongInt(msg.tt_version)); } writer.startElement("", "adapter_server", "", attrs); if (type == A_TO_S) { createDevice(&writer, msg.device); // TODO add fw_version etc? } else if (type == INIT) { writer.characters(" "); } else if (type == PARAM) { createParam(&writer, msg.params, msg.state); } writer.endElement("", "adapter_server", ""); writer.endDocument(); } catch (Poco::Exception& ex) { log.error("*** Exception: \n" + ex.displayText()); } return stream.str(); }
void XMLWriterTest::testAttributeCharacters() { std::ostringstream str; XMLWriter writer(str, 0); writer.startDocument(); AttributesImpl attrs; attrs.addAttribute("", "", "a1", "CDATA", "a b c\n\td"); attrs.addAttribute("", "", "a2", "CDATA", "a b c\r\nd"); writer.startElement("", "", "el", attrs); writer.endElement("", "", "el"); writer.endDocument(); std::string xml = str.str(); assert (xml == "<el a1=\"a b c
	d\" a2=\"a b c
d\"/>"); }
void XMLWriterTest::testAttributes() { std::ostringstream str; XMLWriter writer(str, 0); writer.startDocument(); AttributesImpl attrs; attrs.addAttribute("", "", "a1", "CDATA", "v1"); attrs.addAttribute("", "", "a2", "CDATA", "v2"); writer.startElement("", "", "el", attrs); writer.endElement("", "", "el"); writer.endDocument(); std::string xml = str.str(); assert (xml == "<el a1=\"v1\" a2=\"v2\"/>"); }
void XMLWriterTest::testAttributesPretty() { std::ostringstream str; XMLWriter writer(str, XMLWriter::PRETTY_PRINT | XMLWriter::PRETTY_PRINT_ATTRIBUTES); writer.setNewLine(XMLWriter::NEWLINE_LF); writer.startDocument(); AttributesImpl attrs; attrs.addAttribute("", "", "a1", "CDATA", "v1"); attrs.addAttribute("", "", "a2", "CDATA", "v2"); writer.startElement("", "", "el", attrs); writer.endElement("", "", "el"); writer.endDocument(); std::string xml = str.str(); assert (xml == "<el\n\ta1=\"v1\"\n\ta2=\"v2\"/>\n"); }
void XSDGenerator::exportElements() { ExportedTypes::const_iterator it = _exportElementTypes.begin(); ExportedTypes::const_iterator itEnd = _exportElementTypes.end(); for (; it != itEnd; ++it) { //<element name="xy" type = "prefix:xy"/> AttributesImpl attr; attr.addAttribute(EMPTYSTRING, NAME, NAME, EMPTYSTRING, it->first); attr.addAttribute(EMPTYSTRING, TYPE, TYPE, EMPTYSTRING, WSDLGenerator::TYPE_PREFIX+":"+ it->second); _xsdOut.startElement(SCHEMA_NS, ELEMENT, ELEMENT, attr); _xsdOut.endElement(SCHEMA_NS, ELEMENT, ELEMENT); } _exportElementTypes.clear(); SoapHeaderTypes::const_iterator itS = _exportSoapHeaderTypes.begin(); SoapHeaderTypes::const_iterator itSEnd = _exportSoapHeaderTypes.end(); for (; itS != itSEnd; ++itS) { //<element name="xy" type = "prefix:xy"/> AttributesImpl attr; attr.addAttribute(EMPTYSTRING, NAME, NAME, EMPTYSTRING, itS->first); std::string type = resolveType(itS->second); if (AbstractGenerator::BUILTIN_TYPES.find(type) != AbstractGenerator::BUILTIN_TYPES.end()) { type = mapToSchemaType(type); attr.addAttribute(EMPTYSTRING, TYPE, TYPE, EMPTYSTRING, XSDGenerator::SCHEMA_PREFIX+":"+ type); } else { attr.addAttribute(EMPTYSTRING, TYPE, TYPE, EMPTYSTRING, WSDLGenerator::TYPE_PREFIX+":"+ itS->second); } _xsdOut.startElement(SCHEMA_NS, ELEMENT, ELEMENT, attr); _xsdOut.endElement(SCHEMA_NS, ELEMENT, ELEMENT); } }
void DOMSerializer::handleElement(const Element* pElement) const { if (_pContentHandler) { AutoPtr<NamedNodeMap> pAttrs = pElement->attributes(); AttributesImpl saxAttrs; for (unsigned long i = 0; i < pAttrs->length(); ++i) { Attr* pAttr = static_cast<Attr*>(pAttrs->item(i)); saxAttrs.addAttribute(pAttr->namespaceURI(), pAttr->localName(), pAttr->nodeName(), CDATA, pAttr->value(), pAttr->specified()); } _pContentHandler->startElement(pElement->namespaceURI(), pElement->localName(), pElement->tagName(), saxAttrs); } iterate(pElement->firstChild()); if (_pContentHandler) _pContentHandler->endElement(pElement->namespaceURI(), pElement->localName(), pElement->tagName()); }