Establish a source code generation template using XML and XSL

xiaoxiao2021-03-06  73

1 Introduction

How to deal with complex issues encountered in programming is one of the biggest challenges that IT people often encounter. So we need a variety of tools to help us better understand and manage complexity in software development. Such tools not only help us create better program source code, but also improve software development efficiency. The source code generator is a tool that can generate complex program source code from a simple model. Therefore, the source code generator is an ideal candidate tool that helps us properly handle software complexity.

XML is a W3C (World Wide Web Consortium) designed to describe the public standard of structure data. XSL (Extensible Stylesheet Language) is a language used to convert and format XML. If XML is a tool for expressing information, XSL is a language used to operate this information.

XML and XSL are due to the exchange of internet exchanges and display data. Another reason for the universal XML in the industrial community is that this technology can often provide unexpected solutions for many complex problems. But now about XSL's most striking secrets is probably it can be used to create your own source generator. To create a source code generating template with XML and XSL may be another attractive application in both technologies.

We will explore the possibility of using XML and XSL creation source generators through a simple instance. XML is used to describe the conceptual model of the problem, XSL read information from the XML model and generates a program source code.

Here we assume that readers are familiar with what is XML and XSL's basics. Many books have been described in detail, and readers can find it.

2, source code generator

2.1, how to handle complexity

Complex problems often require complex solutions and cause complex source code. Although a good design can lead to a relatively simple and easy to understand, its application has its limitations. The information contained in the conceptual model is mixed with the language of the language to hinder the design idea hidden behind the program source code. This makes it more difficult to convert demand into program source code, but also affect implementation. One process for this problem is to separate the conceptual model with the process of converting this concept model into program source code, and use a tool generating source code to generate final code.

The first type of source code generator requires two input: describes the information model of the problem area and the processing instruction required to generate the source code (see Figure 1). An overall model for describing the information model structure is also necessary. This overall model is a model of the model or information about the information. The second type of source code generator has a more refined architecture. It can generate an intermediate design model and can generate the final source code after iteration of the intermediate design model (see Figure 2).

The separated information model allows us to map business needs into model elements. On the other hand, the separated implementation logic makes it easier to modify the implementation. These modifications can be either small to a change in programming style, or may be large to another to implement language conversion. Any repetition in the code does not affect the information model, which is logical.

2.2, source code generator and clip

Everyone agrees that scraps are not available in programming. Because it causes code repetition, it is more difficult to maintain software maintenance. Separation of duplicate code is another function or class is a method of comparing. However, scrapbooks in real life are not only a fast, "dirty" source code, but sometimes is the only choice.

When processing source code reuse, not all programming languages ​​are the same. C is unpredictable to provide the most effective source code reuse tools: inheritance, template, and macro. But even this powerful language is inevitably avoided. For example, a class method is required to declare in the header file. H. This has caused two places to be involved in the change of any type of method signature (Method Signature).

This is just the corner of the iceberg. A larger source code repetition is derived from the transformation of the conceptual model to a software program, not all of the unbearable messages can always map to a topination. This causes the code that represents the same information repeatedly in different places. The repetition of the source code makes the program more difficult to understand and maintain.

As an example, let's consider only a few attributes (Property) Employee classes. The concept model of this class is shown in Figure 3. According to the usual program habits, the attributes of a class are often implemented through a private (private) and corresponding publication (public) methods GET and SET. The final design model is also found in Figure 3 in Figure 3. In this method, the name and type of attributes have to appear in several places. Other languages, such as Microsoft C #, by introducing new language characteristics to dominate the properties of the class. This makes the realization of class properties less dependent on scraps. However, any language is impossible to increase the desired characteristics in order to eliminate scraps and unlimited expansion.

Another source that causes information repetition is due to the continuous proposal of product related issues, the new code is constantly added to an originally simple class prototype. Real product code often involves some new error handling, unit testing, and a large number of instructions. A simple class may need to be wrapped in the command line or COM component. As an example, in the COM, as an example, on the one hand, information about the class must be repeated in the IDL file, and on the other hand, support related to COM must also be added. A COM component with a GUI interface requires a Visual Basic code to display and operate the functions. Required with the database, you need to establish a library table (SELECT), Update, Insert, Delete (Select), Update, Insert, Delete (Select), Update, Insert, Delete. In the end, a simple class in the real world may need to be supported by thousands of lines of lines written in different languages ​​in several files. Most of the information implied in these documents is repeated, and may only come from several properties of the class.

2.3, other advantages

Automatically generate code using the source code generator so that the programming format is easier to maintain. If you ask two people to write a simple Employee class, you may get two completely different versions. With the source code generator to create an Employee class, you can ensure that its code is consistent. There is a format, style, rule, and good habits of programming, can be guaranteed by the realization logic of the generator.

The reorganization of the source code is part of each of the daily work of each programmer. But this is also a clip-clipping work that is difficult and easy. The source code restructuring process is simplified in the source code generator. We only need to modify the code to generate logic.

The only lack of automatic source code is to establish an information model and write implementation logic, especially models, logic, and auxiliary code need to be more troublesome when writing different languages. However, this can be solved by making different models with people with different skills to resolve: people with business knowledge build an information model, and people with programming knowledge create code generation logic. This method fundamentally changes the development process of software. Although business experts and programmers still need collaboration, they can independently build their own model.

3, XSL source code generator

3.1 Why use XML and XSL

An important decision for establishing a source code generator is to express information models (IMPLEMENTAON LOC). XML is widely accepted by the industry because of its universal data format because of its independence and platform neutrality of the Internet. The standardization, flexibility, and versatility of XML make it an express information model. Therefore, using XSL to express the implementation logic, it will be sympathy.

Another option is to use XSL to generate a UML model instead of directly generating source code. OMG defined MOF (Meta Object Facility) language can be used to describe the UML-based integral model (Meta-Model). XML Metadata Interchange Format is a format language based on XML expression UML. An intermediate design model from an XML information model can be implemented by XMI or MOF. The process of converting into an intermediate XMI model should be an eye, because XSL simplifies the conversion from XML to XMI. This method of intermediate design models is shown in Figure 2.

This article will explore how to use the XSL to establish a source code generator template. This template reads information from the XML file containing the information model and generates the final code. This article uses a simple example so that the reader can better understand how to use XML and XSL to write its own source code generator. An important advantage that converts the information model to the source code with XSL is that XSL is the statement. XSL is a statement because it only describes the result of conversion, not how to convert the specific steps. This is a higher level of abstraction. Of course, XSL also provides some program control structures. But these features are very limited compared to true programming languages. Therefore, complex source code generating logic is preferably expressed by XSL and some programming language. 3.2, working principle

An XML file organization is like a tree structure. Each node has the name, value, attribute, and child nodes. XML This simple and practical design makes almost any information can be stored in the XML file. An XML file can be combined with a file type definition -dtd (Document Type Definition) file. The DTD file has data describing the XML file structure. The XML file can be identified by the corresponding DTD file to ensure that its structure is valid.

XSL consists of a range of rules. These rules determine how to handle the corresponding elements in the XML file. Every rule has a standard that matches yourself. These standards specify how this rule is activated. Each rule also has the format of the conversion XML element associated with yourself. The XSL rules consists of different levels because each rule can use the tag to stimulate other rules.

The following rules are often encountered, and its significance is also apparent. is used to select a value of an element from an XML file. is used to specify the text. and Used to simulate the IF-THEN-ELSE and Switch control structure. Iterates each element in the XML element. introduces an XSL file into another file.

Handling an XML file with XSL requires an XSL processor. The processor reads the XML file and the XSL format table, and then transforms the XML file with the XSL conversion rule. This article uses Mr. James Clark, developed free XSL processor-XT, readers can get the processor and related information at www.jclark.com/xml/xt.html.

The source code generator needs the following files

(1) DTD file, the file describes the overall model.

(2) XML file, the file describes the information model.

(3) XSL file, the file is used to generate source code.

The data structure of the information model described in the XML file must be uniformly defined in the overall model DTD file. The XSL source creation template reads information from the XML file and generates the corresponding source code.

Techniques with XML and XSL generation program source code are composed of the following steps (as shown in Figure 1)

(1) Business Analysis (Business Analyst) Establishing a Conceptual Model DTD file. This model specifies the data structure in the XML file. These data contain element names, possible attributes, and relationship between each element in an XML file.

For example, when establishing the overall model in Figure 1, we may consider this model only consist of Class elements. Each Class should have a Name property.

Name NmToken #Required>

Each Class should also have a Properties option element. One of the zero or one potential of the element is used?

The Properties element is a set consisting of several Property elements. One of the elements of the element is represented by *.

Each Property element should have a name attribute and a Type property.

"Attlist property

Name Nmtoken #Required

Type cdata #Required>

These DTD statements above specify a simplified overall model. It will be further expanded later. (2) Business Expert creates an information model XML file. This file consists of specific elements, elements, and child elements. The structure of the element must be consistent with the structure defined in the DTD file established in the first step.

For example, for an attribute Name, type String and attribute Salry, the type is a Double class, and its XML file can be defined as

TYPE = "string">

TYPE = "double">

(3) Program Developer (Programmer) uses XSL to establish source code generation logic. The XSL file is divided into two parts. The first part reads information from an XML file. The second part uses the read information to generate source code.

For example, the following XSL template operates the Class element in the XML file. This template is first inserted into the C keyword class, and then selects the Name property of the current Class element, then insert {, activates each sub-element Property template included in the sub-element Properties set, and finally insert}.

Class

{

SELECT = "Properties / Property">

};

The XSL template of the child element Property is inserted into several indent spaces, select the TYPE attribute of the current Property element, then insert space and _, select the Name property of the element, and finally inserted;

_

;

The final source code is as follows

Class Employee

{

String _name;

Double _salary;

}

Although this is a fairly simple example, it has already sketch the specific steps of writing a source code generator.

4, specific example

Below we will discuss an example. We will establish your own source code generator in accordance with the previous steps.

4.1, Overall Model (Meta-Model)

The overall model should be established by business analysts. The overall model specifies the structure of the model. Our overall model is made up of classes. These classes have attributes and related class functions. Figure 4 is the UML model of our overall model. The DTD file model defined according to this overall model is shown in a single list. List a monogram Model.dtd

- Listing 1: Model.dtd - The Meta-model -

PARENTS?, METHODS ?, Properties?)

>

Name Nmtoken #Required

Package NMToken #implied

>

Name Nmtoken #Required

Visibility (public | private) "public"

>

Name Nmtoken #Required

Type cdata #Required

Visibility (PUBLIC | Protected | Private) "public"

Modifier (Virtual | Static) ""

Const (True | False) "False"

>

Name Nmtoken #Required

Type cdata #Required

Default cdata #implied

>

"Attlist property

Name Nmtoken #Required

Type cdata #Required

HAS_GET (TRUE | FALSE) "True"

HAS_SET (TRUE | FALSE) "True"

HAS_DATA (TRUE | FALSE) "True"

IS_UNIQUE (TRUE | FALSE) "false"

>

">>

- End of listing -

转载请注明原文地址:https://www.9cbs.com/read-90633.html

New Post(0)