diff --git a/Command.cpp b/Command.cpp new file mode 100644 index 0000000..aee8334 --- /dev/null +++ b/Command.cpp @@ -0,0 +1,229 @@ +/**Copyright (c) 2013 Jian Luo (romain_cool@163.com) +* Command.cpp +* The implementation of Command class: implement all the functions in Command class +*/ +#include "Command.h" +#include +#include +#include + +using namespace std; + +/** +* Default Constructor +*/ +Command::Command(void) +{ + m_shellName = "AB"; + + string jsonStr = ReadFile(); + + m_jsonList = new JSONList(jsonStr); +} + +/** +* Constructor +* @param shellName: the name of the shell +*/ +Command::Command(string shellName) +{ + m_shellName = shellName; + + string jsonStr = ReadFile(); + + m_jsonList = new JSONList(jsonStr); +} + +Command::~Command(void) +{ + delete m_jsonList; +} + +/** +* Run the command application +*/ +void Command::Run(void) +{ + bool quitFlag = false; + while (!quitFlag) + { + DisplayShell(); + int com = 0; + com = ReadString(); + + switch (com) + { + case 0: + cout << "Command Wrong!\n\n"; + GetHelp(); + break; + case 1: + m_jsonList->getls(); + break; + case 2: + m_jsonList->getcd(m_secondCmd); + break; + case 3: + m_jsonList->getcat(m_secondCmd); + break; + case 4: + m_jsonList->getadd(); + break; + case 5: + m_jsonList->getremove(); + break; + case 6: + GetHelp(); + break; + case 7: + if (m_jsonList->GetDirty()) + { + WriteFile(); + } + quitFlag = true; + break; + default: + break; + } + } +} + +/** +* Display the shell name +*/ +void Command::DisplayShell() +{ + cout << m_shellName << ">"; +} + +/** +* Read a command line from console +*/ +string Command::GetFromConsole() +{ + string line = ""; + getline(cin,line); + return line; +} + +/** +* Analyze the command line +* @return com: command type, 0-7 +*/ +int Command::ReadString() +{ + int com = 0; + string line = ""; + + line = GetFromConsole(); + + // delete the space in front of the command line + int start = 0; + while (line[start] == ' ') + { + start++; + } + line = line.substr(start); + + if (line.empty()) + { + return com; + } + + int i = line.find(" "); + if (i == -1)// only have the first command + { + m_firstCmd = line; + m_secondCmd = ""; + }else + { + m_firstCmd = line.substr(0,i); + m_secondCmd = line.substr(i+1); + + // delete the space in front of the second command + int j = 0; + while (m_secondCmd[j] == ' ') + { + j++; + } + m_secondCmd = '\"' + m_secondCmd.substr(j) + '\"'; + } + + if ("ls" == m_firstCmd && m_secondCmd.empty()) + { + com = 1; + }else if ("cd" == m_firstCmd && !m_secondCmd.empty()) + { + com = 2; + }else if ("cat" == m_firstCmd && !m_secondCmd.empty()) + { + com = 3; + }else if ("add" == m_firstCmd && m_secondCmd.empty()) + { + com = 4; + }else if ("remove" == m_firstCmd && m_secondCmd.empty()) + { + com = 5; + }else if ("!help" == m_firstCmd && m_secondCmd.empty()) + { + com = 6; + }else if ("!quit" == m_firstCmd && m_secondCmd.empty()) + { + com = 7; + } + + return com; +} + +/** +* Read data from file "data.txt" +* @return jsonStr: the data string saved int file "data.txt" +*/ +string Command::ReadFile() +{ + ifstream inFile; + inFile.open("data.txt"); + if (!inFile) + { + cout << "File open error, data load failure!\n"; + exit(0); + } + + string jsonStr; + getline(inFile, jsonStr); + inFile.close(); + + return jsonStr; +} + +/** +* Write data into file "data.txt" when quit from application. +*/ +void Command::WriteFile() +{ + ofstream outFile("data.txt"); + if (!outFile) + { + cout << "File open error, data save failure!\n"; + exit(0); + } + outFile << m_jsonList->GetValue(m_jsonList->GetRootNode()); + outFile.close(); +} + + +/** +* Get help message for the command application +*/ +void Command::GetHelp() +{ + string sb = "Help!\n"; + sb.append("ls :command to list the items in current position\n"); + sb.append("cd [entry] :command to go to the entry like go to a directory\n"); + sb.append("cat [key] :command to display th item data\n"); + sb.append("add :command to add new address entry to JSON\n"); + sb.append("remove [key] :command to remove one or more address entries\n"); + sb.append("!help :commadn to get help\n"); + sb.append("!quit :command to quit from the application\n"); + cout << sb; +} diff --git a/Command.h b/Command.h new file mode 100644 index 0000000..390de31 --- /dev/null +++ b/Command.h @@ -0,0 +1,41 @@ +/**Copyright (c) 2013 Jian Luo (romain_cool@163.com) +* Command.h +* Command class: acts as the interface between the user and the application +*/ +#ifndef COMMAND +#define COMMAND + +#include"JSONList.h" + +class Command +{ +public: + Command(void); + Command(std::string shell); + ~Command(void); + + void Run(void); + + std::string GetFromConsole(); + + int ReadString(); + + std::string ReadFile(); + + void WriteFile(); + + void DisplayShell(); + + void GetHelp(); + +private: + JSONList *m_jsonList; + + std::string m_firstCmd; + std::string m_secondCmd; + + std::string m_shellName; +}; + +#endif + diff --git a/JSONList.cpp b/JSONList.cpp new file mode 100644 index 0000000..e7b8645 --- /dev/null +++ b/JSONList.cpp @@ -0,0 +1,709 @@ +/**Copyright (c) 2013 Jian Luo (romain_cool@163.com) +* JSONList.cpp +* The implementation of JSONList class: implement all the functions in JSONList class +*/ +#include "JSONList.h" +#include +#include + +using namespace std; + +/** +* Default Constructor +*/ +JSONList::JSONList(void) +{ + m_pRootNode = new JSONNode(); + m_pRootNode->key = "root"; + m_pRootNode->pNext = NULL; + m_pRootNode->pPrev = NULL; + m_pRootNode->pSub = NULL; + m_pCurrNode = NULL; + +} + +/** +* Constructor +* @param jsonStr: a complete JSON data structure for address book +*/ +JSONList::JSONList(string jsonStr) +{ + jsonStr = DeleteChar(jsonStr,' '); + if (!isLegalOuter(jsonStr)) + { + cout << "JSON data structure wrong, constructor failure!\n"; + exit(0); + } + + m_dirty = false; + m_pRootNode = new JSONNode(); + m_pRootNode->key = "root"; + m_pRootNode->pPrev = NULL; + m_pRootNode->pNext = NULL; + m_pRootNode->pSub = NULL; + + int i = jsonStr.find("{"); + int j = jsonStr.find(":"); + string key = jsonStr.substr(i+1,j-i-1); + if (key.empty()) + { + exit(0); + } + int k = jsonStr.rfind("}",jsonStr.size()-2); + jsonStr = jsonStr.substr(j+1,k-j); + + JSONNode *node = new JSONNode(); + node->key = key; + if (jsonStr.empty()) + { + node->value = "{}"; + }else + { + node->value = "{}";//jsonStr; + } + + m_pCurrNode = node; + m_pRootNode->pSub = node; + node->pPrev = m_pRootNode; + node->pNext = NULL; + node->pSub = NULL; + + JSONNode * pFlag = m_pCurrNode; + int flag = 1; + while (!jsonStr.empty() && jsonStr != "{}") + { + i = jsonStr.find("\""); + j = jsonStr.find(":"); + k = jsonStr.find("}"); + key = jsonStr.substr(i,j-i); + string value = jsonStr.substr(j+1,k-j); + jsonStr = jsonStr.substr(k+1); + + if (jsonStr == "}") + { + jsonStr = ""; + } + + JSONNode *node = new JSONNode(); + node->key = key; + node->value = "{}"; + node->pNext = NULL; + if (flag == 1) + { + pFlag->pSub = node; + }else + { + pFlag->pNext = node; + } + flag++; + node->pPrev = pFlag; + pFlag = node; + + int subFlag = 1; + JSONNode *subPFlag = node; + while (!value.empty()) + { + int ii = value.find("\""); + int jj = value.find(":"); + int kk = value.find(","); + string subKey = value.substr(ii,jj-ii); + string subValue = value.substr(jj+1,kk-jj-1); + if (kk == -1) + { + subValue = value.substr(jj+1,value.size()-jj-2); + value = ""; + } + value = value.substr(kk+1); + + JSONNode *subNode = new JSONNode(); + subNode->key = subKey; + subNode->value = subValue; + subNode->pNext = NULL; + subNode->pSub = NULL; + if (subFlag == 1) + { + subPFlag->pSub = subNode; + }else + { + subPFlag->pNext = subNode; + } + subFlag++; + subNode->pPrev = subPFlag; + subPFlag = subNode; + } + + } +} + +/** +* Default Destructor +*/ +JSONList::~JSONList(void) +{ + DeleteNode(&m_pRootNode); + m_pRootNode = NULL; +} + +/** +* +*/ +void JSONList::DeleteNode(JSONNode **node) +{ + if (*node == NULL) + { + return; + } + + if ((*node)->pSub == NULL && (*node)->pNext == NULL) + { + JSONNode *pTempNode = (*node)->pPrev; + + delete (*node); + (*node) = NULL; + + return; + } + + DeleteNode(&(*node)->pNext); + (*node)->pNext = NULL; + + DeleteNode(&(*node)->pSub); + (*node)->pSub = NULL; + + DeleteNode(&(*node)); + (*node) = NULL; +} + +/** +* List all keys in current directory +*/ +void JSONList::getls() +{ + if (m_pCurrNode == NULL) + { + return; + } + + JSONNode *pTemp = m_pCurrNode; + while (pTemp != NULL) + { + cout << DeleteChar(pTemp->key, '\"') << " "; + pTemp = pTemp->pNext; + } + cout << endl; +} + +/** +* Go to a entry like go to a directory +* @param key: the key of a entry +*/ +void JSONList::getcd(string key) +{ + JSONNode *pTempNode = m_pRootNode->pSub; + while (pTempNode != NULL) + { + if (pTempNode->key == key) + { + if (pTempNode->pSub != NULL || m_pRootNode->pSub->pSub == NULL) + { + m_pCurrNode = pTempNode->pSub; + return; + }else if (m_pRootNode->pSub->pSub == NULL) + { + m_pCurrNode = NULL; + cout << "NULL in directory, !help\n"; + return; + } + } + + if (pTempNode->pNext != NULL) + { + pTempNode = pTempNode->pNext; + }else + { + pTempNode = pTempNode->pSub; + } + } + +} + +/** +* Display the value of the key +* @param key: the key +*/ +void JSONList::getcat(string key) +{ + JSONNode *pTempNode = m_pCurrNode; + while (pTempNode != NULL) + { + if (pTempNode->key == key) + { + cout << key << ": " << GetValue(pTempNode) << endl; + } + pTempNode = pTempNode->pNext; + } +} + +/** +* Add command: add a node with a given key and a given value +* Enter key and value with a given format +*/ +void JSONList::getadd() +{ + if (m_pCurrNode != NULL && (m_pCurrNode->pPrev->pPrev == NULL || m_pCurrNode->pPrev->pPrev->key != "root")) + { + cout << "Go to \"entries\" before add!\n"; + return; + } + string key; + string content; + cout << "key: "; + getline(cin, key); + key = '\"' + DeleteChar(key, ' ') + '\"'; + + if (isKeyExist(key)) + { + cout << key << " exist in database, enter a different one, try again!\n"; + return; + } + + cout << "value: "; + getline(cin, content); + content = DeleteChar(content, ' '); + + if (!isLegalInner(key + ':' + content)) + { + cout << "Enter wrong value!\n"; + cout << "Format example:\n key: lilei\n value: {\"age\":28,\"mobile\":\"13700000000\",\"address\":\"China\"}\n"; + return; + } + + JSONNode *node = new JSONNode(); + node->key = key; + node->value = content; + node->pNext = NULL; + node->pSub = NULL; + + JSONNode *pTempNode; + if (m_pCurrNode == NULL) + { + pTempNode = m_pRootNode->pSub; + }else + { + pTempNode = m_pCurrNode; + } + + if (m_pRootNode->pSub->pSub != NULL) + { + while (pTempNode->pNext != NULL) + { + pTempNode = pTempNode->pNext; + } + node->pPrev = pTempNode; + pTempNode->pNext = node; + }else + { + node->pPrev = pTempNode; + pTempNode->pSub = node; + } + + content = DeleteChar(content, ' '); + int subFlag = 1; + JSONNode *subPFlag = node; + while (!content.empty()) + { + int ii = content.find("\""); + int jj = content.find(":"); + int kk = content.find(","); + string subKey = content.substr(ii,jj-ii); + string subValue = content.substr(jj+1,kk-jj-1); + if (kk == -1) + { + subValue = content.substr(jj+1,content.size()-jj-2); + content = ""; + } + content = content.substr(kk+1); + JSONNode *subNode = new JSONNode(); + subNode->key = subKey; + subNode->value = subValue; + subNode->pNext = NULL; + subNode->pSub = NULL; + if (subFlag == 1) + { + subPFlag->pSub = subNode; + }else + { + subPFlag->pNext = subNode; + } + subFlag++; + subNode->pPrev = subPFlag; + subPFlag = subNode; + } + m_pCurrNode = m_pRootNode->pSub->pSub; + + m_dirty = true; +} + +/** +* Delete command: delete a node with a given key +* Enter a key which exists in database +*/ +void JSONList::getremove() +{ + if (m_pCurrNode->pPrev->pPrev == NULL || m_pCurrNode->pPrev->pPrev->key != "root") + { + cout << "Go to \"entries\" before remove!\n"; + return; + } + cout << "Please give the key: "; + + string key; + getline(cin, key); + key = '\"' + key + '\"'; + JSONNode *pTempNode = m_pCurrNode; + while (pTempNode != NULL) + { + if (pTempNode->key == key) + { + break; + } + pTempNode = pTempNode->pNext; + } + + if (pTempNode == NULL) + { + return; + } + + JSONNode *pTempPrev = pTempNode->pPrev; + + if (pTempNode->pPrev->pPrev->key == "root") + { + pTempPrev->pSub = pTempNode->pNext; + + m_pCurrNode = m_pCurrNode->pNext; + if (m_pCurrNode != NULL) + { + m_pCurrNode->pPrev = m_pRootNode->pSub; + } + m_pRootNode->pSub->pSub = m_pCurrNode; + }else + { + pTempPrev->pNext = pTempNode->pNext; + } + + DeleteNode(&(pTempNode->pSub)); + delete (pTempNode); + + m_dirty = true; +} + +/** +* Get the value of a JSONNode recursively +* @return: the value of a given node +*/ +string JSONList::GetValue(JSONNode *node) +{ + if (node->pSub == NULL) + { + return (node->value); + } + + string value = "{}"; + int len; + + JSONNode *pTempNode = node->pSub; + while (pTempNode != NULL) + { + len = value.size(); + value.insert(len-1,pTempNode->key); + + len = value.size(); + value.insert(len-1,":"); + + len = value.size(); + value.insert(len-1,GetValue(pTempNode)); + + pTempNode = pTempNode->pNext; + + if (pTempNode != NULL) + { + len = value.size(); + value.insert(len-1,","); + } + } + + return value; +} + +/** +* Delete a given char from a string +* @param str +* @param ch +*/ +string JSONList::DeleteChar(string str, char ch) +{ + int count = 0; + int len = str.size(); + for (int i = 0; i < len; i++) + { + if (str[i] == ch) + { + count++; + } + } + int *arr = new int[count]; + + int j = 0; + for (int i = 0; i < len; i++) + { + if (str[i] == ch) + { + arr[j++] = i; + if (j == count) + { + break; + } + } + } + + for (int i = count-1; i >= 0; i--) + { + str.erase(arr[i],1); + } + + delete []arr; + + return str; +} + +/** +* Judge the format of the outer of a JSON data structure +* Format: {"entries": {"xx": {}, "yy": {}}} +* @param content +*/ +bool JSONList::isLegalOuter(string content) +{ + int len = content.size(); + if (content[0] != '{' || content[len-1] != '}') + { + return false; + } + + content = content.substr(1,len-2); + len = content.size(); + int i = content.find(':'); + if (i == -1 || i == len-1) + { + return false; + } + + string key = content.substr(0,i); + if (key != "\"entries\"" || content[i+1] != '{' || content[len-1] != '}') + { + return false; + } + + content = content.substr(i+2,len-i-3); + len = content.size(); + if (content.empty()) + { + return true; + } + + i = content.find('}'); + if (i == -1) + { + return false; + } + + while (i < len-1) + { + if (content[i+1] != ',') + { + return false; + } + + if (!isLegalInner(content.substr(0,i+1))) + { + return false; + } + content = content.substr(i+2); + len = content.size(); + + i = content.find('}'); + if (i == -1) + { + return false; + } + } + + if (!isLegalInner(content)) + { + return false; + } + + return true; +} + +/** +* Judge the format of the inner of a JSON data structure +* Format: lilei: {\"age\":28,\"mobile\":\"13700000000\",\"address\":\"China\"} +* @param content +*/ +bool JSONList::isLegalInner(string content) +{ + int len = content.size(); + int i = content.find(':'); + if (i == -1 || i == len-1 || content[i+1] != '{' || content[len-1] != '}') + { + return false; + } + + string key = content.substr(0,i); + if (key[0] != '\"' || key[key.size()-1] != '\"' || key.substr(1,key.size()-2).empty()) + { + return false; + } + + content = content.substr(i+2,len-i-3); + len = content.size(); + i = content.find(','); + if (i == -1) + { + return false; + } + string str = content.substr(0,i); + int j = str.find(':'); + if (j == -1 || str.substr(0,j) != "\"age\"" || !isNumber(str.substr(j+1)) ) + { + return false; + } + + content = content.substr(i+1); + len = content.size(); + i = content.find(','); + if (i == -1) + { + return false; + } + str = content.substr(0,i); + j = str.find(':'); + if (j == -1 || str.substr(0,j) != "\"mobile\"" || !isMobile(str.substr(j+1)) ) + { + return false; + } + + content = content.substr(i+1); + len = content.size(); + + str = content; + j = str.find(':'); + if (j == -1 || str.substr(0,j) != "\"address\"" || !isAddress(str.substr(j+1)) ) + { + return false; + } + + return true; +} + +/** +* Judge the format of a number +* @param str +*/ +bool JSONList::isNumber(string str) +{ + int len = str.size(); + for (int i = 0; i < len; i++) + { + if (str[i] < '0' && str[i] > '9') + { + return false; + } + } + + if (str[0] == '0') + { + return false; + } + + return true; +} + +/** +* Judge the format of a mobile number +* @param str +*/ +bool JSONList::isMobile(string str) +{ + int len = str.size(); + if (len != 13) + { + return false; + } + + if (str[0] != '\"' || str[len-1] != '\"') + { + return false; + } + + str = str.substr(1,len-2); + + return isNumber(str); +} + +/** +* Judge the format of an address +* @param str +*/ +bool JSONList::isAddress(string str) +{ + int len = str.size(); + if (str[0] != '\"' || str[len-1] != '\"' || str.substr(1,len-2).empty()) + { + return false; + } + return true; +} + +/** +* Find a node with a given key +* @param key +*/ +bool JSONList::isKeyExist(string key) +{ + JSONNode *pTempNode = m_pCurrNode; + while (pTempNode !=NULL) + { + if (pTempNode->key == key) + { + return true; + } + pTempNode = pTempNode->pNext; + } + return false; +} + +/** +* Get the root node +*/ +JSONNode* JSONList::GetRootNode() +{ + return m_pRootNode; +} + +/** +* Get the current node +*/ +JSONNode* JSONList::GetCurrNode() +{ + return m_pCurrNode; +} + +/** +* Get the dirty flag +*/ +bool JSONList::GetDirty() +{ + return m_dirty; +} diff --git a/JSONList.h b/JSONList.h new file mode 100644 index 0000000..3f6d1f7 --- /dev/null +++ b/JSONList.h @@ -0,0 +1,59 @@ +/**Copyright (c) 2013 Jian Luo (romain_cool@163.com) +* JSONList.h +* JSONList class: acts as the implementation of the commands of the application +*/ +#ifndef JSON_LIST +#define JSON_LIST + +#include + +struct JSONNode +{ + std::string key; + std::string value; + + JSONNode *pPrev; + JSONNode *pNext; + JSONNode *pSub; +}; + +class JSONList +{ +public: + JSONList(void); + JSONList(std::string jsonStr); + ~JSONList(void); + + void getls(); + void getcd(std::string key); + void getcat(std::string key); + void getadd(); + void getremove(); + + void DeleteNode(JSONNode **node); + + std::string DeleteChar(std::string str, char ch); + //std::string deleteQuote(std::string str); + + bool isFormatLegal(std::string content); + bool isKeyExist(std::string key); + + bool isLegalOuter(std::string content); + bool isLegalInner(std::string content); + bool isNumber(std::string str); + bool isMobile(std::string str); + bool isAddress(std::string str); + + std::string GetValue(JSONNode *node); + + JSONNode *GetRootNode(); + JSONNode *GetCurrNode(); + bool GetDirty(); + +private: + JSONNode *m_pRootNode; + JSONNode *m_pCurrNode; + bool m_dirty;//true if change the the address book +}; + +#endif diff --git a/Main.cpp b/Main.cpp new file mode 100644 index 0000000..4d1a2c7 --- /dev/null +++ b/Main.cpp @@ -0,0 +1,26 @@ +/**Copyright (c) 2013 Jian Luo (romain_cool@163.com) +* Main.cpp +* The entrance of the command application +*/ +#include"JSONList.h" +#include"Command.h" + +using namespace std; + +int main(int argc, char **argv) +{ + string name(argv[0]); + size_t loc = name.find_last_of("/"); + if (loc == string::npos) + { + Command cmd; + cmd.Run(); + }else + { + string temp(name.substr(loc+1,name.size()-loc)); + Command cmd(temp); + cmd.Run(); + } + + return 0; +} diff --git a/data.txt b/data.txt new file mode 100644 index 0000000..0b0416b --- /dev/null +++ b/data.txt @@ -0,0 +1 @@ +{"entries":{"lilei":{"age":22,"mobile":"12345678900","address":"China"},"hanmeimei":{"age":22,"mobile":"12345678900","address":"China"},"xiaoming":{"age":22,"mobile":"12345678900","address":"China"}}} \ No newline at end of file diff --git a/makefile b/makefile new file mode 100644 index 0000000..8c5d534 --- /dev/null +++ b/makefile @@ -0,0 +1,10 @@ +AddBook: Main.o Command.o JSONList.o + g++ -o AddBook Main.o Command.o JSONList.o +Main.o: Main.cpp Command.h + g++ -c Main.cpp +Command.o: Command.cpp JSONList.h + g++ -c Command.cpp +JSONList.o: JSONList.cpp + g++ -c JSONList.cpp +clean: + rm AddBook Main.o Command.o JSONList.o diff --git a/src/com/json/JSONOpe.java b/src/com/json/JSONOpe.java new file mode 100644 index 0000000..24fafd9 --- /dev/null +++ b/src/com/json/JSONOpe.java @@ -0,0 +1,250 @@ +package com.json; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Iterator; + +import junit.framework.TestCase; + +import org.json.JSONException; +import org.json.JSONObject; + +/** + * @author hwl + * + */ +public class JSONOpe extends TestCase{ + //当前json对象 + private JSONObject curobj; + //命令 第一个参数 + private String commandFirst = ""; + //命令第二个参数 + private String commandSecond = ""; + //控制是否退出 + private boolean quitFlag = false; + + /** + * 查看目录 + */ + public void getls(){ + Iterator ite = curobj.keys(); + while(ite.hasNext()){ + System.out.println(ite.next().toString()); + } + } + /** + * 进入到某个目录下 + * @param obj + * @param path + * @param flag + */ + public void getcd(JSONObject obj,String path,boolean flag){ + if(!flag){ + flag = obj.has(path); + if(flag) + try { + curobj = (JSONObject) obj.get(path); + } catch (JSONException e) { + //e.printStackTrace(); + System.out.println("cast error!"); + } + else{ + Iterator ite = obj.keys(); + while(ite.hasNext()){ + String key = ite.next().toString(); + try { + obj = (JSONObject) obj.get(key); + getcd(obj,path,flag); + } catch (JSONException e) { + //e.printStackTrace(); + System.out.println("json is end!"); + } + } + } + } + } + /** + * 查看信息 + * @param key + */ + public void getcat(String key){ + Iterator ite = curobj.keys(); + while(ite.hasNext()){ + String temp = ite.next().toString(); + if(temp.equals(key)){ + JSONObject o; + try { + o = (JSONObject) curobj.get(key); + System.out.println(o); + } catch (JSONException e) { + e.printStackTrace(); + } + + } + } + } + /** + * 增加命令 + */ + public void getadd(){ + System.out.println("key:"); + String key = ""; + key = getfromConsole(); + System.out.println("value:"); + String value = ""; + value = getfromConsole(); + try { + JSONObject temp = new JSONObject(value); + System.out.println("add success!"); + curobj.put(key, temp); + } catch (JSONException e) { + //e.printStackTrace(); + System.out.println("the format is wrong!try again"); + }finally{ + } + + } + /** + * 删除命令 + */ + public void getremove(){ + System.out.println("please give the key:"); + String key = ""; + key = getfromConsole();//接收关键字key + curobj.remove(key); + System.out.println(key+" was deleted from JSON"); + } + /** + * 从控制台读入命令 + * @return + */ + public String getfromConsole(){ + String line = ""; + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + try { + line = br.readLine(); + } catch (IOException e) { + e.printStackTrace(); + } + return line; + } + /** + * 分析命令 + * @return + */ + private int readString() { + int com = 0; + String line = null; + line = getfromConsole(); + if("".equals(line)){ + return com; + } + String s[] = line.split(" "); + if(s.length>0&&s.length<3){ + //看第一个参数 + String s0 = s[0]; + if("ls".equals(s0)){ + com = 1; + }else if("cd".endsWith(s0)){ + com = 2; + commandSecond = s[1];//第二个参数 + }else if("cat".equals(s0)){ + com = 3; + commandSecond = s[1]; + }else if("add".equals(s0)){ + com = 4; + }else if("remove".equals(s0)){ + com = 5; + }else if("!help".equals(s0)){ + com = 6; + }else if("!quit".equals(s0)){ + com = 7; + }else{ + return com; + } + }else{ + return com; + } + return com; + } + /** + * 帮助命令 + */ + public void gethelp(){ + StringBuffer sb = new StringBuffer(); + sb.append("ls :command to list the items in current position\n"); + sb.append("cd :command to go to the entry like go to a directory\n"); + sb.append("cat :command to display th item data\n"); + sb.append("add :command to add new address entry to JSON\n"); + sb.append("remove :command to remove one or more address entries\n"); + sb.append("!help :get help\n"); + sb.append("!quit :quit from the application"); + System.out.println(sb.toString()); + } + /** + * 退出命令 + * @param test + */ + public void getquit(JSONOpe test){ + test.setQuitFlag(false); + System.out.println("quited!"); + } + /** + * 程序入口 + * @param args + * @throws JSONException + */ + public static void main(String[] args) throws JSONException { + String sjson = "{\"entries\": {\"lilei\" : {\"age\": 27,\"mobile\" : \"13700000000\",\"address\" : \"Earth somewhere\"}, \"hanmeimei\" : {\"age\": 26,\"mobile\" : \"13700000001\",\"address\" : \"Earth somewhere else\"}}}"; + JSONOpe jsonOpe = new JSONOpe(); + JSONObject root = new JSONObject(sjson); + JSONObject curobj = root; + jsonOpe.setCurobj(curobj); + jsonOpe.setQuitFlag(true); + //循环输入信息 + while(jsonOpe.isQuitFlag()){ + int n = 0; + n = jsonOpe.readString(); + switch(n){ + case 0:System.out.println("input error!type \"!help\" for help\"");break; + case 1:jsonOpe.getls();break; + case 2:{ + boolean flag = false;//每次查询都重置标志位 + curobj = root; + jsonOpe.getcd(curobj,jsonOpe.getCommandSecond(),flag);break; + } + case 3:jsonOpe.getcat(jsonOpe.getCommandSecond());break; + case 4:jsonOpe.getadd();break; + case 5:jsonOpe.getremove();break; + case 6:jsonOpe.gethelp();break; + case 7:jsonOpe.getquit(jsonOpe);break; + default:break; + } + } + } + public JSONObject getCurobj() { + return curobj; + } + public void setCurobj(JSONObject curobj) { + this.curobj = curobj; + } + public String getCommandFirst() { + return commandFirst; + } + public void setCommandFirst(String commandFirst) { + this.commandFirst = commandFirst; + } + public String getCommandSecond() { + return commandSecond; + } + public void setCommandSecond(String commandSecond) { + this.commandSecond = commandSecond; + } + public boolean isQuitFlag() { + return quitFlag; + } + public void setQuitFlag(boolean quitFlag) { + this.quitFlag = quitFlag; + } +}