summaryrefslogtreecommitdiff
path: root/cpp/src
diff options
context:
space:
mode:
authorMatthew Newhook <matthew@zeroc.com>2002-01-29 02:03:02 +0000
committerMatthew Newhook <matthew@zeroc.com>2002-01-29 02:03:02 +0000
commit1a347f053ed10e965411c4768567d3c6d0b0e424 (patch)
tree56685aadd79e5e7b954850369616e4d7915dbe1b /cpp/src
parentfix (diff)
downloadice-1a347f053ed10e965411c4768567d3c6d0b0e424.tar.bz2
ice-1a347f053ed10e965411c4768567d3c6d0b0e424.tar.xz
ice-1a347f053ed10e965411c4768567d3c6d0b0e424.zip
Added test/IceXML/encoding. Various bug fixes for IceXML.
Diffstat (limited to 'cpp/src')
-rw-r--r--cpp/src/IceXML/Output.cpp80
-rw-r--r--cpp/src/IceXML/StreamI.cpp87
-rw-r--r--cpp/src/slice2cpp/Gen.cpp4
3 files changed, 120 insertions, 51 deletions
diff --git a/cpp/src/IceXML/Output.cpp b/cpp/src/IceXML/Output.cpp
index 8646d34fcee..291b89e55ae 100644
--- a/cpp/src/IceXML/Output.cpp
+++ b/cpp/src/IceXML/Output.cpp
@@ -17,8 +17,9 @@ namespace IceXML
{
NextLine nl;
-StartBlock sb;
-EndBlock eb;
+//StartBlock sb;
+//EndBlock eb;
+EndElement ee;
Separator sp;
}
@@ -32,8 +33,7 @@ IceXML::Output::Output() :
_pos(0),
_indent(0),
_separator(true),
- _blockStart("{"),
- _blockEnd("}"),
+ _printed(true),
_useTab(true),
_indentSize(4)
{
@@ -44,8 +44,7 @@ IceXML::Output::Output(ostream& os) :
_pos(0),
_indent(0),
_separator(true),
- _blockStart("{"),
- _blockEnd("}"),
+ _printed(true),
_useTab(true),
_indentSize(4)
{
@@ -56,8 +55,7 @@ IceXML::Output::Output(const char* s) :
_pos(0),
_indent(0),
_separator(true),
- _blockStart("{"),
- _blockEnd("}"),
+ _printed(true),
_useTab(true),
_indentSize(4)
{
@@ -73,6 +71,11 @@ IceXML::Output::open(const char* s)
void
IceXML::Output::print(const char* s)
{
+ if (!_printed)
+ {
+ _out << '>';
+ _printed = true;
+ }
for (unsigned int i = 0; i < strlen(s); ++i)
{
if (s[i] == '\n')
@@ -124,18 +127,6 @@ IceXML::Output::restoreIndent()
}
void
-IceXML::Output::setBeginBlock(const char *bb)
-{
- _blockStart = bb;
-}
-
-void
-IceXML::Output::setEndBlock(const char *eb)
-{
- _blockEnd = eb;
-}
-
-void
IceXML::Output::setIndent(int indentSize)
{
_indentSize = indentSize;
@@ -150,6 +141,12 @@ IceXML::Output::setUseTab(bool useTab)
void
IceXML::Output::nl()
{
+ if (!_printed)
+ {
+ _printed = true;
+ _out << '>';
+ }
+
_out << '\n';
_pos = 0;
_separator = true;
@@ -186,28 +183,51 @@ IceXML::Output::nl()
}
void
-IceXML::Output::sb()
+IceXML::Output::se(const std::string& element)
{
- if (_blockStart.length())
+ nl();
+
+ //
+ // The output of the '>' character is deferred until either the
+ //end-element (in which case a /> is emitted) or until something
+ //is displayed.
+ //
+ _out << '<' << element;
+
+ string::size_type pos = element.find_first_of(" \t");
+ if (pos == string::npos)
+ {
+ _elementStack.push(element);
+ }
+ else
{
- nl();
- _out << _blockStart;
+ _elementStack.push(element.substr(0, pos));
}
- ++_pos;
+
+ ++_pos; // TODO: ???
inc();
_separator = false;
+ _printed = false;
}
void
-IceXML::Output::eb()
+IceXML::Output::ee()
{
+ string element = _elementStack.top();
+ _elementStack.pop();
+
dec();
- if (_blockEnd.length())
+ if (!_printed)
+ {
+ _out << "/>";
+ }
+ else
{
- nl();
- _out << _blockEnd;
+ nl();
+ _out << "</" << element << '>';
}
- --_pos;
+ --_pos; // TODO: ???
+ _printed = true;
}
void
diff --git a/cpp/src/IceXML/StreamI.cpp b/cpp/src/IceXML/StreamI.cpp
index 5123b9749e2..e9c2cc0ab22 100644
--- a/cpp/src/IceXML/StreamI.cpp
+++ b/cpp/src/IceXML/StreamI.cpp
@@ -16,6 +16,8 @@
#include <IceXML/StreamI.h>
+#include <list>
+
//
// For input streaming
//
@@ -168,6 +170,7 @@ IceXML::StreamI::StreamI(const ::Ice::CommunicatorPtr& communicator, std::ostrea
_communicator(communicator),
_input(0),
_os(os),
+ _level(0),
_nextId(0)
{
::Ice::LoggerPtr logger = communicator->getLogger();
@@ -342,6 +345,7 @@ void
IceXML::StreamI::startReadDictionaryElement()
{
startRead(seqElementName);
+ _input->current = _input->current.getFirstChild();
}
void
@@ -921,7 +925,61 @@ IceXML::StreamI::writeString(const string& name, const string& value)
{
// No attributes
assert(name.find_first_of(" \t") == string::npos);
- _os << nl << "<" << name << ">" << value << "</" << name << ">";
+ string v = value;
+
+ //
+ // Find out whether there is a reserved character to avoid
+ // conversion if not necessary.
+ //
+ static const string allReserved = "<>'\"&";
+ if (v.find_first_of(allReserved) != string::npos)
+ {
+ //
+ // First convert all & to &amp;
+ //
+ size_t pos = 0;
+ while ((pos = v.find_first_of('&', pos)) != string::npos)
+ {
+ v.insert(pos+1, "amp;");
+ pos += 4;
+ }
+
+ //
+ // Next convert remaining reserved characters.
+ //
+ static const string reserved = "<>'\"";
+ pos = 0;
+ while ((pos = v.find_first_of(reserved, pos)) != string::npos)
+ {
+ string replace;
+ switch(v[pos])
+ {
+ case '>':
+ replace = "&gt;";
+ break;
+
+ case '<':
+ replace = "&lt;";
+ break;
+
+ case '\'':
+ replace = "&apos;";
+ break;
+
+ case '"':
+ replace = "&quot;";
+ break;
+
+ default:
+ assert(false);
+ }
+ v.erase(pos, 1);
+ v.insert(pos, replace);
+ pos += replace.size();
+ }
+ }
+
+ _os << nl << "<" << name << ">" << v << "</" << name << ">";
}
void
@@ -1014,7 +1072,7 @@ IceXML::StreamI::writeObject(const string& name, const ::Ice::ObjectPtr& obj)
// If at the top level of the document then the object itself must
// be written, otherwise write a reference.
//
- bool writeReference = (_elementStack.size() != 0);
+ bool writeReference = (_level != 0);
//
// If the object doesn't exist in the map add it.
@@ -1159,30 +1217,18 @@ IceXML::StreamI::readObject(const string& name, const string& signatureType, con
void
IceXML::StreamI::startWrite(const string& element)
{
- _os << nl << '<' << element << '>';
- _os.inc();
-
- string::size_type pos = element.find_first_of(" \t");
- if (pos == string::npos)
- {
- _elementStack.push(element);
- }
- else
- {
- _elementStack.push(element.substr(0, pos));
- }
+ _os << se(element);
+ ++_level;
}
void
IceXML::StreamI::endWrite()
{
- string element = _elementStack.top();
- _elementStack.pop();
+ --_level;
- _os.dec();
- _os << nl << "</" << element << '>';
+ _os << ee;
- if (_elementStack.size() == 0)
+ if (_level == 0)
{
dumpUnwrittenObjects();
}
@@ -1198,7 +1244,6 @@ IceXML::StreamI::startRead(const ::std::string& element)
}
if (_input->current.isNull())
{
- cout << "element: " << element << endl;
throw ::Ice::UnmarshalException(__FILE__, __LINE__);
}
@@ -1225,7 +1270,7 @@ IceXML::StreamI::dumpUnwrittenObjects()
//
// Precondition: Must be at the top-level.
//
- assert(_elementStack.size() == 0);
+ assert(_level == 0);
//
// It's necessary to run through the set of unwritten objects
diff --git a/cpp/src/slice2cpp/Gen.cpp b/cpp/src/slice2cpp/Gen.cpp
index 23f6bb1afa6..03bd81faf60 100644
--- a/cpp/src/slice2cpp/Gen.cpp
+++ b/cpp/src/slice2cpp/Gen.cpp
@@ -785,8 +785,10 @@ Slice::Gen::TypesVisitor::visitDictionary(const DictionaryPtr& p)
C << nl << scoped << "::const_iterator p;";
C << nl << "for (p = v.begin(); p != v.end(); ++p)";
C << sb;
+ C << nl << "__os->startWriteDictionaryElement();";
writeGenericMarshalUnmarshalCode(C, keyType, "p->first", true, "\"key\"");
writeGenericMarshalUnmarshalCode(C, valueType, "p->second", true, "\"value\"");
+ C << nl << "__os->endWriteDictionaryElement();";
C << eb;
C << nl << "__os->endWriteDictionary();";
C << eb;
@@ -799,8 +801,10 @@ Slice::Gen::TypesVisitor::visitDictionary(const DictionaryPtr& p)
C << nl << "while (sz--)";
C << sb;
C << nl << "::std::pair<" << ks << ", " << vs << "> pair;";
+ C << nl << "__is->startReadDictionaryElement();";
writeGenericMarshalUnmarshalCode(C, keyType, "pair.first", false, "\"key\"");
writeGenericMarshalUnmarshalCode(C, valueType, "pair.second", false, "\"value\"");
+ C << nl << "__is->endReadDictionaryElement();";
C << nl << "v.insert(v.end(), pair);";
C << eb;
C << nl << "__is->endReadDictionary();";