org.loli.html.HtmlUtil.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of loli-core Show documentation
Show all versions of loli-core Show documentation
a simple micro service framework written in kotlin
The newest version!
package org.loli.html
import org.loli.util.RpcAnnotationUtil
import org.loli.util.RpcMethodDescription
/**
* html常用方法
*/
internal object HtmlUtil {
/**
* 字符到转义字符串
*/
private val charToEscape = HashMap()
init {
charToEscape['"'] = """
charToEscape['&'] = "&"
charToEscape['<'] = "<"
charToEscape['>'] = ">"
charToEscape[' '] = " "
charToEscape['\n'] = "
"
}
/**
* 把文本根据html要求进行转义
* @param txt 文本
* @return html文件
*/
fun escape(txt: String): String{
val buf = StringBuilder()
txt.forEach { c ->
if(charToEscape.containsKey(c)){
buf.append(charToEscape[c])
}else{
buf.append(c)
}
}
return buf.toString()
}
/**
* 根据方法描述,计算一个更可读的格式
*
* 参考:
*```
* add(a,b,c)
* a: Int, 第一个参数
* 返回: Int, 所有参数的和
*```
* @param method 方法定义
* @return 文本
*/
fun formatMethodDesc(method: RpcMethodDescription): String{
val buf = StringBuilder()
// 方法体
// buf.append(method.name).append('(')
// for(param in method.params){
// buf.append(param.name).append(',')
// }
// if(method.params.size > 0){
// buf.deleteCharAt(buf.length - 1)
// }
// buf.append(")\n")
buf.append("")
// params
method.params.forEach {
val typeName = RpcAnnotationUtil.simplifyTypeName(it.type.toString())
buf.append("${it.name} ${escape(typeName)}, ${escape(
it.desc
)} ")
}
// return
if(method.returnType.classifier != Unit::class) {
val typeName = RpcAnnotationUtil.simplifyTypeName(method.returnType.toString())
buf.append("返回 ${escape(typeName)}, ${escape(
method.returnDesc
)} ")
}
buf.append("
")
return buf.toString()
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy