From 9b7524ca26efea618bd147721368d4796ae2a738 Mon Sep 17 00:00:00 2001 From: richie Date: Thu, 29 Aug 2019 17:29:16 +0800 Subject: [PATCH] =?UTF-8?q?http=20API=E4=BB=A5=E5=8F=8A=E5=8D=95=E5=85=83?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E8=A1=A5=E5=85=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- readme.md | 8 ++-- .../com/fanruan/api/cal/ContainerKit.java | 37 ++++++++++++++ .../com/fanruan/api/cal/ParameterKit.java | 15 +++++- .../fanruan/api/conf/BaseConfiguration.java | 14 ++++++ .../api/consts/EncodeConstantsKit.java | 22 --------- .../fanruan/api/macro/EncodeConstants.java | 48 +++++++++++++++++++ .../com/fanruan/api/net/http/HttpKit.java | 4 +- .../fanruan/api/net/http/rs/HttpRequest.java | 4 +- src/main/java/com/fanruan/api/xml/XmlKit.java | 39 +++++++++++++-- .../com/fanruan/api/cal/ContainerKitTest.java | 24 ++++++++++ .../com/fanruan/api/cal/ParameterKitTest.java | 46 ++++++++++++++++++ 11 files changed, 224 insertions(+), 37 deletions(-) create mode 100644 src/main/java/com/fanruan/api/cal/ContainerKit.java delete mode 100644 src/main/java/com/fanruan/api/consts/EncodeConstantsKit.java create mode 100644 src/main/java/com/fanruan/api/macro/EncodeConstants.java create mode 100644 src/test/java/com/fanruan/api/cal/ContainerKitTest.java diff --git a/readme.md b/readme.md index ec85088..6adaf32 100644 --- a/readme.md +++ b/readme.md @@ -24,7 +24,7 @@ SessionKit#getSession(@NotNull String sessionID); ## 提交新API要求 -* 所有的API方法的返回值和参数,仅允许使用基本类型、接口类型或者使用了@Open标记的对象类型。 +* 所有的API方法的返回值和参数,仅允许使用JDK自带的类、接口类型、插件抽象类、注解或者使用了@Open标记的对象类型。 * 所有的API方法均需要有单元测试覆盖。 @@ -54,10 +54,10 @@ SessionKit#getSession(@NotNull String sessionID); ## 如何提交新的API -1、访问[https://git.fanruan.com](https://git.fanruan.com)并注册一个自己的账号; +1、先访问[https://git.fanruan.com](https://git.fanruan.com)并注册一个自己的账号; -2、将[https://git.fanruan.com/fanruan/finekit](https://git.fanruan.com/fanruan/finekit) fork到自己账户下; +2、然后将[https://git.fanruan.com/fanruan/finekit](https://git.fanruan.com/fanruan/finekit) fork到自己账户下; -3、新增相关的API代码,并提交到自己的仓库; +3、克隆FineKit代码到本地,新增相关的API代码,并提交到自己的仓库; 4、提交Pull Request到FineKit主仓库,等待审核通过被合并即可。 \ No newline at end of file diff --git a/src/main/java/com/fanruan/api/cal/ContainerKit.java b/src/main/java/com/fanruan/api/cal/ContainerKit.java new file mode 100644 index 0000000..4dbbb0b --- /dev/null +++ b/src/main/java/com/fanruan/api/cal/ContainerKit.java @@ -0,0 +1,37 @@ +package com.fanruan.api.cal; + +import com.fr.general.FArray; +import com.fr.stable.ArrayProvider; + +import java.util.Collection; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019-08-29 + * 创建容器的工具类 + */ +public class ContainerKit { + + /** + * 创建一个插件可识别的数组对象 + * + * @param array 原始数组 + * @param 数组元素类型 + * @return 插件可识别的数组对象 + */ + public static ArrayProvider newArray(T[] array) { + return new FArray<>(array); + } + + /** + * 创建一个插件可识别的数组对象 + * + * @param collection 原始集合 + * @param 数组元素类型 + * @return 插件可识别的数组对象 + */ + public static ArrayProvider newArray(Collection collection) { + return new FArray<>(collection); + } +} diff --git a/src/main/java/com/fanruan/api/cal/ParameterKit.java b/src/main/java/com/fanruan/api/cal/ParameterKit.java index 26fa124..963e152 100644 --- a/src/main/java/com/fanruan/api/cal/ParameterKit.java +++ b/src/main/java/com/fanruan/api/cal/ParameterKit.java @@ -37,6 +37,17 @@ public class ParameterKit { return new Parameter(name, value); } + /** + * 从字符串中分析中有哪些需要的参数 + * + * @param text 字符串 + * @param hasColumnRow 是否需要分析格子类型的参数 + * @return 字符串中包含的参数集合 + */ + public static Parameter[] analyze4Parameters(String text, boolean hasColumnRow) { + return ParameterHelper.analyze4Parameters(text, hasColumnRow); + } + /** * 分析公式中所携带的参数 * @@ -71,11 +82,11 @@ public class ParameterKit { /** * 创建一个用于计算的参数对名字空间 * - * @param ps ParameterProvider的数组 + * @param ps 参数数组数组 * @return 名字空间,用于传递给算子做计算 */ - public static @NotNull ParameterMapNameSpace createParameterProvider(ParameterProvider[] ps) { + public static @NotNull ParameterMapNameSpace createParameterMapNameSpace(ParameterProvider[] ps) { return ParameterMapNameSpace.create(ps); } } diff --git a/src/main/java/com/fanruan/api/conf/BaseConfiguration.java b/src/main/java/com/fanruan/api/conf/BaseConfiguration.java index 4c5fbba..29147d6 100644 --- a/src/main/java/com/fanruan/api/conf/BaseConfiguration.java +++ b/src/main/java/com/fanruan/api/conf/BaseConfiguration.java @@ -7,6 +7,20 @@ import com.fr.config.DefaultConfiguration; * @author richie * @version 10.0 * Created by richie on 2019-08-16 + * 插件要实现配置类的时候,直接继承该抽象类即可 + *

+ * private static class MyConf extends BaseConfiguration { + * + * private static volatile MyConf instance = null; + * + * public static MyConf getInstance() { + * if (instance == null) { + * instance = getConfigInstance(MyConf.class); + * } + * return instance; + * } + * } + *

*/ public abstract class BaseConfiguration extends DefaultConfiguration { diff --git a/src/main/java/com/fanruan/api/consts/EncodeConstantsKit.java b/src/main/java/com/fanruan/api/consts/EncodeConstantsKit.java deleted file mode 100644 index ca90bab..0000000 --- a/src/main/java/com/fanruan/api/consts/EncodeConstantsKit.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.fanruan.api.consts; -import com.fr.stable.EncodeConstants; - -public class EncodeConstantsKit { - - public static final String ENCODING_UTF_8 = EncodeConstants.ENCODING_UTF_8; - - public static final String ENCODING_GBK = EncodeConstants.ENCODING_GBK; - - public static final String ENCODING_BIG5 = EncodeConstants.ENCODING_BIG5; - - public static final String ENCODING_ISO_8859_1 = EncodeConstants.ENCODING_ISO_8859_1; - - - public static final String ENCODING_UTF_16 = EncodeConstants.ENCODING_UTF_16; - - public static final String ENCODING_EUC_JP = EncodeConstants.ENCODING_EUC_JP; - - public static final String ENCODING_EUC_KR = EncodeConstants.ENCODING_EUC_KR; - - public static final String ENCODING_CP850 = EncodeConstants.ENCODING_CP850; -} diff --git a/src/main/java/com/fanruan/api/macro/EncodeConstants.java b/src/main/java/com/fanruan/api/macro/EncodeConstants.java new file mode 100644 index 0000000..a26e822 --- /dev/null +++ b/src/main/java/com/fanruan/api/macro/EncodeConstants.java @@ -0,0 +1,48 @@ +package com.fanruan.api.macro; + + +/** + * 编码常量 + */ +public interface EncodeConstants { + + /** + * utf-8编码 + */ + String ENCODING_UTF_8 = com.fr.stable.EncodeConstants.ENCODING_UTF_8; + + /** + * gbk编码 + */ + String ENCODING_GBK = com.fr.stable.EncodeConstants.ENCODING_GBK; + + /** + * big5编码,一般用于繁体中文 + */ + String ENCODING_BIG5 = com.fr.stable.EncodeConstants.ENCODING_BIG5; + + /** + * ios-8859-1编码 + */ + String ENCODING_ISO_8859_1 = com.fr.stable.EncodeConstants.ENCODING_ISO_8859_1; + + /** + * utf16编码 + */ + String ENCODING_UTF_16 = com.fr.stable.EncodeConstants.ENCODING_UTF_16; + + /** + * euc-jp编码 + */ + String ENCODING_EUC_JP = com.fr.stable.EncodeConstants.ENCODING_EUC_JP; + + /** + * euc-kr编码 + */ + String ENCODING_EUC_KR = com.fr.stable.EncodeConstants.ENCODING_EUC_KR; + + /** + * cp850编码 + */ + String ENCODING_CP850 = com.fr.stable.EncodeConstants.ENCODING_CP850; +} diff --git a/src/main/java/com/fanruan/api/net/http/HttpKit.java b/src/main/java/com/fanruan/api/net/http/HttpKit.java index de7ae96..6162745 100644 --- a/src/main/java/com/fanruan/api/net/http/HttpKit.java +++ b/src/main/java/com/fanruan/api/net/http/HttpKit.java @@ -1,6 +1,6 @@ package com.fanruan.api.net.http; -import com.fanruan.api.consts.EncodeConstantsKit; +import com.fanruan.api.macro.EncodeConstants; import com.fanruan.api.log.LogKit; import com.fanruan.api.net.http.rs.BaseHttpResponseHandle; import com.fanruan.api.net.http.rs.HttpRequest; @@ -641,7 +641,7 @@ public class HttpKit { */ public static String buildUrl(String url, Map params) { try { - return buildUrl(url, params, EncodeConstantsKit.ENCODING_UTF_8); + return buildUrl(url, params, EncodeConstants.ENCODING_UTF_8); } catch (UnsupportedEncodingException ignore) { } return url; diff --git a/src/main/java/com/fanruan/api/net/http/rs/HttpRequest.java b/src/main/java/com/fanruan/api/net/http/rs/HttpRequest.java index d29aaec..4a5250d 100644 --- a/src/main/java/com/fanruan/api/net/http/rs/HttpRequest.java +++ b/src/main/java/com/fanruan/api/net/http/rs/HttpRequest.java @@ -1,6 +1,6 @@ package com.fanruan.api.net.http.rs; -import com.fanruan.api.consts.EncodeConstantsKit; +import com.fanruan.api.macro.EncodeConstants; import com.fr.third.org.apache.http.HttpEntity; import com.fr.third.org.apache.http.client.config.RequestConfig; import org.jetbrains.annotations.NotNull; @@ -114,7 +114,7 @@ public class HttpRequest { private RequestConfig config = DEFAULT; @Nullable private HttpEntity httpEntity; - private String encoding = EncodeConstantsKit.ENCODING_UTF_8; + private String encoding = EncodeConstants.ENCODING_UTF_8; private HttpRequestType method = HttpRequestType.GET; private Builder() { diff --git a/src/main/java/com/fanruan/api/xml/XmlKit.java b/src/main/java/com/fanruan/api/xml/XmlKit.java index 548b788..37efb56 100644 --- a/src/main/java/com/fanruan/api/xml/XmlKit.java +++ b/src/main/java/com/fanruan/api/xml/XmlKit.java @@ -1,20 +1,49 @@ package com.fanruan.api.xml; +import com.fr.general.xml.GeneralXMLTools; +import com.fr.stable.xml.XMLPrintWriter; import com.fr.stable.xml.XMLReadable; import com.fr.stable.xml.XMLTools; +import com.fr.stable.xml.XMLable; import com.fr.stable.xml.XMLableReader; -import com.fr.stable.xml.XMLPrintWriter; import java.io.InputStream; +/** + * xml读写相关类 + */ public class XmlKit { /** * 从输入流中读取对象 - * @param xmlReadable xml读取对象 - * @param inputStream xml输入流 * + * @param read xml读取对象 + * @param in xml输入流 + */ + public static void readInputStreamXML(XMLReadable read, InputStream in) throws Exception { + XMLTools.readInputStreamXML(read, in); + } + + + /** + * 将xml读取为具体的java对象 + * + * @param reader xml读取器 + * @return java对象 */ - public static void readInputStreamXML(XMLReadable xmlReadable, InputStream inputStream) throws Exception { - XMLTools.readInputStreamXML(xmlReadable, inputStream); + public static XMLable readXMLable(XMLableReader reader) { + return GeneralXMLTools.readXMLable(reader); } + + /** + * 将java对象写为xml文件 + * + * @param writer xml写入器 + * @param xml 实际java对象 + * @param tagName xml标签名 + */ + public static void writeXMLable(XMLPrintWriter writer, XMLable xml, String tagName) { + GeneralXMLTools.writeXMLable(writer, xml, tagName); + } + + } diff --git a/src/test/java/com/fanruan/api/cal/ContainerKitTest.java b/src/test/java/com/fanruan/api/cal/ContainerKitTest.java new file mode 100644 index 0000000..09209c3 --- /dev/null +++ b/src/test/java/com/fanruan/api/cal/ContainerKitTest.java @@ -0,0 +1,24 @@ +package com.fanruan.api.cal; + +import com.fr.stable.ArrayProvider; +import com.fr.third.guava.collect.Lists; +import org.junit.Assert; +import org.junit.Test; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019-08-29 + */ +public class ContainerKitTest { + + @Test + public void newArray() { + + ArrayProvider array = ContainerKit.newArray(new String[]{"abc", "xyz"}); + Assert.assertEquals("xyz", array.elementAt(1)); + + ArrayProvider array2 = ContainerKit.newArray(Lists.newArrayList("abc", "xyz")); + Assert.assertEquals("abc", array2.elementAt(0)); + } +} \ No newline at end of file diff --git a/src/test/java/com/fanruan/api/cal/ParameterKitTest.java b/src/test/java/com/fanruan/api/cal/ParameterKitTest.java index 568949f..9292107 100644 --- a/src/test/java/com/fanruan/api/cal/ParameterKitTest.java +++ b/src/test/java/com/fanruan/api/cal/ParameterKitTest.java @@ -31,6 +31,33 @@ public class ParameterKitTest extends Prepare { Assert.assertArrayEquals(new String[]{"p1", "p2", "p4", "p5"}, names); } + @Test + public void analyze4Parameters() { + String sql = "SELECT * FROM Equipment where 'Tel' = ${if(true, 1, 2)}"; + ParameterProvider[] parameters = ParameterKit.analyze4Parameters(sql, false); + Assert.assertEquals(0, parameters.length); + parameters = ParameterKit.analyze4Parameters(sql, true); + Assert.assertEquals(0, parameters.length); + sql = "SELECT * FROM s订单 where 1=1 ${if(len(aa)==0,\"\",\" and 货主城市 in (\"+\"'\"+treelayer(aa,true,\"\\',\\'\")+\"'\"+\")\")}"; + parameters = ParameterKit.analyze4Parameters(sql, false); + Assert.assertEquals(1, parameters.length); + Assert.assertEquals(parameters[0].getName(), "aa"); + sql = "SELECT * FROM Equipment where 'Tel' = ${if(false, 1, 2)}"; + parameters = ParameterKit.analyze4Parameters(sql, false); + Assert.assertEquals(0, parameters.length); + parameters = ParameterKit.analyze4Parameters(sql, true); + Assert.assertEquals(0, parameters.length); + sql = "SELECT * FROM s订单 where 1=1 ${if(len(aa)==0,\"\",\" and 货主城市 in (\"+\"'\"+treelayer(aa,false,\"\\',\\'\")+\"'\"+\")\")}"; + parameters = ParameterKit.analyze4Parameters(sql, false); + Assert.assertEquals(1, parameters.length); + Assert.assertEquals(parameters[0].getName(), "aa"); + sql = "SELECT * FROM s订单 where 1=1 ${if(len(aa)==0,\"\",\" and 货主城市 in (\"+\"'\"+treelayer(aa,\"true\",\"\\',\\'\")+\"'\"+\")\")}"; + parameters = ParameterKit.analyze4Parameters(sql, false); + Assert.assertEquals(1, parameters.length); + Assert.assertEquals(parameters[0].getName(), "aa"); + + } + @Test public void createParameterMapNameSpace() { Map map = new HashMap<>(); @@ -49,6 +76,25 @@ public class ParameterKitTest extends Prepare { } } + @Test + public void createParameterMapNameSpace2() { + ParameterProvider[] parameters = new ParameterProvider[]{ + ParameterKit.newParameter("p1", 100), + ParameterKit.newParameter("p2", 200) + }; + NameSpace ns = ParameterKit.createParameterMapNameSpace(parameters); + Calculator calculator = Calculator.createCalculator(); + calculator.pushNameSpace(ns); + try { + Object r = calculator.eval("=$p1 + $p2"); + Assert.assertEquals(300, r); + } catch (UtilEvalError utilEvalError) { + Assert.fail(); + } finally { + calculator.removeNameSpace(ns); + } + } + @Test public void newParameter() { ParameterProvider provider = ParameterKit.newParameter();