37 #include <xercesc/dom/DOM.hpp>
38 #include <xercesc/framework/StdOutFormatTarget.hpp>
40 #include "xercesc/dom/DOMTreeWalker.hpp"
50 #include <boost/regex.hpp>
52 XERCES_CPP_NAMESPACE_USE
64 void parseInput(
string input,
string &command,
string &selector,
string &parentName,
string &childOrAtt,
string &contentOrVal) {
67 const boost::regex reBasic(
"(print|export|add|remove|help|rename|quit).*");
75 boost::regex_search(input, match, reBasic);
79 if (command ==
"print") {
80 boost::regex rePrintType(
"print\\s(list).*");
81 boost::regex_search(input, match, rePrintType);
84 if (selector.empty()) {
85 boost::regex reSubtreeRoot(
"print\\s(\\w*)");
86 boost::regex_search(input, match, reSubtreeRoot);
87 parentName = match[1];
89 boost::regex reSubtreeRoot(
"print\\slist\\s(\\w*)");
90 boost::regex_search(input, match, reSubtreeRoot);
91 parentName = match[1];
96 if (command ==
"export") {
97 boost::regex reExportFile(
"export\\s(\\w*).*");
98 boost::regex_search(input, match, reExportFile);
106 if (command ==
"add" || command ==
"remove" || command ==
"rename") {
108 boost::regex reElement(
".*(add|remove|rename)\\selement.*");
109 boost::regex reAttribute(
".*(add|remove|rename)\\sattribute.*");
111 if (boost::regex_match(input, reElement)) {
112 selector =
"element";
113 }
else if (boost::regex_match(input, reAttribute)) {
115 selector =
"attribute";
121 boost::regex reParentNode(
".*" + selector +
"\\s(\\w*).*");
122 if (boost::regex_search(input, match, reParentNode)) {
123 parentName = match[1];
126 boost::regex reChildOrAtt(
".*" + parentName +
"\\s(\\w*).*");
127 if (boost::regex_search(input, match, reChildOrAtt)) {
128 childOrAtt = match[1];
131 boost::regex reContentOrVal(
".*" + childOrAtt +
"\\s(\\w*).*");
132 if (boost::regex_search(input, match, reContentOrVal)) {
133 contentOrVal = match[1];
138 cout <<
"Invalid selector. Must be 'element' or 'attribute'. " << endl;
156 cout <<
"Please enter a command, or enter 'help' to see a list of all commands." << endl;
163 const boost::regex reAdd(
"(add|print|quit|export|remove|rename|help).*");
165 if (boost::regex_match(input, reAdd)) {
168 cout <<
"Invalid input, please try again." << endl;
182 void clearVars(
string &parentName,
string &childOrAtt,
string &contentOrVal) {
202 string parentName =
"";
203 string childOrAtt =
"";
204 string contentOrVal =
"";
205 string selector =
"";
206 string inputString =
"";
210 XMLPlatformUtils::Initialize();
213 XMLCh tempStr[3] = {chLatin_L, chLatin_S, chNull};
214 DOMImplementation *impl = DOMImplementationRegistry::getDOMImplementation(tempStr);
217 DOMDocument* doc = impl->createDocument();
220 while (inputString !=
"quit") {
225 if (command !=
"quit") {
228 parseInput(inputString, command, selector, parentName, childOrAtt, contentOrVal);
231 if (command ==
"add") {
233 if (parentName ==
"null") {
234 addRoot(doc, childOrAtt, contentOrVal);
236 addNode(doc, selector, parentName, childOrAtt, contentOrVal);
241 if (command ==
"remove") {
242 removeNode(doc, selector, parentName, childOrAtt);
246 if (command ==
"print") {
247 if (!doc->getFirstChild()) {
248 cout <<
"There is no tree to print. Must add a root first. " << endl;
252 if (selector.empty()) {
254 }
else if (selector ==
"list") {
257 cout <<
"Print format must be specified as 'xml' or 'list'." << endl;
263 if (command ==
"export") {
264 if (!doc->getFirstChild()) {
265 cout <<
"There is no tree to export. Must add a root first. " << endl;
271 if (command ==
"rename") {
272 renameNode(doc, selector, parentName, childOrAtt, contentOrVal);
275 if (command ==
"help") {
282 clearVars(parentName, childOrAtt, contentOrVal);
286 XMLPlatformUtils::Terminate();
287 cout <<
"Quitting Program." << endl;
296 int main(
int argC,
char* argV[]) {
void renameNode(DOMDocument *&doc, string selector, string oldNameOrParent, string newNameOrAtt, string newContentOrNewAtt)
void removeNode(DOMDocument *&doc, string removeType, string parentName, string childOrAtt)
void treeWalker(DOMDocument *doc, string subRoot)
void exportTree(DOMDocument *doc, string fileName)
void clearVars(string &parentName, string &childOrAtt, string &contentOrVal)
void parseInput(string input, string &command, string &selector, string &parentName, string &childOrAtt, string &contentOrVal)
void printTree(DOMDocument *doc, string subRoot)
void addNode(DOMDocument *&doc, string addType, string parentName, string childOrAtt, string contentOrVal)
int main(int argC, char *argV[])
void addRoot(DOMDocument *&doc, string rootName, string rootContent)