Method, device and electronic device for converting code
By analyzing and refactoring programs based on JSP technology, the problem of high porting cost between JSP programs and front-end separation programs is solved, efficient and accurate code reconstruction is achieved, and economic costs are reduced.
Patent Information
- Application Number
- CN202110900202.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-08-06
- Publication Date
- 2025-04-15
- Estimated Expiration
- 2041-08-06
AI Technical Summary
The porting cost is high between programs developed based on JSP technology and programs developed with existing front- and back-end separation technologies.
By obtaining the first extensible markup language file and Java file, the structured query language code is reconstructed using the extensible markup language parsing method, and the Java code in the Java file is reconstructed through the template generation method to obtain the folder of the second program. The folder of the second program includes the reconstructed structured query language code and the reconstructed Java code.
It effectively reduces the porting cost between JSP programs and front-end separation programs, improves reconstruction efficiency, reduces the possibility of errors in manual reconstruction, and reduces the economic cost during the reconstruction process.
Smart Images

Figure CN113568622B_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to the fields of Internet of Things and financial technology, and more particularly, to a method, device, and electronic device for converting codes. Background Art
[0002] Some companies run applications developed using dynamic web page development technologies (such as Java Server Pages (JSP)) in production environments. JSP integrates the front-end and back-end. However, separating the front-end and back-end can effectively reduce server pressure and ease development complexity.
[0003] In the process of realizing the concept of the present disclosure, the applicant discovered that there are at least the following problems in the related art: the cost of transplanting between programs developed based on JSP technology and programs developed with existing front-end and back-end separation technology is high. Summary of the Invention
[0004] In view of this, the present disclosure provides a method, apparatus, and electronic device for converting codes to reduce the cost of migrating between programs developed based on JSP technology and programs developed using existing front-end and back-end separation technologies.
[0005] One aspect of the present disclosure provides a code conversion method for converting code supported by a first program into code supported by a second program, wherein the first program includes a first extensible markup language file and a Java file, and the first extensible markup language file includes structured query language code. The above method includes: obtaining the first extensible markup language file and the Java file; and reconstructing the structured query language code through an extensible markup language parsing method, and reconstructing the Java code in the Java file through a template generation method, to obtain a folder of the second program, and the folder of the second program includes the reconstructed structured query language code and the reconstructed Java code.
[0006] According to an embodiment of the present disclosure, a first extensible markup language file includes business statements and template statements, and the business statements and template statements can generate structured query language code; reconstructing the structured query language code through the extensible markup language parsing method includes: using an extensible markup language file parser to parse the business statements and template statements respectively to obtain parsing results; and generating a second extensible markup language file for the parsing results according to the persistence format requirements, and generating a Java file corresponding to the second extensible markup language file in a data access object folder.
[0007] According to an embodiment of the present disclosure, an extensible markup language file parser is used to parse business statements and template statements respectively, and the parsing results include: parsing template statements by file traversal, where template statements are statements obtained by parsing a first extensible markup language file; parsing business statements by file traversal, where template statements and business statements are associated and stored in a mapping set; and jointly parsing business statements and template statements to obtain reconstructed structured query language code.
[0008] According to an embodiment of the present disclosure, generating a second extensible markup language file for the parsing result according to the persistence requirement format includes: saving the reconstructed structured query language code as a second extensible markup language file that meets the persistence format requirement.
[0009] According to an embodiment of the present disclosure, generating a Java file corresponding to the second extensible markup language file under the data access object folder includes: for each second extensible markup language file that meets the persistence format requirements, generating a Java file corresponding to the second extensible markup language file that meets the persistence format requirements under the data access object folder.
[0010] According to an embodiment of the present disclosure, reconstructing Java code in a Java file by a template generation method includes: parsing the Java code to generate a Java file in a service folder, and generating a Java file corresponding to the Java file in the service folder in a program interface folder.
[0011] According to an embodiment of the present disclosure, parsing Java code to generate Java files in a service folder includes: parsing Java files in a first program by file traversal; converting a calling method of a structured query language code in a Java file in the first program into a method corresponding to the calling method of the structured query language code under a data access object folder; and generating a Java file in a service folder based on the method corresponding to the calling method of the structured query language code.
[0012] According to an embodiment of the present disclosure, generating a Java file corresponding to a Java file in a service folder in a program interface folder includes: after generating at least one Java file in the service folder, generating a Java file corresponding to the Java file in the service folder in the program interface folder.
[0013] According to an embodiment of the present disclosure, the above method further includes: after generating the Java file in the service folder, in response to a user operation, modifying the Java file in the service folder.
[0014] One aspect of the present disclosure provides a code conversion device for converting code supported by a first program into code supported by a second program, wherein the first program includes a first Extensible Markup Language (XML) file and a Java file, and the first XML file includes Structured Query Language (SQL) code. The device includes a file acquisition module and a code reconstruction module. The file acquisition module is configured to acquire the first XML file and the Java file; the code reconstruction module is configured to reconstruct the SQL code using an XML parsing method, and to reconstruct the Java code in the Java file using a template generation method, thereby obtaining a folder for the second program, which includes the reconstructed SQL code and the reconstructed Java code.
[0015] Another aspect of the present disclosure provides an electronic device, including one or more processors and a storage device, wherein the storage device is used to store executable instructions, and when the executable instructions are executed by the processor, the above method is implemented.
[0016] Another aspect of the present disclosure provides a computer-readable storage medium storing computer-executable instructions, which are used to implement the above method when executed.
[0017] Another aspect of the present disclosure provides a computer program, which includes computer-executable instructions. When the instructions are executed, they are used to implement the above method. BRIEF DESCRIPTION OF THE DRAWINGS
[0018] The above and other objects, features and advantages of the present disclosure will become more apparent through the following description of the embodiments of the present disclosure with reference to the accompanying drawings, in which:
[0019] Figure 1 Schematically illustrates an exemplary system architecture to which the method, apparatus, and electronic device for transcoding according to an embodiment of the present disclosure can be applied;
[0020] Figure 2 The flowchart of the method for converting codes according to an embodiment of the present disclosure is schematically shown;
[0021] Figure 3 A flowchart of a method for reconstructing structured query language code according to an embodiment of the present disclosure is schematically shown;
[0022] Figure 4 Schematically shows a block diagram of an XML parser according to an embodiment of the present disclosure;
[0023] Figure 5 A flowchart schematically illustrates a method for generating a Java file in a service folder according to an embodiment of the present disclosure;
[0024] Figure 6 Schematically shows a block diagram of FreeMarker according to an embodiment of the present disclosure;
[0025] Figure 7 A block diagram schematically shows a device for converting codes according to an embodiment of the present disclosure; and
[0026] Figure 8 The block diagram of an electronic device according to an embodiment of the present disclosure is schematically shown. DETAILED DESCRIPTION
[0027] Hereinafter, embodiments of the present disclosure will be described with reference to the accompanying drawings. However, it should be understood that these descriptions are merely exemplary and are not intended to limit the scope of the present disclosure. In the detailed description below, for ease of explanation, many specific details are set forth to provide a comprehensive understanding of the embodiments of the present disclosure. However, it is apparent that one or more embodiments may also be implemented without these specific details. In addition, in the following description, descriptions of well-known structures and technologies are omitted to avoid unnecessary confusion of the concepts of the present disclosure.
[0028] The terms used herein are only for describing specific embodiments and are not intended to limit the present disclosure. The terms "comprise," "include," etc. used herein indicate the presence of features, steps, operations, and / or components, but do not exclude the presence or addition of one or more other features, steps, operations, or components.
[0029] All terms used herein (including technical and scientific terms) have the meanings commonly understood by those skilled in the art unless otherwise defined. It should be noted that the terms used herein should be interpreted as having a meaning consistent with the context of this specification and should not be interpreted in an idealized or overly rigid manner.
[0030] When using expressions such as "at least one of A, B, and C," they should generally be interpreted in accordance with the meaning of the expression commonly understood by those skilled in the art (for example, "a system having at least one of A, B, and C" should include but is not limited to systems having A alone, B alone, C alone, A and B, A and C, B and C, and / or A, B, and C, etc.). The terms "first" and "second" are used for descriptive purposes only and should not be understood as indicating or implying relative importance or implicitly specifying the number of the indicated technical features. Therefore, features specified as "first" or "second" may explicitly or implicitly include one or more features.
[0031] In order to facilitate understanding of the embodiments of the present disclosure, some concepts involved in the present disclosure are first described.
[0032] The Transmission Control Protocol (TCP) is a connection-oriented, reliable, byte-stream-based transport layer communication protocol. TCP is designed to adapt to a layered protocol hierarchy that supports multiple network applications.
[0033] Hypertext Transfer Protocol (HTTP) is a simple request-response protocol that usually runs on top of TCP.
[0034] Java is an object-oriented programming language that incorporates the strengths of C++ while eliminating concepts like multiple inheritance and pointers that are difficult to understand in C++. This makes Java both powerful and easy to use. Java boasts simplicity, object-orientation, distribution, robustness, security, platform independence and portability, multithreading, and dynamism.
[0035] JavaScript, abbreviated as JS, is a lightweight, interpreted or just-in-time compiled programming language with function-first approach.
[0036] Extensible Markup Language (XML) is a subset of Standard Generalized Markup Language, which is a markup language used to mark electronic files to make them structured.
[0037] JavaScript Object Notation (JSON) is a lightweight data exchange format that uses a text format that is completely independent of the programming language to store and represent data.
[0038] Representational State Transfer, or RESTFUL for short, is a design style and development method for network applications that is based on HTTP and can be defined in XML or JSON format.
[0039] Application Programming Interface (API) is a set of predefined interfaces (such as functions, HTTP interfaces), or refers to the agreement for connecting different components of a software system.
[0040] Separation of front-end and back-end: The front-end can refer to a web server, responsible for returning page information to users. The back-end can refer to an application server, responsible for processing business logic. Separation of front-end and back-end can also refer to front-end Hypertext Markup Language (HTML) pages calling back-end RESTful APIs via Asynchronous JavaScript and XML (AJAX), interacting with JSON data.
[0041] JSP, short for Java Server Pages, is a dynamic web development technology that uses JSP tags to insert Java code into HTML pages. It's a technology that integrates front-end and back-end functionality, with no distinction between the front-end and back-end.
[0042] Spring: It is an open-source, lightweight Java backend development framework with two core features: inversion of control and aspect-oriented design. It is primarily used for backend business development.
[0043] Some companies currently run programs written using JSP technology in production environments. These programs are generally outdated, outdated, and difficult to maintain. However, refactoring these JSP-based programs using new technologies is time-consuming, labor-intensive, and costly. For example, the JSP technology development system is largely incompatible with existing development systems that separate the front-end and back-end, resulting in high migration costs. Related technologies can manually refactor JSP code into Spring code, but this requires operators to be proficient in both JSP and Spring technologies, placing high demands on their programming skills. For example, operators must first analyze the JSP system code before manually refactoring it. This approach not only requires significant labor costs but is also prone to program defects (bugs) during the refactoring process, resulting in financial losses.
[0044] In the related art, the mainstream code conversion method mainly relies on manual operation to achieve the reconstruction of JSP code to Spring code, and the disadvantages include the following.
[0045] For example, the degree of automation is low: code refactoring can only be done manually, which is time-consuming and labor-intensive. It requires business knowledge and is not very efficient.
[0046] For example, the requirements for operators are high: operators need to be proficient in both JSP and front-end and back-end separation technology in order to carry out reconstruction work, which places high requirements on operators.
[0047] For example, there is the problem of misoperation: manual operation may cause problems due to subjective cognition, etc. These problems also require manpower to solve.
[0048] In summary, many companies have no motivation to refactor JSP code.
[0049] In order to at least partially improve the above pain points, the embodiments of the present disclosure provide a method, device and electronic device for converting code. The method for converting code includes a file acquisition process and a code reconstruction process. In the file acquisition process, a first extensible markup language file and a Java file are acquired. After completing the file acquisition process, the code reconstruction process is entered, and the structured query language code is reconstructed by the extensible markup language parsing method, and the Java code in the Java file is reconstructed by the template generation method to obtain a folder of the second program, and the folder of the second program includes the reconstructed structured query language code and the reconstructed Java code.
[0050] The code conversion method, device and electronic device provided by the embodiments of the present disclosure effectively improve the shortcomings of tedious and difficult JSP code reconstruction, and provide a technical solution for efficiently, conveniently and automatically reconstructing JSP code into Spring code, which can effectively improve the reconstruction efficiency and quickly convert simple and clear JSP code into Spring code.
[0051] The code conversion method, device, and electronic device provided in the embodiments of the present disclosure can be used in the field of Internet of Things in terms of code conversion, and can also be used in various fields other than the field of Internet of Things, such as the financial field. The application fields of the code conversion method, device, and electronic device provided in the embodiments of the present disclosure are not limited.
[0052] Figure 1 The following schematically illustrates an exemplary system architecture of a method, apparatus, and electronic device that can be used to convert code according to an embodiment of the present disclosure. Figure 1 The examples shown are merely examples of system architectures to which the embodiments of the present disclosure may be applied, to help those skilled in the art understand the technical content of the present disclosure, but do not mean that the embodiments of the present disclosure may not be used in other devices, systems, environments or scenarios.
[0053] like Figure 1 As shown, the system architecture 100 according to this embodiment may include terminal devices 101, 102, and 103, a network 104, and a server 105. The network 104 may include multiple gateways, routers, hubs, network cables, etc., which are used to provide a medium for communication links between the terminal devices 101, 102, and 103 and the server 105. The network 104 may include various connection types, such as wired or wireless communication links or fiber optic cables, etc.
[0054] Users can use terminal devices 101, 102, and 103 to interact with other terminal devices and server 105 via network 104 to receive or send information, such as receiving Java files or sending code. Terminal devices 101, 102, and 103 can be installed with various communication client applications. For example, web browser applications, mapping applications, software development applications, banking applications, government applications, monitoring applications, search applications, office applications, instant messaging tools, email clients, social platform software, and other applications (only as examples). For example, a user can use terminal device 101 to perform code conversion or send code conversion instructions to the server.
[0055] The terminal devices 101 , 102 , and 103 include but are not limited to smart phones that can use web browsers or clients, virtual reality devices, augmented reality devices, tablet computers, laptop computers, desktop computers, etc.
[0056] The server 105 can receive the request and process the request, and can be a storage server, a background management server, a server cluster, etc. For example, the server 105 can store a program developed based on JSP technology. For example, the server 105 can be used to run a program after code conversion.
[0057] It should be noted that the method for generating a message provided in the embodiment of the present disclosure can generally be executed by the terminal device 101, 102, 103 or the server 105. Accordingly, the apparatus for generating a message provided in the embodiment of the present disclosure can generally be set in the terminal device 101, 102, 103 or the server 105. The method for generating a message provided in the embodiment of the present disclosure can also be executed by a server or server cluster that is different from the server 105 and can communicate with the terminal device 101, 102, 103 and / or the server 105.
[0058] It should be understood that the number of terminal devices, networks and servers is merely illustrative and any number of terminal devices, networks and servers may be provided as required.
[0059] Figure 2 A flowchart of a code conversion method according to an embodiment of the present disclosure is schematically shown. The code conversion method is used to convert code supported by a first program into code supported by a second program, wherein the first program includes a first extensible markup language file and a Java file, and the first extensible markup language file includes structured query language code.
[0060] like Figure 2 As shown, the code conversion method may include operations S210 to S220.
[0061] In operation S210, a first extensible markup language file and a Java file are acquired.
[0062] In this embodiment, a Java folder and a first Extensible Markup Language file containing Structured Query Language (SQL) statements can be obtained from a program installed on an electronic device. A program developed based on JSP technology can include two types of folders: 1. XML folders containing SQL statements; 2. Java folders.
[0063] Programs developed using Spring technology can include several types of folders: 1. Program interface folders (such as the controller folder), which store program interfaces. 2. Service folders (such as the service folder), which store business-related code. 3. Data Access Object (DAO) folders (such as the dao folder; each XML file in the dao folder generates a Java file). 4. Extensible Markup Language folders (such as the XML folder), which store SQL statements.
[0064] In operation S220, the structured query language code is reconstructed by the extensible markup language parsing method, and the Java code in the Java file is reconstructed by the template generation method to obtain a folder of the second program, which includes the reconstructed structured query language code and the reconstructed Java code.
[0065] Specifically, this embodiment includes two types of operations: parsing and reconstructing SQL and parsing and reconstructing Java. Combining these two types of operations allows a program developed based on JSP technology to be reconstructed into a Spring program. It should be noted that during the conversion process, a small amount of manual processing may be required to complete the operation.
[0066] In this example, JSP code is divided into two categories: XML files and Java files. XML parsing is used to reconstruct SQL code, and template generation is used to reconstruct Java code. This method allows for rapid construction of Spring code projects, saving significant workload and reducing the potential problems associated with manual reconstructing.
[0067] In some embodiments, the first Extensible Markup Language file includes business statements and template statements, and the business statements and template statements are capable of generating Structured Query Language codes.
[0068] Figure 3The flowchart of the method for reconstructing structured query language code according to an embodiment of the present disclosure is schematically shown.
[0069] like Figure 3 As shown, reconstructing the structured query language code by using the extensible markup language parsing method may include operations S301 and S302.
[0070] In operation S301, the business statement and template statement are parsed using an XML file parser to obtain parsing results. For example, the XML file parser can be a Dom4j, dom, or other parser. Dom4j is a Java XML application programming interface (API), an upgrade of jdom, used to read and write XML files.
[0071] In operation S302 , a second XML file is generated for the parsing result according to the persistence format requirement, and a Java file corresponding to the second XML file is generated in a data access object folder.
[0072] For example, the persistence format requirements can be those of MyBatis. MyBatis is a persistence layer framework that supports customized SQL, stored procedures, and advanced mapping. MyBatis eliminates nearly all Java Database Connectivity (JDBC) code and manual parameter setting and result set retrieval. MyBatis uses simple XML or annotations to configure and map native information, mapping interfaces and Java Plain Old Java Objects (POJOs) to database records.
[0073] In certain embodiments, the above process can be implemented by an SQL parsing and reconstruction module. For example, the SQL parsing and reconstruction module is based on XML parsing technology and uses Dom4j (an XML parsing and generation tool) as a specific implementation method. The JSP XML file is rewritten into an XML file readable by mybatis (a persistence tool) and placed in an XML folder; at the same time, a corresponding Java file is generated in the dao folder to match the mybatis XML file.
[0074] Specifically, the SQL parsing and reconstruction module first traverses all XML files in JSP-based programs. These XML files are primarily divided into business statements and template statements, which are combined to generate readable SQL statements. Dom4j is used to parse the business statements and template statements separately, formatting and storing them. The module then regenerates the XML files according to the format required by MyBatis, and simultaneously generates Java files in the corresponding dao folder.
[0075] Figure 4 The figure schematically shows a block diagram of an XML parser according to an embodiment of the present disclosure.
[0076] like Figure 4 As shown, XML parsers can include DOM classes, SAX classes, and JAXP classes. Among them, the DOM class can be parsed using Dom4j parser or DOM parser. The SAX class can be parsed using SAX parser. In addition, the JAXP class can also be parsed using DOM parser and SAX parser.
[0077] Dom4j is an open source XML parsing package, an easy-to-use, open source library for XML, XPath, and XSLT. It is used on the Java platform, uses the Java Collection Framework, and fully supports DOM, SAX, and JAXP.
[0078] Dom4j primarily implements its functionality based on the XML-DOM model. The main Dom4j interfaces are defined in the org.Dom4j package. For example, Attribute defines XML attributes. Branch is a node that can contain child nodes, such as XML elements and documents.
[0079] In some embodiments, using an extensible markup language file parser to parse the business statement and the template statement separately, and obtaining the parsing results includes the following operations.
[0080] First, the template statements are parsed by traversing the file, where the template statements are statements obtained by parsing the first extensible markup language file; and the business statements are parsed by traversing the file, where the template statements and the business statements are associated and stored in a mapping set.
[0081] Then, the business statement and the template statement are jointly parsed to obtain the reconstructed structured query language code.
[0082] The following is an example of using Dom4j to parse XML.
[0083] About parsing XML documents.
[0084] Reading and writing XML documents mainly depends on the org.Dom4j.io package, which provides two different methods: DOMReader and SAXReader. Both calling methods rely on interfaces.
[0085] / / Read XML from the file, input file name, return XML document
[0086] public Document read(String fileName)throws MalformedURLException,DocumentException{
[0087] SAXReader reader=new SAXReader();
[0088] Document document=reader.read(new File(fileName));
[0089] return document;
[0090] }
[0091] The reader's read method is overloaded and can read from a variety of sources such as InputStream, File, URL, etc. The resulting Document object represents the entire XML.
[0092] The character encoding used for reading is converted according to the encoding defined in the XML file header. If you encounter garbled characters, you need to keep the encoding name consistent across all locations.
[0093] The next step after reading is to get the root node.
[0094] public Element getRootElement(Document doc){
[0095] return doc.getRootElement();
[0096] }
[0097] Next, traverse the XML tree.
[0098] Specifically, the nodes of the XML tree can be traversed by any of enumeration, recursion and visitors.
[0099] For example, 1. Enumeration (Iterator).
[0100] / / Enumerate all child nodes
[0101] for(Iterator i=root.elementIterator();i.hasNext();){
[0102] Element element=(Element)i.next();
[0103] / / do something
[0104] }
[0105] / / Enumerate the nodes named foo
[0106] for(Iterator i=root.elementIterator(foo);i.hasNext();){
[0107] Element foo=(Element)i.next();
[0108] / / do something
[0109] }
[0110] / / Enumerate properties
[0111] for(Iterator i=root.attributeIterator();i.hasNext();){
[0112] Attribute attribute=(Attribute)i.next();
[0113] / / do something
[0114] }
[0115] For example, 2. Recursion.
[0116] Recursion can also use Iterator as an enumeration method, but the document provides another approach
[0117] public void treeWalk(){
[0118] treeWalk(getRootElement());
[0119] }
[0120] public void treeWalk(Element element){
[0121] for(int i=0,size=element.nodeCount(); i <size;i++){
[0122] Node node=element.node(i);
[0123] if(node instanceof Element){
[0124] treeWalk((Element)node);
[0125] }else{ / / do something....
[0126] }
[0127] }
[0128] }
[0129] For example, 3. Visitor pattern.
[0130] DOM4J's support for Visitor can greatly reduce the amount of code. You only need to define a class to implement the Visitor interface.
[0131] public class MyVisitor extends VisitorSupport{
[0132] public void visit(Element element){
[0133] System.out.println(element.getName());
[0134] }
[0135] public void visit(Attribute attr){
[0136] System.out.println(attr.getName());
[0137] }
[0138] }
[0139] About calling: root.accept(new MyVisitor()).
[0140] The Visitor interface provides multiple overloads of Visit() that use different methods to visit different XML objects. This Visitor automatically traverses all child nodes. If root.accept(MyVisitor) is called, the child nodes will be traversed.
[0141] About string and XML conversion.
[0142] Sometimes you often need to convert a string to XML or vice versa.
[0143] / / Convert XML to string
[0144] Document document=...;
[0145] String text=document.asXML();
[0146] / / Convert string to XML
[0147] String text= <name> James< / name> ;
[0148] Document document=DocumentHelper.parseText(text);
[0149] For example, using XSLT to transform XML
[0150] public Document styleDocument(
[0151] Document document,
[0152] String stylesheet
[0153] )throws Exception{
[0154] / / load the transformer using JAXP
[0155] TransformerFactory factory=TransformerFactory.newInstance();
[0156] Transformer transformer=factory.newTransformer(
[0157] new StreamSource(stylesheet) );
[0159] / / now lets style the given document
[0160] DocumentSource source = new DocumentSource(document);
[0161] DocumentResult result = new DocumentResult();
[0162] transformer.transform(source, result);
[0163] / / return the transformed document
[0164] Document transformedDoc = result.getDocument();
[0165] return transformedDoc;
[0166] }
[0167] Regarding the creation of XML.
[0168] Creating XML can be the work before writing a file.
[0169] public Document createDocument(){
[0170] Document document = DocumentHelper.createDocument();
[0171] Element root = document.addElement("root");
[0172] Element author1 =
[0173] root
[0174] .addElement(author)
[0175] .addAttribute(name, James)
[0176] .addAttribute(location, UK)
[0177] .addText(James Strachan);
[0178] Element author2=
[0179] root
[0180] .addElement(author)
[0181] .addAttribute(name,Bob)
[0182] .addAttribute(location,US)
[0183] .addText(Bob McWhirter);
[0184] return document;
[0185] }
[0186] In some embodiments, generating a second extensible markup language file for the parsed result according to the persistence requirement format includes: saving the reconstructed structured query language code as a second extensible markup language file that meets the persistence format requirement. For example, saving as an XML file readable by mybatis
[0187] In some embodiments, generating a Java file corresponding to the second extensible markup language file under the data access object folder includes: for each second extensible markup language file that meets the persistence format requirements, generating a Java file corresponding to the second extensible markup language file that meets the persistence format requirements under the data access object folder.
[0188] In a specific embodiment, the reconstruction of the structured query language code is achieved through the following operations.
[0189] First, use Dom4j to parse the XML template statements in the JSP program by file traversal and store them in a map.
[0190] Then, parse the XML business statements in the JSP program by file traversal.
[0191] Next, the XML business statement is combined with the XML template statement in the first operation for joint parsing.
[0192] Then, use Dom4j to save the SQL statement parsed in step 2 as an XML file readable by mybatis.
[0193] Then, every time an XML file is generated, a corresponding Java file is generated in the dao folder.
[0194] In some embodiments, reconstructing the Java code in the Java file by the template generation method may include: parsing the Java code to generate a Java file in the service folder, and generating a Java file in the program interface folder corresponding to the Java file in the service folder.
[0195] Specifically, code generation based on a template approach can be implemented using the Java parsing and refactoring module. For example, FreeMarker (a template code generation method) can be used to generate Java code. Similar to SQL parsing, using a template approach also requires parsing Java code, generating a Java file in the service folder, and simultaneously generating a corresponding Java file in the controller folder.
[0196] Figure 5 The flowchart of the method for generating a Java file in a service folder according to an embodiment of the present disclosure is schematically shown.
[0197] like Figure 5 As shown, parsing the Java code to generate a Java file in the service folder may include operations S501 to S503.
[0198] In operation S501, Java files in a first program are parsed by traversing the files.
[0199] In operation S502, the calling method of the structured query language code in the Java file in the first program is converted into a method corresponding to the calling method of the structured query language code in the data access object folder.
[0200] In operation S503 , a Java file in the service folder is generated based on a method corresponding to the calling method of the structured query language code.
[0201] For example, generating a Java file corresponding to a Java file in a service folder in a program interface folder includes: after generating at least one Java file in the service folder, generating a Java file corresponding to the Java file in the service folder in a program interface folder.
[0202] In some embodiments, the above method may further include: after generating the Java file in the service folder, modifying the Java file in the service folder in response to a user operation. This can meet the adjustment requirements of some complex scenarios.
[0203] Figure 6 The figure schematically shows a block diagram of FreeMarker according to an embodiment of the present disclosure.
[0204] like Figure 6 As shown, FreeMarker is a template engine, a general tool for generating output text (HTML web pages, emails, configuration files, source code, etc.) based on templates and data that need to be changed. It is not for end users, but a Java class library, a component that can be embedded in the programs they develop.
[0205] Templates are written in FreeMarker Template Language (FTL), a simple, specialized language. This means that data must be prepared for display in a real programming language, such as database queries and business operations, and then the template displays the prepared data.
[0206] This pattern is often referred to as the MVC (Model View Controller) pattern, and it is a mature pattern for dynamic web pages.
[0207] Its working principle can be described as follows.
[0208] Suppose an HTML page is required in an application system as follows:
[0209]
[0210]
[0211] The username on the page (i.e. "XXBC" above) is the name of the visitor who logged in to this page, and the data should come from the database so that it can be updated at any time. You cannot directly enter "XXBC", "greenmXXX" and links in the HTML page, and you cannot use static HTML code. You can use a template to solve the problem. The template is the same as the static page, and the template will contain some FreeMarker instructions to turn it into dynamic content. For example:
[0212] The template file is stored on a web server. When someone visits the page, FreeMarker kicks in and dynamically transforms the template, replacing the ${...} parts with the latest data. The result is then sent to the visitor's web browser. The visitor's web browser will receive content similar to the first HTML example (that is, HTML code without FreeMarker directives), and the visitor will be unaware of the FreeMarker used on the server. It's important to note that the template file stored on the web server is not modified. The replacement only appears in the web server's response.
[0213] The data prepared for the template as a whole is called a data model. The data model is a tree structure (such as folders and files in a tree topology).
[0214] For example, you can select these values from the data model using the expressions user and latestProduct.name. Analogously to a tree structure, the data model is like a file system, with "(root)" and latestProduct corresponding to directories (folders), and user, url, and name being the files within those directories.
[0215] In general, if Figure 6 As shown, template and data model are the necessary components for FreeMarker to generate output: template + data model = output.
[0216] In a specific embodiment, refactoring Java code may include the following operations.
[0217] First, parse the Java files in the JSP program by file traversal.
[0218] Then, change the SQL call method in the Java file parsed in the previous operation to the corresponding method in the dao folder.
[0219] Next, use FreeMarker to generate the content parsed in the previous operation into a Java file in the service folder. If there are special circumstances, customized improvements can be made.
[0220] Then, for each Java file generated in the service folder, a corresponding Java file is generated in the controller folder.
[0221] Through the combined application of the above-mentioned "XML parsing method" and "template generation method", the basic reconstruction from JSP program to spring program can be completed, and then the reconstruction work can be completed by manual adjustment.
[0222] The code conversion method, device, and electronic device provided by the disclosed embodiments address the high cost and error-proneness of manual code conversion. They introduce XML parsing and template methods to reconstruct XML and Java files, freeing operators from repetitive and time-consuming tasks, saving manpower, improving efficiency, and reducing the likelihood of errors. This significantly reduces costs for enterprises.
[0223] For example, by designing a complete set of JSP reconstruction processes, we can reuse and quickly solve the reconstruction situations of different JSP projects.
[0224] For example, using XML parsing methods and template methods to reconstruct XML files and Java files greatly improves work efficiency and reduces reconstruction costs.
[0225] For example, customized reconstruction can be performed for special files, and the mechanism is flexible, making reconstruction work easier.
[0226] Another aspect of the present disclosure provides a code conversion device for converting code supported by a first program into code supported by a second program, wherein the first program includes a first extensible markup language file and a Java file, and the first extensible markup language file includes structured query language code.
[0227] Figure 7 The figure schematically shows a block diagram of a device for converting codes according to an embodiment of the present disclosure.
[0228] like Figure 7 As shown, the code conversion device 700 may include: a file acquisition module 710 and a code reconstruction module 720.
[0229] The file acquisition module 710 is used to acquire a first extensible markup language file and a Java file.
[0230] The code reconstructing module 720 is used to reconstruct the structured query language code through the extensible markup language parsing method, and reconstruct the Java code in the Java file through the template generation method to obtain a folder of the second program, which includes the reconstructed structured query language code and the reconstructed Java code.
[0231] It should be noted that the implementation methods, technical problems solved, functions realized, and technical effects achieved of each module / unit in the device part embodiment are the same or similar to the implementation methods, technical problems solved, functions realized, and technical effects achieved of each corresponding step in the method part embodiment, and will not be repeated here.
[0232] According to the modules and units of the embodiments of the present invention, any multiple or at least part of the functions of any multiple thereof can be implemented in one module. According to the modules and units of the embodiments of the present invention, any one or more can be split into multiple modules for implementation. According to the modules and units of the embodiments of the present invention, any one or more can be at least partially implemented as a hardware circuit, such as a field programmable gate array (FPGA), a programmable logic array (PLA), a system on a chip, a system on a substrate, a system on a package, an application specific integrated circuit (ASIC), or can be implemented by hardware or firmware of any other reasonable way of integrating or packaging the circuit, or implemented in any one of the three implementation methods of software, hardware and firmware or in an appropriate combination of any several thereof. Alternatively, according to the modules and units of the embodiments of the present invention, one or more can be at least partially implemented as a computer program module, which can perform the corresponding function when the computer program module is run.
[0233] For example, any multiple of the file acquisition module 710 and the code reconstruction module 720 can be combined into one module for implementation, or any one of the modules can be split into multiple modules. Alternatively, at least part of the functions of one or more of these modules can be combined with at least part of the functions of other modules and implemented in one module. According to an embodiment of the present disclosure, at least one of the file acquisition module 710 and the code reconstruction module 720 can be at least partially implemented as a hardware circuit, such as a field programmable gate array (FPGA), a programmable logic array (PLA), a system on a chip, a system on a substrate, a system on a package, an application specific integrated circuit (ASIC), or can be implemented by hardware or firmware such as any other reasonable way of integrating or encapsulating the circuit, or implemented in any one of the three implementation methods of software, hardware and firmware or in an appropriate combination of any of them. Alternatively, at least one of the file acquisition module 710 and the code reconstruction module 720 can be at least partially implemented as a computer program module, which can perform the corresponding function when the computer program module is run.
[0234] Another aspect of the present disclosure provides an electronic device.
[0235] Figure 8 The block diagram of an electronic device according to an embodiment of the present disclosure is schematically shown. Figure 8 The electronic device shown is only an example and should not limit the functions and scope of use of the embodiments of the present disclosure.
[0236] like Figure 8As shown, the electronic device 800 according to an embodiment of the present disclosure includes a processor 801, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 802 or a program loaded from a storage part 808 into a random access memory (RAM) 803. The processor 801 may include, for example, a general-purpose microprocessor (such as a CPU), an instruction set processor and / or a related chipset and / or a special-purpose microprocessor (such as an application-specific integrated circuit (ASIC)), etc. The processor 801 may also include an onboard memory for caching purposes. The processor 801 may include a single processing unit or multiple processing units for performing different actions of the method flow according to an embodiment of the present disclosure.
[0237] Various programs and data required for the operation of the electronic device 800 are stored in the RAM 803. The processor 801, ROM 802, and RAM 803 are communicatively connected to each other via a bus 804. The processor 801 executes the programs in the ROM 802 and / or RAM 803 to perform various operations of the method flow according to the embodiment of the present disclosure. It should be noted that the programs may also be stored in one or more memories other than the ROM 802 and RAM 803. The processor 801 may also execute the programs stored in one or more memories to perform various operations of the method flow according to the embodiment of the present disclosure.
[0238] According to an embodiment of the present disclosure, the electronic device 800 may further include an input / output (I / O) interface 805, which is also connected to the bus 804. The electronic device 800 may further include one or more of the following components connected to the I / O interface 805: an input portion 806 including a keyboard, a mouse, etc.; an output portion 807 including devices such as a cathode ray tube (CRT), a liquid crystal display (LCD), and a speaker; a storage portion 808 including a hard disk; and a communication portion 809 including a network interface card such as a LAN card or a modem. The communication portion 809 performs communication processing via a network such as the Internet. A drive 810 is also connected to the I / O interface 805 as needed. A removable medium 811, such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, etc., is installed in the drive 810 as needed, so that a computer program read therefrom can be installed into the storage portion 808 as needed.
[0239] According to an embodiment of the present disclosure, the method flow according to an embodiment of the present disclosure can be implemented as a computer software program. For example, an embodiment of the present disclosure includes a computer program product, which includes a computer program carried on a computer-readable storage medium, and the computer program includes a program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from the network through the communication part 809, and / or installed from the removable medium 811. When the computer program is executed by the processor 801, the above-mentioned functions defined in the system of the embodiment of the present disclosure are executed. According to an embodiment of the present disclosure, the system, device, apparatus, module, unit, etc. described above can be implemented by a computer program module.
[0240] The present disclosure also provides a computer-readable storage medium, which may be included in the device / apparatus / system described in the above embodiments, or may exist independently and not be incorporated into the device / apparatus / system. The computer-readable storage medium carries one or more programs, and when executed, implements the method according to the embodiments of the present disclosure.
[0241] According to an embodiment of the present disclosure, a computer-readable storage medium may be a non-volatile computer-readable storage medium, for example, it may include but is not limited to: a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination thereof. In the present disclosure, a computer-readable storage medium may be any tangible medium containing or storing a program that can be used by or in combination with an instruction execution system, apparatus, or device. For example, according to an embodiment of the present disclosure, a computer-readable storage medium may include the ROM 802 and / or RAM 803 described above and / or one or more memories other than ROM 802 and RAM 803.
[0242] An embodiment of the present disclosure also includes a computer program product, which includes a computer program containing program code for executing the method provided by the embodiment of the present disclosure. When the computer program product is run on an electronic device, the program code is used to enable the electronic device to implement the image model training method or image processing method provided by the embodiment of the present disclosure.
[0243] When the computer program is executed by the processor 801, the above functions defined in the system / device of the embodiment of the present disclosure are performed. According to the embodiment of the present disclosure, the system, device, module, unit, etc. described above can be implemented by a computer program module.
[0244] In one embodiment, the computer program may be stored on a tangible storage medium such as an optical storage device or a magnetic storage device. In another embodiment, the computer program may be transmitted and distributed in the form of a signal on a network medium, downloaded and installed via the communication portion 809, and / or installed from a removable medium 811. The program code contained in the computer program may be transmitted using any appropriate network medium, including but not limited to wireless, wired, or any suitable combination thereof.
[0245] According to an embodiment of the present disclosure, the program code for executing the computer program provided by the embodiment of the present disclosure can be written in any combination of one or more programming languages. Specifically, these computer programs can be implemented using high-level procedural and / or object-oriented programming languages, and / or assembly / machine languages. Programming languages include, but are not limited to, languages such as Java, C++, python, "C" or similar programming languages. The program code can be executed entirely on the user computing device, partially on the user device, partially on a remote computing device, or entirely on a remote computing device or server. In cases involving a remote computing device, the remote computing device can be connected to the user computing device through any type of network, including a local area network (LAN) or a wide area network (WAN), or can be connected to an external computing device (for example, using an Internet service provider to connect via the Internet).
[0246] Those skilled in the art will understand that the features described in the various embodiments and / or claims of the present disclosure may be combined and / or combined in various ways, even if such combinations or combinations are not explicitly described in the present disclosure. These embodiments are for illustrative purposes only and are not intended to limit the scope of the present disclosure. Although the various embodiments have been described above separately, this does not mean that the measures in the various embodiments cannot be used in combination to advantage. The scope of the present disclosure is defined by the appended claims and their equivalents. Without departing from the scope of the present disclosure, those skilled in the art may make various substitutions and modifications, which should all fall within the scope of the present disclosure.
Claims
1. A method for converting code, for converting a code supported by a first program into a code supported by a second program, wherein: The first program includes a first extensible markup language file and a Java file, the first extensible markup language file includes a structured query language code, and the method includes: Obtaining the first extensible markup language file and the Java file; and The structured query language code is reconstructed by an extensible markup language parsing method, and the Java code in the Java file is reconstructed by a template generation method, to obtain a folder of the second program, wherein the folder of the second program includes the reconstructed structured query language code and the reconstructed Java code, Wherein, the first extensible markup language file includes business statements and template statements, and the business statements and the template statements can generate the structured query language code; The reconstructing the structured query language code by using the extensible markup language parsing method comprises: Using an extensible markup language file parser to parse the business statement and the template statement respectively to obtain parsing results; and A second extensible markup language file is generated for the parsed result according to the persistence format requirement, and a Java file corresponding to the second extensible markup language file is generated in a data access object folder.
2. The method according to claim 1, wherein: The extensible markup language file parser is used to parse the business statement and the template statement respectively, and the parsing results obtained include: Parsing the template statement by file traversal, the template statement is a statement obtained by parsing the first extensible markup language file; parsing the business statement by file traversal, the template statement and the business statement are associated and stored in a mapping set; and The business statement and the template statement are jointly parsed to obtain a reconstructed structured query language code.
3. The method according to claim 2, wherein: The generating a second extensible markup language file for the parsing result according to the persistence requirement format includes: storing the reconstructed structured query language code as a second extensible markup language file that meets the persistence format requirement.
4. The method according to claim 3, wherein: The generating of a Java file corresponding to the second extensible markup language file under the data access object folder includes: for each second extensible markup language file that meets the persistence format requirement, generating a Java file corresponding to the second extensible markup language file that meets the persistence format requirement under the data access object folder.
5. The method according to any one of claims 1 to 4, wherein: The reconstructing the Java code in the Java file by the template generation method includes: The Java code is parsed to generate a Java file in the service folder, and a Java file corresponding to the Java file in the service folder is generated in the program interface folder.
6. The method according to claim 5, wherein: The parsing of the Java code to generate a Java file in the service folder includes: Parsing the Java files in the first program by file traversal; Converting the calling method of the structured query language code in the Java file in the first program into a method corresponding to the calling method of the structured query language code in the data access object folder; and A Java file in the service folder is generated based on a method corresponding to the calling method of the structured query language code.
7. The method according to claim 6, wherein: The generating of a Java file corresponding to the Java file in the service folder in the program interface folder comprises: After at least one Java file is generated in the service folder, a Java file corresponding to the Java file in the service folder is generated in the program interface file.
8. The method according to claim 6, further comprising: After the Java file in the service folder is generated, the Java file in the service folder is modified in response to a user operation.
9. A code conversion device for converting a code supported by a first program into a code supported by a second program, wherein: The first program includes a first extensible markup language file and a Java file, the first extensible markup language file includes a structured query language code, and the device includes: A file acquisition module, used to acquire the first extensible markup language file and the Java file; and a code refactoring module, configured to refactor the structured query language code by an extensible markup language parsing method, and to refactor the Java code in the Java file by a template generation method, to obtain a folder of the second program, wherein the folder of the second program includes the refactored structured query language code and the refactored Java code, Wherein, the first extensible markup language file includes business statements and template statements, and the business statements and the template statements can generate the structured query language code; The reconstructing the structured query language code by using the extensible markup language parsing method comprises: Using an extensible markup language file parser to parse the business statement and the template statement respectively to obtain parsing results; and A second extensible markup language file is generated for the parsed result according to the persistence format requirement, and a Java file corresponding to the second extensible markup language file is generated in a data access object folder.
10. An electronic device comprising: one or more processors; A storage device for storing executable instructions, wherein when the executable instructions are executed by the processor, the method according to any one of claims 1 to 8 is implemented.
11. A computer-readable storage medium storing computer-executable instructions, wherein when the executable instructions are executed by a processor, the method according to any one of claims 1 to 8 is implemented.
Citation Information
Patent Citations
Code language conversion method and device, electronic equipment and storage medium
CN113138767A