Commit aa3f843a authored by unknown's avatar unknown

test

parent 2f8135fd
Pipeline #379 failed with stages
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile default="true" name="Default" enabled="true" />
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="XqueryDemo" />
</profile>
</annotationProcessing>
</component>
<component name="JavacSettings">
<option name="ADDITIONAL_OPTIONS_OVERRIDE">
<module name="XqueryDemo" options="-parameters" />
</option>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="http://maven.aliyun.com/nexus/content/groups/public/" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8.0_371" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.svg" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.linghu</groupId>
<artifactId>XqueryDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.basex</groupId>
<artifactId>basex</artifactId>
<version>9.7.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/net.sf.xmldb-org/xmldb-api -->
<dependency>
<groupId>net.sf.xmldb-org</groupId>
<artifactId>xmldb-api</artifactId>
<version>1.7.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package com.linghu;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MainApp {
public static void main(String[] args) {
SpringApplication.run(MainApp.class,args);
}
}
\ No newline at end of file
package com.linghu.controller;
import com.linghu.response.Msg;
import com.linghu.service.DataBaseService;
import com.linghu.service.Impl.DataBaseServiceImpl;
import com.linghu.util.BaseXClientUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
@RestController
public class OperatorDB {
DataBaseService dataBaseService=new DataBaseServiceImpl();
@GetMapping("/create")
public Msg createApi() throws IOException {
dataBaseService.createDB(BaseXClientUtil.getClient(),"xquery/createDataBase.xqy");
return Msg.success();
}
}
package com.linghu.dao;
import com.linghu.util.BaseXClient;
import javax.xml.bind.JAXBException;
import java.io.IOException;
public interface DataBaseDAO {
//创建数据库
public void createDB(BaseXClient client,String path);
//遍历数据库中的元素
public String selectElements(BaseXClient client,String path) throws IOException, JAXBException;
}
package com.linghu.dao.Impl;
import com.linghu.dao.DataBaseDAO;
import com.linghu.util.BaseXClient;
import com.linghu.util.BaseXClientUtil;
import com.linghu.util.XmlUtil;
import org.springframework.stereotype.Repository;
import javax.xml.bind.JAXBException;
import java.io.IOException;
@Repository
public class DataBaseDAOImpl implements DataBaseDAO {
//新建 数据库
@Override
public void createDB(BaseXClient client, String path){
String paths = BaseXClientUtil.getQueryResource(path);
try {
client.query(paths).execute();
} catch (IOException e) {
e.printStackTrace();
}
}
//执行查询
@Override
public String selectElements(BaseXClient client, String path) throws IOException, JAXBException {
String paths = BaseXClientUtil.getQueryResource(path);
String xmlData = client.query(paths).execute();
return XmlUtil.convertToObject(xmlData, String.class);
}
}
package com.linghu.response;
import java.util.HashMap;
import java.util.Map;
/**
* @author 令狐
* @version 1.0
* Msg:后端程序返回给前端的json数据的Msg对象==>本质就是数据规范
*/
public class Msg {
// 状态码,200 成功,400 失败
private int code;
// 信息
private String msg;
// 返回给浏览器的数据
private Map<String, Object> extend = new HashMap<String, Object>();
public static Msg success() {
Msg res = new Msg();
res.setCode(200);
res.setMsg("success");
return res;
}
public static Msg fail() {
Msg res = new Msg();
res.setCode(400);
res.setMsg("fail");
return res;
}
public Msg add(String key, Object value) {
this.getExtend().put(key, value);
return this;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Map<String, Object> getExtend() {
return extend;
}
public void setExtend(Map<String, Object> extend) {
this.extend = extend;
}
}
package com.linghu.service;
import com.linghu.util.BaseXClient;
public interface DataBaseService {
public void createDB(BaseXClient client, String path);
}
package com.linghu.service.Impl;
import com.linghu.dao.DataBaseDAO;
import com.linghu.dao.Impl.DataBaseDAOImpl;
import com.linghu.service.DataBaseService;
import com.linghu.util.BaseXClient;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class DataBaseServiceImpl implements DataBaseService {
@Resource
DataBaseDAO dataBaseDAO=new DataBaseDAOImpl();
@Override
public void createDB(BaseXClient client, String path) {
dataBaseDAO.createDB(client,path);
}
}
package com.linghu.util;
import java.io.*;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
/**
* Java client for BaseX.
* Works with BaseX 7.0 and later
*
* Documentation: https://docs.basex.org/wiki/Clients
*
* (C) BaseX Team 2005-22, BSD License
*/
public final class BaseXClient implements Closeable {
/** UTF-8 charset. */
private static final Charset UTF8 = Charset.forName("UTF-8");
/** Output stream. */
private final OutputStream out;
/** Input stream (buffered). */
private final BufferedInputStream in;
/** Socket. */
private final Socket socket;
/** Command info. */
private String info;
/**
* Constructor.
* @param host server name
* @param port server port
* @param username user name
* @param password password
* @throws
*/
public BaseXClient(final String host, final int port, final String username,
final String password) throws IOException {
socket = new Socket();
socket.setTcpNoDelay(true);
socket.connect(new InetSocketAddress(host, port), 5000);
in = new BufferedInputStream(socket.getInputStream());
out = socket.getOutputStream();
// receive server response
final String[] response = receive().split(":");
final String code, nonce;
if(response.length > 1) {
// support for digest authentication
code = username + ':' + response[0] + ':' + password;
nonce = response[1];
} else {
// support for cram-md5 (Version < 8.0)
code = password;
nonce = response[0];
}
send(username);
send(md5(md5(code) + nonce));
// receive success flag
if(!ok()) throw new IOException("Access denied.");
}
/**
* Executes a command and serializes the result to an output stream.
* @param command command
* @param output output stream
* @throws IOException Exception
*/
public void execute(final String command, final OutputStream output) throws IOException {
// send {Command}0
send(command);
receive(in, output);
info = receive();
if(!ok()) throw new IOException(info);
}
/**
* Executes a command and returns the result.
* @param command command
* @return result
* @throws IOException Exception
*/
public String execute(final String command) throws IOException {
final ByteArrayOutputStream os = new ByteArrayOutputStream();
execute(command, os);
return new String(os.toByteArray(), UTF8);
}
/**
* Creates a query object.
* @param query query string
* @return query
* @throws IOException Exception
*/
public Query query(final String query) throws IOException {
return new Query(query);
}
/**
* Creates a database.
* @param name name of database
* @param input xml input
* @throws IOException I/O exception
*/
public void create(final String name, final InputStream input) throws IOException {
send(8, name, input);
}
/**
* Adds a document to a database.
* @param path path to resource
* @param input xml input
* @throws IOException I/O exception
*/
public void add(final String path, final InputStream input) throws IOException {
send(9, path, input);
}
/**
* Replaces a document in a database.
* @param path path to resource
* @param input xml input
* @throws IOException I/O exception
*/
public void replace(final String path, final InputStream input) throws IOException {
send(12, path, input);
}
/**
* Stores a binary resource in a database.
* @param path path to resource
* @param input xml input
* @throws IOException I/O exception
*/
public void store(final String path, final InputStream input) throws IOException {
send(13, path, input);
}
/**
* Returns command information.
* @return string info
*/
public String info() {
return info;
}
/**
* Closes the session.
* @throws IOException Exception
*/
@Override
public void close() throws IOException, IOException {
send("exit");
out.flush();
socket.close();
}
/**
* Checks the next success flag.
* @return value of check
* @throws IOException Exception
*/
private boolean ok() throws IOException {
out.flush();
return in.read() == 0;
}
/**
* Returns the next received string.
* @return String result or info
* @throws IOException I/O exception
*/
private String receive() throws IOException {
final ByteArrayOutputStream os = new ByteArrayOutputStream();
receive(in, os);
return new String(os.toByteArray(), UTF8);
}
/**
* Sends a string to the server.
* @param string string to be sent
* @throws IOException I/O exception
*/
private void send(final String string) throws IOException {
out.write((string + '\0').getBytes(UTF8));
}
/**
* Receives a string and writes it to the specified output stream.
* @param input input stream
* @param output output stream
* @throws IOException I/O exception
*/
private static void receive(final InputStream input, final OutputStream output)
throws IOException {
for(int b; (b = input.read()) > 0;) {
// read next byte if 0xFF is received
output.write(b == 0xFF ? input.read() : b);
}
}
/**
* Sends a command, argument, and input.
* @param code command code
* @param path name, or path to resource
* @param input xml input
* @throws IOException I/O exception
*/
private void send(final int code, final String path, final InputStream input) throws IOException {
out.write(code);
send(path);
send(input);
}
/**
* Sends an input stream to the server.
* @param input xml input
* @throws IOException I/O exception
*/
private void send(final InputStream input) throws IOException {
final BufferedInputStream bis = new BufferedInputStream(input);
final BufferedOutputStream bos = new BufferedOutputStream(out);
for(int b; (b = bis.read()) != -1;) {
// 0x00 and 0xFF will be prefixed by 0xFF
if(b == 0x00 || b == 0xFF) bos.write(0xFF);
bos.write(b);
}
bos.write(0);
bos.flush();
info = receive();
if(!ok()) throw new IOException(info);
}
/**
* Returns an MD5 hash.
* @param pw String
* @return String
*/
private static String md5(final String pw) {
final StringBuilder sb = new StringBuilder();
try {
final MessageDigest md = MessageDigest.getInstance("MD5");
md.update(pw.getBytes());
for(final byte b : md.digest()) {
final String s = Integer.toHexString(b & 0xFF);
if(s.length() == 1) sb.append('0');
sb.append(s);
}
} catch(final NoSuchAlgorithmException ex) {
// should not occur
ex.printStackTrace();
}
return sb.toString();
}
/**
* Inner class for iterative query execution.
*/
public class Query implements Closeable {
/** Query id. */
private final String id;
/** Cached results. */
private ArrayList<byte[]> cache;
/** Cache pointer. */
private int pos;
/**
* Standard constructor.
* @param query query string
* @throws IOException I/O exception
*/
Query(final String query) throws IOException {
id = exec(0, query);
}
/**
* Binds a value to an external variable.
* @param name name of variable
* @param value value
* @throws IOException I/O exception
*/
public void bind(final String name, final String value) throws IOException {
bind(name, value, "");
}
/**
* Binds a value with the specified type to an external variable.
* @param name name of variable
* @param value value
* @param type type (can be an empty string)
* @throws IOException I/O exception
*/
public void bind(final String name, final String value, final String type) throws IOException {
cache = null;
exec(3, id + '\0' + name + '\0' + value + '\0' + type);
}
/**
* Binds a value to the context item.
* @param value value
* @throws IOException I/O exception
*/
public void context(final String value) throws IOException {
context(value, "");
}
/**
* Binds a value with the specified type to the context item.
* @param value value
* @param type type (can be an empty string)
* @throws IOException I/O exception
*/
public void context(final String value, final String type) throws IOException {
cache = null;
exec(14, id + '\0' + value + '\0' + type);
}
/**
* Checks for the next item.
* @return result of check
* @throws IOException I/O exception
*/
public boolean more() throws IOException {
if(cache == null) {
out.write(4);
send(id);
cache = new ArrayList<>();
final ByteArrayOutputStream os = new ByteArrayOutputStream();
while(in.read() > 0) {
receive(in, os);
cache.add(os.toByteArray());
os.reset();
}
if(!ok()) throw new IOException(receive());
pos = 0;
}
if(pos < cache.size()) return true;
cache = null;
return false;
}
/**
* Returns the next item.
* @return item string
* @throws IOException I/O Exception
*/
public String next() throws IOException {
return more() ? new String(cache.set(pos++, null), UTF8) : null;
}
/**
* Returns the whole result of the query.
* @return query result
* @throws IOException I/O Exception
*/
public String execute() throws IOException {
return exec(5, id);
}
/**
* Returns query info in a string.
* @return query info
* @throws IOException I/O exception
*/
public String info() throws IOException {
return exec(6, id);
}
/**
* Returns serialization parameters in a string.
* @return query info
* @throws IOException I/O exception
*/
public String options() throws IOException {
return exec(7, id);
}
/**
* Closes the query.
* @throws IOException I/O exception
*/
@Override
public void close() throws IOException {
exec(2, id);
}
/**
* Executes the specified command.
* @param code command code
* @param arg argument
* @return resulting string
* @throws IOException I/O exception
*/
private String exec(final int code, final String arg) throws IOException {
out.write(code);
send(arg);
final String s = receive();
if(!ok()) throw new IOException(receive());
return s;
}
}
}
\ No newline at end of file
package com.linghu.util;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
/**
* @author linghu 改造了一下工具类
* @description
* @date 2023-04-12 09:32
*/
public class BaseXClientUtil {
/**
* 创建client连接对象,并获取连接
*/
public static BaseXClient getClient() throws IOException {
BaseXClient client = new BaseXClient("localhost", 1984, "admin", "admin");
return client;
}
/**
* 这个方法的功能是执行XQuery查询
* @param client
* @param path
* @param
* @return
*/
// public static String executeQuery(BaseXClient client, String path) {
// //有调用了getQueryResource这个方法,把Xquery对应文件的路径传入
// return getQueryResource(path);
// }
/**
*这个方法就是专门处理"xquery/calculate_flow/flow/createCalculateFlow.xqy"
* @param path "xquery/calculate_flow/flow/createCalculateFlow.xqy",这是指定路径下的Xquery文件
* @return
*/
public static String getQueryResource(String path){
//创建一个 StringBuilder 对象,用于构建返回的字符串。
StringBuilder stringBuilder = new StringBuilder();
try {
/**
* ClassPathResource专门用来访问类路径上的资源类resource下的文件
*/
Resource resource = new ClassPathResource(path);
InputStream resourceAsStream = resource.getInputStream();
InputStreamReader reader = new InputStreamReader(resourceAsStream, StandardCharsets.UTF_8);
BufferedReader bufReader = new BufferedReader(reader);
String strLine = null;
while ((strLine = bufReader.readLine()) != null) {
stringBuilder.append(strLine).append("\n");
}
bufReader.close();
} catch (IOException e) {
e.printStackTrace();
}
return stringBuilder.toString();
}
}
package com.linghu.util;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.StringReader;
import java.io.StringWriter;
/**
* 封装了XML转换成object,object转换成XML的代码
*
* @author xcq
* {@code @date} 2023/03/09
*/
public class XmlUtil {
/**
* xml字符串转换为对象
*
* @param tClazz t clazz
* @param string 字符串
* @return {@link T}
* @throws JAXBException jaxbexception
*/
public static <T>T convertToObject(String string, Class<T> tClazz) throws JAXBException {
JAXBContext context = JAXBContext.newInstance(tClazz);
Unmarshaller unmarshaller = context.createUnmarshaller();
try (StringReader sr = new StringReader(string)) {
return (T) unmarshaller.unmarshal(sr);
}
}
/**
* 将对象转为xml字符串 编码为utf-8
*
* @param obj 待转的对象
* @return String xml字符串
*/
public static String objectToXml(Object obj) throws JAXBException {
JAXBContext context = JAXBContext.newInstance(obj.getClass());
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
// marshaller.setProperty(CharacterEscapeHandler.class.getName(), (CharacterEscapeHandler) (ch, start, length, isAttVal, writer) -> writer.write(ch, start, length));
StringWriter writer = new StringWriter();
marshaller.marshal(obj, writer);
return writer.toString();
}
}
db:add('linghu', <a/>, 'doc06.xml')
\ No newline at end of file
for $book in //book
let $title := $book/title/text()
let $author := $book/author/text()
return
<book id="{xs:string($book/@id)}">
<title>{xs:string($title)}</title>
<author>{xs:string($author)}</author>
</book>
\ No newline at end of file
package com.linghu.dao.Impl;
import com.linghu.dao.DataBaseDAO;
import com.linghu.util.BaseXClient;
import com.linghu.util.BaseXClientUtil;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import javax.xml.bind.JAXBException;
import java.io.IOException;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootApplication
class DataBaseDAOImplTest {
DataBaseDAO dataBaseDAO=new DataBaseDAOImpl();
@Test
void createDB() {
BaseXClient client = null;
try {
client = BaseXClientUtil.getClient();//获取Base X连接
} catch (IOException e) {
e.printStackTrace();
}
dataBaseDAO.createDB(client,"xquery/createDataBase.xqy");
}
@Test
void selectElements() throws JAXBException, IOException {
BaseXClient client = null;
try {
client = BaseXClientUtil.getClient();//获取Base X连接
} catch (IOException e) {
e.printStackTrace();
}
String s = dataBaseDAO.selectElements(client, "xquery/selectBookElement.xqy");
System.out.println(s);
}
}
\ No newline at end of file
package com.linghu.service.Impl;
import com.linghu.service.DataBaseService;
import com.linghu.util.BaseXClientUtil;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
class DataBaseServiceImplTest {
DataBaseService dataBaseService=new DataBaseServiceImpl();
@Test
void createDB() throws IOException {
dataBaseService.createDB(BaseXClientUtil.getClient(),"xquery/createDataBase.xqy");
}
}
\ No newline at end of file
db:add('linghu', <a/>, 'doc06.xml')
\ No newline at end of file
for $book in //book
let $title := $book/title/text()
let $author := $book/author/text()
return
<book id="{xs:string($book/@id)}">
<title>{xs:string($title)}</title>
<author>{xs:string($author)}</author>
</book>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment