The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | #include "XLIFFFile.h" |
| 2 | #include <stdio.h> |
| 3 | #include "ValuesFile.h" |
| 4 | |
| 5 | XMLNode* create_string_node(const StringResource& str, const string& name); |
| 6 | |
| 7 | static int |
| 8 | Parse_test() |
| 9 | { |
| 10 | XLIFFFile* xf = XLIFFFile::Parse("testdata/xliff1.xliff"); |
| 11 | if (xf == NULL) { |
| 12 | return 1; |
| 13 | } |
| 14 | |
| 15 | set<StringResource> const& strings = xf->GetStringResources(); |
| 16 | |
| 17 | if (false) { |
| 18 | for (set<StringResource>::iterator it=strings.begin(); it!=strings.end(); it++) { |
| 19 | const StringResource& str = *it; |
| 20 | printf("STRING!!! id=%s index=%d value='%s' pos=%s file=%s version=%d(%s)\n", |
| 21 | str.id.c_str(), str.index, |
| 22 | str.value->ContentsToString(ANDROID_NAMESPACES).c_str(), |
| 23 | str.pos.ToString().c_str(), str.file.c_str(), str.version, |
| 24 | str.versionString.c_str()); |
| 25 | } |
| 26 | printf("XML:[[%s]]\n", xf->ToString().c_str()); |
| 27 | } |
| 28 | |
| 29 | delete xf; |
| 30 | return 0; |
| 31 | } |
| 32 | |
| 33 | static XMLNode* |
| 34 | add_html_tag(XMLNode* addTo, const string& tag) |
| 35 | { |
| 36 | vector<XMLAttribute> attrs; |
| 37 | XMLNode* node = XMLNode::NewElement(GENERATED_POS, "", tag, attrs, XMLNode::EXACT); |
| 38 | addTo->EditChildren().push_back(node); |
| 39 | return node; |
| 40 | } |
| 41 | |
| 42 | static int |
| 43 | create_string_node_test() |
| 44 | { |
| 45 | int err = 0; |
| 46 | StringResource res; |
| 47 | vector<XMLAttribute> attrs; |
| 48 | res.value = XMLNode::NewElement(GENERATED_POS, "", "something", attrs, XMLNode::EXACT); |
| 49 | res.value->EditChildren().push_back(XMLNode::NewText(GENERATED_POS, " begin ", XMLNode::EXACT)); |
| 50 | |
| 51 | XMLNode* child; |
| 52 | |
| 53 | child = add_html_tag(res.value, "b"); |
| 54 | child->EditChildren().push_back(XMLNode::NewText(GENERATED_POS, "b", XMLNode::EXACT)); |
| 55 | |
| 56 | child = add_html_tag(res.value, "i"); |
| 57 | child->EditChildren().push_back(XMLNode::NewText(GENERATED_POS, "i", XMLNode::EXACT)); |
| 58 | |
| 59 | child = add_html_tag(child, "b"); |
| 60 | child->EditChildren().push_back(XMLNode::NewText(GENERATED_POS, "b", XMLNode::EXACT)); |
| 61 | |
| 62 | child = add_html_tag(res.value, "u"); |
| 63 | child->EditChildren().push_back(XMLNode::NewText(GENERATED_POS, "u", XMLNode::EXACT)); |
| 64 | |
| 65 | |
| 66 | res.value->EditChildren().push_back(XMLNode::NewText(GENERATED_POS, " end ", XMLNode::EXACT)); |
| 67 | |
| 68 | XMLNode* xliff = create_string_node(res, "blah"); |
| 69 | |
| 70 | string oldString = res.value->ToString(XLIFF_NAMESPACES); |
| 71 | string newString = xliff->ToString(XLIFF_NAMESPACES); |
| 72 | |
| 73 | if (false) { |
| 74 | printf("OLD=\"%s\"\n", oldString.c_str()); |
| 75 | printf("NEW=\"%s\"\n", newString.c_str()); |
| 76 | } |
| 77 | |
| 78 | const char* const EXPECTED_OLD |
| 79 | = "<something> begin <b>b</b><i>i<b>b</b></i><u>u</u> end </something>"; |
| 80 | if (oldString != EXPECTED_OLD) { |
| 81 | fprintf(stderr, "oldString mismatch:\n"); |
| 82 | fprintf(stderr, " expected='%s'\n", EXPECTED_OLD); |
| 83 | fprintf(stderr, " actual='%s'\n", oldString.c_str()); |
| 84 | err |= 1; |
| 85 | } |
| 86 | |
| 87 | const char* const EXPECTED_NEW |
| 88 | = "<blah xml:space=\"preserve\"> begin <g ctype=\"bold\">b</g>" |
| 89 | "<g ctype=\"italic\">i<g ctype=\"bold\">b</g></g><g ctype=\"underline\">u</g>" |
| 90 | " end </blah>"; |
| 91 | if (newString != EXPECTED_NEW) { |
| 92 | fprintf(stderr, "newString mismatch:\n"); |
| 93 | fprintf(stderr, " expected='%s'\n", EXPECTED_NEW); |
| 94 | fprintf(stderr, " actual='%s'\n", newString.c_str()); |
| 95 | err |= 1; |
| 96 | } |
| 97 | |
| 98 | if (err != 0) { |
| 99 | fprintf(stderr, "create_string_node_test failed\n"); |
| 100 | } |
| 101 | return err; |
| 102 | } |
| 103 | |
| 104 | int |
| 105 | XLIFFFile_test() |
| 106 | { |
| 107 | bool all = true; |
| 108 | int err = 0; |
| 109 | |
| 110 | if (all) err |= Parse_test(); |
| 111 | if (all) err |= create_string_node_test(); |
| 112 | |
| 113 | return err; |
| 114 | } |
| 115 | |