Nicolae Brinza has proposed merging lp:~nbrinza/zorba/parse-fragment into 
lp:zorba.

Requested reviews:
  David Graf (davidagraf)

For more details, see:
https://code.launchpad.net/~nbrinza/zorba/parse-fragment/+merge/103624

Small optimization in the serializer to avoid a repeated string comparison
-- 
https://code.launchpad.net/~nbrinza/zorba/parse-fragment/+merge/103624
Your team Zorba Coders is subscribed to branch lp:zorba.
=== modified file 'src/api/serialization/serializer.cpp'
--- src/api/serialization/serializer.cpp	2012-04-24 12:39:38 +0000
+++ src/api/serialization/serializer.cpp	2012-04-26 07:56:21 +0000
@@ -100,7 +100,6 @@
   }
 }
 
-
 ////////////////////////////////////////////////////////////////////////////////
 //                                                                            //
 //  Default emitter                                                           //
@@ -194,7 +193,7 @@
       unicode::code_point cp = utf8::next_char(temp);
 
       // raise an error iff (1) the serialization format is XML 1.0 and (2) the given character is an invalid XML 1.0 character
-      if (ser && ser->method == PARAMETER_VALUE_XML && ser->version == "1.0" && !xml::is_valid(cp))
+      if (ser && ser->method == PARAMETER_VALUE_XML && ser->version == PARAMETER_VALUE_VERSION_1_0 && !xml::is_valid(cp))
         throw XQUERY_EXCEPTION( err::FOCH0001, ERROR_PARAMS( cp ) );
 
       if (cp >= 0x10000 && cp <= 0x10FFFF)
@@ -219,7 +218,7 @@
 
     // raise an error iff (1) the serialization format is XML 1.0 and (2) the given character is an invalid XML 1.0 character
     if (ser && ser->method == PARAMETER_VALUE_XML &&
-        ser->version == "1.0" && !xml::is_valid(static_cast<unsigned>(*chars)))
+        ser->version == PARAMETER_VALUE_VERSION_1_0 && !xml::is_valid(static_cast<unsigned>(*chars)))
       throw XQUERY_EXCEPTION(
         err::XQST0090,
         ERROR_PARAMS( static_cast<unsigned>( *chars ), xml::v1_0 )
@@ -431,7 +430,7 @@
   }
   else if (item->getNodeKind() == store::StoreConsts::attributeNode)
   {
-    throw XQUERY_EXCEPTION(err::SENR0001, 
+    throw XQUERY_EXCEPTION(err::SENR0001,
     ERROR_PARAMS(item->getStringValue(), ZED(AttributeNode)));
   }
   else
@@ -858,7 +857,7 @@
   emitter::emit_declaration();
 
   if (ser->omit_xml_declaration == PARAMETER_VALUE_NO) {
-    tr << "<?xml version=\"" << ser->version;
+    tr << "<?xml version=\"" << ser->version_string;
     switch (ser->encoding) {
       case PARAMETER_VALUE_UTF_8:
       case PARAMETER_VALUE_UTF_16:
@@ -1218,7 +1217,7 @@
         // an element written as <br/> or <br></br> in an XSLT stylesheet MUST
         // be output as <br>.
         if (is_html_empty_content_model_element(item) &&
-            ztd::equals(ser->version, "4.0", 3))
+            ser->version == PARAMETER_VALUE_VERSION_4_0)
           tr << ">";
         else
           tr << "/>";
@@ -2004,7 +2003,8 @@
 
   undeclare_prefixes = PARAMETER_VALUE_NO;
 
-  version = "1.0";
+  version = PARAMETER_VALUE_VERSION_1_0;
+  version_string = "1.0";
   version_has_default_value = true;
 
   indent = PARAMETER_VALUE_NO;
@@ -2119,8 +2119,18 @@
   }
   else if (!strcmp(aName, "version"))
   {
-    version = aValue;
+    version_string = aValue;
     version_has_default_value = false;
+    if (version_string == "1.0")
+      version = PARAMETER_VALUE_VERSION_1_0;
+    else if (version_string == "1.1")
+      version = PARAMETER_VALUE_VERSION_1_1;
+    else if (version_string == "4.0")
+      version = PARAMETER_VALUE_VERSION_4_0;
+    else if (version_string == "4.01")
+      version = PARAMETER_VALUE_VERSION_4_01;
+    else
+      version = PARAMETER_VALUE_VERSION_OTHER;
   }
   else if (!strcmp(aName, "doctype-system"))
   {
@@ -2156,51 +2166,52 @@
 void
 serializer::validate_parameters(void)
 {
-  if (method == PARAMETER_VALUE_XML || method == PARAMETER_VALUE_XHTML) 
+  if (method == PARAMETER_VALUE_XML || method == PARAMETER_VALUE_XHTML)
   {
     // XML-only validation
-    if (method == PARAMETER_VALUE_XML) 
+    if (method == PARAMETER_VALUE_XML)
     {
-      if (version != "1.0" && version != "1.1")
+      if (version != PARAMETER_VALUE_VERSION_1_0 && version != PARAMETER_VALUE_VERSION_1_1)
         throw XQUERY_EXCEPTION(
           err::SESU0013, ERROR_PARAMS( version, "XML", "\"1.0\", \"1.1\"" )
         );
     }
 
     // XHTML-only validation
-    if (method == PARAMETER_VALUE_XHTML) 
+    if (method == PARAMETER_VALUE_XHTML)
     {
     }
 
     // XML and XHTML validation
 
-    if (omit_xml_declaration == PARAMETER_VALUE_YES) 
+    if (omit_xml_declaration == PARAMETER_VALUE_YES)
     {
       if (standalone != PARAMETER_VALUE_OMIT)
         throw XQUERY_EXCEPTION(
           err::SEPM0009, ERROR_PARAMS( ZED( SEPM0009_NotOmit ) )
         );
-      if (version != "1.0" && !doctype_system.empty())
+      if (version != PARAMETER_VALUE_VERSION_1_0 && !doctype_system.empty())
         throw XQUERY_EXCEPTION(
           err::SEPM0009, ERROR_PARAMS( ZED( SEPM0009_Not10 ) )
         );
     }
 
-    if (undeclare_prefixes == PARAMETER_VALUE_YES && version == "1.0")
+    if (undeclare_prefixes == PARAMETER_VALUE_YES && version == PARAMETER_VALUE_VERSION_1_0)
       throw XQUERY_EXCEPTION( err::SEPM0010 );
   }
 
-  if (method == PARAMETER_VALUE_HTML) 
+  if (method == PARAMETER_VALUE_HTML)
   {
     // Default value for "version" when method is HTML is "4.0"
-    if (version_has_default_value) 
+    if (version_has_default_value)
     {
-      version = "4.0";
+      version = PARAMETER_VALUE_VERSION_4_0;
+      version_string = "4.0";
     }
-    else if (!(ztd::equals(version, "4.0", 3) || ztd::equals(version, "4.01", 4))) 
+    else if (version != PARAMETER_VALUE_VERSION_4_0 && version != PARAMETER_VALUE_VERSION_4_01)
     {
       throw XQUERY_EXCEPTION(
-        err::SESU0013, ERROR_PARAMS( version, "HTML", "\"4.0\", \"4.01\"" )
+        err::SESU0013, ERROR_PARAMS( version_string, "HTML", "\"4.0\", \"4.01\"" )
       );
     }
   }
@@ -2278,13 +2289,13 @@
 
   validate_parameters();
 
-  if (!setup(aOStream)) 
+  if (!setup(aOStream))
   {
     return;
   }
 
   // in case we use SAX event notifications
-  if (aHandler) 
+  if (aHandler)
   {
     // only allow XML-based methods for SAX notifications
     if (method != PARAMETER_VALUE_XML &&
@@ -2303,10 +2314,10 @@
 
   store::Item_t lItem;
   //+  aObject->open();
-  while (aObject->next(lItem)) 
+  while (aObject->next(lItem))
   {
     // PUL's cannot be serialized
-    if (lItem->isPul()) 
+    if (lItem->isPul())
     {
       throw ZORBA_EXCEPTION(zerr::ZAPI0007_CANNOT_SERIALIZE_PUL);
     }
@@ -2329,7 +2340,7 @@
 {
   validate_parameters();
 
-  if (!setup(stream)) 
+  if (!setup(stream))
   {
     return;
   }
@@ -2338,27 +2349,27 @@
 
   store::Item_t lItem;
   //object->open();
-  while (object->next(lItem)) 
+  while (object->next(lItem))
   {
     Zorba_SerializerOptions_t* lSerParams = aHandler(aHandlerData);
-    if (lSerParams) 
+    if (lSerParams)
     {
       SerializerImpl::setSerializationParameters(*this, *lSerParams);
-      if (!setup(stream)) 
+      if (!setup(stream))
       {
         return;
       }
     }
 
     // PUL's cannot be serialized
-    if (lItem->isPul()) 
+    if (lItem->isPul())
     {
       throw ZORBA_EXCEPTION(zerr::ZAPI0007_CANNOT_SERIALIZE_PUL);
     }
 
     e->emit_item(&*lItem);
   }
- 
+
   //object->close();
   e->emit_declaration_end();
 }

=== modified file 'src/api/serialization/serializer.h'
--- src/api/serialization/serializer.h	2012-04-24 12:39:38 +0000
+++ src/api/serialization/serializer.h	2012-04-26 07:56:21 +0000
@@ -71,7 +71,15 @@
     PARAMETER_VALUE_BINARY,
 
     PARAMETER_VALUE_UTF_8,
-    PARAMETER_VALUE_UTF_16
+    PARAMETER_VALUE_UTF_16,
+    
+    // Values for the XML/HTML version
+    PARAMETER_VALUE_VERSION_1_0,          // used for XML 1.0
+    PARAMETER_VALUE_VERSION_1_1,          // used for XML 1.1
+    PARAMETER_VALUE_VERSION_4_0,          // used for HTML 4.0
+    PARAMETER_VALUE_VERSION_4_01,         // used for HTML 4.01
+    PARAMETER_VALUE_VERSION_OTHER         // given by the version string
+    
   } PARAMETER_VALUE_TYPE;
 
 protected:
@@ -98,7 +106,8 @@
   short int standalone;            // implemented
   short int undeclare_prefixes;    // "yes" or "no", implemented
   void* use_character_maps;        // TODO: list of pairs
-  zstring version;                 // "1.0"
+  short int version;               // "1.0"
+  zstring version_string;          // this the version as a string
   short int indent;                // "yes" or "no", implemented
   bool version_has_default_value;  // Used during validation to set version to
                                    // "4.0" when output method is "html"
@@ -251,7 +260,7 @@
 
     /**
      *  Serializes the given string, performing character expansion
-     *  if necessary. 
+     *  if necessary.
      *
      *  @return returns the number of bytes that have not been written. This
      *          info is used only for streamable items expansion.

=== added file 'test/rbkt/ExpQueryResults/zorba/parsing_and_serializing/parse-xml-fragment-19.xml.res'
--- test/rbkt/ExpQueryResults/zorba/parsing_and_serializing/parse-xml-fragment-19.xml.res	1970-01-01 00:00:00 +0000
+++ test/rbkt/ExpQueryResults/zorba/parsing_and_serializing/parse-xml-fragment-19.xml.res	2012-04-26 07:56:21 +0000
@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<from1>Jani</from1>
+<from2>Jani</from2>
+<from3>Jani</from3>
\ No newline at end of file

=== added file 'test/rbkt/Queries/zorba/parsing_and_serializing/parse-xml-fragment-19.xq'
--- test/rbkt/Queries/zorba/parsing_and_serializing/parse-xml-fragment-19.xq	1970-01-01 00:00:00 +0000
+++ test/rbkt/Queries/zorba/parsing_and_serializing/parse-xml-fragment-19.xq	2012-04-26 07:56:21 +0000
@@ -0,0 +1,9 @@
+(: 
+  Test that the backwards-compatible parse-xml-fragment() function works.
+:)
+import module namespace z = "http://www.zorba-xquery.com/modules/xml";;
+
+z:parse-xml-fragment("<from1>Jani</from1>
+<from2>Jani</from2>
+<from3>Jani</from3>",
+"e")

-- 
Mailing list: https://launchpad.net/~zorba-coders
Post to     : zorba-coders@lists.launchpad.net
Unsubscribe : https://launchpad.net/~zorba-coders
More help   : https://help.launchpad.net/ListHelp

Reply via email to