diff options
| author | Andreas Stöckel <astoecke@techfak.uni-bielefeld.de> | 2015-04-16 01:11:44 +0200 | 
|---|---|---|
| committer | Andreas Stöckel <astoecke@techfak.uni-bielefeld.de> | 2016-04-25 22:26:12 +0200 | 
| commit | 1e44e62ad33ac8e73af18de01b122e3c3198e438 (patch) | |
| tree | 3e6bba32a57a339b96c082aae8fcb5afbdfcbef4 /src | |
| parent | cd0e0eaee10d6587a4547af4d86f261d34a54ee0 (diff) | |
Generate unique IDs for XmlOutput and serialize those
Diffstat (limited to 'src')
| -rw-r--r-- | src/core/common/VariantConverter.cpp | 24 | ||||
| -rw-r--r-- | src/plugins/xml/XmlOutput.cpp | 27 | 
2 files changed, 45 insertions, 6 deletions
| diff --git a/src/core/common/VariantConverter.cpp b/src/core/common/VariantConverter.cpp index 699576b..ad0da83 100644 --- a/src/core/common/VariantConverter.cpp +++ b/src/core/common/VariantConverter.cpp @@ -304,15 +304,27 @@ bool VariantConverter::toString(Variant &var, Logger &logger, Mode mode)  				return true;  			}  			case VariantType::OBJECT: { -				// Print object address and type +				// Fetch the attached object, abort if it is a nullptr  				Variant::objectType obj = var.asObject(); -				std::stringstream ss; -				ss << "<object " << obj.get(); -				if (obj.get() != nullptr) { -					ss << " (" << obj->type()->name << ")"; +				if (obj == nullptr) { +					var = "<null>"; +					return true; +				} + +				// Check whether the object has an id attached -- if yes, output +				// that id +				Rooted<ManagedVariant> id = obj->readData<ManagedVariant>("id"); +				if (id != nullptr && id->v.isString()) { +					var = id->v; +					return true;  				} -				ss << ">"; + +				// Otherwise print object address and type +				std::stringstream ss; +				ss << "<object " << obj.get() << " (" << obj->type()->name +				   << ")>";  				var = ss.str().c_str(); +  				return true;  			}  			case VariantType::FUNCTION: { diff --git a/src/plugins/xml/XmlOutput.cpp b/src/plugins/xml/XmlOutput.cpp index e1703c4..cd1d187 100644 --- a/src/plugins/xml/XmlOutput.cpp +++ b/src/plugins/xml/XmlOutput.cpp @@ -24,6 +24,10 @@  #include <core/common/Variant.hpp>  #include <core/common/VariantWriter.hpp> +// TODO: This should not be here -- remove this once we have a proper +// transformation system. +#include <transformations/uniqueid/UniqueIdTransformation.hpp> +  namespace ousia {  namespace xml { @@ -53,6 +57,23 @@ struct TransformParams {  	}  }; +/** + * Helper function used for attaching the unique id of nodes (if available) to + * the XML elements. + * + * @param elem is the XML element to which the unique id should be attached. + * @param managed is the managed object from which the id should be looked up. + */ +static void attachId(Handle<Element> elem, Handle<Managed> managed) +{ +	// Check whether the managed object has an "id" attached to it +	Rooted<ManagedVariant> id = managed->readData<ManagedVariant>("id"); +	if (id != nullptr && id->v.isString()) { +		// We have an id that is a string, add it to the element +		elem->getAttributes().emplace("id", id->v.asString()); +	} +} +  /*   * These are forward method declarations to allow for cross-references of   * methods. @@ -156,6 +177,9 @@ void XmlTransformer::writeXml(Handle<Document> doc, std::ostream &out,                                Logger &logger, ResourceManager &resourceManager,                                bool pretty, bool flat)  { +	// Create unique ids for the nodes +	UniqueIdTransformation::transform(doc); +  	Manager &mgr = doc->getManager();  	// the outermost tag is the document itself.  	Rooted<Element> document{new Element{mgr, {nullptr}, "document"}}; @@ -741,6 +765,9 @@ static Rooted<Element> transformAnchor(Handle<Element> parent, Handle<Anchor> a,  		    attrs, "a:start")};  		// and handle the children.  		transformChildren(a->getAnnotation().get(), elem, P); + +		// attach a possible id to the anchor +		attachId(elem, a->getAnnotation());  	} else if (a->isEnd()) {  		/*  		 * in principle !a->isStart() should imply a->isEnd() but if no | 
