Branch libreoffice-5-0-4
[LibreOffice.git] / l10ntools / source / helper.cxx
blob85bfe4a17f96546e73dc61e0dc053671c399c0be
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include "helper.hxx"
12 namespace helper {
14 OString escapeAll(
15 const OString& rText, const OString& rUnEscaped, const OString& rEscaped )
17 assert( rEscaped.getLength() == 2*rUnEscaped.getLength() );
18 OStringBuffer sReturn;
19 for ( sal_Int32 nIndex = 0; nIndex < rText.getLength(); ++nIndex )
21 sal_Int32 nUnEscapedOne = rUnEscaped.indexOf(rText[nIndex]);
22 if( nUnEscapedOne != -1 )
24 sReturn.append(rEscaped.copy(nUnEscapedOne*2,2));
26 else
27 sReturn.append(rText[nIndex]);
29 return sReturn.makeStringAndClear();
33 OString unEscapeAll(
34 const OString& rText, const OString& rEscaped, const OString& rUnEscaped)
36 assert( rEscaped.getLength() == 2*rUnEscaped.getLength() );
37 OStringBuffer sReturn;
38 const sal_Int32 nLength = rText.getLength();
39 for ( sal_Int32 nIndex = 0; nIndex < nLength; ++nIndex )
41 if( rText[nIndex] == '\\' && nIndex+1 < nLength )
43 sal_Int32 nEscapedOne = rEscaped.indexOf(rText.copy(nIndex,2));
44 if( nEscapedOne != -1 )
46 sReturn.append(rUnEscaped[nEscapedOne/2]);
47 ++nIndex;
49 else
51 sReturn.append(rText[nIndex]);
54 else
55 sReturn.append(rText[nIndex]);
57 return sReturn.makeStringAndClear();
61 OString QuotHTML(const OString &rString)
63 OStringBuffer sReturn;
64 for (sal_Int32 i = 0; i < rString.getLength(); ++i)
66 switch (rString[i])
68 case '<':
69 sReturn.append("&lt;");
70 break;
71 case '>':
72 sReturn.append("&gt;");
73 break;
74 case '"':
75 sReturn.append("&quot;");
76 break;
77 case '\'':
78 sReturn.append("&apos;");
79 break;
80 case '&':
81 if (rString.match("&amp;", i))
82 sReturn.append('&');
83 else
84 sReturn.append("&amp;");
85 break;
86 default:
87 sReturn.append(rString[i]);
88 break;
91 return sReturn.makeStringAndClear();
94 OString UnQuotHTML( const OString& rString )
96 OStringBuffer sReturn;
97 for (sal_Int32 i = 0; i != rString.getLength();) {
98 if (rString.match("&amp;", i)) {
99 sReturn.append('&');
100 i += RTL_CONSTASCII_LENGTH("&amp;");
101 } else if (rString.match("&lt;", i)) {
102 sReturn.append('<');
103 i += RTL_CONSTASCII_LENGTH("&lt;");
104 } else if (rString.match("&gt;", i)) {
105 sReturn.append('>');
106 i += RTL_CONSTASCII_LENGTH("&gt;");
107 } else if (rString.match("&quot;", i)) {
108 sReturn.append('"');
109 i += RTL_CONSTASCII_LENGTH("&quot;");
110 } else if (rString.match("&apos;", i)) {
111 sReturn.append('\'');
112 i += RTL_CONSTASCII_LENGTH("&apos;");
113 } else {
114 sReturn.append(rString[i]);
115 ++i;
118 return sReturn.makeStringAndClear();
121 bool isWellFormedXML( OString const & text )
123 xmlDocPtr doc;
124 OString content;
125 bool result = true;
127 content = "<root>";
128 content += text;
129 content += "</root>";
130 doc = xmlParseMemory(content.getStr(),(int)content.getLength());
131 if (doc == NULL) {
132 result = false;
134 xmlFreeDoc(doc);
135 xmlCleanupParser();
136 return result;
139 //Convert xmlChar* to OString
140 OString xmlStrToOString( const xmlChar* pString )
142 xmlChar* pTemp = xmlStrdup( pString );
143 OString sResult =
144 static_cast<OString>(reinterpret_cast<sal_Char*>( pTemp ));
145 xmlFree( pTemp );
146 return sResult;
151 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */