1 package liquibase.serializer.core.string;
2
3 import liquibase.change.Change;
4 import liquibase.change.ChangeProperty;
5 import liquibase.change.ColumnConfig;
6 import liquibase.change.ConstraintsConfig;
7 import liquibase.change.custom.CustomChange;
8 import liquibase.changelog.ChangeSet;
9 import liquibase.changelog.DatabaseChangeLog;
10 import liquibase.exception.UnexpectedLiquibaseException;
11 import liquibase.serializer.ChangeLogSerializer;
12 import liquibase.sql.visitor.SqlVisitor;
13 import liquibase.util.StringUtils;
14
15 import java.lang.reflect.Field;
16 import java.util.Collection;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.SortedSet;
20 import java.util.TreeSet;
21 import java.io.OutputStream;
22 import java.io.IOException;
23
24 public class StringChangeLogSerializer implements ChangeLogSerializer {
25
26 private static final int INDENT_LENGTH = 4;
27
28 public String[] getValidFileExtensions() {
29 return new String[] { "txt" };
30 }
31
32 public String serialize(DatabaseChangeLog databaseChangeLog) {
33 return null;
34 }
35
36 public String serialize(Change change) {
37 return change.getChangeMetaData().getName() + ":" + serializeObject(change, 1);
38 }
39
40 public String serialize(SqlVisitor visitor) {
41 return visitor.getName() + ":" + serializeObject(visitor, 1);
42 }
43
44 private String serializeObject(Object objectToSerialize, int indent) {
45 try {
46 StringBuffer buffer = new StringBuffer();
47 buffer.append("[");
48
49 SortedSet<String> values = new TreeSet<String>();
50 Class<? extends Object> classToCheck = objectToSerialize.getClass();
51 while (!classToCheck.equals(Object.class)) {
52 for (Field field : classToCheck.getDeclaredFields()) {
53 field.setAccessible(true);
54 ChangeProperty changePropertyAnnotation = field.getAnnotation(ChangeProperty.class);
55 if (changePropertyAnnotation != null && !changePropertyAnnotation.includeInSerialization()) {
56 continue;
57 }
58 if (field.getName().equals("serialVersionUID")) {
59 continue;
60 }
61 if (field.getName().equals("$VRc")) {
62 continue;
63 }
64 if (field.getName().equals("serialVersionUID")) {
65 continue;
66 }
67
68 String propertyName = field.getName();
69
70 Object value = field.get(objectToSerialize);
71 if (value instanceof ColumnConfig) {
72 values.add(indent(indent)
73 + serializeColumnConfig((ColumnConfig) field.get(objectToSerialize), indent + 1));
74 } else if (value instanceof ConstraintsConfig) {
75 values.add(indent(indent)
76 + serializeConstraintsConfig((ConstraintsConfig) field.get(objectToSerialize),
77 indent + 1));
78 } else if (value instanceof CustomChange) {
79 values.add(indent(indent)
80 + serializeCustomChange((CustomChange) field.get(objectToSerialize), indent + 1));
81 } else {
82 if (value != null) {
83 if (value instanceof Map) {
84 values.add(indent(indent) + propertyName + "="
85 + serializeObject((Map) value, indent + 1));
86 } else if (value instanceof Collection) {
87 values.add(indent(indent) + propertyName + "="
88 + serializeObject((Collection) value, indent + 1));
89 } else {
90 values.add(indent(indent) + propertyName + "=\"" + value.toString() + "\"");
91 }
92 }
93 }
94 }
95 classToCheck = classToCheck.getSuperclass();
96 }
97
98 if (values.size() > 0) {
99 buffer.append("\n");
100 buffer.append(StringUtils.join(values, "\n"));
101 buffer.append("\n");
102 }
103 buffer.append(indent(indent - 1)).append("]");
104 return buffer.toString().replace("\r\n", "\n").replace("\r", "\n");
105
106 } catch (Exception e) {
107 throw new UnexpectedLiquibaseException(e);
108 }
109
110 }
111
112 private String indent(int indent) {
113 return StringUtils.repeat(" ", INDENT_LENGTH * indent);
114 }
115
116 private String serializeObject(Collection collection, int indent) {
117 if (collection.size() == 0) {
118 return "[]";
119 }
120
121 String returnString = "[\n";
122 for (Object object : collection) {
123 if (object instanceof ColumnConfig) {
124 returnString += indent(indent) + serializeColumnConfig((ColumnConfig) object, indent + 1) + ",\n";
125 } else {
126 returnString += indent(indent) + object.toString() + ",\n";
127 }
128 }
129 returnString = returnString.replaceFirst(",$", "");
130 returnString += indent(indent - 1) + "]";
131
132 return returnString;
133
134 }
135
136 private String serializeObject(Map collection, int indent) {
137 if (collection.size() == 0) {
138 return "[]";
139 }
140
141 String returnString = "{\n";
142 for (Object key : new TreeSet(collection.keySet())) {
143 returnString += indent(indent) + key.toString() + "=\"" + collection.get(key) + "\",\n";
144 }
145 returnString = returnString.replaceFirst(",$", "");
146 returnString += indent(indent - 1) + "}";
147
148 return returnString;
149
150 }
151
152 public String serialize(ColumnConfig columnConfig) {
153 return null;
154 }
155
156 private String serializeColumnConfig(ColumnConfig columnConfig, int indent) {
157 return "column:" + serializeObject(columnConfig, indent);
158 }
159
160 private String serializeConstraintsConfig(ConstraintsConfig constraintsConfig, int indent) {
161 return "constraints:" + serializeObject(constraintsConfig, indent);
162 }
163
164 private String serializeCustomChange(CustomChange customChange, int indent) {
165 return "customChange:" + serializeObject(customChange, indent);
166 }
167
168 public String serialize(ChangeSet changeSet) {
169 return null;
170 }
171
172 public void write(List<ChangeSet> changeSets, OutputStream out) throws IOException {
173
174 }
175 }