[llvm-commits] [hlvm] r38051 - in /hlvm/trunk: hlvm/Writer/XML/XMLWriter.cpp hlvm/Writer/XML/XMLWriter.h test/xml2xml/bundle.hlx tools/hlvm-xml2xml/hlvm-xml2xml.cpp

Reid Spencer reid at x10sys.com
Sat Jul 7 16:59:19 PDT 2007


Author: reid
Date: Sat Jul  7 18:59:19 2007
New Revision: 38051

URL: http://llvm.org/viewvc/llvm-project?rev=38051&view=rev
Log:
Convert to using the libxml2 xmlwriter interface for generating the XML output.
This is another code shrinkage win, plus it is a tested interface. Additionally,
modify the bundle.hlx test to match the output generated by xmlwriter. This
completes the conversion to libxml2 and all tests pass.

Modified:
    hlvm/trunk/hlvm/Writer/XML/XMLWriter.cpp
    hlvm/trunk/hlvm/Writer/XML/XMLWriter.h
    hlvm/trunk/test/xml2xml/bundle.hlx
    hlvm/trunk/tools/hlvm-xml2xml/hlvm-xml2xml.cpp

Modified: hlvm/trunk/hlvm/Writer/XML/XMLWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/hlvm/Writer/XML/XMLWriter.cpp?rev=38051&r1=38050&r2=38051&view=diff

==============================================================================
--- hlvm/trunk/hlvm/Writer/XML/XMLWriter.cpp (original)
+++ hlvm/trunk/hlvm/Writer/XML/XMLWriter.cpp Sat Jul  7 18:59:19 2007
@@ -30,6 +30,7 @@
 #include <hlvm/Writer/XML/XMLWriter.h>
 #include <hlvm/AST/AST.h>
 #include <hlvm/AST/Bundle.h>
+#include <libxml/xmlwriter.h>
 #include <iostream>
 #include <cassert>
 
@@ -37,53 +38,44 @@
 
 namespace {
 
-// An ostream that automatically indents and provides methods to control it.
-class ostream_indent
-{
-  uint32_t indent_;
-public:
-  std::ostream& strm_;
-  ostream_indent(std::ostream& os) : indent_(0), strm_(os) {}
-
-  inline void nl()
-  {
-    strm_ << '\n';
-    strm_.width(indent_);
-    strm_ << ' ';
-  }
-
-  inline void in( bool with_newline = false)
-  {
-    indent_++;
-    if (with_newline)
-      nl();
-  }
-
-  inline void out( bool with_newline = false)
-  {
-    indent_--;
-    if (with_newline)
-      nl();
-  }
-};
-
 class XMLWriterImpl : public XMLWriter {
-  ostream_indent ind_;
-  std::ostream& out_;
-  AST::AST* node_;
+  xmlTextWriterPtr writer;
+  AST::AST* node;
 public:
-  XMLWriterImpl(std::ostream& out)
-    : ind_(out), out_(out), node_(0)
-  { }
+  XMLWriterImpl(const char* fname)
+    : writer(0), node(0)
+  { 
+    writer = xmlNewTextWriterFilename(fname,0);
+    assert(writer && "Can't allocate writer");
+    xmlTextWriterSetIndent(writer,1);
+    xmlTextWriterSetIndentString(writer,reinterpret_cast<const xmlChar*>("  "));
+  }
 
   virtual ~XMLWriterImpl() 
   { 
-    out_.flush();
+    xmlFreeTextWriter(writer);
   }
 
   virtual void write(AST::AST* node);
 
 private:
+  inline void writeComment(const char* cmt)
+    { xmlTextWriterWriteComment(writer,
+        reinterpret_cast<const xmlChar*>(cmt)); }
+  inline void startElement(const char* elem) 
+    { xmlTextWriterStartElement(writer, 
+        reinterpret_cast<const xmlChar*>(elem)); }
+  inline void endElement() 
+    { xmlTextWriterEndElement(writer); }
+  inline void writeAttribute(const char*name, const char*val)
+    { xmlTextWriterWriteAttribute(writer, 
+        reinterpret_cast<const xmlChar*>(name), 
+        reinterpret_cast<const xmlChar*>(val)); }
+  inline void writeElement(const char* elem, const char* body)
+    { xmlTextWriterWriteElement(writer,
+        reinterpret_cast<const xmlChar*>(elem),
+        reinterpret_cast<const xmlChar*>(body)); }
+
   inline void putHeader();
   inline void putFooter();
   inline void put(AST::Node* node);
@@ -126,26 +118,24 @@
 void
 XMLWriterImpl::putHeader() 
 {
-  out_ << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
-  out_ << "<hlvm xmlns=\"http://hlvm.org/src/hlvm/Reader/XML/HLVM.rng\">";
-  ind_.in(true);
+  xmlTextWriterStartDocument(writer,0,"UTF-8",0);
+  startElement("hlvm");
+  writeAttribute("xmlns","http://hlvm.org/src/hlvm/Reader/XML/HLVM.rng");
 }
 
 void
 XMLWriterImpl::putFooter()
 {
-  ind_.out(true);
-  out_ << "</hlvm>\n";
+  endElement();
 }
 
 inline void 
 XMLWriterImpl::put(AST::Bundle* b)
 {
-  out_ << "<bundle pubid=\"" << b->getName() << "\">";
-  ind_.in(true);
-  ind_.out(true);
-  out_ << "</bundle>";
-  ind_.out(false);
+  startElement("bundle");
+  writeAttribute("pubid",b->getName().c_str());
+  endElement();
+  xmlTextWriterEndDocument(writer);
 }
 
 void
@@ -270,7 +260,7 @@
 void
 XMLWriterImpl::write(AST::AST* ast) 
 {
-  node_ = ast;
+  node = ast;
   putHeader();
   put(ast->getRoot());
   putFooter();
@@ -279,7 +269,7 @@
 }
 
 XMLWriter* 
-hlvm::XMLWriter::create(std::ostream& out)
+hlvm::XMLWriter::create(const char* fname)
 {
-  return new XMLWriterImpl(out);
+  return new XMLWriterImpl(fname);
 }

Modified: hlvm/trunk/hlvm/Writer/XML/XMLWriter.h
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/hlvm/Writer/XML/XMLWriter.h?rev=38051&r1=38050&r2=38051&view=diff

==============================================================================
--- hlvm/trunk/hlvm/Writer/XML/XMLWriter.h (original)
+++ hlvm/trunk/hlvm/Writer/XML/XMLWriter.h Sat Jul  7 18:59:19 2007
@@ -41,7 +41,7 @@
     /// This method instantiates an XMLReader that is prepared to read from
     /// the path provided.
     /// @brief Create a new XmlReader
-    static XMLWriter* create(std::ostream& out);
+    static XMLWriter* create(const char* fname);
 
     virtual ~XMLWriter() {}
   };

Modified: hlvm/trunk/test/xml2xml/bundle.hlx
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/test/xml2xml/bundle.hlx?rev=38051&r1=38050&r2=38051&view=diff

==============================================================================
--- hlvm/trunk/test/xml2xml/bundle.hlx (original)
+++ hlvm/trunk/test/xml2xml/bundle.hlx Sat Jul  7 18:59:19 2007
@@ -1,6 +1,4 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <hlvm xmlns="http://hlvm.org/src/hlvm/Reader/XML/HLVM.rng">
- <bundle pubid="name">
-
- </bundle>
+  <bundle pubid="name"/>
 </hlvm>

Modified: hlvm/trunk/tools/hlvm-xml2xml/hlvm-xml2xml.cpp
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/tools/hlvm-xml2xml/hlvm-xml2xml.cpp?rev=38051&r1=38050&r2=38051&view=diff

==============================================================================
--- hlvm/trunk/tools/hlvm-xml2xml/hlvm-xml2xml.cpp (original)
+++ hlvm/trunk/tools/hlvm-xml2xml/hlvm-xml2xml.cpp Sat Jul  7 18:59:19 2007
@@ -89,7 +89,7 @@
     }
 
     XMLReader* rdr = XMLReader::create(InputFilename);
-    XMLWriter* wrtr = XMLWriter::create(*Out);
+    XMLWriter* wrtr = XMLWriter::create(OutputFilename.c_str());
     rdr->read();
     AST::AST* node = rdr->get();
     if (node) {





More information about the llvm-commits mailing list