source: trunk/src/objects/RpLibObj.cc @ 1568

Last change on this file since 1568 was 1566, checked in by dkearney, 15 years ago

updates to Rappture::Library and Rappture::Number objects to demo how the library should store objects and generate the xml for those objects upon request. opting for configure() and dump() functions for objects. the object should know how to configure it self from a string of xml, tree object or (in the future) blob of hdf5. added some rudimentary examples of using the library object.

File size: 4.2 KB
Line 
1/*
2 * ----------------------------------------------------------------------
3 *  Rappture Library Source
4 *
5 * ======================================================================
6 *  AUTHOR:  Derrick S. Kearney, Purdue University
7 *  Copyright (c) 2005-2009  Purdue Research Foundation
8 *
9 *  See the file "license.terms" for information on usage and
10 *  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11 * ======================================================================
12 */
13
14#include "expat.h"
15#include "RpParserXML.h"
16#include "RpLibObj.h"
17#include "RpBuffer.h"
18#include "RpEntityRef.h"
19#include "RpEncode.h"
20#include "RpTree.h"
21
22#include "RpNumber.h"
23
24#include <stdlib.h>
25#include <time.h>
26#include <cctype>
27
28using namespace Rappture;
29
30Library::Library ()
31{
32    __libInit();
33}
34
35Library::~Library ()
36{
37    __libFree();
38}
39
40void
41Library::__libInit()
42{
43    _objList = Rp_ChainCreate();
44    return;
45}
46
47void
48Library::__libFree()
49{
50    if (_objList != NULL) {
51        Rp_ChainLink *l = NULL;
52        Rappture::Object *objVal = NULL;
53        l = Rp_ChainFirstLink(_objList);
54        while(l) {
55            objVal = (Rappture::Object *) Rp_ChainGetValue(l);
56            delete objVal;
57            objVal = NULL;;
58            l = Rp_ChainNextLink(l);
59        }
60        Rp_ChainDestroy(_objList);
61        _objList = NULL;
62    }
63    return;
64}
65
66Outcome &
67Library::loadXml (const char *xmltext)
68{
69    Rp_ParserXml *p = NULL;
70
71    _status.addContext("Rappture::Library::loadXml");
72
73    if (xmltext == NULL) {
74        _status.addError("xmltext was NULL");
75        return _status;
76    }
77
78    // parse xml file
79    p = Rp_ParserXmlCreate();
80    if (p == NULL) {
81        _status.addError("error while creating xml parser");
82        return _status;
83    }
84    Rp_ParserXmlParse(p, xmltext);
85    // FIXME: add error check for Rp_ParserXmlParse
86
87    // convert xml tree into chain of rappture objects
88    Rp_Chain *tmpObjList = Rp_ChainCreate();
89    __parseTree2ObjectList(p,tmpObjList);
90    if (!_status) {
91        __libFree();
92        _objList = tmpObjList;
93    }
94
95    return _status;
96}
97
98Outcome &
99Library::loadFile (const char *filename)
100{
101    _status.addContext("Rappture::Library::loadFile");
102
103    // load xml file
104    Rappture::Buffer fileBuf;
105    if (!fileBuf.load(_status, filename)) {
106        return _status;
107    }
108
109    loadXml(fileBuf.bytes());
110
111    return _status;
112}
113
114const char *
115Library::xml() const
116{
117    Rp_ParserXml *p = Rp_ParserXmlCreate();
118    Rp_ChainLink *l = NULL;
119
120    l = Rp_ChainFirstLink(_objList);
121
122    while (l != NULL) {
123        Rappture::Object *o = (Rappture::Object *) Rp_ChainGetValue(l);
124        o->dump(RPCONFIG_TREE,(ClientData)p);
125        l = Rp_ChainNextLink(l);
126    }
127
128    return Rp_ParserXmlXml(p);
129}
130
131void
132Library::__parseTree2ObjectList(
133    Rp_ParserXml *p,
134    Rp_Chain *retObjList)
135{
136    _status.addContext("Rappture::Library::__parseTree2ObjectList");
137
138    if (p == NULL) {
139        _status.addError("parser is NULL");
140        return;
141    }
142
143    if (retObjList == NULL) {
144        _status.addError("return object list is NULL");
145        return;
146    }
147
148    // get the children nodes of "tool", "input", and "output"
149    Rp_Chain *children = Rp_ChainCreate();
150    // Rp_ParserXmlChildren(p, "tool", NULL, children);
151    Rp_ParserXmlChildren(p, "input", NULL, children);
152    Rp_ParserXmlChildren(p, "output", NULL, children);
153
154    Rp_ChainLink *l = Rp_ChainFirstLink(children);
155    while (l != NULL) {
156        Rp_TreeNode child = (Rp_TreeNode) Rp_ChainGetValue(l);
157        const char *label = Rp_TreeNodeLabel(child);
158
159        Rp_ParserXmlBaseNode(p,child);
160
161        // find the correct object to create
162        // Rappture::Object *nobj = NULL;
163        if (('n' == *label) && (strcmp(label,"number") == 0)) {
164            Rappture::Number *obj = new Rappture::Number();
165            obj->configure(RPCONFIG_TREE,(void*)p);
166            Rp_ChainAppend(retObjList,(void*)obj);
167        } else {
168            _status.addError("unrecognized object type: %s",label);
169        }
170
171        l = Rp_ChainNextLink(l);
172    }
173
174    // FIXME: should return the base node to the previous base
175    Rp_ParserXmlBaseNode(p,NULL);
176
177    return;
178}
179
180Outcome &
181Library::outcome() const
182{
183    return _status;
184}
185
186const Rp_Chain *
187Library::contains() const
188{
189    return _objList;
190}
Note: See TracBrowser for help on using the repository browser.