import org.b3log.latke.repository.jdbc.util.FieldDefinition;
import org.apache.commons.io.IOUtils;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class testRepositoriesMap {
/**
* /** to json "nullable".
*/
private static final String NULLABLE = "nullable";
/**
* /** to json "length".
*/
private static final String LENGTH = "length";
/**
* ** to json "iskey".
*/
private static final String ISKEY = "iskey";
/**
* the default keyname.
*/
public static final String OID = "oId";
/**
* /** to json "keys".
*/
private static final String KEYS = "keys";
/**
* /** to json "name".
*/
private static final String NAME = "name";
/**
* /** to json "repositories".
*/
private static final String REPOSITORIES = "faka";
/**
* /** to json "type".
*/
private static final String TYPE = "type";
/**
* Stores all repository filed definition in a Map.
*
* <p>
* key: the name of the repository value (or table name with prefix): list
* of all the FieldDefinition
* </p>
*/
private static Map<String, List<FieldDefinition>> repositoriesMap = null;
/**
* Repositories description (repository.json).
*/
private static JSONObject jsonObject;
/**
* @param args
* @throws JSONException
*/
public static void main(String[] args) throws JSONException {
// TODO Auto-generated method stub
final InputStream inputStream = testRepositoriesMap.class
.getClassLoader().getResourceAsStream("liulixin.json");
try {
final String description = IOUtils.toString(inputStream);
jsonObject = new JSONObject(description);
} catch (final Exception e) {
}
repositoriesMap = new HashMap<String, List<FieldDefinition>>();
final JSONArray repositoritArray = jsonObject
.getJSONArray(REPOSITORIES);
JSONObject repositoryObject;
JSONObject fieldDefinitionObject;
for (int i = 0; i < repositoritArray.length(); i++) {
repositoryObject = repositoritArray.getJSONObject(i);
final String repositoryName = repositoryObject.getString(NAME);
System.out.println(repositoryName);
final List<FieldDefinition> fieldDefinitions = new ArrayList<FieldDefinition>();
repositoriesMap.put(repositoryName, fieldDefinitions);
final JSONArray keysJsonArray = repositoryObject.getJSONArray(KEYS);
FieldDefinition definition;
for (int j = 0; j < keysJsonArray.length(); j++) {
fieldDefinitionObject = keysJsonArray.getJSONObject(j);
definition = fillFieldDefinitionData(fieldDefinitionObject);
fieldDefinitions.add(definition);
System.out.println(fieldDefinitionObject.getString(TYPE));
}
}
about_repositoryMap();
}
static void about_repositoryMap() {
System.out.println("inside about_repositoryMap()");
System.out.println(repositoriesMap.get("comment").get(1).getName());
System.out.println(repositoriesMap.get("comment").get(1).getType());
System.out.println(repositoriesMap.get("comment").get(1).getLength());
}
/**
* fillFieldDefinitionData.
*
* @param fieldDefinitionObject
* josn model
* @return {@link FieldDefinition}
* @throws JSONException
* JSONException
*/
private static FieldDefinition fillFieldDefinitionData(
final JSONObject fieldDefinitionObject) throws JSONException {
final FieldDefinition fieldDefinition = new FieldDefinition();
fieldDefinition.setName(fieldDefinitionObject.getString(NAME));
// final Integer type =
// JSONTYPETOJDBCTYPEMAP
// .get(fieldDefinitionObject.getString(TYPE));
// if (type == null) {
// LOGGER.severe("the type [" + fieldDefinitionObject.getString(TYPE)
// + "] no mapping defined now!!!!");
// throw new RuntimeException("the type ["
// + fieldDefinitionObject.getString(TYPE)
// + "] no mapping defined now!!!!");
// }
fieldDefinition.setType(fieldDefinitionObject.getString(TYPE));
fieldDefinition.setNullable(fieldDefinitionObject.optBoolean(NULLABLE));
fieldDefinition.setLength(fieldDefinitionObject.optInt(LENGTH));
fieldDefinition.setIsKey(fieldDefinitionObject.optBoolean(ISKEY));
/**
* the default key name is 'old'.
*/
if (OID.equals(fieldDefinition.getName())) {
fieldDefinition.setIsKey(true);
}
return fieldDefinition;
}
}
部分输出:
inside about_repositoryMap()
commentContent
String
2000
源代码:http://pan.baidu.com/share/link?shareid=842398635&uk=3878681452