Pretty SQL , Json , XML optimize

This commit is contained in:
MaxKey
2022-11-08 16:02:54 +08:00
parent 3ecfa6d480
commit 3297ee30aa
5 changed files with 68 additions and 12 deletions

View File

@@ -18,7 +18,9 @@
package org.maxkey.pretty;
public interface Pretty {
public final static String LINE_BREAK = "\n";
public String format(String source);
public String formatln(String source);
}

View File

@@ -18,27 +18,20 @@
package org.maxkey.pretty;
import org.maxkey.pretty.impl.JsonPretty;
import org.maxkey.pretty.impl.SqlPretty;
import org.maxkey.pretty.impl.XmlPretty;
public class PrettyFactory {
static final Pretty jsonPretty = new JsonPretty();
static final Pretty sqlPretty = new SqlPretty();
static final Pretty xmlPretty = new XmlPretty();
public static Pretty getJsonPretty() {
return jsonPretty;
return JsonPretty.getInstance();
}
public static Pretty getXmlPretty() {
return xmlPretty;
return XmlPretty.getInstance();
}
public static Pretty getSqlPretty() {
return sqlPretty;
return XmlPretty.getInstance();
}
}

View File

@@ -30,10 +30,23 @@ import com.google.gson.JsonParser;
public class JsonPretty implements Pretty{
static JsonPretty instance ;
public JsonPretty() {
}
public static JsonPretty getInstance() {
if (null == instance) {
synchronized (JsonPretty.class) {
if (instance == null) {
instance = new JsonPretty();
}
}
}
return instance;
}
/**
* prettyJson use jackson
* @param bean
@@ -68,6 +81,15 @@ public class JsonPretty implements Pretty{
return json;
}
/**
* prettyJson use Gson
* @param bean
* @return String
*/
public String formatln(Object bean){
return LINE_BREAK + format(bean);
}
/**
* prettyJson use Gson
* @param JSON String
@@ -76,5 +98,10 @@ public class JsonPretty implements Pretty{
public String format(String jsonString){
return format(JsonParser.parseString(jsonString));
}
@Override
public String formatln(String source) {
return LINE_BREAK + format(source);
}
}

View File

@@ -27,6 +27,8 @@ import org.maxkey.pretty.Pretty;
public class SqlPretty implements Pretty{
static SqlPretty instance ;
public static final String WHITESPACE = " \n\r\f\t";
private static final Set<String> BEGIN_CLAUSES = new HashSet<String>();
private static final Set<String> END_CLAUSES = new HashSet<String>();
@@ -80,10 +82,26 @@ public class SqlPretty implements Pretty{
}
public static SqlPretty getInstance() {
if (null == instance) {
synchronized (JsonPretty.class) {
if (instance == null) {
instance = new SqlPretty();
}
}
}
return instance;
}
@Override
public String format(String source) {
return new FormatProcess( source ).perform();
}
@Override
public String formatln(String source) {
return LINE_BREAK + format(source);
}
private static class FormatProcess {
boolean beginLine = true;

View File

@@ -29,10 +29,22 @@ import org.xml.sax.InputSource;
public class XmlPretty implements Pretty{
static XmlPretty instance ;
public XmlPretty() {
}
public static XmlPretty getInstance() {
if (null == instance) {
synchronized (JsonPretty.class) {
if (instance == null) {
instance = new XmlPretty();
}
}
}
return instance;
}
@Override
public String format(String xmlString){
@@ -56,4 +68,8 @@ public class XmlPretty implements Pretty{
}
}
@Override
public String formatln(String source) {
return LINE_BREAK + format(source);
}
}