XML: add HTML features 56/12056/9
Calixte DENIZET [Wed, 17 Jul 2013 16:39:54 +0000 (18:39 +0200)]
Change-Id: Iac791ef297f78173cf694dd8ad4964c24f154c4d

24 files changed:
SEP/INDEX
SEP/SEP_129_html.odt [new file with mode: 0644]
scilab/CHANGES_5.5.X
scilab/modules/xml/Makefile.am
scilab/modules/xml/Makefile.in
scilab/modules/xml/help/en_US/htmlDump.xml [new file with mode: 0644]
scilab/modules/xml/help/en_US/htmlRead.xml [new file with mode: 0644]
scilab/modules/xml/help/en_US/htmlReadStr.xml [new file with mode: 0644]
scilab/modules/xml/help/en_US/htmlWrite.xml [new file with mode: 0644]
scilab/modules/xml/includes/gw_xml.h
scilab/modules/xml/includes/xml.h
scilab/modules/xml/sci_gateway/c/gw_xml.c
scilab/modules/xml/sci_gateway/cpp/sci_htmlDump.cpp [new file with mode: 0644]
scilab/modules/xml/sci_gateway/cpp/sci_htmlRead.cpp [new file with mode: 0644]
scilab/modules/xml/sci_gateway/cpp/sci_htmlReadStr.cpp [new file with mode: 0644]
scilab/modules/xml/sci_gateway/cpp/sci_htmlWrite.cpp [new file with mode: 0644]
scilab/modules/xml/sci_gateway/cpp/sci_xmlWrite.cpp
scilab/modules/xml/sci_gateway/xml_gateway.xml
scilab/modules/xml/src/cpp/XMLDocument.cpp
scilab/modules/xml/src/cpp/XMLDocument.hxx
scilab/modules/xml/tests/unit_tests/htmlRead.dia.ref [new file with mode: 0644]
scilab/modules/xml/tests/unit_tests/htmlRead.tst [new file with mode: 0644]
scilab/modules/xml/xml.vcxproj
scilab/modules/xml/xml.vcxproj.filters

index 7d36398..297a003 100644 (file)
--- a/SEP/INDEX
+++ b/SEP/INDEX
@@ -124,3 +124,4 @@ SEP #125: New input argument to matfile_open to open Matlab 7.3 files.
 SEP #126: New function unwrap
 SEP #127: jmat tagged as obsolete. New input argument to flipdim to replace jmat.
 SEP #128: nanreglin.
+SEP #129: HTLM parsing functions.
\ No newline at end of file
diff --git a/SEP/SEP_129_html.odt b/SEP/SEP_129_html.odt
new file mode 100644 (file)
index 0000000..577d0d8
Binary files /dev/null and b/SEP/SEP_129_html.odt differ
index 215154e..912d37b 100644 (file)
@@ -140,7 +140,8 @@ Scilab
  - ilib_build_jar - Builds Java packages from sources into a JAR file
  - ifftshift - Inverses FFT shift
  - unwrap - Unwraps/unfolds a Y(x) profile
- - getPreferencesValue/setPreferencesValue - get or set values in the preferences file.
+ - getPreferencesValue/setPreferencesValue - Get or set values in the preferences file.
+ - htmlDump/htmlRead/htmlReadStr/htmlWrite - Read and write data from/in HTML files.
 
 * modulo() and pmodulo() now support integers & hypermatrices (See bug #13002).
 
index 1c768fd..9d8ce50 100644 (file)
@@ -81,14 +81,18 @@ GATEWAY_CXX_SOURCES = sci_gateway/cpp/sci_xmlRead.cpp \
                      sci_gateway/cpp/sci_xmlRemove.cpp \
                      sci_gateway/cpp/sci_xmlSetAttributes.cpp \
                      sci_gateway/cpp/sci_xmlName.cpp \
-                     sci_gateway/cpp/sci_xmlAppend.cpp
+                     sci_gateway/cpp/sci_xmlAppend.cpp \
+                     sci_gateway/cpp/sci_htmlRead.cpp \
+                     sci_gateway/cpp/sci_htmlReadStr.cpp \
+                     sci_gateway/cpp/sci_htmlWrite.cpp \
+                     sci_gateway/cpp/sci_htmlDump.cpp
 
 libscixml_la_CPPFLAGS = -I$(srcdir)/includes/ \
     -I$(srcdir)/src/cpp/ \
     -I$(srcdir)/src/c/ \
     -I$(top_srcdir)/modules/core/includes/ \
     -I$(top_srcdir)/modules/api_scilab/includes/ \
-       -I$(top_srcdir)/modules/localization/includes/ \
+    -I$(top_srcdir)/modules/localization/includes/ \
     -I$(top_srcdir)/modules/output_stream/includes/ \
     -I$(top_srcdir)/modules/string/includes/ \
     -I$(top_srcdir)/modules/fileio/includes/ \
index 45f75ab..703654b 100644 (file)
@@ -229,7 +229,11 @@ am__objects_5 = sci_gateway/cpp/libscixml_la-sci_xmlRead.lo \
        sci_gateway/cpp/libscixml_la-sci_xmlRemove.lo \
        sci_gateway/cpp/libscixml_la-sci_xmlSetAttributes.lo \
        sci_gateway/cpp/libscixml_la-sci_xmlName.lo \
-       sci_gateway/cpp/libscixml_la-sci_xmlAppend.lo
+       sci_gateway/cpp/libscixml_la-sci_xmlAppend.lo \
+       sci_gateway/cpp/libscixml_la-sci_htmlRead.lo \
+       sci_gateway/cpp/libscixml_la-sci_htmlReadStr.lo \
+       sci_gateway/cpp/libscixml_la-sci_htmlWrite.lo \
+       sci_gateway/cpp/libscixml_la-sci_htmlDump.lo
 am_libscixml_la_OBJECTS = $(am__objects_4) $(am__objects_5)
 libscixml_la_OBJECTS = $(am_libscixml_la_OBJECTS)
 libscixml_la_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CXX $(AM_LIBTOOLFLAGS) \
@@ -726,14 +730,18 @@ GATEWAY_CXX_SOURCES = sci_gateway/cpp/sci_xmlRead.cpp \
                      sci_gateway/cpp/sci_xmlRemove.cpp \
                      sci_gateway/cpp/sci_xmlSetAttributes.cpp \
                      sci_gateway/cpp/sci_xmlName.cpp \
-                     sci_gateway/cpp/sci_xmlAppend.cpp
+                     sci_gateway/cpp/sci_xmlAppend.cpp \
+                     sci_gateway/cpp/sci_htmlRead.cpp \
+                     sci_gateway/cpp/sci_htmlReadStr.cpp \
+                     sci_gateway/cpp/sci_htmlWrite.cpp \
+                     sci_gateway/cpp/sci_htmlDump.cpp
 
 libscixml_la_CPPFLAGS = -I$(srcdir)/includes/ \
     -I$(srcdir)/src/cpp/ \
     -I$(srcdir)/src/c/ \
     -I$(top_srcdir)/modules/core/includes/ \
     -I$(top_srcdir)/modules/api_scilab/includes/ \
-       -I$(top_srcdir)/modules/localization/includes/ \
+    -I$(top_srcdir)/modules/localization/includes/ \
     -I$(top_srcdir)/modules/output_stream/includes/ \
     -I$(top_srcdir)/modules/string/includes/ \
     -I$(top_srcdir)/modules/fileio/includes/ \
@@ -1005,6 +1013,18 @@ sci_gateway/cpp/libscixml_la-sci_xmlName.lo:  \
 sci_gateway/cpp/libscixml_la-sci_xmlAppend.lo:  \
        sci_gateway/cpp/$(am__dirstamp) \
        sci_gateway/cpp/$(DEPDIR)/$(am__dirstamp)
+sci_gateway/cpp/libscixml_la-sci_htmlRead.lo:  \
+       sci_gateway/cpp/$(am__dirstamp) \
+       sci_gateway/cpp/$(DEPDIR)/$(am__dirstamp)
+sci_gateway/cpp/libscixml_la-sci_htmlReadStr.lo:  \
+       sci_gateway/cpp/$(am__dirstamp) \
+       sci_gateway/cpp/$(DEPDIR)/$(am__dirstamp)
+sci_gateway/cpp/libscixml_la-sci_htmlWrite.lo:  \
+       sci_gateway/cpp/$(am__dirstamp) \
+       sci_gateway/cpp/$(DEPDIR)/$(am__dirstamp)
+sci_gateway/cpp/libscixml_la-sci_htmlDump.lo:  \
+       sci_gateway/cpp/$(am__dirstamp) \
+       sci_gateway/cpp/$(DEPDIR)/$(am__dirstamp)
 
 libscixml.la: $(libscixml_la_OBJECTS) $(libscixml_la_DEPENDENCIES) $(EXTRA_libscixml_la_DEPENDENCIES) 
        $(AM_V_CXXLD)$(libscixml_la_LINK) -rpath $(pkglibdir) $(libscixml_la_OBJECTS) $(libscixml_la_LIBADD) $(LIBS)
@@ -1025,6 +1045,10 @@ distclean-compile:
 
 @AMDEP_TRUE@@am__include@ @am__quote@sci_gateway/c/$(DEPDIR)/libscixml_la-gw_xml.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@sci_gateway/cpp/$(DEPDIR)/libscixml_la-sci_extraction.Plo@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@sci_gateway/cpp/$(DEPDIR)/libscixml_la-sci_htmlDump.Plo@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@sci_gateway/cpp/$(DEPDIR)/libscixml_la-sci_htmlRead.Plo@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@sci_gateway/cpp/$(DEPDIR)/libscixml_la-sci_htmlReadStr.Plo@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@sci_gateway/cpp/$(DEPDIR)/libscixml_la-sci_htmlWrite.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@sci_gateway/cpp/$(DEPDIR)/libscixml_la-sci_insertion.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@sci_gateway/cpp/$(DEPDIR)/libscixml_la-sci_percent_XMLAttr_e.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@sci_gateway/cpp/$(DEPDIR)/libscixml_la-sci_percent_XMLAttr_length.Plo@am__quote@
@@ -1488,6 +1512,34 @@ sci_gateway/cpp/libscixml_la-sci_xmlAppend.lo: sci_gateway/cpp/sci_xmlAppend.cpp
 @AMDEP_TRUE@@am__fastdepCXX_FALSE@     DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
 @am__fastdepCXX_FALSE@ $(AM_V_CXX@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libscixml_la_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o sci_gateway/cpp/libscixml_la-sci_xmlAppend.lo `test -f 'sci_gateway/cpp/sci_xmlAppend.cpp' || echo '$(srcdir)/'`sci_gateway/cpp/sci_xmlAppend.cpp
 
+sci_gateway/cpp/libscixml_la-sci_htmlRead.lo: sci_gateway/cpp/sci_htmlRead.cpp
+@am__fastdepCXX_TRUE@  $(AM_V_CXX)$(LIBTOOL) $(AM_V_lt) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libscixml_la_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -MT sci_gateway/cpp/libscixml_la-sci_htmlRead.lo -MD -MP -MF sci_gateway/cpp/$(DEPDIR)/libscixml_la-sci_htmlRead.Tpo -c -o sci_gateway/cpp/libscixml_la-sci_htmlRead.lo `test -f 'sci_gateway/cpp/sci_htmlRead.cpp' || echo '$(srcdir)/'`sci_gateway/cpp/sci_htmlRead.cpp
+@am__fastdepCXX_TRUE@  $(AM_V_at)$(am__mv) sci_gateway/cpp/$(DEPDIR)/libscixml_la-sci_htmlRead.Tpo sci_gateway/cpp/$(DEPDIR)/libscixml_la-sci_htmlRead.Plo
+@AMDEP_TRUE@@am__fastdepCXX_FALSE@     $(AM_V_CXX)source='sci_gateway/cpp/sci_htmlRead.cpp' object='sci_gateway/cpp/libscixml_la-sci_htmlRead.lo' libtool=yes @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCXX_FALSE@     DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCXX_FALSE@ $(AM_V_CXX@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libscixml_la_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o sci_gateway/cpp/libscixml_la-sci_htmlRead.lo `test -f 'sci_gateway/cpp/sci_htmlRead.cpp' || echo '$(srcdir)/'`sci_gateway/cpp/sci_htmlRead.cpp
+
+sci_gateway/cpp/libscixml_la-sci_htmlReadStr.lo: sci_gateway/cpp/sci_htmlReadStr.cpp
+@am__fastdepCXX_TRUE@  $(AM_V_CXX)$(LIBTOOL) $(AM_V_lt) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libscixml_la_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -MT sci_gateway/cpp/libscixml_la-sci_htmlReadStr.lo -MD -MP -MF sci_gateway/cpp/$(DEPDIR)/libscixml_la-sci_htmlReadStr.Tpo -c -o sci_gateway/cpp/libscixml_la-sci_htmlReadStr.lo `test -f 'sci_gateway/cpp/sci_htmlReadStr.cpp' || echo '$(srcdir)/'`sci_gateway/cpp/sci_htmlReadStr.cpp
+@am__fastdepCXX_TRUE@  $(AM_V_at)$(am__mv) sci_gateway/cpp/$(DEPDIR)/libscixml_la-sci_htmlReadStr.Tpo sci_gateway/cpp/$(DEPDIR)/libscixml_la-sci_htmlReadStr.Plo
+@AMDEP_TRUE@@am__fastdepCXX_FALSE@     $(AM_V_CXX)source='sci_gateway/cpp/sci_htmlReadStr.cpp' object='sci_gateway/cpp/libscixml_la-sci_htmlReadStr.lo' libtool=yes @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCXX_FALSE@     DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCXX_FALSE@ $(AM_V_CXX@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libscixml_la_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o sci_gateway/cpp/libscixml_la-sci_htmlReadStr.lo `test -f 'sci_gateway/cpp/sci_htmlReadStr.cpp' || echo '$(srcdir)/'`sci_gateway/cpp/sci_htmlReadStr.cpp
+
+sci_gateway/cpp/libscixml_la-sci_htmlWrite.lo: sci_gateway/cpp/sci_htmlWrite.cpp
+@am__fastdepCXX_TRUE@  $(AM_V_CXX)$(LIBTOOL) $(AM_V_lt) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libscixml_la_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -MT sci_gateway/cpp/libscixml_la-sci_htmlWrite.lo -MD -MP -MF sci_gateway/cpp/$(DEPDIR)/libscixml_la-sci_htmlWrite.Tpo -c -o sci_gateway/cpp/libscixml_la-sci_htmlWrite.lo `test -f 'sci_gateway/cpp/sci_htmlWrite.cpp' || echo '$(srcdir)/'`sci_gateway/cpp/sci_htmlWrite.cpp
+@am__fastdepCXX_TRUE@  $(AM_V_at)$(am__mv) sci_gateway/cpp/$(DEPDIR)/libscixml_la-sci_htmlWrite.Tpo sci_gateway/cpp/$(DEPDIR)/libscixml_la-sci_htmlWrite.Plo
+@AMDEP_TRUE@@am__fastdepCXX_FALSE@     $(AM_V_CXX)source='sci_gateway/cpp/sci_htmlWrite.cpp' object='sci_gateway/cpp/libscixml_la-sci_htmlWrite.lo' libtool=yes @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCXX_FALSE@     DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCXX_FALSE@ $(AM_V_CXX@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libscixml_la_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o sci_gateway/cpp/libscixml_la-sci_htmlWrite.lo `test -f 'sci_gateway/cpp/sci_htmlWrite.cpp' || echo '$(srcdir)/'`sci_gateway/cpp/sci_htmlWrite.cpp
+
+sci_gateway/cpp/libscixml_la-sci_htmlDump.lo: sci_gateway/cpp/sci_htmlDump.cpp
+@am__fastdepCXX_TRUE@  $(AM_V_CXX)$(LIBTOOL) $(AM_V_lt) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libscixml_la_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -MT sci_gateway/cpp/libscixml_la-sci_htmlDump.lo -MD -MP -MF sci_gateway/cpp/$(DEPDIR)/libscixml_la-sci_htmlDump.Tpo -c -o sci_gateway/cpp/libscixml_la-sci_htmlDump.lo `test -f 'sci_gateway/cpp/sci_htmlDump.cpp' || echo '$(srcdir)/'`sci_gateway/cpp/sci_htmlDump.cpp
+@am__fastdepCXX_TRUE@  $(AM_V_at)$(am__mv) sci_gateway/cpp/$(DEPDIR)/libscixml_la-sci_htmlDump.Tpo sci_gateway/cpp/$(DEPDIR)/libscixml_la-sci_htmlDump.Plo
+@AMDEP_TRUE@@am__fastdepCXX_FALSE@     $(AM_V_CXX)source='sci_gateway/cpp/sci_htmlDump.cpp' object='sci_gateway/cpp/libscixml_la-sci_htmlDump.lo' libtool=yes @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCXX_FALSE@     DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCXX_FALSE@ $(AM_V_CXX@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libscixml_la_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o sci_gateway/cpp/libscixml_la-sci_htmlDump.lo `test -f 'sci_gateway/cpp/sci_htmlDump.cpp' || echo '$(srcdir)/'`sci_gateway/cpp/sci_htmlDump.cpp
+
 mostlyclean-libtool:
        -rm -f *.lo
 
diff --git a/scilab/modules/xml/help/en_US/htmlDump.xml b/scilab/modules/xml/help/en_US/htmlDump.xml
new file mode 100644 (file)
index 0000000..ce289e6
--- /dev/null
@@ -0,0 +1,94 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+* Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+* Copyright (C) 2013 - Scilab Enterprises - Calixte DENIZET
+*
+* This file must be used under the terms of the CeCILL.
+* This source file is licensed as described in the file COPYING, which
+* you should have received as part of this distribution.  The terms
+* are also available at
+* http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
+*
+-->
+<refentry xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svg="http://www.w3.org/2000/svg" xmlns:ns5="http://www.w3.org/1999/xhtml" xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:db="http://docbook.org/ns/docbook" xmlns:scilab="http://www.scilab.org" xml:id="htmlDump" xml:lang="en">
+    <refnamediv>
+        <refname>htmlDump</refname>
+        <refpurpose>Dump a HTML document</refpurpose>
+    </refnamediv>
+    <refsynopsisdiv>
+        <title>Calling Sequence</title>
+        <synopsis>
+            str = htmlDump(xmlDoc [, indent])
+        </synopsis>
+    </refsynopsisdiv>
+    <refsection>
+        <title>Arguments</title>
+        <variablelist>
+            <varlistentry>
+                <term>xmlDoc</term>
+                <listitem>
+                    <para>xmlDoc, a XML document</para>
+                </listitem>
+            </varlistentry>
+            <varlistentry>
+                <term>indent</term>
+                <listitem>
+                    <para>indent, a boolean</para>
+                </listitem>
+            </varlistentry>
+            <varlistentry>
+                <term>str</term>
+                <listitem>
+                    <para>str, a matrix of strings</para>
+                </listitem>
+            </varlistentry>
+        </variablelist>
+    </refsection>
+    <refsection>
+        <title>Description</title>
+        <para>Dump a HTML document. If indent is false (by default it is true), no indentation and carriage return will be added.</para>
+    </refsection>
+    <refsection>
+        <title>Examples</title>
+        <programlisting role="example"><![CDATA[
+    doc = htmlReadStr(["<!DOCTYPE html PUBLIC ""-//W3C//DTD HTML 4.01//EN""";
+                       """http://www.w3.org/TR/html4/strict.dtd"">";
+                       "<html lang=""en"">";
+                       "  <head>";
+                       "    <meta http-equiv=""content-type"" content=""text/html; charset=utf-8"">";
+                       "    <title>title</title>";
+                       "    <link rel=""stylesheet"" type=""text/css"" href=""style.css"">";
+                       "    <script type=""text/javascript"" src=""script.js""></script>";
+                       "  </head>";
+                       "  <body>";
+                       "  </body>";
+                       "</html>"]);
+
+    // Dump a HTML document
+    htmlDump(doc)
+
+    // Dump a HTML document without indentation
+    htmlDump(doc, %f)
+
+    // Free the resources associated with the document
+    xmlDelete(doc);
+    ]]></programlisting>
+    </refsection>
+    <refsection role="see also">
+        <title>See Also</title>
+        <simplelist type="inline">
+            <member>
+                <link linkend="xmlWrite">htmlWrite</link>
+            </member>
+        </simplelist>
+    </refsection>
+    <refsection>
+        <title>History</title>
+        <revhistory>
+            <revision>
+                <revnumber>5.5.0</revnumber>
+                <revremark>HTML features added.</revremark>
+            </revision>
+        </revhistory>
+    </refsection>
+</refentry>
diff --git a/scilab/modules/xml/help/en_US/htmlRead.xml b/scilab/modules/xml/help/en_US/htmlRead.xml
new file mode 100644 (file)
index 0000000..edfa50e
--- /dev/null
@@ -0,0 +1,102 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+* Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+* Copyright (C) 2012 - Scilab Enterprises - Calixte DENIZET
+*
+* This file must be used under the terms of the CeCILL.
+* This source file is licensed as described in the file COPYING, which
+* you should have received as part of this distribution.  The terms
+* are also available at
+* http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
+*
+-->
+<refentry xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svg="http://www.w3.org/2000/svg" xmlns:ns5="http://www.w3.org/1999/xhtml" xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:db="http://docbook.org/ns/docbook" xmlns:scilab="http://www.scilab.org" xml:id="htmlRead" xml:lang="en">
+    <refnamediv>
+        <refname>htmlRead</refname>
+        <refpurpose>Read a HTML stream from a local or distant file</refpurpose>
+    </refnamediv>
+    <refsynopsisdiv>
+        <title>Calling Sequence</title>
+        <synopsis>
+            doc = htmlRead(path [, encoding])
+        </synopsis>
+    </refsynopsisdiv>
+    <refsection>
+        <title>Arguments</title>
+        <variablelist>
+            <varlistentry>
+                <term>path</term>
+                <listitem>
+                    <para>a string, the path to the file to read.</para>
+                </listitem>
+            </varlistentry>
+            <varlistentry>
+                <term>encoding</term>
+                <listitem>
+                    <para>a string, the file encoding.</para>
+                </listitem>
+            </varlistentry>
+            <varlistentry>
+                <term>doc</term>
+                <listitem>
+                    <para>a mlist typed XMLDoc</para>
+                </listitem>
+            </varlistentry>
+        </variablelist>
+    </refsection>
+    <refsection>
+        <title>Description</title>
+        <para>Read and parse a HTML file. The returned document allows to access to the DOM tree which is kept in memory.</para>
+        <para>In general, a HTML file cannot be read with a XML parser because a HTML is rarely well-formated, so a HTML parser is required and it is more compliant.</para>
+        <para>Once the HTML file is parsed, it can be seen as a XML file in memory, so usual operations can be done.</para>
+        <para>The encoding argument is used to precise the file encoding.</para>
+        <para>
+            It is important to notice that the tree must be freed (to avoid memory leaks) with the function <link linkend="xmlDelete">xmlDelete</link>.
+        </para>
+    </refsection>
+    <refsection>
+        <title>Examples</title>
+        <programlisting role="example"><![CDATA[
+    doc = htmlRead("http://www.scilab.org");
+    e = doc.root.children(1).children(1)
+    e.name
+    e.content
+
+    // Now we can retrieve all the nodes with an 'href' attribute
+    q = xmlXPath(doc, "//*[@href]");
+    q(1).attributes
+
+    // We delete the doc
+    xmlDelete(doc);
+    ]]></programlisting>
+    </refsection>
+    <refsection role="see also">
+        <title>See Also</title>
+        <simplelist type="inline">
+            <member>
+                <link linkend="htmlReadStr">htmlReadStr</link>
+            </member>
+            <member>
+                <link linkend="xmlGetOpenDocs">xmlGetOpenDocs</link>
+            </member>
+            <member>
+                <link linkend="xmlDelete">xmlDelete</link>
+            </member>
+            <member>
+                <link linkend="xmlDocument">xmlDocument</link>
+            </member>
+            <member>
+                <link linkend="xmlXPath">xmlXPath</link>
+            </member>
+        </simplelist>
+    </refsection>
+    <refsection>
+        <title>History</title>
+        <revhistory>
+            <revision>
+                <revnumber>5.5.0</revnumber>
+                <revremark>HTML features added.</revremark>
+            </revision>
+        </revhistory>
+    </refsection>
+</refentry>
diff --git a/scilab/modules/xml/help/en_US/htmlReadStr.xml b/scilab/modules/xml/help/en_US/htmlReadStr.xml
new file mode 100644 (file)
index 0000000..0e99556
--- /dev/null
@@ -0,0 +1,97 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+* Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+* Copyright (C) 2012 - Scilab Enterprises - Calixte DENIZET
+*
+* This file must be used under the terms of the CeCILL.
+* This source file is licensed as described in the file COPYING, which
+* you should have received as part of this distribution.  The terms
+* are also available at
+* http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
+*
+-->
+<refentry xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svg="http://www.w3.org/2000/svg" xmlns:ns5="http://www.w3.org/1999/xhtml" xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:db="http://docbook.org/ns/docbook" xmlns:scilab="http://www.scilab.org" xml:id="htmlReadStr" xml:lang="en">
+    <refnamediv>
+        <refname>htmlReadStr</refname>
+        <refpurpose>Read a HTML tree from a string</refpurpose>
+    </refnamediv>
+    <refsynopsisdiv>
+        <title>Calling Sequence</title>
+        <synopsis>
+            doc = htmlReadStr(string)
+        </synopsis>
+    </refsynopsisdiv>
+    <refsection>
+        <title>Arguments</title>
+        <variablelist>
+            <varlistentry>
+                <term>string</term>
+                <listitem>
+                    <para>a string, containing XML code.</para>
+                </listitem>
+            </varlistentry>
+            <varlistentry>
+                <term>doc</term>
+                <listitem>
+                    <para>a mlist typed XMLDoc</para>
+                </listitem>
+            </varlistentry>
+        </variablelist>
+    </refsection>
+    <refsection>
+        <title>Description</title>
+        <para>Read and parse a HTML string. The returned document allows to access to the DOM tree which is kept in memory.</para>
+        <para>
+            It is important to notice that the tree must be freed (to avoid memory leaks) with the function <link linkend="xmlDelete">xmlDelete</link>.
+        </para>
+    </refsection>
+    <refsection>
+        <title>Examples</title>
+        <programlisting role="example"><![CDATA[
+    doc = htmlReadStr(["<!DOCTYPE html PUBLIC ""-//W3C//DTD HTML 4.01//EN""";
+                       """http://www.w3.org/TR/html4/strict.dtd"">";
+                       "<html lang=""en"">";
+                       "  <head>";
+                       "    <meta http-equiv=""content-type"" content=""text/html; charset=utf-8"">";
+                       "    <title>title</title>";
+                       "    <link rel=""stylesheet"" type=""text/css"" href=""style.css"">";
+                       "    <script type=""text/javascript"" src=""script.js""></script>";
+                       "  </head>";
+                       "  <body>";
+                       "  </body>";
+                       "</html>"]);
+
+    // Dump a HTML document
+    htmlDump(doc)
+
+    // Free the resources associated with the document
+    xmlDelete(doc);
+    ]]></programlisting>
+    </refsection>
+    <refsection role="see also">
+        <title>See Also</title>
+        <simplelist type="inline">
+            <member>
+                <link linkend="htmlRead">htmlRead</link>
+            </member>
+            <member>
+                <link linkend="xmlGetOpenDocs">xmlGetOpenDocs</link>
+            </member>
+            <member>
+                <link linkend="xmlDelete">xmlDelete</link>
+            </member>
+            <member>
+                <link linkend="xmlDocument">xmlDocument</link>
+            </member>
+        </simplelist>
+    </refsection>
+    <refsection>
+        <title>History</title>
+        <revhistory>
+            <revision>
+                <revnumber>5.5.0</revnumber>
+                <revremark>HTML features added.</revremark>
+            </revision>
+        </revhistory>
+    </refsection>
+</refentry>
diff --git a/scilab/modules/xml/help/en_US/htmlWrite.xml b/scilab/modules/xml/help/en_US/htmlWrite.xml
new file mode 100644 (file)
index 0000000..d5a7af9
--- /dev/null
@@ -0,0 +1,90 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+* Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+* Copyright (C) 2012 - Scilab Enterprises - Calixte DENIZET
+*
+* This file must be used under the terms of the CeCILL.
+* This source file is licensed as described in the file COPYING, which
+* you should have received as part of this distribution.  The terms
+* are also available at
+* http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
+*
+-->
+<refentry xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svg="http://www.w3.org/2000/svg" xmlns:ns5="http://www.w3.org/1999/xhtml" xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:db="http://docbook.org/ns/docbook" xmlns:scilab="http://www.scilab.org" xml:id="htmlWrite" xml:lang="en">
+    <refnamediv>
+        <refname>htmlWrite</refname>
+        <refpurpose>Write a HTML document in a file</refpurpose>
+    </refnamediv>
+    <refsynopsisdiv>
+        <title>Calling Sequence</title>
+        <synopsis>
+            htmlWrite(doc [, filename] [, indent])
+            htmlWrite(doc [, indent])
+        </synopsis>
+    </refsynopsisdiv>
+    <refsection>
+        <title>Arguments</title>
+        <variablelist>
+            <varlistentry>
+                <term>doc</term>
+                <listitem>
+                    <para>doc, a mlist typed XMLDoc</para>
+                </listitem>
+            </varlistentry>
+            <varlistentry>
+                <term>filename</term>
+                <listitem>
+                    <para>filename, a string</para>
+                </listitem>
+            </varlistentry>
+            <varlistentry>
+                <term>indent</term>
+                <listitem>
+                    <para>indent, a boolean</para>
+                </listitem>
+            </varlistentry>
+        </variablelist>
+    </refsection>
+    <refsection>
+        <title>Description</title>
+        <para>Write a HTML document in a file with the given filename or with the document url. If indent is true, then the document will be indented, by default indent is true.</para>
+    </refsection>
+    <refsection>
+        <title>Examples</title>
+        <programlisting role="example"><![CDATA[
+    doc = htmlRead("http://www.scilab.org");
+    htmlWrite(doc, TMPDIR+"/scilab.html", %f);
+
+    // Now we open the previous file, modify it and save it
+    doc1 = htmlRead(TMPDIR+"/scilab.html");
+    xp = xmlXPath(doc1, "//title");
+    e = xp(1);
+    e.content = "Hello Scilab World";
+    htmlWrite(doc1);
+
+    // Now we check that the modification has been done
+    doc2 = htmlRead(TMPDIR+"/scilab.html");
+    xp = xmlXPath(doc2, "//title");
+    xp(1).content
+
+    xmlDelete("all");
+    ]]></programlisting>
+    </refsection>
+    <refsection role="see also">
+        <title>See Also</title>
+        <simplelist type="inline">
+            <member>
+                <link linkend="xmlDump">xmlDump</link>
+            </member>
+        </simplelist>
+    </refsection>
+    <refsection>
+        <title>History</title>
+        <revhistory>
+            <revision>
+                <revnumber>5.5.0</revnumber>
+                <revremark>HTML features added.</revremark>
+            </revision>
+        </revhistory>
+    </refsection>
+</refentry>
index 0e5ee12..46892db 100644 (file)
@@ -74,6 +74,10 @@ int sci_xmlRemove(char *fname, unsigned long fname_len);
 int sci_xmlSetAttributes(char *fname, unsigned long fname_len);
 int sci_xmlName(char *fname, unsigned long fname_len);
 int sci_xmlAppend(char *fname, unsigned long fname_len);
+int sci_htmlRead(char *fname, unsigned long fname_len);
+int sci_htmlReadStr(char *fname, unsigned long fname_len);
+int sci_htmlWrite(char *fname, unsigned long fname_len);
+int sci_htmlDump(char *fname, unsigned long fname_len);
 
 /*--------------------------------------------------------------------------*/
 #endif /* __GW_XML_H__ */
index a9f6f32..c3e684d 100644 (file)
@@ -19,3 +19,5 @@
 #include <libxml/xmlschemas.h>
 #include <libxml/relaxng.h>
 #include <libxml/xmlreader.h>
+#include <libxml/HTMLparser.h>
+#include <libxml/xmlsave.h>
index 674a37d..80bc9d9 100644 (file)
@@ -98,7 +98,11 @@ static gw_generic_table Tab[] =
     {sci_xmlRemove, "xmlRemove"},
     {sci_xmlSetAttributes, "xmlSetAttributes"},
     {sci_xmlName, "xmlName"},
-    {sci_xmlAppend, "xmlAppend"}
+    {sci_xmlAppend, "xmlAppend"},
+    {sci_htmlRead, "htmlRead"},
+    {sci_htmlReadStr, "htmlReadStr"},
+    {sci_htmlWrite, "htmlWrite"},
+    {sci_htmlDump, "htmlDump"}
 };
 
 /*--------------------------------------------------------------------------*/
diff --git a/scilab/modules/xml/sci_gateway/cpp/sci_htmlDump.cpp b/scilab/modules/xml/sci_gateway/cpp/sci_htmlDump.cpp
new file mode 100644 (file)
index 0000000..b0dddf0
--- /dev/null
@@ -0,0 +1,127 @@
+/*
+ * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+ * Copyright (C) 2011 - Scilab Enterprises - Calixte DENIZET
+ *
+ * This file must be used under the terms of the CeCILL.
+ * This source file is licensed as described in the file COPYING, which
+ * you should have received as part of this distribution.  The terms
+ * are also available at
+ * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
+ *
+ */
+
+#include <string>
+#include <vector>
+
+#include "XMLObject.hxx"
+#include "XMLDocument.hxx"
+#include "XMLElement.hxx"
+#include "XMLNs.hxx"
+#include "XMLAttr.hxx"
+#include "SplitString.hxx"
+
+extern "C"
+{
+#include "gw_xml.h"
+#include "Scierror.h"
+#include "api_scilab.h"
+#include "xml_mlist.h"
+#include "localization.h"
+}
+
+using namespace org_modules_xml;
+
+/*--------------------------------------------------------------------------*/
+int sci_htmlDump(char *fname, unsigned long fname_len)
+{
+    org_modules_xml::XMLDocument * doc = 0;
+    int id;
+    int type;
+    int b;
+    SciErr err;
+    int *addr = 0;
+
+    std::vector < std::string > lines;
+    std::vector < const char *>clines;
+    bool indent = true;
+
+    CheckLhs(1, 1);
+    CheckRhs(1, 2);
+
+    err = getVarAddressFromPosition(pvApiCtx, 1, &addr);
+    if (err.iErr)
+    {
+        printError(&err, 0);
+        Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
+        return 0;
+    }
+
+    type = isXMLObject(addr, pvApiCtx);
+    if (!type)
+    {
+        Scierror(999, gettext("%s: Wrong type for input argument #%d: A %s expected.\n"), fname, 1, "XML object");
+        return 0;
+    }
+
+    id = getXMLObjectId(addr, pvApiCtx);
+    doc = XMLObject::getFromId<org_modules_xml::XMLDocument>(id);
+    if (!doc)
+    {
+        Scierror(999, gettext("%s: XML object does not exist.\n"), fname);
+        return 0;
+    }
+
+    if (Rhs == 2)
+    {
+        err = getVarAddressFromPosition(pvApiCtx, 2, &addr);
+        if (err.iErr)
+        {
+            printError(&err, 0);
+            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
+            return 0;
+        }
+
+        if (!isBooleanType(pvApiCtx, addr) || !checkVarDimension(pvApiCtx, addr, 1, 1))
+        {
+            Scierror(999, gettext("%s: Wrong type for input argument #%d: A %s expected.\n"), fname, 2, "boolean");
+            return 0;
+        }
+
+        if (getScalarBoolean(pvApiCtx, addr, &b))
+        {
+            return 0;
+        }
+        indent = b != 0;
+    }
+
+    lines = std::vector < std::string > ();
+    SplitString::split(doc->dumpHTML(indent), lines);
+    clines = std::vector < const char *>(lines.size());
+
+    for (unsigned int i = 0; i < lines.size(); i++)
+    {
+        clines[i] = lines[i].c_str();
+    }
+
+    if (clines.size())
+    {
+        err = createMatrixOfString(pvApiCtx, Rhs + 1, (int)lines.size(), 1, const_cast < const char *const *>(&(clines[0])));
+    }
+    else
+    {
+        err = createMatrixOfDouble(pvApiCtx, Rhs + 1, 0, 0, 0);
+    }
+
+    if (err.iErr)
+    {
+        printError(&err, 0);
+        Scierror(999, _("%s: Memory allocation error.\n"), fname);
+        return 0;
+    }
+
+    LhsVar(1) = Rhs + 1;
+    PutLhsVar();
+    return 0;
+}
+
+/*--------------------------------------------------------------------------*/
diff --git a/scilab/modules/xml/sci_gateway/cpp/sci_htmlRead.cpp b/scilab/modules/xml/sci_gateway/cpp/sci_htmlRead.cpp
new file mode 100644 (file)
index 0000000..cfb069f
--- /dev/null
@@ -0,0 +1,106 @@
+/*
+ * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+ * Copyright (C) 2011 - Scilab Enterprises - Calixte DENIZET
+ *
+ * This file must be used under the terms of the CeCILL.
+ * This source file is licensed as described in the file COPYING, which
+ * you should have received as part of this distribution.  The terms
+ * are also available at
+ * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
+ *
+ */
+
+#include "XMLObject.hxx"
+#include "XMLDocument.hxx"
+#include "xml.h"
+
+extern "C"
+{
+#include "gw_xml.h"
+#include "Scierror.h"
+#include "api_scilab.h"
+#include "xml_mlist.h"
+#include "localization.h"
+}
+
+using namespace org_modules_xml;
+
+/*--------------------------------------------------------------------------*/
+int sci_htmlRead(char *fname, unsigned long fname_len)
+{
+    org_modules_xml::XMLDocument * doc;
+    SciErr err;
+    int *addr = 0;
+    char *path = 0;
+    char *encoding = 0;
+
+    std::string error;
+
+    CheckLhs(1, 2);
+    CheckRhs(1, 1);
+
+    err = getVarAddressFromPosition(pvApiCtx, 1, &addr);
+    if (err.iErr)
+    {
+        printError(&err, 0);
+        Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
+        return 0;
+    }
+
+    if (!isStringType(pvApiCtx, addr) || !checkVarDimension(pvApiCtx, addr, 1, 1))
+    {
+        Scierror(999, gettext("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 1);
+        return 0;
+    }
+
+    if (getAllocatedSingleString(pvApiCtx, addr, &path) != 0)
+    {
+        Scierror(999, _("%s: No more memory.\n"), fname);
+        return 0;
+    }
+
+    if (Rhs == 2)
+    {
+       err = getVarAddressFromPosition(pvApiCtx, 2, &addr);
+       if (err.iErr)
+       {
+           printError(&err, 0);
+           Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
+           return 0;
+       }
+       
+       if (!isStringType(pvApiCtx, addr) || !checkVarDimension(pvApiCtx, addr, 1, 1))
+       {
+           Scierror(999, gettext("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 2);
+           return 0;
+       }
+       
+       if (getAllocatedSingleString(pvApiCtx, addr, &encoding) != 0)
+       {
+           Scierror(999, _("%s: No more memory.\n"), fname);
+           return 0;
+       }
+    }
+
+    doc = new org_modules_xml::XMLDocument((const char *)path, false, &error, (const char *)encoding, true);
+    freeAllocatedSingleString(path);
+
+    if (!error.empty())
+    {
+        delete doc;
+
+        Scierror(999, gettext("%s: Cannot read the file:\n%s"), fname, error.c_str());
+        return 0;
+    }
+
+    if (!doc->createOnStack(Rhs + 1, pvApiCtx))
+    {
+        return 0;
+    }
+
+    LhsVar(1) = Rhs + 1;
+    PutLhsVar();
+    return 0;
+}
+
+/*--------------------------------------------------------------------------*/
diff --git a/scilab/modules/xml/sci_gateway/cpp/sci_htmlReadStr.cpp b/scilab/modules/xml/sci_gateway/cpp/sci_htmlReadStr.cpp
new file mode 100644 (file)
index 0000000..a11be80
--- /dev/null
@@ -0,0 +1,82 @@
+/*
+ * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+ * Copyright (C) 2013 - Scilab Enterprises - Calixte DENIZET
+ *
+ * This file must be used under the terms of the CeCILL.
+ * This source file is licensed as described in the file COPYING, which
+ * you should have received as part of this distribution.  The terms
+ * are also available at
+ * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
+ *
+ */
+
+#include "XMLObject.hxx"
+#include "XMLDocument.hxx"
+#include "XMLRhsValue.hxx"
+
+extern "C"
+{
+#include "gw_xml.h"
+#include "Scierror.h"
+#include "api_scilab.h"
+#include "xml_mlist.h"
+#include "localization.h"
+}
+
+using namespace org_modules_xml;
+
+/*--------------------------------------------------------------------------*/
+int sci_htmlReadStr(char *fname, unsigned long fname_len)
+{
+    org_modules_xml::XMLDocument * doc;
+    SciErr err;
+    int *addr = 0;
+    char *encoding = 0;
+
+    std::string * code;
+    std::string error;
+
+    CheckLhs(1, 2);
+    CheckRhs(1, 1);
+
+    err = getVarAddressFromPosition(pvApiCtx, 1, &addr);
+    if (err.iErr)
+    {
+        printError(&err, 0);
+        Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
+        return 0;
+    }
+
+    if (!isStringType(pvApiCtx, addr))
+    {
+        Scierror(999, gettext("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 1);
+        return 0;
+    }
+
+    if (!XMLRhsValue::get(fname, addr, &code, pvApiCtx))
+    {
+        return 0;
+    }
+
+    doc = new org_modules_xml::XMLDocument(*code, false, &error, 0, true);
+    delete code;
+
+    if (!error.empty())
+    {
+        delete doc;
+
+        Scierror(999, gettext("%s: Cannot parse the string:\n%s"), fname, error.c_str());
+        return 0;
+    }
+
+    if (!doc->createOnStack(Rhs + 1, pvApiCtx))
+    {
+        return 0;
+    }
+
+    LhsVar(1) = Rhs + 1;
+    PutLhsVar();
+    return 0;
+}
+
+/*--------------------------------------------------------------------------*/
diff --git a/scilab/modules/xml/sci_gateway/cpp/sci_htmlWrite.cpp b/scilab/modules/xml/sci_gateway/cpp/sci_htmlWrite.cpp
new file mode 100644 (file)
index 0000000..16d7445
--- /dev/null
@@ -0,0 +1,183 @@
+/*
+ * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+ * Copyright (C) 2011 - Scilab Enterprises - Calixte DENIZET
+ *
+ * This file must be used under the terms of the CeCILL.
+ * This source file is licensed as described in the file COPYING, which
+ * you should have received as part of this distribution.  The terms
+ * are also available at
+ * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
+ *
+ */
+
+#include "XMLObject.hxx"
+#include "XMLDocument.hxx"
+#include "XMLElement.hxx"
+
+extern "C"
+{
+#include <string.h>
+#include "gw_xml.h"
+#include "Scierror.h"
+#include "api_scilab.h"
+#include "xml_mlist.h"
+#include "libxml/tree.h"
+#include "expandPathVariable.h"
+#include "MALLOC.h"
+#include "localization.h"
+#ifdef _MSC_VER
+#include "strdup_windows.h"
+#endif
+}
+
+using namespace org_modules_xml;
+
+/*--------------------------------------------------------------------------*/
+int sci_htmlWrite(char *fname, unsigned long fname_len)
+{
+    org_modules_xml::XMLDocument * doc = 0;
+    xmlDoc *document = 0;
+    SciErr err;
+    int *addr = 0;
+    char *path = 0;
+    char *expandedPath = 0;
+    int indent = 1;
+    int ret = 0;
+
+    CheckLhs(1, 1);
+    CheckRhs(1, 3);
+
+    err = getVarAddressFromPosition(pvApiCtx, 1, &addr);
+    if (err.iErr)
+    {
+        printError(&err, 0);
+        Scierror(999, gettext("%s: Can not read input argument #%d.\n"), fname, 1);
+        return 0;
+    }
+
+    if (!isXMLDoc(addr, pvApiCtx))
+    {
+        Scierror(999, gettext("%s: Wrong type for input argument #%d: A %s expected.\n"), fname, 1, "XMLDoc");
+        return 0;
+    }
+
+    doc = XMLObject::getFromId<org_modules_xml::XMLDocument>(getXMLObjectId(addr, pvApiCtx));
+    if (!doc)
+    {
+        Scierror(999, gettext("%s: XML Document does not exist.\n"), fname);
+        return 0;
+    }
+    document = doc->getRealDocument();
+
+    if (Rhs >= 2)
+    {
+        err = getVarAddressFromPosition(pvApiCtx, 2, &addr);
+        if (err.iErr)
+        {
+            printError(&err, 0);
+            Scierror(999, gettext("%s: Can not read input argument #%d.\n"), fname, 2);
+            return 0;
+        }
+
+        if (Rhs == 2 && !isStringType(pvApiCtx, addr) && !isBooleanType(pvApiCtx, addr))
+        {
+            Scierror(999, gettext("%s: Wrong type for input argument #%d: A string or a boolean expected.\n"), fname, 2);
+            return 0;
+        }
+
+        if (Rhs == 3 && !isStringType(pvApiCtx, addr))
+        {
+            Scierror(999, gettext("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 2);
+            return 0;
+        }
+
+        if (isStringType(pvApiCtx, addr))
+        {
+            if (!checkVarDimension(pvApiCtx, addr, 1, 1))
+            {
+                Scierror(999, gettext("%s: Wrong dimension for input argument #%d: A string expected.\n"), fname, 2);
+                return 0;
+            }
+
+            if (getAllocatedSingleString(pvApiCtx, addr, &path) != 0)
+            {
+                Scierror(999, gettext("%s: No more memory.\n"), fname);
+                return 0;
+            }
+
+            if (!strlen(path))
+            {
+                freeAllocatedSingleString(path);
+                Scierror(999, gettext("%s: Wrong size for input argument #%d: Non-empty string expected.\n"), fname, 2);
+                return 0;
+            }
+
+            expandedPath = expandPathVariable(path);
+
+            freeAllocatedSingleString(path);
+        }
+        else
+        {
+            if (!document->URL)
+            {
+                Scierror(999, gettext("%s: The XML Document has not an URI and there is no second argument.\n"), fname);
+                return 0;
+            }
+
+            if (!isBooleanType(pvApiCtx, addr) || !checkVarDimension(pvApiCtx, addr, 1, 1))
+            {
+                Scierror(999, gettext("%s: Wrong dimension for input argument #%d: A boolean expected.\n"), fname, 2);
+                return 0;
+            }
+
+            expandedPath = strdup((const char *)document->URL);
+            getScalarBoolean(pvApiCtx, addr, &indent);
+        }
+
+        if (Rhs == 3)
+        {
+            err = getVarAddressFromPosition(pvApiCtx, 3, &addr);
+            if (err.iErr)
+            {
+                printError(&err, 0);
+                FREE(expandedPath);
+                Scierror(999, gettext("%s: Can not read input argument #%d.\n"), fname, 3);
+                return 0;
+            }
+
+            if (!isBooleanType(pvApiCtx, addr) || !checkVarDimension(pvApiCtx, addr, 1, 1))
+            {
+                FREE(expandedPath);
+                Scierror(999, gettext("%s: Wrong type for input argument #%d: A boolean expected.\n"), fname, 3);
+                return 0;
+            }
+
+            getScalarBoolean(pvApiCtx, addr, &indent);
+        }
+    }
+    else
+    {
+        if (!document->URL)
+        {
+            Scierror(999, gettext("%s: The XML Document has not an URI and there is no second argument.\n"), fname);
+            return 0;
+        }
+        expandedPath = strdup((const char *)document->URL);
+    }
+
+    if (!doc->saveToHTMLFile(expandedPath, indent == 1))
+    {
+        Scierror(999, gettext("%s: Cannot write the file: %s\n"), fname, expandedPath);
+        FREE(expandedPath);
+        return 0;
+    }
+
+    FREE(expandedPath);
+
+    LhsVar(1) = 0;
+    PutLhsVar();
+
+    return 0;
+}
+
+/*--------------------------------------------------------------------------*/
index f3ff476..4177329 100644 (file)
@@ -165,9 +165,7 @@ int sci_xmlWrite(char *fname, unsigned long fname_len)
         expandedPath = strdup((const char *)document->URL);
     }
 
-    xmlThrDefIndentTreeOutput(1);
-    ret = xmlSaveFormatFile(expandedPath, document, indent);
-    if (ret == -1)
+    if (!doc->saveToFile(expandedPath, indent == 1))
     {
         Scierror(999, gettext("%s: Cannot write the file: %s\n"), fname, expandedPath);
         FREE(expandedPath);
index d7a5eea..a13f154 100644 (file)
     <PRIMITIVE gatewayId="68" primitiveId="78" primitiveName="xmlSetAttributes" />
     <PRIMITIVE gatewayId="68" primitiveId="79" primitiveName="xmlName" />
     <PRIMITIVE gatewayId="68" primitiveId="80" primitiveName="xmlAppend" />
+    <PRIMITIVE gatewayId="68" primitiveId="81" primitiveName="htmlRead" />
+    <PRIMITIVE gatewayId="68" primitiveId="82" primitiveName="htmlReadStr" />
+    <PRIMITIVE gatewayId="68" primitiveId="83" primitiveName="htmlWrite" />
+    <PRIMITIVE gatewayId="68" primitiveId="84" primitiveName="htmlDump" />
 </GATEWAY>
index 78dd360..d7b6f93 100644 (file)
@@ -25,376 +25,511 @@ extern "C"
 #include "localization.h"
 }
 
+#include <iostream>
+
 #define BUFFER_SIZE 1024
 
 namespace org_modules_xml
 {
 
-std::string * XMLDocument::errorBuffer = 0;
-std::string * XMLDocument::errorXPathBuffer = 0;
-std::list < XMLDocument * >&XMLDocument::openDocs = *new std::list < XMLDocument * >();
+    std::string * XMLDocument::errorBuffer = 0;
+    std::string * XMLDocument::errorXPathBuffer = 0;
+    std::list < XMLDocument * >&XMLDocument::openDocs = *new std::list < XMLDocument * >();
 
-XMLDocument::XMLDocument(const char *path, bool validate, std::string * error, const char * encoding): XMLObject()
-{
-    char *expandedPath = expandPathVariable(const_cast<char *>(path));
-    if (expandedPath)
+    XMLDocument::XMLDocument(const char *path, bool validate, std::string * error, const char * encoding, const bool html): XMLObject()
     {
-        document = readDocument(const_cast<const char *>(expandedPath), encoding, validate, error);
-        FREE(expandedPath);
-        if (document)
+        char *expandedPath = expandPathVariable(const_cast<char *>(path));
+        if (expandedPath)
         {
-            openDocs.push_back(this);
-            scope->registerPointers(document, this);
+            if (html)
+            {
+                document = readHTMLDocument(const_cast<const char *>(expandedPath), encoding, error);
+            }
+            else
+            {
+                document = readDocument(const_cast<const char *>(expandedPath), encoding, validate, error);
+            }
+
+            FREE(expandedPath);
+            if (document)
+            {
+                openDocs.push_back(this);
+                scope->registerPointers(document, this);
+            }
+        }
+        else
+        {
+            document = 0;
+            *error = std::string(gettext("Invalid file name: ")) + std::string(path);
         }
-    }
-    else
-    {
-        document = 0;
-        *error = std::string(gettext("Invalid file name: ")) + std::string(path);
-    }
 
-    id = scope->getVariableId(*this);
-    scilabType = XMLDOCUMENT;
-}
+        id = scope->getVariableId(*this);
+        scilabType = XMLDOCUMENT;
+    }
 
-XMLDocument::XMLDocument(const std::string & xmlCode, bool validate, std::string * error, const char * encoding): XMLObject()
-{
-    document = readDocument(xmlCode, encoding, validate, error);
-    if (document)
+    XMLDocument::XMLDocument(const std::string & xmlCode, bool validate, std::string * error, const char * encoding, const bool html): XMLObject()
     {
-        openDocs.push_back(this);
-    }
-    scope->registerPointers(document, this);
-    id = scope->getVariableId(*this);
-    scilabType = XMLDOCUMENT;
-}
+        if (html)
+        {
+            document = readHTMLDocument(xmlCode, encoding, error);
+        }
+        else
+        {
+            document = readDocument(xmlCode, encoding, validate, error);
+        }
 
-XMLDocument::XMLDocument(char *uri, char *version): XMLObject()
-{
-    char *newUri = 0;
-    char *expandedPath = 0;
+        if (document)
+        {
+            openDocs.push_back(this);
+        }
+        scope->registerPointers(document, this);
+        id = scope->getVariableId(*this);
+        scilabType = XMLDOCUMENT;
+    }
 
-    if (!version)
+    XMLDocument::XMLDocument(char *uri, char *version): XMLObject()
     {
-        version = const_cast < char *>("1.0");
-    }
-    document = xmlNewDoc((xmlChar *) version);
-    openDocs.push_back(this);
-    scope->registerPointers(document, this);
-    id = scope->getVariableId(*this);
-    scilabType = XMLDOCUMENT;
+        char *newUri = 0;
+        char *expandedPath = 0;
 
-    expandedPath = expandPathVariable(const_cast < char *>(uri));
+        if (!version)
+        {
+            version = const_cast < char *>("1.0");
+        }
+        document = xmlNewDoc((xmlChar *) version);
+        openDocs.push_back(this);
+        scope->registerPointers(document, this);
+        id = scope->getVariableId(*this);
+        scilabType = XMLDOCUMENT;
 
-    if (expandedPath)
-    {
-        newUri = (char *)xmlMalloc(sizeof(char *) * (strlen(expandedPath) + 1));
-        memcpy(newUri, expandedPath, sizeof(char) * (strlen(expandedPath) + 1));
-        document->URL = (xmlChar *) newUri;
-        FREE(expandedPath);
+        expandedPath = expandPathVariable(const_cast < char *>(uri));
+
+        if (expandedPath)
+        {
+            newUri = (char *)xmlMalloc(sizeof(char *) * (strlen(expandedPath) + 1));
+            memcpy(newUri, expandedPath, sizeof(char) * (strlen(expandedPath) + 1));
+            document->URL = (xmlChar *) newUri;
+            FREE(expandedPath);
+        }
     }
-}
 
-XMLDocument::~XMLDocument()
-{
-    scope->unregisterPointer(document);
-    scope->removeId(id);
-    if (document)
+    XMLDocument::~XMLDocument()
     {
-        openDocs.remove(this);
-        if (openDocs.size() == 0 && XMLValidation::getOpenValidationFiles().size() == 0)
+        scope->unregisterPointer(document);
+        scope->removeId(id);
+        if (document)
+        {
+            openDocs.remove(this);
+            if (openDocs.size() == 0 && XMLValidation::getOpenValidationFiles().size() == 0)
+            {
+                resetScope();
+            }
+            xmlFreeDoc(document);
+        }
+        if (errorBuffer)
+        {
+            delete errorBuffer;
+            errorBuffer = 0;
+        }
+        if (errorXPathBuffer)
         {
-            resetScope();
+            delete errorXPathBuffer;
+            errorXPathBuffer = 0;
         }
-        xmlFreeDoc(document);
     }
-    if (errorBuffer)
+
+    void *XMLDocument::getRealXMLPointer() const
     {
-        delete errorBuffer;
-        errorBuffer = 0;
+        return static_cast < void *>(document);
     }
-    if (errorXPathBuffer)
+
+    const XMLXPath *XMLDocument::makeXPathQuery(const char *query, char **namespaces, int length, const XMLElement * e, std::string * error)
     {
-        delete errorXPathBuffer;
-        errorXPathBuffer = 0;
-    }
-}
+        if (errorXPathBuffer)
+        {
+            delete errorXPathBuffer;
+        }
+        errorXPathBuffer = new std::string();
 
-void *XMLDocument::getRealXMLPointer() const
-{
-    return static_cast < void *>(document);
-}
+        xmlXPathContext *ctxt = xmlXPathNewContext(document);
 
-const XMLXPath *XMLDocument::makeXPathQuery(const char *query, char **namespaces, int length, const XMLElement * e, std::string * error)
-{
-    if (errorXPathBuffer)
-    {
-        delete errorXPathBuffer;
-    }
-    errorXPathBuffer = new std::string();
+        if (!ctxt)
+        {
+            errorXPathBuffer->append(gettext("Cannot create a parser context"));
+            *error = *errorXPathBuffer;
+            return 0;
+        }
 
-    xmlXPathContext *ctxt = xmlXPathNewContext(document);
+        if (e)
+        {
+            ctxt->node = (xmlNode *) e->getRealXMLPointer();
+        }
 
-    if (!ctxt)
-    {
-        errorXPathBuffer->append(gettext("Cannot create a parser context"));
-        *error = *errorXPathBuffer;
-        return 0;
-    }
+        if (namespaces)
+        {
+            for (int i = 0; i < length; i++)
+            {
+                xmlXPathRegisterNs(ctxt, (const xmlChar *)namespaces[i], (const xmlChar *)namespaces[i + length]);
+            }
+        }
 
-    if (e)
-    {
-        ctxt->node = (xmlNode *) e->getRealXMLPointer();
-    }
+        xmlSetStructuredErrorFunc(ctxt, XMLDocument::errorXPathFunction);
+        xmlXPathCompExpr *expr = xmlXPathCtxtCompile(ctxt, (const xmlChar *)query);
 
-    if (namespaces)
-    {
-        for (int i = 0; i < length; i++)
+        if (!expr)
         {
-            xmlXPathRegisterNs(ctxt, (const xmlChar *)namespaces[i], (const xmlChar *)namespaces[i + length]);
+            xmlSetStructuredErrorFunc(ctxt, 0);
+            xmlXPathFreeContext(ctxt);
+            *error = *errorXPathBuffer;
+            return 0;
         }
-    }
 
-    xmlSetStructuredErrorFunc(ctxt, XMLDocument::errorXPathFunction);
-    xmlXPathCompExpr *expr = xmlXPathCtxtCompile(ctxt, (const xmlChar *)query);
+        xmlXPathObject *xpath = xmlXPathCompiledEval(expr, ctxt);
 
-    if (!expr)
-    {
         xmlSetStructuredErrorFunc(ctxt, 0);
         xmlXPathFreeContext(ctxt);
-        *error = *errorXPathBuffer;
-        return 0;
-    }
+        xmlXPathFreeCompExpr(expr);
+        if (!xpath)
+        {
+            *error = *errorXPathBuffer;
+            return 0;
+        }
 
-    xmlXPathObject *xpath = xmlXPathCompiledEval(expr, ctxt);
+        return new XMLXPath(*this, xpath);
+    }
 
-    xmlSetStructuredErrorFunc(ctxt, 0);
-    xmlXPathFreeContext(ctxt);
-    xmlXPathFreeCompExpr(expr);
-    if (!xpath)
+    const XMLObject *XMLDocument::getXMLObjectParent() const
     {
-        *error = *errorXPathBuffer;
         return 0;
     }
 
-    return new XMLXPath(*this, xpath);
-}
-
-const XMLObject *XMLDocument::getXMLObjectParent() const
-{
-    return 0;
-}
-
-const std::string XMLDocument::toString() const
-{
-    std::ostringstream oss;
+    const std::string XMLDocument::toString() const
+    {
+        std::ostringstream oss;
 
-    oss << "XML Document" << std::endl
-        << "url: " << getDocumentURL() << std::endl
-        << "root: " << "XML Element";
+        oss << "XML Document" << std::endl
+            << "url: " << getDocumentURL() << std::endl
+            << "root: " << "XML Element";
 
-    return oss.str();
-}
+        return oss.str();
+    }
 
-const std::string XMLDocument::dump(bool indent) const
-{
-    xmlChar *buffer = 0;
-    int size = 0;
-    xmlDocDumpFormatMemory(document, &buffer, &size, indent ? 1 : 0);
-    std::string str = std::string((const char *)buffer);
-    xmlFree(buffer);
+    const std::string XMLDocument::dump(bool indent) const
+    {
+        xmlChar *buffer = 0;
+        int size = 0;
+        xmlDocDumpFormatMemory(document, &buffer, &size, indent ? 1 : 0);
+        std::string str((const char *)buffer);
+        xmlFree(buffer);
 
-    return str;
-}
+        return str;
+    }
 
-const XMLElement *XMLDocument::getRoot() const
-{
-    xmlNode *root = xmlDocGetRootElement(document);
-    if (!root)
+    const std::string XMLDocument::dumpHTML(bool indent) const
     {
-        return 0;
-    }
+        xmlBuffer * buffer = xmlBufferCreate();
+        int ret;
+        int options = XML_SAVE_AS_HTML;
+        if (indent)
+        {
+            options |= XML_SAVE_FORMAT;
+        }
 
-    XMLObject *obj = scope->getXMLObjectFromLibXMLPtr(root);
+        xmlThrDefIndentTreeOutput(1);
+        xmlSaveCtxtPtr ctxt = xmlSaveToBuffer(buffer, 0, options);
+        ret = xmlSaveDoc(ctxt, document);
+        xmlSaveFlush(ctxt);
+        xmlSaveClose(ctxt);
 
-    if (obj)
-    {
-        return static_cast < XMLElement * >(obj);
-    }
+        std::string str((const char *)xmlBufferDetach(buffer));
+        xmlBufferFree(buffer);
 
-    return new XMLElement(*this, root);
-}
+        return str;
+    }
 
-void XMLDocument::setRoot(const XMLElement & elem) const
-{
-    xmlNode *root = xmlDocGetRootElement(document);
-    if (root != elem.getRealNode())
+    const XMLElement *XMLDocument::getRoot() const
     {
-        xmlNode *cpy = xmlCopyNodeList(elem.getRealNode());
-        xmlUnlinkNode(cpy);
-        xmlDocSetRootElement(document, cpy);
-    }
-}
+        xmlNode *root = xmlDocGetRootElement(document);
+        if (!root)
+        {
+            return 0;
+        }
 
-void XMLDocument::setRoot(const std::string & xmlCode, std::string * error) const
-{
-    XMLDocument doc = XMLDocument(xmlCode, false, error);
+        XMLObject *obj = scope->getXMLObjectFromLibXMLPtr(root);
 
-    if (error->empty())
-    {
-        setRoot(*doc.getRoot());
+        if (obj)
+        {
+            return static_cast < XMLElement * >(obj);
+        }
+
+        return new XMLElement(*this, root);
     }
-}
 
-const char *XMLDocument::getDocumentURL() const
-{
-    if (document->URL)
+    void XMLDocument::setRoot(const XMLElement & elem) const
     {
-        return (const char *)document->URL;
+        xmlNode *root = xmlDocGetRootElement(document);
+        if (root != elem.getRealNode())
+        {
+            xmlNode *cpy = xmlCopyNodeList(elem.getRealNode());
+            xmlUnlinkNode(cpy);
+            xmlDocSetRootElement(document, cpy);
+        }
     }
-    else
+
+    void XMLDocument::setRoot(const std::string & xmlCode, std::string * error) const
     {
-        return "Undefined";
-    }
-}
+        XMLDocument doc = XMLDocument(xmlCode, false, error);
 
-void XMLDocument::setDocumentURL(const std::string & url) const
-{
-    char *expandedPath = 0;
-    char *newURL = 0;
-    expandedPath = expandPathVariable(const_cast < char *>(url.c_str()));
+        if (error->empty())
+        {
+            setRoot(*doc.getRoot());
+        }
+    }
 
-    if (expandedPath)
+    const char *XMLDocument::getDocumentURL() const
     {
-        xmlFree((void *)document->URL);
-        newURL = (char *)xmlMalloc(sizeof(char *) * (strlen(expandedPath) + 1));
-        memcpy(newURL, expandedPath, sizeof(char) * (strlen(expandedPath) + 1));
-        document->URL = (xmlChar *) newURL;
-        FREE(expandedPath);
+        if (document->URL)
+        {
+            return (const char *)document->URL;
+        }
+        else
+        {
+            return "Undefined";
+        }
     }
-}
 
-const std::list < XMLDocument * >&XMLDocument::getOpenDocuments()
-{
-    return openDocs;
-}
-
-void XMLDocument::closeAllDocuments()
-{
-    int size = (int)openDocs.size();
-    XMLDocument **arr = new XMLDocument *[size];
-    int j = 0;
-
-    for (std::list < XMLDocument * >::iterator i = openDocs.begin(); i != openDocs.end(); i++, j++)
+    void XMLDocument::setDocumentURL(const std::string & url) const
     {
-        arr[j] = *i;
+        char *expandedPath = 0;
+        char *newURL = 0;
+        expandedPath = expandPathVariable(const_cast < char *>(url.c_str()));
+
+        if (expandedPath)
+        {
+            xmlFree((void *)document->URL);
+            newURL = (char *)xmlMalloc(sizeof(char *) * (strlen(expandedPath) + 1));
+            memcpy(newURL, expandedPath, sizeof(char) * (strlen(expandedPath) + 1));
+            document->URL = (xmlChar *) newURL;
+            FREE(expandedPath);
+        }
     }
-    for (j = 0; j < size; j++)
+
+    const std::list < XMLDocument * >&XMLDocument::getOpenDocuments()
     {
-        delete arr[j];
+        return openDocs;
     }
-    delete[]arr;
-}
-
-xmlDoc *XMLDocument::readDocument(const char *filename, const char * encoding, bool validate, std::string * error)
-{
-    xmlParserCtxt *ctxt = initContext(error, validate);
-    xmlDoc *doc = 0;
-    int options = XML_PARSE_NSCLEAN | XML_PARSE_NOBLANKS;
 
-    if (validate)
+    void XMLDocument::closeAllDocuments()
     {
-        options |= XML_PARSE_DTDVALID;
+        int size = (int)openDocs.size();
+        XMLDocument **arr = new XMLDocument *[size];
+        int j = 0;
+
+        for (std::list < XMLDocument * >::iterator i = openDocs.begin(); i != openDocs.end(); i++, j++)
+        {
+            arr[j] = *i;
+        }
+        for (j = 0; j < size; j++)
+        {
+            delete arr[j];
+        }
+        delete[]arr;
     }
 
-    if (!ctxt)
+    xmlDoc *XMLDocument::readDocument(const char *filename, const char * encoding, bool validate, std::string * error)
     {
+        xmlParserCtxt *ctxt = initContext(error, validate);
+        xmlDoc *doc = 0;
+        int options = XML_PARSE_NSCLEAN | XML_PARSE_NOBLANKS;
+
+        if (validate)
+        {
+            options |= XML_PARSE_DTDVALID;
+        }
+
+        if (!ctxt)
+        {
+            xmlSetGenericErrorFunc(0, errorFunctionWithoutOutput);
+            return 0;
+        }
+
+        doc = xmlCtxtReadFile(ctxt, filename, encoding, options);
+        if (!doc || !ctxt->valid)
+        {
+            *error = *errorBuffer;
+        }
+
         xmlSetGenericErrorFunc(0, errorFunctionWithoutOutput);
-        return 0;
+        xmlFreeParserCtxt(ctxt);
+
+        return doc;
     }
 
-    doc = xmlCtxtReadFile(ctxt, filename, encoding, options);
-    if (!doc || !ctxt->valid)
+    xmlDoc *XMLDocument::readHTMLDocument(const char *filename, const char * encoding, std::string * error)
     {
-        *error = *errorBuffer;
-    }
+        htmlParserCtxt *ctxt = initHTMLContext(error);
+        htmlDocPtr doc = 0;
+        int options = HTML_PARSE_NOWARNING | HTML_PARSE_NOBLANKS | HTML_PARSE_COMPACT;
 
-    xmlSetGenericErrorFunc(0, errorFunctionWithoutOutput);
-    xmlFreeParserCtxt(ctxt);
+        if (!ctxt)
+        {
+            xmlSetGenericErrorFunc(0, errorFunctionWithoutOutput);
+            return 0;
+        }
 
-    return doc;
-}
+        doc = htmlCtxtReadFile(ctxt, filename, encoding, options);
+        if (!doc || !ctxt->valid)
+        {
+            *error = *errorBuffer;
+        }
 
-xmlDoc *XMLDocument::readDocument(const std::string & xmlCode, const char * encoding, bool validate, std::string * error)
-{
-    xmlParserCtxt *ctxt = initContext(error, validate);
-    xmlDoc *doc = 0;
-    int options = XML_PARSE_NSCLEAN | XML_PARSE_NOBLANKS;
+        xmlSetGenericErrorFunc(0, errorFunctionWithoutOutput);
+        htmlFreeParserCtxt(ctxt);
 
-    if (validate)
-    {
-        options |= XML_PARSE_DTDVALID;
+        return (xmlDoc *)doc;
     }
 
-    if (!ctxt)
+    xmlDoc *XMLDocument::readDocument(const std::string & xmlCode, const char * encoding, bool validate, std::string * error)
     {
+        xmlParserCtxt *ctxt = initContext(error, validate);
+        xmlDoc *doc = 0;
+        int options = XML_PARSE_NSCLEAN | XML_PARSE_NOBLANKS;
+
+        if (validate)
+        {
+            options |= XML_PARSE_DTDVALID;
+        }
+
+        if (!ctxt)
+        {
+            xmlSetGenericErrorFunc(0, errorFunctionWithoutOutput);
+            return 0;
+        }
+
+        doc = xmlCtxtReadDoc(ctxt, (const xmlChar *)xmlCode.c_str(), 0, encoding, options);
+        if (!doc || !ctxt->valid)
+        {
+            *error = *errorBuffer;
+        }
+
         xmlSetGenericErrorFunc(0, errorFunctionWithoutOutput);
-        return 0;
+        xmlFreeParserCtxt(ctxt);
+
+        return doc;
     }
 
-    doc = xmlCtxtReadDoc(ctxt, (const xmlChar *)xmlCode.c_str(), 0, 0, options);
-    if (!doc || !ctxt->valid)
+    xmlDoc *XMLDocument::readHTMLDocument(const std::string & htmlCode, const char * encoding, std::string * error)
     {
-        *error = *errorBuffer;
-    }
+        htmlParserCtxt *ctxt = initHTMLContext(error);
+        htmlDocPtr doc = 0;
+        int options = HTML_PARSE_NOWARNING | HTML_PARSE_NOBLANKS | HTML_PARSE_COMPACT;
 
-    xmlSetGenericErrorFunc(0, errorFunctionWithoutOutput);
-    xmlFreeParserCtxt(ctxt);
+        if (!ctxt)
+        {
+            xmlSetGenericErrorFunc(0, errorFunctionWithoutOutput);
+            return 0;
+        }
 
-    return doc;
-}
+        doc = htmlCtxtReadDoc(ctxt, (const xmlChar *)htmlCode.c_str(), 0, encoding, options);
+        if (!doc || !ctxt->valid)
+        {
+            *error = *errorBuffer;
+        }
 
-xmlParserCtxt *XMLDocument::initContext(std::string * error, bool validate)
-{
-    xmlParserCtxt *ctxt;
+        xmlSetGenericErrorFunc(0, errorFunctionWithoutOutput);
+        htmlFreeParserCtxt(ctxt);
 
-    if (errorBuffer)
+        return (xmlDoc *)doc;
+    }
+
+    bool XMLDocument::saveToFile(const std::string & filename, const bool indent) const
     {
-        delete errorBuffer;
+        xmlThrDefIndentTreeOutput(1);
+        return xmlSaveFormatFile(filename.c_str(), document, indent) != -1;
     }
-    errorBuffer = new std::string();
 
-    ctxt = xmlNewParserCtxt();
-    if (!ctxt)
+    bool XMLDocument::saveToHTMLFile(const std::string & filename, const bool indent) const
     {
-        errorBuffer->append(gettext("Cannot create a parser context"));
-        *error = *errorBuffer;
-        return 0;
+        int ret;
+        int options = XML_SAVE_AS_HTML;
+        if (indent)
+        {
+            options |= XML_SAVE_FORMAT;
+        }
+
+        xmlThrDefIndentTreeOutput(1);
+        xmlSaveCtxtPtr ctxt = xmlSaveToFilename(filename.c_str(), 0, options);
+        ret = xmlSaveDoc(ctxt, document);
+        xmlSaveFlush(ctxt);
+        xmlSaveClose(ctxt);
+
+        return ret != -1;
     }
 
-    if (validate)
+    xmlParserCtxt *XMLDocument::initContext(std::string * error, bool validate)
     {
-        ctxt->vctxt.error = (xmlValidityErrorFunc) errorFunction;
+        xmlParserCtxt *ctxt;
+
+        if (errorBuffer)
+        {
+            delete errorBuffer;
+        }
+        errorBuffer = new std::string();
+
+        ctxt = xmlNewParserCtxt();
+        if (!ctxt)
+        {
+            errorBuffer->append(gettext("Cannot create a parser context"));
+            *error = *errorBuffer;
+            return 0;
+        }
+
+        if (validate)
+        {
+            ctxt->vctxt.error = (xmlValidityErrorFunc) errorFunction;
+        }
+
+        xmlSetGenericErrorFunc(ctxt, errorFunction);
+
+        return ctxt;
     }
 
-    xmlSetGenericErrorFunc(ctxt, errorFunction);
+    htmlParserCtxt *XMLDocument::initHTMLContext(std::string * error)
+    {
+        htmlParserCtxt *ctxt;
 
-    return ctxt;
-}
+        if (errorBuffer)
+        {
+            delete errorBuffer;
+        }
+        errorBuffer = new std::string();
 
-void XMLDocument::errorFunction(void *ctx, const char *msg, ...)
-{
-    char str[BUFFER_SIZE];
-    va_list args;
+        ctxt = htmlNewParserCtxt();
+        if (!ctxt)
+        {
+            errorBuffer->append(gettext("Cannot create a parser context"));
+            *error = *errorBuffer;
+            return 0;
+        }
 
-    va_start(args, msg);
-    vsnprintf(str, BUFFER_SIZE, msg, args);
-    va_end(args);
-    errorBuffer->append(str);
-}
+        xmlSetGenericErrorFunc((xmlParserCtxt *)ctxt, errorFunction);
 
-void XMLDocument::errorXPathFunction(void *ctx, xmlError * error)
-{
-    errorXPathBuffer->append(error->message);
-}
+        return ctxt;
+    }
+
+    void XMLDocument::errorFunction(void *ctx, const char *msg, ...)
+    {
+        char str[BUFFER_SIZE];
+        va_list args;
+
+        va_start(args, msg);
+        vsnprintf(str, BUFFER_SIZE, msg, args);
+        va_end(args);
+        errorBuffer->append(str);
+    }
+
+    void XMLDocument::errorXPathFunction(void *ctx, xmlError * error)
+    {
+        errorXPathBuffer->append(error->message);
+    }
 }
index d473b38..efab974 100644 (file)
@@ -67,7 +67,7 @@ public:
      * @param validate a boolean to indicate if the document must be validated in using a DTD
      * @param error a pointer to a string which will receive the error message
      */
-    XMLDocument(const char *path, bool validate, std::string * error, const char * encoding = 0);
+           XMLDocument(const char *path, bool validate, std::string * error, const char * encoding = 0, const bool html = false);
 
     /**
      * Builds a document with a given code
@@ -75,7 +75,7 @@ public:
      * @param validate a boolean to indicate if the document must be validated in using a DTD
      * @param error a pointer to a string which will receive the error message
      */
-    XMLDocument(const std::string & xmlCode, bool validate, std::string * error, const char * encoding = 0);
+    XMLDocument(const std::string & xmlCode, bool validate, std::string * error, const char * encoding = 0, const bool html = false);
 
     /**
      * Builds a simple document
@@ -97,6 +97,20 @@ public:
     }
 
     /**
+     * @param filename the file where to write xml
+     * @param indent if true, the xml is indented
+     * @return true if all is ok
+     */
+    bool saveToFile(const std::string & filename, const bool indent) const;
+
+    /**
+     * @param filename the file where to write xml
+     * @param indent if true, the xml is indented
+     * @return true if all is ok
+     */
+    bool saveToHTMLFile(const std::string & filename, const bool indent) const;
+
+    /**
      * @return the document root
      */
     const XMLElement *getRoot() const;
@@ -136,6 +150,7 @@ public:
 
     const XMLObject *getXMLObjectParent() const;
     const std::string dump(bool indent) const;
+    const std::string dumpHTML(bool indent) const;
     const std::string toString() const;
 
 private:
@@ -179,6 +194,24 @@ private:
     static xmlDoc *readDocument(const std::string & xmlCode, const char * encoding, bool validate, std::string * error);
 
     /**
+     * Reads and parses a document given in a file.
+     * @param filename the file name
+     * @param validate a boolean to indicate if the document must be validated in using a DTD
+     * @param error a string where to write the parsing errors
+     * @return a pointer on a xmlDoc
+     */
+    static xmlDoc *readHTMLDocument(const char *filename, const char * encoding, std::string * error);
+
+    /**
+     * Read and parse a document given in a string.
+     * @param xmlCode the XML code
+     * @param validate a boolean to indicate if the document must be validated in using a DTD
+     * @param error a string where to write the parsing errors
+     * @return a pointer on a xmlDoc
+     */
+    static xmlDoc *readHTMLDocument(const std::string & xmlCode, const char * encoding, std::string * error);
+
+    /**
      * Initializes the context
      * @param error a string where to write the parsing errors
      * @param validate a boolean to indicate if the document must be validated in using a DTD
@@ -186,6 +219,14 @@ private:
      */
     static xmlParserCtxt *initContext(std::string * error, bool validate);
 
+    /**
+     * Initializes the context
+     * @param error a string where to write the parsing errors
+     * @param validate a boolean to indicate if the document must be validated in using a DTD
+     * @return a pointer on a context
+     */
+    static htmlParserCtxt *initHTMLContext(std::string * error);
+
     static std::string * errorBuffer;
     static std::string * errorXPathBuffer;
 };
diff --git a/scilab/modules/xml/tests/unit_tests/htmlRead.dia.ref b/scilab/modules/xml/tests/unit_tests/htmlRead.dia.ref
new file mode 100644 (file)
index 0000000..258bb9b
--- /dev/null
@@ -0,0 +1,19 @@
+// ===========================================================================
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2014 - Scilab Enterprises - Calixte DENIZET
+//
+//  This file is distributed under the same license as the Scilab package.
+// ===========================================================================
+doc = htmlRead(SCI + "/modules/helptools/data/pages/homepage-en_US.html");
+xp = xmlXPath(doc,"//a/@href");
+assert_checktrue(xp.size > 0);
+links = xmlAsText(xp);
+assert_checktrue(or(links=="http://www.scilab.org/"));
+htmlWrite(doc, TMPDIR + "/foo.html");
+xmlDelete(doc);
+doc = htmlRead(TMPDIR + "/foo.html");
+xp = xmlXPath(doc,"//a/@href");
+assert_checktrue(xp.size > 0);
+links2 = xmlAsText(xp);
+assert_checkequal(links, links2);
+xmlDelete(doc);
diff --git a/scilab/modules/xml/tests/unit_tests/htmlRead.tst b/scilab/modules/xml/tests/unit_tests/htmlRead.tst
new file mode 100644 (file)
index 0000000..24b67c4
--- /dev/null
@@ -0,0 +1,20 @@
+// ===========================================================================
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2014 - Scilab Enterprises - Calixte DENIZET
+//
+//  This file is distributed under the same license as the Scilab package.
+// ===========================================================================
+
+doc = htmlRead(SCI + "/modules/helptools/data/pages/homepage-en_US.html");
+xp = xmlXPath(doc,"//a/@href");
+assert_checktrue(xp.size > 0);
+links = xmlAsText(xp);
+assert_checktrue(or(links=="http://www.scilab.org/"));
+htmlWrite(doc, TMPDIR + "/foo.html");
+xmlDelete(doc);
+doc = htmlRead(TMPDIR + "/foo.html");
+xp = xmlXPath(doc,"//a/@href");
+assert_checktrue(xp.size > 0);
+links2 = xmlAsText(xp);
+assert_checkequal(links, links2);
+xmlDelete(doc);
index 39c8478..c647cdf 100644 (file)
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="sci_gateway\cpp\sci_extraction.cpp" />
+    <ClCompile Include="sci_gateway\cpp\sci_htmlDump.cpp" />
+    <ClCompile Include="sci_gateway\cpp\sci_htmlRead.cpp" />
+    <ClCompile Include="sci_gateway\cpp\sci_htmlReadStr.cpp" />
+    <ClCompile Include="sci_gateway\cpp\sci_htmlWrite.cpp" />
     <ClCompile Include="sci_gateway\cpp\sci_insertion.cpp" />
     <ClCompile Include="sci_gateway\cpp\sci_percent_c_i_XMLAttr.cpp" />
     <ClCompile Include="sci_gateway\cpp\sci_percent_foo_i_XMLList.cpp" />
index 927c63d..243f220 100644 (file)
     <ClCompile Include="sci_gateway\cpp\sci_xmlAppend.cpp">
       <Filter>Source Files</Filter>
     </ClCompile>
+    <ClCompile Include="sci_gateway\cpp\sci_htmlDump.cpp">
+      <Filter>Source Files</Filter>
+    </ClCompile>
+    <ClCompile Include="sci_gateway\cpp\sci_htmlRead.cpp">
+      <Filter>Source Files</Filter>
+    </ClCompile>
+    <ClCompile Include="sci_gateway\cpp\sci_htmlReadStr.cpp">
+      <Filter>Source Files</Filter>
+    </ClCompile>
+    <ClCompile Include="sci_gateway\cpp\sci_htmlWrite.cpp">
+      <Filter>Source Files</Filter>
+    </ClCompile>
   </ItemGroup>
 </Project>
\ No newline at end of file