LibreOffice Module forms (master) 1
xmlhelper.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20
21#include "xmlhelper.hxx"
22
23#include <rtl/ustring.hxx>
24#include <com/sun/star/uno/Reference.hxx>
25#include <com/sun/star/xml/dom/DocumentBuilder.hpp>
27
29using com::sun::star::container::XNameContainer;
30using com::sun::star::xml::dom::DocumentBuilder;
31using com::sun::star::xml::dom::XDocumentBuilder;
32
33
34// determine valid XML name
35
36
37// character class:
38// 1: NameStartChar
39// 2: NameChar
40// 4: NCNameStartChar
41// 8: NCNameChar
43{
44 sal_uInt8 nClass = 0;
45
46 // NameStartChar
47 if( (c >= 'A' && c <= 'Z')
48 || c == '_'
49 || (c >= 'a' && c <= 'z')
50 || (c >= 0x00C0 && c <= 0x00D6)
51 || (c >= 0x00D8 && c <= 0x00F6)
52 || (c >= 0x00F8 && c <= 0x02FF)
53 || (c >= 0x0370 && c <= 0x037D)
54 || (c >= 0x037F && c <= 0x1FFF)
55 || (c >= 0x200C && c <= 0x200D)
56 || (c >= 0x2070 && c <= 0x218F)
57 || (c >= 0x2C00 && c <= 0x2FEF)
58 || (c >= 0x3001 && c <= 0xD7FF)
59 || (c >= 0xF900 && c <= 0xFDCF)
60 || (c >= 0xFDF0 && c <= 0xFFFD)
61
62 // surrogates
63 || (c >= 0xD800 && c <= 0xDBFF)
64 || (c >= 0xDC00 && c <= 0xDFFF) )
65 {
66 nClass = 15;
67 }
68 else if( c == '-'
69 || c == '.'
70 || (c >= '0' && c <= '9')
71 || (c == 0x00B7)
72 || (c >= 0x0300 && c <= 0x036F)
73 || (c >= 0x203F && c <= 0x2040) )
74 {
75 nClass = 10;
76 }
77 else if( c == ':' )
78 {
79 nClass = 3;
80 }
81
82 return nClass;
83}
84
85bool isValidQName( const OUString& sName,
86 const Reference<XNameContainer>& /*xNamespaces*/ )
87{
88 sal_Int32 nLength = sName.getLength();
89 const sal_Unicode* pName = sName.getStr();
90
91 bool bRet = false;
92 sal_Int32 nColon = 0;
93 if( nLength > 0 )
94 {
95 bRet = ( ( lcl_getCharClass( pName[0] ) & 4 ) != 0 );
96 for( sal_Int32 n = 1; n < nLength; n++ )
97 {
98 sal_uInt8 nClass = lcl_getCharClass( pName[n] );
99 bRet &= ( ( nClass & 2 ) != 0 );
100 if( nClass == 3 )
101 nColon++;
102 }
103 }
104 if( nColon > 1 )
105 bRet = false;
106
107 return bRet;
108}
109
110bool isValidPrefixName( const OUString& sName,
111 const Reference<XNameContainer>& /*xNamespaces*/ )
112{
113 sal_Int32 nLength = sName.getLength();
114 const sal_Unicode* pName = sName.getStr();
115 bool bRet = false;
116
117 if( nLength > 0 )
118 {
119 bRet = ( ( lcl_getCharClass( pName[0] ) & 4 ) != 0 );
120 for( sal_Int32 n = 1; n < nLength; n++ )
121 bRet &= ( ( lcl_getCharClass( pName[n] ) & 8 ) != 0 );
122 }
123
124 return bRet;
125}
126
128{
129 Reference<XDocumentBuilder> xBuilder(DocumentBuilder::create(::comphelper::getProcessComponentContext()));
130 return xBuilder;
131}
132
133/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const char * pName
OUString sName
sal_Int64 n
unsigned char sal_uInt8
sal_uInt16 sal_Unicode
static sal_uInt8 lcl_getCharClass(sal_Unicode c)
Definition: xmlhelper.cxx:42
bool isValidPrefixName(const OUString &sName, const Reference< XNameContainer > &)
Definition: xmlhelper.cxx:110
Reference< XDocumentBuilder > getDocumentBuilder()
Definition: xmlhelper.cxx:127
bool isValidQName(const OUString &sName, const Reference< XNameContainer > &)
Definition: xmlhelper.cxx:85
sal_Int32 nLength