浏览代码

Pretty SQL , Json , XML optimize

MaxKey 2 年之前
父节点
当前提交
3297ee30aa

+ 3 - 1
maxkey-common/src/main/java/org/maxkey/pretty/Pretty.java

@@ -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);
 }

+ 4 - 11
maxkey-common/src/main/java/org/maxkey/pretty/PrettyFactory.java

@@ -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();
     }
     
 }

+ 27 - 0
maxkey-common/src/main/java/org/maxkey/pretty/impl/JsonPretty.java

@@ -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
@@ -70,11 +83,25 @@ public class JsonPretty  implements Pretty{
 	
 	/**
 	 * prettyJson use Gson
+	 * @param bean
+	 * @return String
+	 */
+	public  String formatln(Object bean){
+		return LINE_BREAK + format(bean);
+	}
+	
+	/**
+	 * prettyJson use Gson
 	 * @param JSON String
 	 * @return String
 	 */
 	public  String format(String  jsonString){
 		return format(JsonParser.parseString(jsonString));
 	}
+
+	@Override
+	public String formatln(String source) {
+		return LINE_BREAK + format(source);
+	}
 	
 }

+ 18 - 0
maxkey-common/src/main/java/org/maxkey/pretty/impl/SqlPretty.java

@@ -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;

+ 16 - 0
maxkey-common/src/main/java/org/maxkey/pretty/impl/XmlPretty.java

@@ -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);
+	}
 }