bump product version to 5.0.4.1
[LibreOffice.git] / forms / source / xforms / xmlhelper.cxx
blobf9b58f0477e0bb1095e454b698cee98078e364ac
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/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include "xmlhelper.hxx"
23 #include "unohelper.hxx"
24 #include <rtl/ustring.hxx>
25 #include <com/sun/star/uno/Reference.hxx>
26 #include <com/sun/star/xml/dom/DocumentBuilder.hpp>
27 #include <comphelper/processfactory.hxx>
29 using com::sun::star::uno::Reference;
30 using com::sun::star::uno::UNO_QUERY_THROW;
31 using com::sun::star::container::XNameContainer;
32 using com::sun::star::xml::dom::DocumentBuilder;
33 using com::sun::star::xml::dom::XDocumentBuilder;
37 // determine valid XML name
40 // character class:
41 // 1: NameStartChar
42 // 2: NameChar
43 // 4: NCNameStartChar
44 // 8: NCNameChar
45 static inline sal_uInt8 lcl_getCharClass( sal_Unicode c )
47 sal_uInt8 nClass = 0;
49 // NameStartChar
50 if( (c >= 'A' && c <= 'Z')
51 || c == '_'
52 || (c >= 'a' && c <= 'z')
53 || (c >= 0x00C0 && c <= 0x00D6)
54 || (c >= 0x00D8 && c <= 0x00F6)
55 || (c >= 0x00F8 && c <= 0x02FF)
56 || (c >= 0x0370 && c <= 0x037D)
57 || (c >= 0x037F && c <= 0x1FFF)
58 || (c >= 0x200C && c <= 0x200D)
59 || (c >= 0x2070 && c <= 0x218F)
60 || (c >= 0x2C00 && c <= 0x2FEF)
61 || (c >= 0x3001 && c <= 0xD7FF)
62 || (c >= 0xF900 && c <= 0xFDCF)
63 || (c >= 0xFDF0 && c <= 0xFFFD)
65 // surrogates
66 || (c >= 0xD800 && c <= 0xDBFF)
67 || (c >= 0xDC00 && c <= 0xDFFF) )
69 nClass = 15;
71 else if( c == '-'
72 || c == '.'
73 || (c >= '0' && c <= '9')
74 || (c == 0x00B7)
75 || (c >= 0x0300 && c <= 0x036F)
76 || (c >= 0x203F && c <= 0x2040) )
78 nClass = 10;
80 else if( c == ':' )
82 nClass = 3;
85 return nClass;
88 bool isValidQName( const OUString& sName,
89 const Reference<XNameContainer>& /*xNamespaces*/ )
91 sal_Int32 nLength = sName.getLength();
92 const sal_Unicode* pName = sName.getStr();
94 bool bRet = false;
95 sal_Int32 nColon = 0;
96 if( nLength > 0 )
98 bRet = ( ( lcl_getCharClass( pName[0] ) & 4 ) != 0 );
99 for( sal_Int32 n = 1; n < nLength; n++ )
101 sal_uInt8 nClass = lcl_getCharClass( pName[n] );
102 bRet &= ( ( nClass & 2 ) != 0 );
103 if( nClass == 3 )
104 nColon++;
107 if( nColon > 1 )
108 bRet = false;
110 return bRet;
113 bool isValidPrefixName( const OUString& sName,
114 const Reference<XNameContainer>& /*xNamespaces*/ )
116 sal_Int32 nLength = sName.getLength();
117 const sal_Unicode* pName = sName.getStr();
118 bool bRet = false;
120 if( nLength > 0 )
122 bRet = ( ( lcl_getCharClass( pName[0] ) & 4 ) != 0 );
123 for( sal_Int32 n = 1; n < nLength; n++ )
124 bRet &= ( ( lcl_getCharClass( pName[n] ) & 8 ) != 0 );
127 return bRet;
130 Reference<XDocumentBuilder> getDocumentBuilder()
132 Reference<XDocumentBuilder> xBuilder(DocumentBuilder::create(::comphelper::getProcessComponentContext()));
133 return xBuilder;
136 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */