]>
Commit | Line | Data |
---|---|---|
b75a7d8f A |
1 | /****************************************************************************** |
2 | * Copyright (C) 2002, International Business Machines Corporation and | |
3 | * others. All Rights Reserved. | |
4 | ******************************************************************************/ | |
5 | #include "xml2txt.h" | |
6 | ||
7 | static bool DTDFLAG = false; | |
8 | static char* gTxtFile; | |
9 | static char* gXmlFile; | |
10 | static const char *sourceDir; | |
11 | static const char *destDir; | |
12 | static bool gDoNamespaces = false; | |
13 | static bool gDoSchema = false; | |
14 | static bool gDoCreate = false; | |
15 | static XMLCh* gEncodingName = 0; | |
16 | static XMLFormatter::UnRepFlags gUnRepFlags = XMLFormatter::UnRep_CharRef; | |
17 | static DOMParser::ValSchemes gValScheme = DOMParser::Val_Auto; | |
18 | static XMLFormatter* gFormatter = 0; | |
19 | ||
20 | ||
21 | ||
22 | enum | |
23 | { | |
24 | HELP, | |
25 | SOURCEDIR, | |
26 | DESTDIR, | |
27 | }; | |
28 | //#define UOPTION_TXT UOPTION_DEF("txt", 't', UOPT_NO_ARG) | |
29 | //#define UOPTION_RES UOPTION_DEF("res", 'r', UOPT_NO_ARG) | |
30 | ||
31 | UOption options[]={ | |
32 | UOPTION_HELP_H, | |
33 | UOPTION_SOURCEDIR, | |
34 | UOPTION_DESTDIR, | |
35 | }; | |
36 | ||
37 | ||
38 | ||
39 | #ifdef XP_MAC_CONSOLE | |
40 | #include <console.h> | |
41 | #endif | |
42 | ||
43 | ||
44 | // --------------------------------------------------------------------------- | |
45 | // | |
46 | // Usage() | |
47 | // | |
48 | // --------------------------------------------------------------------------- | |
49 | void usage() | |
50 | { | |
51 | cout << "\nUsage: XML2TXT [OPTIONS] [FILES]\n\n" | |
52 | "This program is used to convert XML files to TXT files.\n" | |
53 | "Please refer to the following options. Options are not \n" | |
54 | "case sensitive.\n" | |
55 | "Options:\n" | |
56 | "\t-s or --sourcedir \t source directory for files followed by path, default is current directory.\n" | |
57 | "\t-d or --destdir \t destination directory, followed by the path, default is current directory.\n" | |
58 | "\t-h or -? or --help \t this usage text.\n" | |
59 | "\nAttention: \n" | |
60 | "\tThe text file's encoding is the same as the source file's.\n" | |
61 | ||
62 | << endl; | |
63 | } | |
64 | ||
65 | int main(int argC, char* argV[]) | |
66 | { | |
67 | int retval = 0; | |
68 | const char* arg=NULL; | |
69 | ||
70 | try | |
71 | { | |
72 | XMLPlatformUtils::Initialize(); | |
73 | } | |
74 | ||
75 | catch(const XMLException& toCatch) | |
76 | { | |
77 | cerr << "Error during Xerces-c Initialization.\n" | |
78 | << " Exception message:" | |
79 | << DOMString(toCatch.getMessage()) << endl; | |
80 | return 1; | |
81 | } | |
82 | ||
83 | #ifdef XP_MAC_CONSOLE | |
84 | ||
85 | argC = ccommand((char***)&argV); | |
86 | #endif | |
87 | ||
88 | argC = u_parseArgs(argC, argV, (int32_t)(sizeof(options)/sizeof(options[0])), options); | |
89 | ||
90 | if(argC<0) { | |
91 | cout << "error in command line argument" << argV[-argC] << endl; | |
92 | } | |
93 | ||
94 | // Watch for special case help request | |
95 | if(argC<2 || options[HELP].doesOccur) { | |
96 | usage(); | |
97 | return argC < 0 ? U_ILLEGAL_ARGUMENT_ERROR : U_ZERO_ERROR; | |
98 | } | |
99 | ||
100 | if(options[SOURCEDIR].doesOccur) { | |
101 | sourceDir = options[SOURCEDIR].value; | |
102 | } | |
103 | else { | |
104 | #ifdef WIN32 | |
105 | destDir = _getcwd(NULL, 0); | |
106 | #else | |
107 | destDir = getcwd(NULL, 0); | |
108 | #endif | |
109 | } | |
110 | ||
111 | if(options[DESTDIR].doesOccur) { | |
112 | destDir = options[DESTDIR].value; | |
113 | } | |
114 | else { | |
115 | #ifdef WIN32 | |
116 | destDir = _getcwd(NULL, 0); | |
117 | #else | |
118 | destDir = getcwd(NULL, 0); | |
119 | #endif | |
120 | } | |
121 | ||
122 | for(int i = 1; i< argC; i++) { | |
123 | arg = getLongPathname(argV[i]); | |
124 | ||
125 | gXmlFile = CreateFile(arg, sourceDir); | |
126 | ||
127 | gTxtFile = CreateTxtName(arg, destDir); | |
128 | ||
129 | ||
130 | retval = ProcessTxtFile(); | |
131 | } | |
132 | XMLPlatformUtils::Terminate(); | |
133 | return retval; | |
134 | } | |
135 | ||
136 | ||
137 | ||
138 | int ProcessTxtFile() | |
139 | { | |
140 | int retval = 0; | |
141 | ||
142 | DOMParser* parser; | |
143 | DOMTreeErrorReporter* errReporter; | |
144 | parser = new DOMParser(); | |
145 | errReporter = new DOMTreeErrorReporter(); | |
146 | parser->setValidationScheme(gValScheme); | |
147 | parser->setDoNamespaces(true); | |
148 | parser->setDoSchema(gDoSchema); | |
149 | ||
150 | parser->setErrorHandler(errReporter); | |
151 | parser->setCreateEntityReferenceNodes(gDoCreate); | |
152 | parser->setToCreateXMLDeclTypeNode(true); | |
153 | ||
154 | // | |
155 | // Parse the XML file, catching any XML exceptions that might propogate | |
156 | // out of it. | |
157 | // | |
158 | bool errorsOccured = false; | |
159 | try | |
160 | { | |
161 | parser->parse(gXmlFile); | |
162 | int errorCount = parser->getErrorCount(); | |
163 | if (errorCount > 0) | |
164 | errorsOccured = true; | |
165 | } | |
166 | ||
167 | catch (const XMLException& e) | |
168 | { | |
169 | ||
170 | ||
171 | cerr << "An error occured during parsing\n Message: " | |
172 | << DOMString(e.getMessage()) << endl; | |
173 | errorsOccured = true; | |
174 | } | |
175 | ||
176 | ||
177 | catch (const DOM_DOMException& e) | |
178 | { | |
179 | cerr << "A DOM error occured during parsing\n DOMException code: " | |
180 | << e.code << endl; | |
181 | errorsOccured = true; | |
182 | } | |
183 | ||
184 | catch (...) | |
185 | { | |
186 | cerr << "An error occured during parsing\n " << endl; | |
187 | errorsOccured = true; | |
188 | } | |
189 | ||
190 | if(!errorsOccured && !errReporter->getSawErrors()) | |
191 | { | |
192 | DOM_Node document = parser->getDocument(); | |
193 | Check(document); //if check fails, exit(0); else excute the following code | |
194 | if(DTDFLAG == false){ | |
195 | cout << "DTD no assigned!" << endl; | |
196 | exit(0); | |
197 | } | |
198 | } | |
199 | ||
200 | // If the parse and doubt-check was successful, output the document data from the DOM tree | |
201 | if (!errorsOccured && !errReporter->getSawErrors()) | |
202 | { | |
203 | DOM_Node doc = parser->getDocument(); | |
204 | DOMPrintFormatTarget *formatTarget = new DOMPrintFormatTarget(gTxtFile); | |
205 | ||
206 | ||
207 | ||
208 | if (gEncodingName == 0) | |
209 | { | |
210 | DOMString encNameStr("UTF-8"); | |
211 | DOM_Node aNode = doc.getFirstChild(); | |
212 | if (aNode.getNodeType() == DOM_Node::XML_DECL_NODE) | |
213 | { | |
214 | DOMString aStr = ((DOM_XMLDecl &)aNode).getEncoding(); | |
215 | if (aStr != "") | |
216 | { | |
217 | encNameStr = aStr; | |
218 | } | |
219 | } | |
220 | unsigned int lent = encNameStr.length(); | |
221 | gEncodingName = new XMLCh[lent + 1]; | |
222 | XMLString::copyNString(gEncodingName, encNameStr.rawBuffer(), lent); | |
223 | gEncodingName[lent] = 0; | |
224 | } | |
225 | ||
226 | ||
227 | try | |
228 | { | |
229 | gFormatter = new XMLFormatter(gEncodingName, formatTarget, | |
230 | XMLFormatter::NoEscapes, gUnRepFlags); | |
231 | ofstream ofile(gTxtFile, ios::trunc); | |
232 | cout << doc; | |
233 | } | |
234 | catch (XMLException& e) | |
235 | { | |
236 | cerr << "An error occurred during creation of output transcoder. Msg is:" | |
237 | << endl | |
238 | << DOMString(e.getMessage()) << endl; | |
239 | retval = 3; | |
240 | } | |
241 | delete formatTarget; | |
242 | delete gFormatter; | |
243 | } | |
244 | delete errReporter; | |
245 | delete parser; | |
246 | parser = NULL; | |
247 | errReporter = NULL; | |
248 | delete gEncodingName; | |
249 | gEncodingName=NULL; | |
250 | return retval; | |
251 | } | |
252 | ||
253 | ||
254 | ||
255 | ||
256 | ||
257 | ||
258 | //---------------------------------------------------------------------------- | |
259 | // double-check before DOM Tree PrintOut | |
260 | //---------------------------------------------------------------------------- | |
261 | void Check( DOM_Node &document) | |
262 | { | |
263 | // Get the name and value out for convenience | |
264 | DOMString nodeName = document.getNodeName(); //<tag name>, type | |
265 | DOMString nodeValue = document.getNodeValue(); //<tag content> | |
266 | ||
267 | DOMString attributeKey, attributeVal; //(key/name)(val/filename) | |
268 | unsigned long lent = nodeValue.length(); | |
269 | switch (document.getNodeType()) | |
270 | { | |
271 | case DOM_Node::TEXT_NODE: | |
272 | { | |
273 | break; | |
274 | } | |
275 | ||
276 | case DOM_Node::PROCESSING_INSTRUCTION_NODE : | |
277 | { | |
278 | break; | |
279 | } | |
280 | ||
281 | ||
282 | case DOM_Node::DOCUMENT_NODE : | |
283 | { | |
284 | ||
285 | DOM_Node child = document.getFirstChild(); | |
286 | while( child != 0) | |
287 | { | |
288 | Check(child); | |
289 | child = child.getNextSibling(); | |
290 | } | |
291 | break; | |
292 | } | |
293 | ||
294 | case DOM_Node::ELEMENT_NODE : | |
295 | { | |
296 | DOM_NamedNodeMap attributes = document.getAttributes(); | |
297 | int attrCount = attributes.getLength(); | |
298 | int item_num=0; | |
299 | for (int i = 0; i < attrCount; i++) | |
300 | { | |
301 | DOM_Node attribute = attributes.item(i); | |
302 | ||
303 | if(attribute.getNodeName().equals("key")||attribute.getNodeName().equals("name")){ | |
304 | attributeKey = attribute.getNodeValue(); | |
305 | } | |
306 | else if(attribute.getNodeName().equals("val")||attribute.getNodeName().equals("filename")){ | |
307 | attributeVal = attribute.getNodeValue(); | |
308 | item_num = i; | |
309 | } | |
310 | else{ | |
311 | //call error report | |
312 | ErrorReport(document, 0); | |
313 | } | |
314 | } | |
315 | ||
316 | if(document.getParentNode().getNodeName().equals("array") && attributeKey!=NULL){ | |
317 | ErrorReport(document, 1); //ErrorType =1--the element in the array has name | |
318 | } | |
319 | else if(document.getParentNode().getNodeName().equals("table") && attributeKey==NULL){ | |
320 | ErrorReport(document, 2); //element in a table has no name | |
321 | } | |
322 | ||
323 | if(document.getNodeName().equals("table")) | |
324 | { | |
325 | //unsigned int Child_Num; | |
326 | if(document.hasChildNodes()) | |
327 | { | |
328 | ChildName* cn = new ChildName(); | |
329 | cn->SetNext(NULL); | |
330 | ChildName* head = CheckNameDuplicate(document, cn); | |
331 | DelChildName(head); | |
332 | } | |
333 | } | |
334 | else if(document.getNodeName().equals("array")) {} | |
335 | else if(document.getNodeName().equals("resourceBundle")) {} | |
336 | ||
337 | else if(document.getNodeName().equals("str")||document.getNodeName().equals("importBin")) | |
338 | { | |
339 | CheckEscape(attributes, attributeVal, item_num); | |
340 | } | |
341 | ||
342 | else if(document.getNodeName().equals("intVector")) | |
343 | { | |
344 | DOMString ivstring; | |
345 | ivstring = CheckIntvector(attributeVal, document); | |
346 | if(ivstring !=NULL) | |
347 | attributes.item(item_num).setNodeValue(ivstring); | |
348 | } | |
349 | ||
350 | else if(document.getNodeName().equals("int")) | |
351 | { | |
352 | CheckInt(attributeVal, document); | |
353 | } | |
354 | ||
355 | else if(document.getNodeName().equals("bin")) | |
356 | { | |
357 | CheckBin(attributeVal, document); | |
358 | } | |
359 | ||
360 | else if(document.getNodeName().equals("import")) {} | |
361 | else if(document.getNodeName().equals("alias")) {} | |
362 | else { | |
363 | ErrorReport(document, 6); | |
364 | } | |
365 | ||
366 | ||
367 | DOM_Node child = document.getFirstChild(); | |
368 | if (child != 0) | |
369 | { | |
370 | while( child != 0) | |
371 | { | |
372 | Check(child); | |
373 | child = child.getNextSibling(); | |
374 | } | |
375 | } | |
376 | break; | |
377 | } | |
378 | ||
379 | ||
380 | case DOM_Node::ENTITY_REFERENCE_NODE: | |
381 | { | |
382 | break; | |
383 | } | |
384 | ||
385 | ||
386 | case DOM_Node::CDATA_SECTION_NODE: | |
387 | { | |
388 | break; | |
389 | } | |
390 | ||
391 | ||
392 | case DOM_Node::COMMENT_NODE: | |
393 | { | |
394 | break; | |
395 | } | |
396 | ||
397 | ||
398 | case DOM_Node::DOCUMENT_TYPE_NODE: | |
399 | { | |
400 | DTDFLAG = true; | |
401 | break; | |
402 | } | |
403 | ||
404 | ||
405 | case DOM_Node::ENTITY_NODE: | |
406 | { | |
407 | break; | |
408 | } | |
409 | ||
410 | ||
411 | case DOM_Node::XML_DECL_NODE: | |
412 | { | |
413 | break; | |
414 | } | |
415 | ||
416 | ||
417 | default: | |
418 | cerr << "Unrecognized node type = " | |
419 | << (long)document.getNodeType() << endl; | |
420 | } | |
421 | } | |
422 | ||
423 | void CheckEscape(DOM_NamedNodeMap attributes, DOMString attributeVal, int item_num) | |
424 | { | |
425 | unsigned int len; | |
426 | char Escape[7] = {'\\', 'u', '0', '0', '2', '2', '\0'}; | |
427 | len = attributeVal.length(); | |
428 | DOMString fromStr; | |
429 | DOMString toStr; | |
430 | const XMLCh quote[] = {(unsigned short)0x22, (unsigned short) 0}; | |
431 | if(len>0) | |
432 | { | |
433 | for(unsigned int i=0; i<len; i++) | |
434 | { | |
435 | fromStr = attributeVal.substringData (i,1); | |
436 | char* temp=fromStr.transcode(); | |
437 | if(fromStr.equals(quote)) | |
438 | { | |
439 | toStr.appendData(Escape); | |
440 | } | |
441 | else | |
442 | toStr.appendData(fromStr); | |
443 | } | |
444 | attributes.item(item_num).setNodeValue(toStr); | |
445 | } | |
446 | } | |
447 | ||
448 | DOMString getAttributeKey(DOM_Node CNode) | |
449 | { | |
450 | DOM_NamedNodeMap attributes = CNode.getAttributes(); | |
451 | int attrCount = attributes.getLength(); | |
452 | DOMString attributeKey; | |
453 | ||
454 | for (int i = 0; i < attrCount; i++) | |
455 | { | |
456 | DOM_Node attribute = attributes.item(i); | |
457 | ||
458 | if(attribute.getNodeName().equals("key")) | |
459 | attributeKey = attribute.getNodeValue(); | |
460 | } | |
461 | return attributeKey; | |
462 | } | |
463 | ||
464 | void DelChildName(ChildName* cn) | |
465 | { | |
466 | ChildName* temp = cn->Next; | |
467 | while(temp!=NULL) | |
468 | { | |
469 | delete cn; | |
470 | cn = NULL; | |
471 | cn = temp; | |
472 | temp = temp->Next; | |
473 | } | |
474 | delete cn; | |
475 | } | |
476 | ||
477 | ChildName* CheckNameDuplicate(DOM_Node document, ChildName* cn) | |
478 | { | |
479 | DOM_Node CNode = document.getFirstChild(); | |
480 | ||
481 | while(CNode!=NULL) | |
482 | { | |
483 | if(CNode.getNodeName().equals("string")||CNode.getNodeName().equals("bin")||CNode.getNodeName().equals("int")||CNode.getNodeName().equals("intvector")||CNode.getNodeName().equals("import")||CNode.getNodeName().equals("table")||CNode.getNodeName().equals("array")) | |
484 | { | |
485 | DOMString cname = getAttributeKey(CNode); | |
486 | char* string = cname.transcode(); | |
487 | ChildName* temp = cn; | |
488 | while(temp->Next!=NULL) | |
489 | { | |
490 | if(cname.equals(temp->Name)) | |
491 | { | |
492 | DelChildName(cn); | |
493 | ErrorReport(CNode, 5); //name duplication | |
494 | } | |
495 | temp = temp ->Next; | |
496 | } | |
497 | ||
498 | ChildName* childname = new ChildName(); | |
499 | childname->SetName(cname); | |
500 | childname->SetNext(cn); | |
501 | cn = childname; | |
502 | } | |
503 | CNode = CNode.getNextSibling(); | |
504 | } | |
505 | return cn; | |
506 | } | |
507 | ||
508 | unsigned int GetCNodeNum(DOM_Node document) | |
509 | { | |
510 | unsigned int num=0; | |
511 | DOM_Node CNode = document.getFirstChild(); | |
512 | while(CNode!=NULL) | |
513 | { | |
514 | if(CNode.getNodeName().equals("string")||CNode.getNodeName().equals("bin")||CNode.getNodeName().equals("int")||CNode.getNodeName().equals("intvector")||CNode.getNodeName().equals("import")||CNode.getNodeName().equals("table")||CNode.getNodeName().equals("array")) | |
515 | num++; | |
516 | CNode = CNode.getNextSibling(); | |
517 | } | |
518 | return num; | |
519 | } | |
520 | ||
521 | void CheckBin(DOMString attributeVal, DOM_Node document) | |
522 | { | |
523 | char *stopstring; | |
524 | char toConv[2] = {'\0', '\0'}; | |
525 | char* string = attributeVal.transcode(); | |
526 | int count = strlen(string); | |
527 | if(count > 0) | |
528 | { | |
529 | if((count % 2)==0) | |
530 | { | |
531 | for(int i=0; i<count; i++) | |
532 | { | |
533 | toConv[0]=string[i]; | |
534 | int value = strtoul(toConv, &stopstring, 16); | |
535 | unsigned int len = stopstring-toConv; | |
536 | if(len!= strlen(toConv)) | |
537 | { | |
538 | ErrorReport(document, 4); //invalid bin value | |
539 | } | |
540 | } | |
541 | } | |
542 | else | |
543 | ErrorReport(document, 4); //invalid bin value | |
544 | } | |
545 | } | |
546 | ||
547 | ||
548 | void CheckInt(DOMString attributeVal, DOM_Node document) | |
549 | { | |
550 | char *stopstring; | |
551 | char* string= attributeVal.transcode(); | |
552 | long value = strtoul(string, &stopstring, 0); | |
553 | unsigned int len=stopstring-string; | |
554 | if(len!=strlen(string)) | |
555 | ErrorReport(document, 3); //invalid int value | |
556 | } | |
557 | ||
558 | DOMString CheckIntvector(DOMString attributeVal, DOM_Node document) | |
559 | { | |
560 | DOMString ivstring; | |
561 | char* string ; | |
562 | if(attributeVal != NULL) | |
563 | { | |
564 | string = attributeVal.transcode(); | |
565 | char integer[32]; | |
566 | char *stopstring; | |
567 | int i,j; | |
568 | int len = strlen(string); | |
569 | int begin,end; | |
570 | int value; | |
571 | begin = end =0; | |
572 | for(i = 0; i < len; i++) | |
573 | { | |
574 | if(string[i]==(char)32 && i!= (len-1)){ | |
575 | end = i+1; | |
576 | for(j = begin; j < end; j++) | |
577 | integer[j-begin] = string[j]; | |
578 | ||
579 | ||
580 | integer[end-begin]='\0'; | |
581 | ivstring.appendData(integer); | |
582 | ivstring.appendData(","); | |
583 | ||
584 | value = strtoul(integer, &stopstring, 0); | |
585 | int l = stopstring - integer; | |
586 | if((stopstring - integer)!=(end - begin -1)) | |
587 | ErrorReport(document, 3); //invalid int value | |
588 | begin = end; | |
589 | } | |
590 | } | |
591 | if(string[len-1]!=(char)32) | |
592 | { | |
593 | for(j = begin; j < len; j++) | |
594 | integer[j-begin] = string[j]; | |
595 | integer[len-begin] = '\0'; | |
596 | ivstring.appendData(integer); | |
597 | ||
598 | value = strtoul(integer, &stopstring, 0); | |
599 | int l = stopstring - integer; | |
600 | if((stopstring - integer)!=(len - begin)) | |
601 | ErrorReport(document, 3); | |
602 | } | |
603 | return ivstring; | |
604 | } | |
605 | else | |
606 | return NULL; | |
607 | ||
608 | } | |
609 | ||
610 | // --------------------------------------------------------------------------- | |
611 | // ostream << DOM_Node | |
612 | // | |
613 | // Stream out a DOM node, and, recursively, all of its children. | |
614 | // --------------------------------------------------------------------------- | |
615 | ||
616 | ostream& operator<<(ostream& target, DOM_Node& toWrite) | |
617 | { | |
618 | // Get the name and value out for convenience | |
619 | DOMString nodeName = toWrite.getNodeName(); //<tag name>, type | |
620 | DOMString nodeValue = toWrite.getNodeValue(); //<tag content> | |
621 | ||
622 | DOMString attributeKey, attributeVal; //(key/name)(val/filename) | |
623 | unsigned long lent = nodeValue.length(); | |
624 | ||
625 | ||
626 | switch (toWrite.getNodeType()) | |
627 | { | |
628 | case DOM_Node::TEXT_NODE: | |
629 | { | |
630 | gFormatter->formatBuf(nodeValue.rawBuffer(), | |
631 | lent, XMLFormatter::CharEscapes); | |
632 | break; | |
633 | } | |
634 | ||
635 | ||
636 | case DOM_Node::PROCESSING_INSTRUCTION_NODE : | |
637 | { | |
638 | break; | |
639 | } | |
640 | ||
641 | ||
642 | case DOM_Node::DOCUMENT_NODE : | |
643 | { | |
644 | ||
645 | DOM_Node child = toWrite.getFirstChild(); | |
646 | while( child != 0) | |
647 | { | |
648 | target << child; | |
649 | child = child.getNextSibling(); | |
650 | } | |
651 | break; | |
652 | } | |
653 | ||
654 | ||
655 | case DOM_Node::ELEMENT_NODE : | |
656 | { | |
657 | ||
658 | DOM_NamedNodeMap attributes = toWrite.getAttributes(); | |
659 | int attrCount = attributes.getLength(); | |
660 | for (int i = 0; i < attrCount; i++) | |
661 | { | |
662 | DOM_Node attribute = attributes.item(i); | |
663 | ||
664 | if(attribute.getNodeName().equals("key")||attribute.getNodeName().equals("name")){ | |
665 | attributeKey = attribute.getNodeValue(); | |
666 | } | |
667 | else if(attribute.getNodeName().equals("val")||attribute.getNodeName().equals("filename")){ | |
668 | attributeVal = attribute.getNodeValue(); | |
669 | } | |
670 | } | |
671 | ||
672 | //Print Out | |
673 | if(nodeName.equals("resourceBundle")) | |
674 | *gFormatter << attributeKey; | |
675 | else | |
676 | { | |
677 | if(nodeName.equals("bin") && attributeVal==NULL) | |
678 | *gFormatter <<attributeKey << ":" << nodeName << chSpace<< "{" << chDoubleQuote <<attributeVal << chDoubleQuote; | |
679 | else if(nodeName.equals("str")) | |
680 | *gFormatter <<attributeKey << chSpace<< "{" << chDoubleQuote <<attributeVal << chDoubleQuote; | |
681 | else if(nodeName.equals("intVector")) | |
682 | *gFormatter <<attributeKey << ":" << "intvector" << chSpace<< "{" <<attributeVal ; | |
683 | else if(nodeName.equals("importBin")) | |
684 | *gFormatter <<attributeKey << ":" << "import" << chSpace<< "{" << chDoubleQuote <<attributeVal << chDoubleQuote ; | |
685 | else | |
686 | *gFormatter <<attributeKey << ":" << nodeName << chSpace<< "{" << attributeVal; | |
687 | } | |
688 | ||
689 | ||
690 | attributeKey = attributeVal = NULL; | |
691 | ||
692 | ||
693 | DOM_Node child = toWrite.getFirstChild(); | |
694 | if (child != 0) | |
695 | { | |
696 | while( child != 0) | |
697 | { | |
698 | target << child; | |
699 | child = child.getNextSibling(); | |
700 | } | |
701 | if(!nodeName.equals("resourceBundle")) | |
702 | *gFormatter << "}"; | |
703 | } | |
704 | else | |
705 | { | |
706 | if(!nodeName.equals("resourceBundle")) | |
707 | *gFormatter << "}"; | |
708 | } | |
709 | break; | |
710 | } | |
711 | ||
712 | ||
713 | case DOM_Node::ENTITY_REFERENCE_NODE: | |
714 | { | |
715 | break; | |
716 | } | |
717 | ||
718 | ||
719 | case DOM_Node::CDATA_SECTION_NODE: | |
720 | { | |
721 | break; | |
722 | } | |
723 | ||
724 | ||
725 | case DOM_Node::COMMENT_NODE: | |
726 | { | |
727 | break; | |
728 | } | |
729 | ||
730 | ||
731 | case DOM_Node::DOCUMENT_TYPE_NODE: | |
732 | { | |
733 | DOM_DocumentType doctype = (DOM_DocumentType &)toWrite; | |
734 | break; | |
735 | } | |
736 | ||
737 | ||
738 | case DOM_Node::ENTITY_NODE: | |
739 | { | |
740 | break; | |
741 | } | |
742 | ||
743 | ||
744 | case DOM_Node::XML_DECL_NODE: | |
745 | { | |
746 | break; | |
747 | } | |
748 | ||
749 | ||
750 | default: | |
751 | cerr << "Unrecognized node type = " << (long)toWrite.getNodeType() << endl; | |
752 | } | |
753 | return target; | |
754 | } | |
755 | ||
756 | void ErrorReport(DOM_Node& toWrite, int ErrorType){ | |
757 | ||
758 | DOM_NamedNodeMap attributes; | |
759 | DOM_Node attribute; | |
760 | int attrCount, i; | |
761 | ||
762 | cout << "\nerror occurs at:\n"; | |
763 | DOMString ErrorMsg; | |
764 | while(toWrite.getParentNode()!=NULL){ | |
765 | //do | |
766 | ErrorMsg.insertData(0, ")"); | |
767 | ||
768 | attributes = toWrite.getAttributes(); | |
769 | attrCount = attributes.getLength(); | |
770 | ||
771 | if(attrCount!=0) | |
772 | { | |
773 | for (i = attrCount-1; i>=0; i--) | |
774 | { | |
775 | attribute = attributes.item(i); | |
776 | ErrorMsg.insertData(0, " ; "); | |
777 | ErrorMsg.insertData(0, attribute.getNodeValue()); | |
778 | } | |
779 | } | |
780 | ErrorMsg.insertData(0, "("); | |
781 | ErrorMsg.insertData(0, toWrite.getNodeName()); | |
782 | ErrorMsg.insertData(0, "==>"); | |
783 | toWrite = toWrite.getParentNode(); | |
784 | } | |
785 | ErrorMsg.appendData("\n"); | |
786 | ||
787 | switch (ErrorType) | |
788 | { | |
789 | case 1: | |
790 | ErrorMsg.appendData("The element in the array can't have a name!\n"); | |
791 | break; | |
792 | case 2: | |
793 | ErrorMsg.appendData("The element in the table should have a name!\n"); | |
794 | break; | |
795 | case 3: | |
796 | ErrorMsg.appendData("Invalid integer value!\n"); | |
797 | break; | |
798 | case 4: | |
799 | ErrorMsg.appendData("Invalid bin!\n"); | |
800 | break; | |
801 | case 5: | |
802 | ErrorMsg.appendData("Name Duplication in the table!\n"); | |
803 | break; | |
804 | case 6: | |
805 | ErrorMsg.appendData("Invalid element name! Remember to assign correct DTD file on the xml file.\n"); | |
806 | break; | |
807 | } | |
808 | cout << ErrorMsg; | |
809 | exit(0); | |
810 | } | |
811 | ||
812 | char* CreateTxtName(const char* arg, const char* Dir) | |
813 | { | |
814 | char* temp = CreateFile(arg, Dir); | |
815 | int len = strlen(temp); | |
816 | temp[len-1] = 't'; | |
817 | temp[len-2] = 'x'; | |
818 | temp[len-3] = 't'; | |
819 | return temp; | |
820 | ||
821 | /*char drive[_MAX_DRIVE]; | |
822 | char dir[_MAX_DIR]; | |
823 | char fname[_MAX_FNAME]; | |
824 | char ext[_MAX_EXT]; | |
825 | _splitpath(gXmlFile, drive, dir, fname, ext); | |
826 | strcpy(gTxtFile, "\0"); | |
827 | if (drive != NULL) { | |
828 | strcat(gTxtFile, drive); | |
829 | } | |
830 | if (dir != NULL) { | |
831 | strcat(gTxtFile, dir); | |
832 | } | |
833 | if (fname !=NULL) { | |
834 | strcat(gTxtFile, fname); | |
835 | } | |
836 | strcat(gTxtFile, "tempfile.txt");*/ | |
837 | } | |
838 | ||
839 | char* CreateFile(const char* arg, const char* Dir) | |
840 | { char* temp = new char[256]; | |
841 | char a[2]={'\\', '\0'}; | |
842 | char* currdir; | |
843 | if(sourceDir!=NULL) { | |
844 | strcpy(temp, Dir); | |
845 | int len = strlen(temp); | |
846 | if(temp[len - 1]!='\\') | |
847 | strcat(temp, a); | |
848 | strcat(temp, arg); | |
849 | } | |
850 | else { | |
851 | char drive[_MAX_DRIVE]; | |
852 | char dir[_MAX_DIR]; | |
853 | char fname[_MAX_FNAME]; | |
854 | char ext[_MAX_EXT]; | |
855 | _splitpath(arg, drive, dir, fname, ext); | |
856 | ||
857 | if(*drive == NULL && *dir == NULL) { | |
858 | #ifdef WIN32 | |
859 | currdir = _getcwd(NULL, 0); | |
860 | #else | |
861 | currdir = getcwd(NULL, 0); | |
862 | #endif | |
863 | strcpy(temp, currdir); | |
864 | strcat(temp, a); | |
865 | } | |
866 | strcat(temp, arg); | |
867 | } | |
868 | return temp; | |
869 | } | |
870 | ||
871 | ||
872 | // --------------------------------------------------------------------------- | |
873 | // ostream << DOMString | |
874 | // | |
875 | // Stream out a DOM string. Doing this requires that we first transcode | |
876 | // to char * form in the default code page for the system | |
877 | // --------------------------------------------------------------------------- | |
878 | ||
879 | ostream& operator<< (ostream& target, const DOMString& s) | |
880 | { | |
881 | char *p = s.transcode(); | |
882 | target << p; | |
883 | delete [] p; | |
884 | return target; | |
885 | } | |
886 | ||
887 | ||
888 | XMLFormatter& operator<< (XMLFormatter& strm, const DOMString& s) | |
889 | { | |
890 | unsigned int lent = s.length(); | |
891 | ||
892 | if (lent <= 0) | |
893 | return strm; | |
894 | ||
895 | XMLCh* buf = new XMLCh[lent + 1]; | |
896 | XMLString::copyNString(buf, s.rawBuffer(), lent); | |
897 | buf[lent] = 0; | |
898 | strm << buf; | |
899 | delete [] buf; | |
900 | return strm; | |
901 | } | |
902 | ||
903 |