Author: Zhaoqing Wang and Harry H. Cheng
To overcome the write-to-write restrictions in the XML-based application, you can use an embedded C / C interpreter.
XML is changing the world of information sharing and information exchange. The XML standard allows users to define their own data and documents by open, independent platforms, suppliers and languages for electronic data exchange, data management, and publishing tasks. Its self-description flexible tag (used to mark the beginning and end of the relevant data block) Construct a related data object (called elements - may be a database, webpage fragment (link, number, metadata, text, and images) or spreadsheet The contents of the content). Since XML uses content package separation content and representation, the structure makes data reusability, simple deportability, and reconfiguibility.
Since XML is a well-format tag language, you need to use programming techniques to perform and process related tasks, such as analysis, generation, operation, and verify XML data. To this end, XML-based applications are usually written using C and C . But these two languages have a certain issue - although the C / C code is theoretically portable, the associated compilation and linking process is unavailable. Therefore, most C / C code need to compile and link through different methods on different platforms. Really portable C / C code is not dynamically generated and executed; therefore, in these cases, XML data is typically processed using Java and Java virtual machines.
This article will introduce and demonstrate the Ch language environment (download) and CH package for Oracle C / C XML Developer's Kit (XDK) (download) for cross-platform scripting, shell programming, numerical calculations, network computing, and embedded scripts Embedded C / C interpreter. CH XML Package is used to integrate portable XML data and portable C / C code; it makes a large number of existing applications and technologies based on C / C with XML documents to seamless work.
characteristic
The CH locales with the C-compatible CH locales are an embedded interpreter for cross-platform C / C scripts. CH supports all functions in the ISO 1990 C standard and most of the new features in ISO C99, such as complex numbers, variable length arrays (VLAs), binary constants, and IEEE 754 floating point algorithms. The language also supports classes, objects, and packages in C to implement object-based programming. The main features include:
Interpretation: The C program can be performed in CH without having to make a boring compilation / link / execution / debug loop. Interactivity: Users can run C code in interactive, and enter code by line. Therefore, CH can be easily used to test new functions. It is also an excellent real-time interaction computing environment. Embedability: CH can be embedded in other applications, hardware, and handheld devices, allowing users do not have to develop and maintain special scripting language in different platforms. Value calculation: In addition to supporting all C types (such as CHAR, ITIT, DOUBLE, and new COMPLEX and VLA), CH will also calculate arrays as a primary object. Many advanced numerical functions (such as differential equations, integration, Fourier analysis, and two-dimensional / three-dimensional drawings) make CH a powerful locale for solving engineering and scientific issues. Ultra-high: CH makes up for the gap between the low-level language and the UHF language (VHLL). As a super-set, CH contains low-level language functions. But as VHLL, it enables the program to easily transplant between different platforms. Object-based: CH supports classes, objects, and encapsulation in C , enabling you to use data abstraction and information hide and simplified I / O processing based on object-based programming. Text Processing: CH has advanced text processing functions, such as built-in string data types and foreach-loop. These features are especially useful for system management, shell programming, and web-based applications. This feature can also be used to develop portable code to process portable data. Cross-platform Shell: For easy users, CH provides a universal shell. It can be used as a login command shell, similar to the C-shell, BSHELL, BASH, or Korn shell, and MS-DOS shell in Windows. Safe network calculation: Safety CH is a new design using different security layers (such as sandbox, programmer / management control, hidden pointer, limited function, string type automatic memory management, and auto array binding check), Effectively solve the security problem of network computing. Portability: The CH program can be run in different platforms, including Windows and UNIX. Programmers can develop and maintain programs on a computer, then deploy these programs to all platforms supported by CH. A wide range of libraries: All existing C libraries and modules can be part of the CH library. Therefore, the potential of the CH library is almost unlimited. For example, CH supports POSIX, TCP / IP socket, Winsock, Win32, X11 / Motif, GTK , OpenGL, ODBC, LAPACK, LDAP, NAG Statistics, Intel OpenCV (used for computer version and image processing), National Instrument Ni-Daq and Ni-Motion, etc. Enable Web: By developing modules for web servers (such as the public gateway interface (CGI) class), CH allows for quick development and deployment of web-based applications and services. structure
CH XML Package for Oracle XDK content (available for Windows) includes:
Choraxml / DEMOS - Original Oracle XDK presentation code written in C / C , can run Choraxml / DL-CH dynamic load library Choraxml / lib-chrax ML / INCLUDE-CH master file choraxml / bin-oracle xdk dynamic library And command choraxml / src- is used to develop the source code for the CH XML package.
To install a CH XML for Oracle XDK, perform the following steps. Run CH XML needs to install CH. If you are not installed in your computer, download and install CH from http://www.softintegration.com. Start CH. From http://iel.ucdavis.edu/projects/chxml/ download file choraxml_v1.0.0.window.tar.gz. Use the following command to decompress the ZIP file in the CH command shell. gzip -cd choraxml_v1.0.0.window.tar.gz | Tar -XVF -
Follow the instructions in the readme file to install the choraxml package and set the environment variable ORA_NLS33 and ORA_XML_MESG (these two variables are variables required for Oracle XDK). Go to Demos directory and type file name (such as domnamespace.c) to run the program by explaining.
Integrated Oracle XDK with CH
One of the most important features of CH is that the binary static or dynamic C / C library is easily integrated with the locale without having to recompile. For example, using CH XML Package for Oracle C / C XDK, the C / C application using the Oracle XML library can be performed through the interpreter cross-platform. You can also perform them via the Internet.
Integrate with DOM API
The XML Document Object Model (DOM) API creates a tree structure in memory to store data for the XML document. Typically, there is no callback function based on the DOM-based XML C / C application. But in the Oracle XDK's DOM API creates a relatively simple relatively simple.
Figure 1 demonstrates how Oracle XDK is integrated with CH. The architecture contains three layers: top layer is the user's application, which is an existing application using the C / C XML library. The intermediate layer is a CH packager, which is a binary function and a middleware between the interpretation functions based on this article. We developed a CH packaging or CH binding for Oracle XDK; source code for developing a CH packager is also provided in distribution. This open source can be used to transplant CH to different platforms and different versions of XDK. The bottom layer is the original C / C binary code provided by XDK.
Figure 1: Architecture of DOM and CH integration
Through the CH binding of Oracle XDK, the user's application can be executed by explaining the user's application without having a cross-platform compilation: When the XML application calls the XML function, the program will call the XML-CH function. The XML-CH function typically calls the XML CH Dynamic Library (CHDL) function, which then calls the corresponding binary function in the Oracle XDK. For example, in the application of DomNameSpace.c, the XMLPARSE () function will be called. XMLPARSE.CHF will be used when the application domnamespace.c starts. XmlParse.chf calls the binary function XMLPARSE_CHDL () by dynamic loading library (which calls binary functions XMLPARSE ()). The sample code of XMLPARSE.CHF is listed below.
UWORD XMLPARSE (Xmlctx * CTX, ORATEXT * URI, ORATEXT * INCODING, UB4 FLAGS) {
Void * fptr;
UWord RetVal;
FPTR = DLSYM (_choraxml_handle, "xmlparse_chdl");
IF (fptr == null) {
FPRINTF (_stderr, "Error: SDLSYM ():% S / N", __FUNC___, DLERROR ()); Return -1;
}
Dlrunfun (FPTR, & RetVal, XMLPARSE, CTX, URI, Incoding, Flags);
Return RetVal;
}
Among them, _choraxml_handle is the file handle of the XML CH dynamic load library. Function DLSYM () Gets the address of the XMLPARSE_CHDL () function in the dynamic load library. The function dlrunfun () calls XMLPARSE_CHDL () in accordance with this address.
The following is the source code of the XMLPARSE_CHDL () function in the XML CH dynamic load library.
Exportch UWORD XMLPARSE_CHDL (Void * VARG) {
VA_LIST AP;
XMLCTX * CTX;
Const orabext * URI;
Const orabext * incoding;
UB4 FLAGS;
UWord RetVal;
/ / Get all parameters passing to the binary function
CH_VASTART (AP, VARG);
CTX = CH_VAARG (AP, XMLCTX *);
URI = CH_VAARG (AP, Const ORatext *);
INCODING = Ch_vaarg (AP, Const ORatext *);
Flags = ch_vaarg (AP, UB4);
// Call the binary XMLPARSE () function
Retval = XMLPARSE (CTX, URI, Incoding, Flags);
CH_VAEND (AP);
Return RetVal;
}
Function XMLPARSE_CHDL () Gets its parameter value from the XMLParse () function in the user application layer through the function ch_vaarg (). It then calls the binary function XMLPARSE () and returns its value.
Integrate with SAX API using a callback function
SIMPLE API for XML (SAX) uses an event-based model to process an XML document; the SAX-based analyzer is called the function or c in C when the tag (such as start tag or end tag) is encountered. The SAX application defines the callback function for the XML document. The CH packaging provides registration for such callback functions (as shown in Figure 2) When an application calls a function (with parameters with a function (a callback function defined in the user space), the application will call the callback function. Passage to the CH function as the parameter of the function. However, the callback function should be registered in the CH packaging program. The CH function passes the address to the CHDL function to register the callback function in the CH package. When an event is encountered, the XML bin library function will eventually call the callback function.
Figure 2: Architecture of SAX with a callback function with CH
For example, the SAX API contains functions XSlsetOutputSax (xslctx * xslssctx, xmlsaxcb * s). The second parameter (ie, the structural pointer) of the type "XMLSAXCB *" contains a member field of the function pointer. Function file xslsetOutputSax.chf is similar to XmlParse.chf in the previous section. The source code of the function XSlsetOutputSax_ChDL () is listed below.
......
Static Xmlsaxcb saxcb_ch;
....
Static Sword StartDocument_CHDL_Funarg (Void * CTX);
Static void * startdocument_chdl_funptr = null;
....
Exportch UWORD XSLSETOUTPUTSAX_CHDL (VOID * VARG) {VA_LIST AP;
Xslctx * xslssctX;
Xmlsaxcb * saxcb;
XMLSAXCB * SAXCB_TMP;
UWord RetVal;
....
SAXCB = CH_VAARG (AP, Xmlsaxcb *);
....
IF (SAXCB! = NULL)
{
....
STARTDocument_CHDL_FUNPTR = (void *) Saxcb-> startDocument
IF (saxcb-> startdocument! = null)
SAXCB_CH.StartDocument = startDocument_chdl_funarg;
....
}
SAXCB_TMP = & SAXCB_CH;
Retval = xslsetOutputSax (xslssctx, saxcb_tmp);
CH_VAEND (AP);
Return RetVal;
}
Static Sword StartDocument_Chdl_Funarg (Void * CTX) {
Sword retval = 0;
Ch_callfuncbyaddr (null, startdocument_chdl_funptr, & retval, ctx);
Return RetVal;
}
Function startDocument_chdl_funarg () is used to register the callback function when the event "starting document" is encountered. STARTDocument_CHDL_FUNPTR Gets the pointer to the user-defined callback function via ch_vaarg (). The function ch_callfuncbyaddr () calls the callback function in the user application layer.
Integrate with ODBC with Oracle Database
Using XML applications often require access to databases such as Microsoft Access or Oracle Database. This task can be easily completed using a CH ODBC kit (shown in Figure 3). The application can call the XML API for document processing, and invoke OBDC to direct and interpretative database access.
Figure 3: Integration with the database
For example, the following CH code is connected to the database using ODBC. It inserts and deletes records.
/ ************************************************** **********************
* This is an example of using an ODBC API in CH.
* Before running this example, you must
* Create a database in Microsoft Access, then
* In Windows ODBC Driver Manager
* The database is registered as "Test.mdb".
*********************************************************** ********************** /
#include
#include
#include
#define test_len 10
#DEFINE NAME_LEN 30
Int main () {
Henv Henv;
HDBC HDBC;
Uchar szdsn [SQL_MAX_DSN_LENGTH 1];
Uchar szdescription [255];
Sword CBDSN;
Sword cbdescription;
HSTMT HSTMT;
Retcode RC;
Uchar sztest [test_len 1];
Uchar ** szgetdata;
SDWORD INUMROWS = 0; SDWORD CBTEST = SQL_NTS;
SDWORD AGE = 29;
Uchar szname [name_len];
SDWORD SAGE;
SDWORD CBNAME, CBAGE
Sqlallocenv (& HENV);
SQLACCONNECT (HENV, & HDBC);
Rc = SqlDataSource (HENV, SQL_FETCH_FIRST, SZDSN, SIZEOF (SZDSN),
& CBDSN, Szdescription, Sizeof (Szdescription),
& cbdescription);
Printf ("% S / N", SZDESCRIPTION;
Printf ("% s / n", szdsn);
// Connect to the data source
Rc = SqlConnect (HDBC, "TEST", 5, NULL, SQL_NTS, NULL, SQL_NTS);
IF (rc! = SQL_SUCCESS)
Printf ("INVALID HANDLE, Connect Failed / N);
Rc = SQLAlocStmt (HDBC, & HSTMT);
IF (rc! = SQL_SUCCESS)
Printf ("INVALID HANDLE, Allocate Statement Failed / N");
// Insert a record
Rc = SQLPRepare (HSTMT, "INSERT INTO INFO (NAME, AGE) VALUES (?,?)",
SQL_NTS);
IF (rc! = SQL_SUCCESS)
Printf ("INVALID HANDLE, INSERT FAILED / N");
// Binding will be used as input data
Rc = SQLBINDPARAMETER (HSTMT, 1, SQL_PARAM_INPUT, SQL_C_CHAR,
SQL_CHAR, TEST_LEN, 0, SZTEST, 0, & CBTEST; // Bind SzTest to Name
Rc = SQLBINDPARAMETER (HSTMT, 2, SQL_PARAM_INPUT, SQL_C_USHORT,
SQL_NUMERIC, 10, 0, & AGE, 0, & CBTEST;
// Bind the variable AGE to record agnge
IF (rc! = SQL_SUCCESS)
Printf ("INVALID HANDLE, BIND FAILED / N);
STRCPY (SZTEST, "You");
RC = SQLEXECUTE (HSTMT);
IF (rc! = SQL_SUCCESS)
Printf ("INVALID HANDLE, EXECUTE FAILED / N");
/ / Connect the statement to the original database
Rc = SQLAlocStmt (HDBC, & HSTMT);
/ / Delete Records Name "You"
Rc = SqlpRepare (HSTMT, "Delete from info where name = 'you'", sql_nts);
IF (rc! = SQL_SUCCESS)
Printf ("INVALID HANDLE, DELETE FAILED / N");
RC = SQLEXECUTE (HSTMT);
IF (rc! = SQL_SUCCESS)
Printf ("INVALID HANDLE, EXECUTE FAILED / N"); SQLFreeStmt (HSTMT, SQL_Close);
Sqldisconnect (HDBC);
SQLFreeConnect (HDBC);
SQLFreeEnv (HENV);
}
Application example
Oracle XDK contains multiple C / C demos for handling XML documents; using CH Package, all of these C / C programs can be performed by explaining without compilation. For example, Figure 4 shows the interactive execution of the C program DomNameSpace.c in the CH command shell. When you type a file name in the command shell, the program is executed and the output as shown in Figure 4 is displayed. The program DomNamespace.c can also be executed from the IDE of the CH. Multiple IDEs (for example, UltraEdit and SliciDIT) can easily use and run the Ch program.
Figure 4: Interactive execution of C program domnamespace.c.
If CH is nested as a scripting engine in the application, the program will be able to use the application dynamically controlled XML document. At this time, the program will be processed as a C script.
XML documents are widely used in web-based applications and integration. Like Perl, Python or PHP, interpretive C / C scripts can be used to create dynamic web pages in CH. The CH CGI tool bag contains four class -Request, Response, Server, and cookie amic API similar to Active Serve Server Pages and JavaServer Pages. For web-based applications, the CH CGI script usually has a file extension .ch. For example, you can start programs domnamespace.ch by clicking the hyperlink in a web browser. Subsequently, it can print and display the program results inside the web browser. The program domnamespace.ch is modified based on program DomnameSpace.c and contains the following changes:
#include
.... // is the same as DomNamespace.c
int main ()
{
XMLCTX * CTX;
Class CRESPONSE RESPONSE
Response.setContentType ("text / plain");
Response.begin ();
... // The same as DomNameSpace.c
...
Response.end ();
Other resources
CH Professional Edition and Toolkit C / C / C / C XML Developer Toolkit Oracle XDK Product Center Oracle XDK Download
RETURN (Ecode? -1: 0);
}
Class Response package HTTP style response. Response :: beGin () begins to send the output. CRESPONSE :: end () ends the standard output.
Figure 5 shows a demo page using a WEB-based application using CH XML Package. The "Function" area contains the CGI demo link; when you click a link (such as "DomnameSpace.ch"), the corresponding CGI program will be performed.
Figure 5: CH XML Package for Oracle XDK Demo Page
Whether in the CH command shell (as shown in FIG. 4) or in the IDE (as shown in Figure 6), the output is the same.
Figure 6: Output Conclusions of C Programs DomNamespace.ch via the Web
Integrated portable XML data has excellent potential for portable C / C code in effectively processing documents and data. Using the C / C interpreter CH and CH XML packets, applications that use Oracle C / C XDK can be performed without having to perform a bored compilation / link / execution / debug loop by explaining. In addition, since CH is an embedded C / C interpreter, the application can use the CH embedded as a scripting engine to process an XML document using the C / C script.
Zhaoqing Wang
(zqwang@iel.ucdavis.edu) is a postdoctoral post-doctoral in the integrated engineering laboratory of Davis Gas University. He specializes in network computing and open architecture software integration.
Harry H. Cheng is a professor and director of the Integrated Engineering Laboratory of Machinery and Aviation Engineering Departments in Davis Ga.