Convert From ContentXml To SchemaDefinition
unknown
java
3 years ago
7.4 kB
5
Indexable
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;
public class Main {
private static final String rootPath = "/home/abdullah/Pictures";
private static final String inputFileName = "input.txt";
private static final String outputFileName = "output.txt";
private static final String FILE_SEPARATOR = "/";
private static final int PRETTY_PRINT_INDENT_FACTOR = 1;
public static void main (String[] args) throws IOException {
PrintWriter writer = new PrintWriter(rootPath + FILE_SEPARATOR + outputFileName);
BufferedReader bufferedReader = new BufferedReader(new FileReader(rootPath + FILE_SEPARATOR + inputFileName));
String line = bufferedReader.readLine();
int index = 1;
while (line != null) {
String json = getJSON(line);
writer.write("========================= " + index + " ================\n\n");
writer.write(json);
writer.write("\n\n====================================================\n\n");
index++;
line = bufferedReader.readLine();
}
writer.flush();
writer.close();
}
private static String getJSON(String xml) {
if (xml == null || xml.equals("")) return "";
String json = null;
try {
JSONObject xmlJSONObj = XML.toJSONObject(xml);
JSONObject schemaData = xmlJSONObj.getJSONObject("schemaData");
JSONObject schema = schemaData.getJSONObject("schema");
JSONObject newJSONObject = new JSONObject();
newJSONObject.put("adhocViews", getAdhocViews(schema));
newJSONObject.put("businessViews", getBusinessViews(schema));
// add folders
JSONObject foldersJson = null;
try {
foldersJson = schema.getJSONObject("folders");
} catch (JSONException e ) {
}
JSONArray newFolders = new JSONArray();
if (foldersJson != null) {
Iterator<String> keys = foldersJson.keys();
while (keys.hasNext()) {
String key = keys.next();
JSONObject folder = foldersJson.getJSONObject(key);
newFolders.put(folder);
}
}
newJSONObject.put("folders", newFolders);
// name
String schemaName = schema.getString("name");
newJSONObject.put("name", schemaName);
newJSONObject.put("description", schema.has("description") ? schema.getString("description") : null);
newJSONObject.put("label", schema.has("label") ? schema.getString("label") : null);
newJSONObject.put("department", schema.has("department") ? schema.getString("department") : null);
// permisionCode
newJSONObject.put("permissionCode", 7);
// add tables
newJSONObject.put("tables", new JSONArray());
// add joins
newJSONObject.put("joins", new JSONArray());
// loaderData
newJSONObject.put("loaderData", new JSONObject());
// scchemaId
newJSONObject.put("schemaId", 0);
JSONObject schemaDefinitionObject = new JSONObject();
schemaDefinitionObject.put("schemaDefinition", newJSONObject);
json = schemaDefinitionObject.toString(PRETTY_PRINT_INDENT_FACTOR);
} catch (JSONException je) {
throw je;
}
return json;
}
private static JSONArray getAdhocViews(JSONObject schema) {
JSONArray adhocViews = new JSONArray();
if (schema.has("adhocViews")) {
Object adhocViewsObj = schema.get("adhocViews");
if (adhocViewsObj instanceof String) {
return adhocViews;
}
JSONObject adhocViewsJSON = (JSONObject) adhocViewsObj;
Object adhocViewObject = adhocViewsJSON.get("adhocView");
if (adhocViewObject instanceof JSONObject) {
adhocViews.put(adhocViewObject);
} else if (adhocViewObject instanceof JSONArray) {
for (int i = 0; i < ((JSONArray) adhocViewObject).length(); i++) {
adhocViews.put(((JSONArray) adhocViewObject).get(i));
}
}
for (int i = 0; i < adhocViews.length(); i++) {
JSONObject adhocView = adhocViews.getJSONObject(i);
JSONArray columnsArr = getColumns(adhocView);
adhocView.put("columns", columnsArr);
}
}
return adhocViews;
}
private static JSONArray getBusinessViews(JSONObject schema) {
JSONArray businessViews = new JSONArray();
if (schema.has("businessViews")) {
Object businessViewsObj = schema.get("businessViews");
if (businessViewsObj instanceof String) {
return businessViews;
}
JSONObject businessViewsJSON = (JSONObject) businessViewsObj;
Object businessViewObject = businessViewsJSON.get("businessView");
if (businessViewObject instanceof JSONObject) {
businessViews.put(businessViewObject);
} else if (businessViewObject instanceof JSONArray) {
for (int i = 0; i < ((JSONArray) businessViewObject).length(); i++) {
businessViews.put(((JSONArray) businessViewObject).get(i));
}
}
for (int i = 0; i < businessViews.length(); i++) {
JSONObject businessView = businessViews.getJSONObject(i);
JSONArray columnsArr = getColumns(businessView);
businessView.put("columns", columnsArr);
}
}
return businessViews;
}
private static JSONArray getColumns(JSONObject view) {
JSONArray columns = new JSONArray();
if (view.has("column")) {
Object column = view.get("column");
if (column instanceof JSONObject) {
JSONObject columnJSONObject = view.getJSONObject("column");
if (columnJSONObject.has("type")) {
columnJSONObject.put("mappedDataType", columnJSONObject.getString("type"));
columnJSONObject.remove("type");
} else {
columnJSONObject.put("mappedDataType", "null");
}
columns.put(columnJSONObject);
} else {
columns = view.getJSONArray("column");
for (int j = 0; j < columns.length(); j++) {
JSONObject columnObj = columns.getJSONObject(j);
if (columnObj.has("type")) {
columnObj.put("mappedDataType", columnObj.getString("type"));
columnObj.remove("type");
} else {
columnObj.put("mappedDataType", "null");
}
};
}
view.remove("column");
}
return columns;
}
}
Editor is loading...