From a8d75ecc3c0b8686113dbb34b0f6590e432724b5 Mon Sep 17 00:00:00 2001 From: gurenkai Date: Mon, 28 Aug 2023 23:33:00 +0800 Subject: [PATCH] :construction: --- build.gradle | 4 + src/main/java/top/leftblue/publish/Test.java | 164 +++++++++++++----- .../top/leftblue/publish/module/Commend.java | 12 ++ .../top/leftblue/publish/module/NewPost.java | 34 ++-- .../service/impl/MetaWebLogServiceImpl.java | 2 +- 5 files changed, 158 insertions(+), 58 deletions(-) create mode 100644 src/main/java/top/leftblue/publish/module/Commend.java diff --git a/build.gradle b/build.gradle index c71cd0d..e99f7a2 100644 --- a/build.gradle +++ b/build.gradle @@ -42,3 +42,7 @@ build { // build frontend before build tasks.getByName('compileJava').dependsOn('buildFrontend') } + +tasks.withType(JavaCompile) { + options.encoding = "UTF-8" +} \ No newline at end of file diff --git a/src/main/java/top/leftblue/publish/Test.java b/src/main/java/top/leftblue/publish/Test.java index 2139c6f..ca512d6 100644 --- a/src/main/java/top/leftblue/publish/Test.java +++ b/src/main/java/top/leftblue/publish/Test.java @@ -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 = """ - - - - - - - faultCode - - 500 - - - - faultString - - 相同标题的博文已存在 - - - - - - """; + + + metaWeblog.newPost + + + + + + 1 + + + + + + + ricky_grk@163.com + + + + + + + 36129341DF84A28B63AEB80608D0A5A5E50C3784A2B841EF3ECA0496F8FC5E99 + + + + + + + + + title + + + + test title2 + + + + + + description + + + + test content + + + + + + + + + + true + + + + + """; + Commend commend = xml2Bean2(xml, Commend.class); + System.out.println(commend); - Class clz = MWAResult.class; - MWAResult mwaResult = null; - try { - mwaResult = clz.getDeclaredConstructor().newInstance(); - } catch (Exception e) { - throw new RuntimeException(e); - } + } + + private static final List valueElement = + List.of("methodName", "string", "int", "boolean"); + + private static T xml2Bean2(String xml, Class 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 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 buildStruct(Element structElement, Class 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); } diff --git a/src/main/java/top/leftblue/publish/module/Commend.java b/src/main/java/top/leftblue/publish/module/Commend.java new file mode 100644 index 0000000..cea953d --- /dev/null +++ b/src/main/java/top/leftblue/publish/module/Commend.java @@ -0,0 +1,12 @@ +package top.leftblue.publish.module; + +import lombok.Data; + +@Data +public class Commend { + + private String methodName; + + private NewPost newPost; + +} diff --git a/src/main/java/top/leftblue/publish/module/NewPost.java b/src/main/java/top/leftblue/publish/module/NewPost.java index ed01058..3806acf 100644 --- a/src/main/java/top/leftblue/publish/module/NewPost.java +++ b/src/main/java/top/leftblue/publish/module/NewPost.java @@ -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 { - public static final NewPost INSTANCE = new NewPost(); - private String blogid; private String username; private String password; @@ -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()); } }); @@ -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); diff --git a/src/main/java/top/leftblue/publish/service/impl/MetaWebLogServiceImpl.java b/src/main/java/top/leftblue/publish/service/impl/MetaWebLogServiceImpl.java index 2e66db5..9ca1755 100644 --- a/src/main/java/top/leftblue/publish/service/impl/MetaWebLogServiceImpl.java +++ b/src/main/java/top/leftblue/publish/service/impl/MetaWebLogServiceImpl.java @@ -73,7 +73,7 @@ public Map newMediaObject(String blogid, String username, String } private void newPost(MWACmd mwaCmd) { - NewPost newPost = NewPost.INSTANCE.from(mwaCmd); + // NewPost newPost = NewPost.INSTANCE.from(mwaCmd); }