Exemplo n.º 1
0
void XMLWriterTest::testAttributeNamespaces()
{
	std::ostringstream str;
	XMLWriter writer(str, 0);
	Poco::XML::AttributesImpl attrs;
	attrs.addAttribute("urn:other", "myattr", "", "", "attrValue");
	attrs.addAttribute("urn:ns", "myattr2", "", "", "attrValue2");
	writer.startDocument();
	writer.startElement("urn:ns", "r", "", attrs);
	writer.characters("data");
	writer.endElement("urn:ns", "r", "");
	writer.endDocument();
	std::string xml = str.str();
	assert (xml == "<ns2:r ns1:myattr=\"attrValue\" ns2:myattr2=\"attrValue2\" xmlns:ns1=\"urn:other\" xmlns:ns2=\"urn:ns\">data</ns2:r>");
}
Exemplo n.º 2
0
void XMLWriterTest::testNamespacesNestedCanonical()
{
	std::ostringstream str;
	XMLWriter writer(str, XMLWriter::CANONICAL_XML);
	writer.startDocument();
	writer.startElement("urn:ns1", "r", "");
	writer.startElement("urn:ns1", "e", "");
	writer.endElement("urn:ns1", "e", "");
	Poco::XML::AttributesImpl attrs;
	attrs.addAttribute("urn:ns1", "myattr", "myattr", "", "attrValue");
	writer.startElement("urn:ns2", "f", "", attrs);
	writer.endElement("urn:ns2", "f", "");
	writer.endElement("urn:ns1", "r", "");
	writer.endDocument();
	std::string xml = str.str();
	assert (xml == "<r xmlns=\"urn:ns1\"><e></e><ns1:f xmlns:ns1=\"urn:ns2\" myattr=\"attrValue\"></ns1:f></r>");
}
Exemplo n.º 3
0
 void Component::writeXML(Poco::XML::XMLWriter &writer) const {
   Poco::XML::AttributesImpl attr;
   attr.addAttribute("", "pos", "", "", m_pos.toString());
   writer.startElement("", "Component", "", attr);
   writer.endElement("", "Component", "");
 }
Exemplo n.º 4
0
void VSXMLWriter::startElement(const Poco::XML::XMLString& namespaceURI, const Poco::XML::XMLString& localName, const Poco::XML::XMLString& qname, const Poco::XML::Attributes& attributes)
{
	if (!_tagClosed.back())
	{
		_ostr << ">\r\n";
		_tagClosed.back() = true;
	}
	indent();
	++_indent;
	_ostr << "<" << qname;
	if (attributes.getLength() > 0)
	{
		Poco::XML::AttributesImpl sortedAttributes;
		if (qname == "VisualStudioProject")
		{
			sortedAttributes.addAttribute("", "", "Name", "CDATA", attributes.getValue("Name"));
			sortedAttributes.addAttribute("", "", "Version", "CDATA", attributes.getValue("Version"));
			sortedAttributes.addAttribute("", "", "ProjectType", "CDATA", attributes.getValue("ProjectType"));
			sortedAttributes.addAttribute("", "", "ProjectGUID", "CDATA", attributes.getValue("ProjectGUID"));
			sortedAttributes.addAttribute("", "", "RootNamespace", "CDATA", attributes.getValue("RootNamespace"));
			sortedAttributes.addAttribute("", "", "Keyword", "CDATA", attributes.getValue("Keyword"));
		}
		else
		{
			if (attributes.getIndex("Name") != -1)
			{
				sortedAttributes.addAttribute("", "", "Name", "CDATA", attributes.getValue("Name"));
			}
			for (int i = 0; i < attributes.getLength(); i++)
			{
				if (attributes.getQName(i) != "Name")
				{
					std::string value = attributes.getValue(i);
					if (_convertBool && (value == "true" || value == "false"))	
						value = Poco::toUpper(value);
					sortedAttributes.addAttribute(attributes.getURI(i), attributes.getLocalName(i), attributes.getQName(i), attributes.getType(i), value);
				}
			}
		}
		for (int i = 0; i < sortedAttributes.getLength(); i++)
		{
			_ostr << "\r\n";
			indent();
			_ostr << sortedAttributes.getQName(i) << "=\"";
			std::string value = sortedAttributes.getValue(i);
			for (Poco::XML::XMLString::const_iterator itv = value.begin(); itv != value.end(); ++itv)
			{
				char c = *itv;
				switch (c)
				{
				case '"':  _ostr << "&quot;"; break;
				case '\'': _ostr << "&apos;"; break;
				case '&':  _ostr << "&amp;"; break;
				case '<':  _ostr << "&lt;"; break;
				case '>':  _ostr << "&gt;"; break;
				case '\t': _ostr << "&#x9;"; break;
				case '\r': _ostr << "&#xD;"; break;
				case '\n': _ostr << "&#xA;"; break;
				default:   _ostr << c; break;
				}
			}
			_ostr << "\"";
		}
	}
	_tagClosed.push_back(false);
}
Exemplo n.º 5
0
std::string XSDGenerator::generateComplexType(const Poco::CppParser::Struct* pType)
{
	std::string fullName = pType->fullName();
	std::string complexName = generateComplexTypeName(pType);
	if (_handledTypes.find(fullName) != _handledTypes.end())
		return complexName;
	_handledTypes.insert(fullName);
	
	std::vector<const Poco::CppParser::Struct*> detectedTypes;

	// types are always a sequence, a vector has always infinite as limit
	//<complexType name="someName">
	//    <sequence>
	//          <element name="whereExample" type="string" minOccurs="0" maxOccurs="unbounded"/>
	const Poco::XML::AttributesImpl emptyAttr;
	Poco::XML::AttributesImpl attr;
	attr.addAttribute(EMPTYSTRING, NAME, NAME, EMPTYSTRING, complexName);

	_xsdOut.startElement(SCHEMA_NS, COMPLEXTYPE, COMPLEXTYPE, attr);
	SerializerGenerator::VarGet varMatch;
	SerializerGenerator::OrderedVars attrs;
	SerializerGenerator::OrderedVars elems;
	SerializerGenerator::matchVarsWithFunctions(pType, varMatch);
	SerializerGenerator::doElemAttrSplit(varMatch, attrs, elems);

	Poco::CppParser::Struct::BaseIterator itB = pType->baseBegin();
	Poco::CppParser::Struct::BaseIterator itBEnd = pType->baseEnd();
	// mutiple inheritance is not supported (we could do it by defining for all members for all structs a group
	// and for each multiple inheritance we have to define another group which groups together these groups :-)
	// Anyway, too much work for a rarely used feature
	bool usesInheritance = false;
	for (; itB != itBEnd; ++itB)
	{
		const Poco::CppParser::Struct::Base& b = *itB;
		if (GenUtility::mustBeSerializable(b.name))
		{
			if (!b.pClass)
				throw CodeGenerationException("Missing class", b.name);
			detectedTypes.push_back(b.pClass);
			usesInheritance = true;
			_xsdOut.startElement(SCHEMA_NS, COMPLEXCONTENT, COMPLEXCONTENT, emptyAttr);
			Poco::XML::AttributesImpl extAttr;
			extAttr.addAttribute(EMPTYSTRING, BASE, BASE, EMPTYSTRING, WSDLGenerator::TYPE_PREFIX+":"+generateComplexTypeName(b.pClass));
			_xsdOut.startElement(SCHEMA_NS, EXTENSION, EXTENSION, extAttr);
			break;
		}
	}

	// check for header attribute: 
	// remove the header stuff from the type
	// continue writing the body type
	std::vector<Poco::XML::AttributesImpl> soapHeaderElements;
	std::vector<Poco::XML::AttributesImpl> soapBodyElements;

	SerializerGenerator::OrderedVars::const_iterator itElems = elems.begin();
	SerializerGenerator::OrderedVars::const_iterator itElemsEnd = elems.end();
	for (; itElems != itElemsEnd; ++itElems)
	{
		std::string elemName;
		bool isVector = false;
		bool isMandatory = true;
		bool isNullable = false;
		std::string resolvedType;
		std::string soapHeader;
		Poco::CodeGeneration::CodeGenerator::Properties varProps;
		Poco::CodeGeneration::GeneratorEngine::parseProperties(itElems->second->second.first, varProps);
		detectProperties(itElems->second->second.first, isVector, isMandatory, isNullable, elemName, resolvedType);
		const Poco::CppParser::Symbol* pSym = Poco::CppParser::NameSpace::root()->lookup(resolvedType);
		if (pSym == 0)
		{
			pSym = pType->lookup(resolvedType); // for inner classes
			if (pSym != 0) // 0 is ok for primitve types
				resolvedType = pSym->fullName();
		}
		if (GenUtility::isPtrType(resolvedType) || GenUtility::isNullableType(resolvedType) || GenUtility::isOptionalType(resolvedType))
		{
			std::vector<std::string> tt = GenUtility::getInnerTemplateTypes(resolvedType);
			if (tt.size() == 1 || (tt.size() == 2 && GenUtility::isNullableType(tt[0])))
			{
				resolvedType = tt[tt.size() - 1];
				pSym = pType->lookup(resolvedType);
				if (!pSym)
					pSym = Poco::CppParser::NameSpace::root()->lookup(resolvedType);
			}
			else if (tt.size() > 0)
			{
				throw CodeGenerationException("Unsupported template type", resolvedType + "<" + tt[0] + ">");
			}
			else throw CodeGenerationException("Unparsable template type", resolvedType);
		}
		Poco::XML::AttributesImpl elemAttrs;
		elemAttrs.addAttribute(EMPTYSTRING, NAME, NAME, EMPTYSTRING, elemName);
		if (!isMandatory || isVector)
			elemAttrs.addAttribute(EMPTYSTRING, MINOCCURS, MINOCCURS, EMPTYSTRING, "0");
		if (isVector)
			elemAttrs.addAttribute(EMPTYSTRING, MAXOCCURS, MAXOCCURS, EMPTYSTRING, UNBOUNDED);
		if (isNullable)
			elemAttrs.addAttribute(EMPTYSTRING, NILLABLE, NILLABLE, EMPTYSTRING, "true");
		
		if (GenUtility::isVectorType(resolvedType))
		{
			// we have a matrix
			throw CodeGenerationException("A collection of collections is not supported with an XML schema. Create a class that holds the inner collection, and then create a collection of that class.");
		}

		if (resolvedType.find("std::") == 0 || AbstractGenerator::BUILTIN_TYPES.find(resolvedType) != AbstractGenerator::BUILTIN_TYPES.end())
		{
			const std::string& xsdType = mapToSchemaType(resolvedType);
			elemAttrs.addAttribute(EMPTYSTRING, TYPE, TYPE, EMPTYSTRING, SCHEMA_PREFIX+":"+xsdType);
		}
		else
		{
			const Poco::CppParser::Struct* pResType = dynamic_cast<const Poco::CppParser::Struct*>(pSym);
			if (pResType == 0)
			{
				if (pSym && pSym->kind() == Poco::CppParser::Symbol::SYM_ENUM)
				{
					// map to integer
					const std::string& xsdType = mapToSchemaType("int");
					elemAttrs.addAttribute(EMPTYSTRING, TYPE, TYPE, EMPTYSTRING, SCHEMA_PREFIX+":"+xsdType);
				}	
				else
					throw CodeGenerationException("Missing class", resolvedType);
			}
			else
			{
				detectedTypes.push_back(pResType);
				elemAttrs.addAttribute(EMPTYSTRING, TYPE, TYPE, EMPTYSTRING, WSDLGenerator::TYPE_PREFIX+":"+XSDGenerator::generateComplexTypeName(pResType));
			}
		}
		soapBodyElements.push_back(elemAttrs);
	}

	if (!soapBodyElements.empty())
	{
		_xsdOut.startElement(SCHEMA_NS, SEQUENCE, SEQUENCE, emptyAttr);
		std::vector<Poco::XML::AttributesImpl>::const_iterator it = soapBodyElements.begin();
		std::vector<Poco::XML::AttributesImpl>::const_iterator itEnd = soapBodyElements.end();
		for (; it != itEnd; ++it)
		{
			_xsdOut.startElement(SCHEMA_NS, ELEMENT, ELEMENT, *it);
			_xsdOut.endElement(SCHEMA_NS, ELEMENT, ELEMENT);
		}
		_xsdOut.endElement(SCHEMA_NS, SEQUENCE, SEQUENCE);
	}

	// attrs
	SerializerGenerator::OrderedVars::const_iterator itAttrs = attrs.begin();
	SerializerGenerator::OrderedVars::const_iterator itAttrsEnd = attrs.end();
	for (; itAttrs != itAttrsEnd; ++itAttrs)
	{
		std::string attrName;
		bool isVector = false;
		bool isMandatory = true;
		bool isNullable = false;
		std::string resolvedType;
		detectProperties(itAttrs->second->second.first, isVector, isMandatory, isNullable, attrName, resolvedType);
		if (isVector)
			throw CodeGenerationException("A vector cannot be serialized as an attribute.");
		if (isNullable)
			throw CodeGenerationException("A Nullable cannot be serialized as an attribute.");

		const Poco::CppParser::Symbol* pSym = Poco::CppParser::NameSpace::root()->lookup(resolvedType);
		if (pSym == 0)
		{
			pSym = pType->lookup(resolvedType); // for inner classes
			if (pSym != 0) // 0 is ok for primitve types
				resolvedType = pSym->fullName();
		}
		Poco::XML::AttributesImpl attrAttrs;
		attrAttrs.addAttribute(EMPTYSTRING, NAME, NAME, EMPTYSTRING, attrName);
		if (isMandatory)
			attrAttrs.addAttribute(EMPTYSTRING, USE, USE, EMPTYSTRING, REQUIRED);
		Poco::replaceInPlace(resolvedType, "mutable", "");
		Poco::trimInPlace(resolvedType);
		if (resolvedType.find("std::") == 0 || AbstractGenerator::BUILTIN_TYPES.find(resolvedType) != AbstractGenerator::BUILTIN_TYPES.end())
		{
			// a simple type
			const std::string& xsdType = mapToSchemaType(resolvedType);
			attrAttrs.addAttribute(EMPTYSTRING, TYPE, TYPE, EMPTYSTRING, SCHEMA_PREFIX+":"+xsdType);
		}
		else
		{
			if (pSym && pSym->kind() == Poco::CppParser::Symbol::SYM_ENUM)
			{
				// map to integer
				const std::string& xsdType = mapToSchemaType("int");
				attrAttrs.addAttribute(EMPTYSTRING, TYPE, TYPE, EMPTYSTRING, SCHEMA_PREFIX+":"+xsdType);
			}	
			else throw CodeGenerationException("An attribute can only have a simple content.");		
		}
		//<element name="whereExample" type="string" minOccurs="0" maxOccurs="unbounded"/>
		_xsdOut.startElement(SCHEMA_NS, ATTRIBUTE, ATTRIBUTE, attrAttrs);
		_xsdOut.endElement(SCHEMA_NS, ATTRIBUTE, ATTRIBUTE);
	}

	if (usesInheritance)
	{
		_xsdOut.endElement(SCHEMA_NS, EXTENSION, EXTENSION);
		_xsdOut.endElement(SCHEMA_NS, COMPLEXCONTENT, COMPLEXCONTENT);
	}
	_xsdOut.endElement(SCHEMA_NS, COMPLEXTYPE, COMPLEXTYPE);

	generateTypes(detectedTypes);
	return complexName;
}
Exemplo n.º 6
0
    void Mapper::Export(const std::string &file)
    {
        InputModuleOIS::LogInfo("Exporting default input mappings to file " + file + "...");

        std::fstream file_op(file.c_str(), std::ios::out);

        Poco::XML::XMLWriter writer(file_op, Poco::XML::XMLWriter::CANONICAL);
	    writer.startDocument();
	    writer.startElement("", "", "input");

        {
            const InputModuleOIS::KeyEventInfoMap &events = module_->GetRegisteredKeyEvents();
            for ( InputModuleOIS::KeyEventInfoMap::const_iterator state = events.begin() ; 
                  state != events.end() ;
                  ++state )
            {
                for ( InputModuleOIS::KeyEventInfoVector::const_iterator info = state->second.begin() ; 
                  info != state->second.end() ;
                  ++info )
                {
                    Poco::XML::AttributesImpl attrs;
                    attrs.addAttribute("", "", "state", "CDATA", ToString(static_cast<int>(state->first)));
                    attrs.addAttribute("", "", "start_event", "CDATA", ToString(info->pressed_event_id_));
                    attrs.addAttribute("", "", "end_event", "CDATA", ToString(info->released_event_id_));
	                attrs.addAttribute("", "", "modifier", "CDATA", ToString(info->modifier_));
                    attrs.addAttribute("", "", "key", "CDATA", ToString(info->key_));
	                writer.emptyElement("", "", "action", attrs);
                }
            }
        }

        {
            const InputModuleOIS::SliderInfoMap &events = module_->GetRegisteredSliderEvents();
            for ( InputModuleOIS::SliderInfoMap::const_iterator state = events.begin() ; 
                  state != events.end() ;
                  ++state )
            {
                for ( InputModuleOIS::SliderInfoVector::const_iterator info = state->second.begin() ; 
                      info != state->second.end() ;
                      ++info )
                {
                    Poco::XML::AttributesImpl attrs;
                    attrs.addAttribute("", "", "state", "CDATA", ToString(static_cast<int>(state->first)));
                    attrs.addAttribute("", "", "start_event", "CDATA", ToString(info->dragged_event_));
                    attrs.addAttribute("", "", "end_event", "CDATA", ToString(info->stopped_event_));
	                attrs.addAttribute("", "", "modifier", "CDATA", ToString(info->modifier_));
                    attrs.addAttribute("", "", "button", "CDATA", ToString(info->button_));
                    attrs.addAttribute("", "", "type", "CDATA", ToString(info->slider_));
	                writer.emptyElement("", "", "action_slider", attrs);
                }
            }
        }
        writer.endElement("", "", "input");
	    writer.endDocument();
    }