Skip to content

Commit

Permalink
🚧
Browse files Browse the repository at this point in the history
  • Loading branch information
RickyGu777 committed Aug 28, 2023
1 parent cf9d278 commit a8d75ec
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 58 deletions.
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,7 @@ build {
// build frontend before build
tasks.getByName('compileJava').dependsOn('buildFrontend')
}

tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
164 changes: 119 additions & 45 deletions src/main/java/top/leftblue/publish/Test.java
Original file line number Diff line number Diff line change
@@ -1,65 +1,139 @@
package top.leftblue.publish;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.xml.sax.InputSource;
import top.leftblue.publish.dto.MWAResult;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.StringReader;
import java.lang.reflect.InvocationTargetException;
import top.leftblue.publish.module.Commend;
import top.leftblue.publish.module.NewPost;

public class Test {

public static void main(String[] args) throws DocumentException {
public static void main(String[] args) throws Exception {
String xml = """
<?xml version="1.0" encoding="utf-8"?>
<methodResponse>
<fault>
<value>
<struct>
<member>
<name>faultCode</name>
<value>
<int>500</int>
</value>
</member>
<member>
<name>faultString</name>
<value>
<string>相同标题的博文已存在</string>
</value>
</member>
</struct>
</value>
</fault>
</methodResponse>""";
<methodCall>
<methodName>
metaWeblog.newPost
</methodName>
<params>
<param>
<value>
<string>
1
</string>
</value>
</param>
<param>
<value>
<string>
[email protected]
</string>
</value>
</param>
<param>
<value>
<string>
36129341DF84A28B63AEB80608D0A5A5E50C3784A2B841EF3ECA0496F8FC5E99
</string>
</value>
</param>
<param>
<value>
<struct>
<member>
<name>
title
</name>
<value>
<string>
test title2
</string>
</value>
</member>
<member>
<name>
description
</name>
<value>
<string>
test content
</string>
</value>
</member>
</struct>
</value>
</param>
<param>
<value>
<string>
true
</string>
</value>
</param>
</params>
</methodCall>""";
Commend commend = xml2Bean2(xml, Commend.class);
System.out.println(commend);

Class<MWAResult> clz = MWAResult.class;
MWAResult mwaResult = null;
try {
mwaResult = clz.getDeclaredConstructor().newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
}

private static final List<String> valueElement =
List.of("methodName", "string", "int", "boolean");

private static <T> T xml2Bean2(String xml, Class<T> clz) throws Exception {
Document document = DocumentHelper.parseText(xml);
Element root = document.getRootElement();
for (int i = 0; i < root.elements().size(); i++) {
Element element = root.elements().get(i);
// 处理结构体
if ("struct".equals(element.getName())){
if ("param".equals(element.getParent().getParent().getName())){
Element element = document.getRootElement();
T object = clz.getDeclaredConstructor().newInstance();
return element2Bean(element, object, null);
}

}
private static <T> T element2Bean(Element element, T object, Integer index) throws Exception {
Field[] fields = object.getClass().getDeclaredFields();
for (int i = 0; i < element.elements().size(); i++) {
Element child = element.elements().get(i);
if (valueElement.contains(child.getName())) {
setValue(fields[index == null ? i : index], object, child.getText().trim());
} else if ("struct".equals(child.getName())) {
Class<?> type = fields[index == null ? i : index].getType();
Object struct = buildStruct(child, type);
setValue(fields[index == null ? i : index], object, struct);
} else if ("params".equals(child.getName())) {
Object childObject = fields[i].getType().getDeclaredConstructor().newInstance();
element2Bean(child, childObject, null);
setValue(fields[i], object, childObject);
} else {
element2Bean(child, object, index == null ? i : index);
}
}
for (Element element : root.elements()) {
return object;
}

private static <T> T buildStruct(Element structElement, Class<T> clz)
throws NoSuchMethodException, InvocationTargetException, InstantiationException,
IllegalAccessException, NoSuchFieldException {
T object = clz.getDeclaredConstructor().newInstance();
for (Element member : structElement.elements()) {
Field memberField = clz.getDeclaredField(member.element("name").getText().trim());
setValue(memberField, object,
member.element("value").element("string").getText().trim());
}
return object;
}

private static void setValue(Field field, Object obj, Object val)
throws IllegalAccessException {
field.setAccessible(true);
if (field.getType() == boolean.class || field.getType() == Boolean.class) {
field.set(obj, Boolean.valueOf(val.toString()));
} else if (field.getType() == int.class || field.getType() == Integer.class) {
field.set(obj, Integer.valueOf(val.toString()));
} else {
field.set(obj, val);
}
System.out.println(document.getRootElement().elements().get(0).elements());
field.setAccessible(false);
}


Expand Down
12 changes: 12 additions & 0 deletions src/main/java/top/leftblue/publish/module/Commend.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package top.leftblue.publish.module;

import lombok.Data;

@Data
public class Commend {

private String methodName;

private NewPost newPost;

}
34 changes: 22 additions & 12 deletions src/main/java/top/leftblue/publish/module/NewPost.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package top.leftblue.publish.module;

import java.util.ArrayList;
import lombok.Data;
import lombok.EqualsAndHashCode;
import top.leftblue.publish.dto.MWACmd;
import top.leftblue.publish.dto.MWAPost;

import java.util.ArrayList;

@EqualsAndHashCode(callSuper = true)
@Data
public class NewPost extends ModuleConvertor<NewPost> {

public static final NewPost INSTANCE = new NewPost();

private String blogid;
private String username;
private String password;
Expand All @@ -36,7 +33,8 @@ public NewPost from(MWACmd mwaCmd) {
mwaCmd.getParams().get(3).getValue().getMembers().forEach(member -> {
switch (member.getStrName()) {
case MWAPost.Fields.title -> mwaPost.setTitle(member.getStrValue().getStrValue());
case MWAPost.Fields.description -> mwaPost.setDescription(member.getStrValue().getStrValue());
case MWAPost.Fields.description ->
mwaPost.setDescription(member.getStrValue().getStrValue());
}
});

Expand All @@ -47,15 +45,27 @@ public NewPost from(MWACmd mwaCmd) {
public MWACmd toMWARequest() {
MWACmd mwaCmd = new MWACmd("metaWeblog.newPost", new ArrayList<>(5));
MWACmd.Param postid =
MWACmd.Param.builder().value(MWACmd.Value.builder().strValue("1").build()).build();
MWACmd.Param username = MWACmd.Param.builder().value(MWACmd.Value.builder().strValue(this.username).build()).build();
MWACmd.Param password = MWACmd.Param.builder().value(MWACmd.Value.builder().strValue(this.password).build()).build();
MWACmd.Param.builder().value(MWACmd.Value.builder().strValue("1").build()).build();
MWACmd.Param username =
MWACmd.Param.builder().value(MWACmd.Value.builder().strValue(this.username).build())
.build();
MWACmd.Param password =
MWACmd.Param.builder().value(MWACmd.Value.builder().strValue(this.password).build())
.build();
MWACmd.Param members =
MWACmd.Param.builder().value(MWACmd.Value.builder().members(new ArrayList<>()).build()).build();
members.getValue().getMembers().add(MWACmd.Member.builder().strName(MWAPost.Fields.title).strValue(MWACmd.NameValue.builder().strValue(this.mwaPost.getTitle()).build()).build());
members.getValue().getMembers().add(MWACmd.Member.builder().strName(MWAPost.Fields.description).strValue(MWACmd.NameValue.builder().strValue(this.mwaPost.getDescription()).build()).build());
MWACmd.Param.builder().value(MWACmd.Value.builder().members(new ArrayList<>()).build())
.build();
members.getValue().getMembers().add(MWACmd.Member.builder().strName(MWAPost.Fields.title)
.strValue(MWACmd.NameValue.builder().strValue(this.mwaPost.getTitle()).build())
.build());
members.getValue().getMembers().add(
MWACmd.Member.builder().strName(MWAPost.Fields.description).strValue(
MWACmd.NameValue.builder().strValue(this.mwaPost.getDescription()).build())
.build());
MWACmd.Param publish =
MWACmd.Param.builder().value(MWACmd.Value.builder().strValue(String.valueOf(this.publish)).build()).build();
MWACmd.Param.builder()
.value(MWACmd.Value.builder().strValue(String.valueOf(this.publish)).build())
.build();
mwaCmd.getParams().add(postid);
mwaCmd.getParams().add(username);
mwaCmd.getParams().add(password);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public Map<String, String> newMediaObject(String blogid, String username, String
}

private void newPost(MWACmd mwaCmd) {
NewPost newPost = NewPost.INSTANCE.from(mwaCmd);
// NewPost newPost = NewPost.INSTANCE.from(mwaCmd);
}


Expand Down

0 comments on commit a8d75ec

Please sign in to comment.