[rkward-cvs] SF.net SVN: rkward: [1861] trunk/rkward

tfry at users.sourceforge.net tfry at users.sourceforge.net
Sun May 6 13:53:23 UTC 2007


Revision: 1861
          http://svn.sourceforge.net/rkward/?rev=1861&view=rev
Author:   tfry
Date:     2007-05-06 06:53:22 -0700 (Sun, 06 May 2007)

Log Message:
-----------
Add includes and snippets. Tested in plot t distribution plugin

Modified Paths:
--------------
    trunk/rkward/ChangeLog
    trunk/rkward/rkward/misc/xmlhelper.cpp
    trunk/rkward/rkward/misc/xmlhelper.h
    trunk/rkward/rkward/plugins/distributions/plot_t_distribution.xml

Modified: trunk/rkward/ChangeLog
===================================================================
--- trunk/rkward/ChangeLog	2007-05-04 10:39:44 UTC (rev 1860)
+++ trunk/rkward/ChangeLog	2007-05-06 13:53:22 UTC (rev 1861)
@@ -1,3 +1,4 @@
+- support for including files and snippets in xml files		TODO: document, use
 - all file selection line edits gain filename-completion
 - add a basic file selector window
 - fixed: starting the scatterplot plugin in wizard mode would crash rkward

Modified: trunk/rkward/rkward/misc/xmlhelper.cpp
===================================================================
--- trunk/rkward/rkward/misc/xmlhelper.cpp	2007-05-04 10:39:44 UTC (rev 1860)
+++ trunk/rkward/rkward/misc/xmlhelper.cpp	2007-05-06 13:53:22 UTC (rev 1861)
@@ -21,6 +21,8 @@
 
 #include <qstringlist.h>
 #include <qfile.h>
+#include <qfileinfo.h>
+#include <qdir.h>
 
 #include "../debug.h"
 
@@ -47,7 +49,7 @@
 	return static_xml_helper;
 }
 
-QDomElement XMLHelper::openXMLFile (const QString &filename, int debug_level) {
+QDomElement XMLHelper::openXMLFile (const QString &filename, int debug_level, bool with_includes, bool with_snippets) {
 	RK_TRACE (XML);
 
 	int error_line, error_column;
@@ -64,9 +66,95 @@
 	}
 	f.close();
 
-	return doc.documentElement ();
+	QDomElement ret = doc.documentElement ();
+	if (with_includes) {
+		XMLChildList includes = nodeListToChildList (doc.elementsByTagName ("include"));
+		for (XMLChildList::const_iterator it = includes.constBegin (); it != includes.constEnd (); ++it) {
+			// resolve the file to include
+			QDomElement el = *it;
+
+			QString inc_filename = getStringAttribute (el, "file", QString::null, DL_ERROR);
+			QDir base = QFileInfo (filename).dir (true);
+			inc_filename = base.filePath (inc_filename);
+
+			// import
+			QDomElement included = openXMLFile (inc_filename, debug_level, true, false);
+			QDomElement copied = doc.importNode (included, true).toElement ();
+
+			// insert everything within the document tag
+			replaceWithChildren (&el, copied);
+		}
+	}
+
+	if (with_snippets) {
+		return (resolveSnippets (ret));
+	}
+
+	return (ret);
 }
 
+void XMLHelper::replaceWithChildren (QDomNode *replaced, const QDomElement &replacement_parent) {
+	RK_TRACE (XML);
+	RK_ASSERT (replaced);
+
+	QDomNode parent = replaced->parentNode ();
+	XMLChildList replacement_children = getChildElements (replacement_parent, QString::null, DL_WARNING);
+	for (XMLChildList::const_iterator it = replacement_children.constBegin (); it != replacement_children.constEnd (); ++it) {
+		parent.insertBefore (*it, *replaced);
+	}
+	parent.removeChild (*replaced);
+}
+
+XMLChildList XMLHelper::nodeListToChildList (const QDomNodeList &from) {
+	RK_TRACE (XML);
+
+	int count = from.count ();
+	XMLChildList ret;
+	for (int i = 0; i < count; ++i) {
+		ret.append (from.item (i).toElement ());
+	}
+	return ret;
+}
+
+QDomElement XMLHelper::resolveSnippets (QDomElement &from_doc) {
+	RK_TRACE (XML);
+
+	XMLChildList refs = nodeListToChildList (from_doc.elementsByTagName ("insert"));
+	int ref_count = refs.count ();
+
+	if (!ref_count) {	// nothing to resolve
+		return (from_doc);
+	}
+
+	QDomElement snippets_section = getChildElement (from_doc, "snippets", DL_ERROR);
+	XMLChildList snippets = getChildElements (snippets_section, "snippet", DL_ERROR);
+
+	for (XMLChildList::const_iterator it = refs.constBegin (); it != refs.constEnd (); ++it) {
+		QDomElement ref = *it;
+		QString id = getStringAttribute (ref, "snippet", QString::null, DL_ERROR);
+		displayError (&ref, "resolving snippet '" + id + "'", DL_DEBUG, DL_DEBUG);
+
+		// resolve the reference
+		QDomElement snippet;
+		for (XMLChildList::const_iterator it = snippets.constBegin(); it != snippets.constEnd (); ++it) {
+			if (getStringAttribute (*it, "id", QString::null, DL_ERROR) == id) {
+				snippet = *it;
+				break;
+			}
+		}
+		if (snippet.isNull ()) {
+			displayError (&ref, "no such snippet '" + id + "'", DL_ERROR, DL_ERROR);
+		}
+
+		// now insert it.
+		replaceWithChildren (&ref, snippet.cloneNode (true).toElement ());
+	}
+
+	qDebug ("%s", from_doc.ownerDocument().toString ().latin1 ());
+
+	return from_doc;
+}
+
 XMLChildList XMLHelper::getChildElements (const QDomElement &parent, const QString &name, int debug_level) {
 	RK_TRACE (XML);
 

Modified: trunk/rkward/rkward/misc/xmlhelper.h
===================================================================
--- trunk/rkward/rkward/misc/xmlhelper.h	2007-05-04 10:39:44 UTC (rev 1860)
+++ trunk/rkward/rkward/misc/xmlhelper.h	2007-05-06 13:53:22 UTC (rev 1861)
@@ -47,9 +47,14 @@
 When calling this function, highestError () will be reset to 0.
 @param filename the name of the file to parse
 @param debug_level level of debug message to generate if opening/parsing fails
+ at param with_includes should be helper take care of resolving "include" elements?
+ at param with_snippets should be helper take care of resolving "insert" elements?
 @returns the document-element of the file. */
-	QDomElement openXMLFile (const QString &filename, int debug_level);
+	QDomElement openXMLFile (const QString &filename, int debug_level, bool with_includes=true, bool with_snippets=true);
 
+/** resolve "snippet" elements in the given element. It is assumed that the from element will contain a "snippets" section, i.e. it is generally a document. */
+	QDomElement resolveSnippets (QDomElement &from);
+
 /** returns all (direct) child elements with a given tag-name of the given parent
 @param parent the element whose children to return
 @param name the tag-name to look for (if none given, will return all children)
@@ -137,6 +142,9 @@
 /** @returns a pointer to the default instance of XMLHelper (if none has been created so far, this will happen automatically. */
 	static XMLHelper *getStaticHelper ();
 private:
+/** copy the node list into a child list. The main effect is that a child list is not updated according to document changes */
+	XMLChildList nodeListToChildList (const QDomNodeList &from);
+	void replaceWithChildren (QDomNode *replaced, const QDomElement &replacement_parent);
 	int highest_error;
 	static XMLHelper *static_xml_helper;
 	QString filename;

Modified: trunk/rkward/rkward/plugins/distributions/plot_t_distribution.xml
===================================================================
--- trunk/rkward/rkward/plugins/distributions/plot_t_distribution.xml	2007-05-04 10:39:44 UTC (rev 1860)
+++ trunk/rkward/rkward/plugins/distributions/plot_t_distribution.xml	2007-05-06 13:53:22 UTC (rev 1861)
@@ -3,10 +3,11 @@
 	<code file="plot_t_distribution.php" />
 	<help file="plot_t_distribution.rkh" />
 	<logic>
-		<convert id="dodist" mode="equals" sources="function.string" standard="pt" />
-		<connect client="lower.enabled" governor="dodist" />
-		<set id="plotoptions.allow_log" to="false"/>
+		<insert snippet="common_logic"/>
 	</logic>
+	<snippets>
+		<include file="plot_dist_common.xml"/>
+	</snippets>
 	<dialog label="Plot Student T probabilities" >
 		<tabbook>
 			<tab label="Plot Student T probabilities" >
@@ -29,15 +30,7 @@
 						</row>
 					</column>
 					<column>
-						<radio id="function" label="Choose type of function plot" >
-							<option value="dt" label="Plot density function" />
-							<option value="pt" label="Plot distribution" />
-						</radio>
-						<checkbox id="log" label="Logarithmic" value="1" value_unchecked="0"/>
-						<checkbox id="lower" label="Lower Tail" value="1" value_unchecked="0" checked="true"/>
-						<stretch/>
-						<preview id="preview" label="Preview"/>
-						<embed id="plotoptions" component="rkward::plot_options" as_button="true" label="Plot Options" />
+						<insert snippet="common_options"/>
 					</column>
 				</row>
 			</tab>


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the rkward-tracker mailing list