Remove trailing white spaces
[SXSI/xpathcomp.git] / XMLDocShredder.cpp
index c048d2e..cb70e19 100644 (file)
 
 #include <iostream>
 #include "XMLDocShredder.h"
-#include "SXSIStorageInterface.h"
 #include <libxml++/exceptions/parse_error.h>
 #include "Utils.h"
 
 using namespace Glib;
 
+void XMLDocShredder::doText(){
+
+  if (!buffer.empty()){
+    tb->NewOpenTag(PCDATA_OPEN_TAG);
+    tb->NewText(buffer);
+    tb->NewClosingTag(PCDATA_OPEN_TAG);
+  };
+  buffer.clear();
+
+}
+
 void XMLDocShredder::setProperties(){
   /* instruct the parser to expand entity references and report as 
    * regular PCDATA
@@ -58,79 +68,76 @@ XMLDocShredder::XMLDocShredder(const unsigned char * data,
                               bool iet, 
                               bool dtc)                        
 {
-  last_text = false;
+  tree = NULL;
   reader_ = new TextReader(data,size,"");
   setProperties();
-  storageIfc_ = new SXSIStorageInterface(sf,iet,dtc);
-  buffer = "";
+  tb  = new XMLTreeBuilder();
+  buffer.clear();
+  tb->OpenDocument(iet,sf,dtc);
 }
 
 XMLDocShredder::XMLDocShredder(const string inFileName,int sf, bool iet, bool dtc)
 {
-  last_text = false;
+  tree = NULL;
   reader_ = new TextReader(inFileName);
   setProperties();
-  storageIfc_ = new SXSIStorageInterface(sf,iet,dtc);
-  buffer = "";
+  tb = new XMLTreeBuilder();
+  buffer.clear();
+  tb->OpenDocument(iet,sf,dtc);
 }
 
 XMLDocShredder::~XMLDocShredder()
 {
        delete reader_;
-       delete storageIfc_;
+       reader_ = NULL;
+       delete tb;
+       tb = NULL;
 
 }
 
 
 void XMLDocShredder::processStartElement()
 {
-       // fetch element name; this will be the full qualified name
-       ustring name = reader_->get_name();
-       bool empty = false;
-       
-       storageIfc_->newText(buffer); //prevText
-       buffer.erase();
-       
-       storageIfc_->newChild(name);
-
-       /* We must be really carefull here. calling process attributes moves
-          the document pointer on the last attribute, hence calling reader_->is_empty
-          afterwards will yield the wrong result. It is better to call it while we are
-          on the element and generate a nodeFinished() call at the end */
-       empty = reader_->is_empty_element();
-
-
-       // now, process attributes
-       if (reader_->has_attributes())
-         {
-           processAttributes();
-         };
-
-       
-       if (empty){
-           storageIfc_->newText("");  //myText
-           storageIfc_->nodeFinished(name);       
-       };
-
-
+  doText();
+  // fetch element name; this will be the full qualified name
+  ustring name = reader_->get_name();
+  bool empty = false;
+  size_t found = name.find_first_of(':');
+  if (found == ustring::npos)  
+    tb->NewOpenTag(name);
+  else
+    tb->NewOpenTag(name.substr(found+1,name.length() - found - 1));
+  
+  /* We must be really carefull here. calling process attributes moves
+     the document pointer on the last attribute, hence calling reader_->is_empty
+     afterwards will yield the wrong result. It is better to call it while we are
+     on the element and generate a nodeFinished() call at the end */
+  empty = reader_->is_empty_element();
+  
+  
+  // now, process attributes
+  if (reader_->has_attributes())
+    processAttributes();
+  
+  
+  if (empty)
+    tb->NewClosingTag(name);
+  
+  
 }
 
 void XMLDocShredder::processEndElement()
 {
-  // tell the storage interface that the current node has been completely processed
-  storageIfc_->newText(buffer); //prevText
-  buffer.erase();
-  storageIfc_->nodeFinished(reader_->get_name());
+  doText();
+  ustring name = reader_->get_name();
+  tb->NewClosingTag(name);
 }
 
 void XMLDocShredder::processPCDATA()
 {
-       // send the content of this PCDATA node to the storage interface as a text node
-         
-       if (reader_->has_value())
-       {
-         buffer += reader_->get_value();
-       };
+  // send the content of this PCDATA node to the storage interface as a text node
+  if (reader_->has_value())
+    buffer += reader_->get_value();
 
 }
 
@@ -139,10 +146,9 @@ void XMLDocShredder::processAttributes()
        reader_->move_to_first_attribute();
                
        string nspaceStr = "xmlns";
-       storageIfc_->newText(""); //prevText
-       storageIfc_->newChild("<@>");
+       tb->NewOpenTag(ATTRIBUTE_OPEN_TAG);
        do
-       {
+         {
                ustring name = reader_->get_name();
                ustring value = reader_->get_value();
                
@@ -152,8 +158,7 @@ void XMLDocShredder::processAttributes()
                
                if ((name.find(nspaceStr.c_str(), 0, 5)) == 0)
                {
-                       storageIfc_->newChild(":" + value);
-                       storageIfc_->nodeFinished(":" + value); 
+                 //TODO 
                }
                
                /* otherwise, this is an ordinary attribute, so we construct a new child node of the 
@@ -163,38 +168,41 @@ void XMLDocShredder::processAttributes()
                 
                else
                {
-                 storageIfc_->newText(""); //prevText
-                 storageIfc_->newChild(name);
-                 storageIfc_->newText(value);
-                 storageIfc_->nodeFinished(name);
+                 string attname = "<@>"+name;
+                 tb->NewOpenTag(attname);
+                 tb->NewOpenTag(ATTRIBUTE_DATA_OPEN_TAG);
+                 tb->NewText(value);
+                 tb->NewClosingTag(ATTRIBUTE_DATA_OPEN_TAG);
+                 tb->NewClosingTag(attname);
                }
        }
        while (reader_->move_to_next_attribute());
-       storageIfc_->newText(""); //nextText
-       storageIfc_->nodeFinished("<@>");
+       tb->NewClosingTag(ATTRIBUTE_OPEN_TAG);
 }
 
 void XMLDocShredder::processSignificantWhitespace()
 {
-  // each significant whitespace sequence constructs a text node
-  buffer += reader_->get_value();      
-       
+  if (reader_->has_value())
+    buffer += reader_->get_value();
+
 }
 
 void XMLDocShredder::processStartDocument(const string docName)
 {
   // tell storage interface to construct the document name
-  storageIfc_->newChild("");  
+
+  tb->NewOpenTag(DOCUMENT_OPEN_TAG);
   
 }
 
 void XMLDocShredder::processEndDocument()
 {
-       /* tell the storage interface that document parsing has finished, and structures
-        * can now be written to disk. */
-  storageIfc_->newText("");
-  storageIfc_->nodeFinished("");
-  storageIfc_->parsingFinished();      
+  doText();
+  /* tell the storage interface that document parsing has finished, and structures
+   * can now be written to disk. */
+  tb->NewClosingTag(DOCUMENT_OPEN_TAG);
+  tree = tb->CloseDocument();
+
 }
 
 void XMLDocShredder::processComment()
@@ -231,11 +239,8 @@ void XMLDocShredder::processCDATASection()
         * model.  Instead, we simply pass the converted text value to the storage interface as 
         * a text node attached to the current context node.
         */
-  
-       ustring value = reader_->get_value();
-       storageIfc_->newText(value);
-       last_text = true;
-
+  if (reader_->has_value())
+    buffer+= reader_->get_value();
 }
 
 void XMLDocShredder::processUnknownNodeType()