@@ -21,49 +21,48 @@ using namespace rapidxml;
2121
2222void ParseString (const FunctionCallbackInfo<Value>& args) {
2323 Isolate* isolate = args.GetIsolate ();
24-
25- if (args.Length () != 2 )
24+
25+ if (args.Length () != 2 )
2626 {
2727 isolate->ThrowException (Exception::TypeError (
2828 String::NewFromUtf8 (isolate, " Wrong number of arguments" )));
2929 return ;
3030 }
31-
31+
3232 if (!args[0 ]->IsString ())
3333 {
3434 isolate->ThrowException (Exception::TypeError (
3535 String::NewFromUtf8 (isolate, " First argument must be a string" )));
3636 return ;
3737 }
38-
38+
3939 if (!args[1 ]->IsFunction ())
4040 {
4141 isolate->ThrowException (Exception::TypeError (
4242 String::NewFromUtf8 (isolate, " Second argument must be a callback" )));
4343 return ;
4444 }
45-
45+
4646 String::Utf8Value param1 (args[0 ]->ToString ());
47-
47+
4848 char *xml = new char [param1.length () + 1 ];
4949 std::strcpy (xml, *param1);
50-
51- xml_document<> doc;
52- doc.parse <0 >(xml);
53-
50+
5451 Local<Object> obj = Object::New (isolate);
55-
56- std::stack<xml_node<> *> nodeStack;
57- std::stack<Local<Object> > objStack;
58-
59- nodeStack.push (doc.first_node ());
60- objStack.push (obj);
61-
6252 Local<Value> errorString = Null (isolate);
63-
53+
6454 try
6555 {
66-
56+
57+ xml_document<> doc;
58+ doc.parse <0 >(xml);
59+
60+ std::stack<xml_node<> *> nodeStack;
61+ std::stack<Local<Object> > objStack;
62+
63+ nodeStack.push (doc.first_node ());
64+ objStack.push (obj);
65+
6766 while (nodeStack.size () > 0 )
6867 {
6968 xml_node<> *node = nodeStack.top ();
@@ -73,18 +72,18 @@ void ParseString(const FunctionCallbackInfo<Value>& args) {
7372 objStack.pop ();
7473 continue ;
7574 }
76-
75+
7776 Local<Object> obj = objStack.top ();
78-
77+
7978 Local<Object> newObj = Object::New (isolate);
80-
79+
8180 bool hasChild = false ;
82-
81+
8382 // Need to reduce duplicate code here
8483 if (!node->first_node () || (node->first_node () && node->first_node ()->type () != node_cdata && node->first_node ()->type () != node_data))
8584 {
8685 hasChild = true ;
87-
86+
8887 Local<Array> lst;
8988 if (node != doc.first_node ())
9089 {
@@ -98,7 +97,7 @@ void ParseString(const FunctionCallbackInfo<Value>& args) {
9897 lst = Array::New (isolate, 1 );
9998 obj->Set (String::NewFromUtf8 (isolate, node->name ()), lst);
10099 }
101-
100+
102101 lst->Set (lst->Length ()-1 , newObj);
103102 }
104103 else
@@ -121,17 +120,17 @@ void ParseString(const FunctionCallbackInfo<Value>& args) {
121120 lst = Array::New (isolate, 1 );
122121 obj->Set (String::NewFromUtf8 (isolate, node->name ()), lst);
123122 }
124-
123+
125124 if (node->first_attribute ()) {
126125 Local<Object> attrObj = Object::New (isolate);
127126 newObj->Set (String::NewFromUtf8 (isolate, " _" ), String::NewFromUtf8 (isolate, node->first_node ()->value ()));
128127 newObj->Set (String::NewFromUtf8 (isolate, " $" ), attrObj);
129-
128+
130129 for (xml_attribute<> *attr = node->first_attribute (); attr; attr = attr->next_attribute ())
131130 {
132131 attrObj->Set (String::NewFromUtf8 (isolate, attr->name ()), String::NewFromUtf8 (isolate, attr->value ()));
133132 }
134-
133+
135134 lst->Set (lst->Length ()-1 , newObj);
136135 }
137136 else {
@@ -143,29 +142,29 @@ void ParseString(const FunctionCallbackInfo<Value>& args) {
143142 obj->Set (String::NewFromUtf8 (isolate, node->name ()), String::NewFromUtf8 (isolate, node->first_node ()->value ()));
144143 }
145144 }
146-
145+
147146 nodeStack.pop ();
148147 nodeStack.push (node->next_sibling ());
149-
148+
150149 if (hasChild) {
151-
150+
152151 if (node->first_attribute ())
153152 {
154153 Local<Object> attrObj = Object::New (isolate);
155154 newObj->Set (String::NewFromUtf8 (isolate, " $" ), attrObj);
156-
155+
157156 for (xml_attribute<> *attr = node->first_attribute (); attr; attr = attr->next_attribute ())
158157 {
159158 attrObj->Set (String::NewFromUtf8 (isolate, attr->name ()), String::NewFromUtf8 (isolate, attr->value ()));
160159 }
161160 }
162-
161+
163162 nodeStack.push (node->first_node ());
164163 objStack.push (newObj);
165164 }
166-
165+
167166 }
168-
167+
169168 }
170169 catch (const std::runtime_error& e)
171170 {
@@ -187,13 +186,13 @@ void ParseString(const FunctionCallbackInfo<Value>& args) {
187186 {
188187 errorString = String::NewFromUtf8 (isolate, " An unknown error occurred while parsing." );
189188 }
190-
189+
191190 delete[] xml;
192-
191+
193192 Local<Function> cb = Local<Function>::Cast (args[1 ]);
194193 const unsigned argc = 2 ;
195194 Local<Value> argv[argc] = { errorString, obj };
196-
195+
197196 cb->Call (Null (isolate), argc, argv);
198197}
199198
0 commit comments