From 344e0e58ad63fba551f46fc4d9a2468e395f473a Mon Sep 17 00:00:00 2001 From: jsdelfino Date: Sun, 18 Oct 2009 22:24:59 +0000 Subject: Minor code cleanup, refactored list functions and cleaned up function names, moved support for elements to a new header file. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@826543 13f79535-47bb-0310-9956-ffa450edef68 --- cpp/sca/kernel/xml.hpp | 111 ++++++++++++------------------------------------- 1 file changed, 27 insertions(+), 84 deletions(-) (limited to 'cpp/sca/kernel/xml.hpp') diff --git a/cpp/sca/kernel/xml.hpp b/cpp/sca/kernel/xml.hpp index 135587a86f..16fddef1ef 100644 --- a/cpp/sca/kernel/xml.hpp +++ b/cpp/sca/kernel/xml.hpp @@ -32,6 +32,8 @@ #include #include #include "list.hpp" +#include "value.hpp" +#include "element.hpp" #include "monad.hpp" namespace tuscany { @@ -42,7 +44,7 @@ namespace tuscany { class XMLReader { public: enum TokenType { - None = 0, Element = 1, Attribute = 2, EndElement = 15, Identifier = 100, Text = 101, End = 103 + None = 0, Element = 1, Attribute = 2, Text = 3, EndElement = 15, Identifier = 100, End = 101 }; XMLReader(xmlTextReaderPtr xml) : xml(xml), tokenType(None) { @@ -55,24 +57,21 @@ public: } /** - * Read the next token and return its type. + * Read the next XML token and return its type. */ int read() { if (tokenType == End) return tokenType; if (tokenType == Element) { isEmptyElement = xmlTextReaderIsEmptyElement(xml); - hasValue = xmlTextReaderHasValue(xml); hasAttributes = xmlTextReaderHasAttributes(xml); return tokenType = Identifier; } - if (hasValue && tokenType == Identifier) - return tokenType = Text; - if (hasAttributes && (tokenType == Identifier || tokenType == Text) && xmlTextReaderMoveToFirstAttribute(xml) == 1) + if (tokenType == Identifier && hasAttributes && xmlTextReaderMoveToFirstAttribute(xml) == 1) return tokenType = Attribute; if (tokenType == Attribute && xmlTextReaderMoveToNextAttribute(xml) == 1) return tokenType = Attribute; - if (isEmptyElement && (tokenType == Identifier || tokenType == Text || tokenType == Attribute)) + if (isEmptyElement && (tokenType == Identifier || tokenType == Attribute)) return tokenType = EndElement; if (!xmlTextReaderRead(xml)) return tokenType = End; @@ -96,15 +95,13 @@ private: */ const value endElement("<"); const value startElement(">"); -const value attribute("attribute"); -const value element("element"); /** * Read an XML identifier. */ const value readIdentifier(XMLReader& reader) { const char* name = (const char*)xmlTextReaderConstName(reader); - return value(name); + return name; } /** @@ -112,7 +109,7 @@ const value readIdentifier(XMLReader& reader) { */ const value readText(XMLReader& reader) { const char *val = (const char*)xmlTextReaderConstValue(reader); - return value(std::string(val)); + return std::string(val); } /** @@ -121,7 +118,7 @@ const value readText(XMLReader& reader) { const value readAttribute(XMLReader& reader) { const char *name = (const char*)xmlTextReaderConstName(reader); const char *val = (const char*)xmlTextReaderConstValue(reader); - return value(makeList(attribute, value(name), value(std::string(val)))); + return mklist(attribute, name, std::string(val)); } /** @@ -145,14 +142,14 @@ const value readToken(XMLReader& reader) { } /** - * Read a list of XML tokens. + * Read a list of values from XML tokens. */ const list readList(const list& listSoFar, XMLReader& reader) { const value token = readToken(reader); if(isNil(token) || endElement == token) return reverse(listSoFar); if(startElement == token) - return readList(cons(value(readList(makeList(element), reader)), listSoFar), reader); + return readList(cons(readList(mklist(element), reader), listSoFar), reader); return readList(cons(token, listSoFar), reader); } @@ -162,7 +159,7 @@ const list readList(const list& listSoFar, XMLReader& reader) { const list read(XMLReader& reader) { value nextToken = readToken(reader); if (startElement == nextToken) - return makeList(value(readList(makeList(element), reader))); + return mklist(readList(mklist(element), reader)); return list(); } @@ -202,64 +199,6 @@ const list readXML(const list& ilist) { return read(reader); } -/** - * Returns true if a value is an XML attribute. - */ -const bool isAttribute(const list& l) { - return !isNil(l) && car(l) == attribute; -} - -/** - * Returns the name of an XML attribute. - */ -const std::string attributeName(const list& l) { - return cadr(l); -} - -/** - * Returns the text value of an XML attribute. - */ -const std::string attributeText(const list& l) { - return caddr(l); -} - -/** - * Returns true if a value is an XML element. - */ -const bool isElement(const list& l) { - return !isNil(l) && car(l) == element; -} - -/** - * Returns the name of an XML element. - */ -const std::string elementName(const list& l) { - return cadr(l); -} - -/** - * Returns true if an XML element contains text content. - */ -const bool elementHasText(const list& l) { - if (isNil(cddr(l))) - return false; - return isString(caddr(l)); -} - -/** - * Returns the text content of an XML element. - */ -const std::string elementText(const list& l) { - return caddr(l); -} - -/** - * Returns the children of an XML element. - */ -const list elementChildren(const list& l) { - return cddr(l); -} - /** * Default encoding used to write XML documents. */ @@ -273,18 +212,16 @@ const failable writeList(const list& l, const xmlTextW return true; // Write an attribute - const list token(car(l)); - if (isAttribute(token)) { - if (xmlTextWriterWriteAttribute(xml, (const xmlChar*)attributeName(token).c_str(), (const xmlChar*)attributeText(token).c_str()) < 0) + const value token(car(l)); + if (isTaggedList(token, attribute)) { + if (xmlTextWriterWriteAttribute(xml, (const xmlChar*)std::string(attributeName(token)).c_str(), (const xmlChar*)std::string(attributeValue(token)).c_str()) < 0) return std::string("xmlTextWriterWriteAttribute failed"); - } else if (isElement(token)) { + } else if (isTaggedList(token, element)) { // Write an element - if (xmlTextWriterStartElement(xml, (const xmlChar*)elementName(token).c_str()) < 0) + if (xmlTextWriterStartElement(xml, (const xmlChar*)std::string(elementName(token)).c_str()) < 0) return std::string("xmlTextWriterStartElement failed"); - if (elementHasText(token) && xmlTextWriterWriteString(xml, (const xmlChar*)elementText(token).c_str()) < 0) - return std::string("xmlTextWriterWriteString failed"); // Write its children const failable w = writeList(elementChildren(token), xml); @@ -293,6 +230,12 @@ const failable writeList(const list& l, const xmlTextW if (xmlTextWriterEndElement(xml) < 0) return std::string("xmlTextWriterEndElement failed"); + + } else { + + // Write XML text + if (xmlTextWriterWriteString(xml, (const xmlChar*)std::string(token).c_str()) < 0) + return std::string("xmlTextWriterWriteString failed"); } // Go on @@ -336,7 +279,7 @@ template int writeCallback(void *context, const char* buffer, int le } /** - * Write a list of values as an XML document. + * Convert a list of values to an XML document. */ template const failable writeXML(const lambda& reduce, const R& initial, const list& l) { XMLWriteContext cx(reduce, initial); @@ -352,13 +295,13 @@ template const failable writeXML(const lambda writeXMLList(const list& listSoFar, const std::string& s) { return cons(s, listSoFar); } -/** - * Write a list of values as an XML document represented as a list of strings. - */ const failable, std::string> writeXML(const list& l) { const failable, std::string> ls = writeXML >(writeXMLList, list(), l); if (!hasValue(ls)) -- cgit v1.2.3