Line data Source code
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 "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>
28 :
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;
34 :
35 :
36 :
37 : // determine valid XML name
38 :
39 :
40 : // character class:
41 : // 1: NameStartChar
42 : // 2: NameChar
43 : // 4: NCNameStartChar
44 : // 8: NCNameChar
45 0 : static inline sal_uInt8 lcl_getCharClass( sal_Unicode c )
46 : {
47 0 : sal_uInt8 nClass = 0;
48 :
49 : // NameStartChar
50 0 : if( (c >= 'A' && c <= 'Z')
51 0 : || c == '_'
52 0 : || (c >= 'a' && c <= 'z')
53 0 : || (c >= 0x00C0 && c <= 0x00D6)
54 0 : || (c >= 0x00D8 && c <= 0x00F6)
55 0 : || (c >= 0x00F8 && c <= 0x02FF)
56 0 : || (c >= 0x0370 && c <= 0x037D)
57 0 : || (c >= 0x037F && c <= 0x1FFF)
58 0 : || (c >= 0x200C && c <= 0x200D)
59 0 : || (c >= 0x2070 && c <= 0x218F)
60 0 : || (c >= 0x2C00 && c <= 0x2FEF)
61 0 : || (c >= 0x3001 && c <= 0xD7FF)
62 0 : || (c >= 0xF900 && c <= 0xFDCF)
63 0 : || (c >= 0xFDF0 && c <= 0xFFFD)
64 :
65 : // surrogates
66 0 : || (c >= 0xD800 && c <= 0xDBFF)
67 0 : || (c >= 0xDC00 && c <= 0xDFFF) )
68 : {
69 0 : nClass = 15;
70 : }
71 0 : else if( c == '-'
72 0 : || c == '.'
73 0 : || (c >= '0' && c <= '9')
74 0 : || (c == 0x00B7)
75 0 : || (c >= 0x0300 && c <= 0x036F)
76 0 : || (c >= 0x203F && c <= 0x2040) )
77 : {
78 0 : nClass = 10;
79 : }
80 0 : else if( c == ':' )
81 : {
82 0 : nClass = 3;
83 : }
84 :
85 0 : return nClass;
86 : }
87 :
88 0 : bool isValidQName( const OUString& sName,
89 : const Reference<XNameContainer>& /*xNamespaces*/ )
90 : {
91 0 : sal_Int32 nLength = sName.getLength();
92 0 : const sal_Unicode* pName = sName.getStr();
93 :
94 0 : bool bRet = false;
95 0 : sal_Int32 nColon = 0;
96 0 : if( nLength > 0 )
97 : {
98 0 : bRet = ( ( lcl_getCharClass( pName[0] ) & 4 ) != 0 );
99 0 : for( sal_Int32 n = 1; n < nLength; n++ )
100 : {
101 0 : sal_uInt8 nClass = lcl_getCharClass( pName[n] );
102 0 : bRet &= ( ( nClass & 2 ) != 0 );
103 0 : if( nClass == 3 )
104 0 : nColon++;
105 : }
106 : }
107 0 : if( nColon > 1 )
108 0 : bRet = false;
109 :
110 0 : return bRet;
111 : }
112 :
113 0 : bool isValidPrefixName( const OUString& sName,
114 : const Reference<XNameContainer>& /*xNamespaces*/ )
115 : {
116 0 : sal_Int32 nLength = sName.getLength();
117 0 : const sal_Unicode* pName = sName.getStr();
118 0 : bool bRet = false;
119 :
120 0 : if( nLength > 0 )
121 : {
122 0 : bRet = ( ( lcl_getCharClass( pName[0] ) & 4 ) != 0 );
123 0 : for( sal_Int32 n = 1; n < nLength; n++ )
124 0 : bRet &= ( ( lcl_getCharClass( pName[n] ) & 8 ) != 0 );
125 : }
126 :
127 0 : return bRet;
128 : }
129 :
130 0 : Reference<XDocumentBuilder> getDocumentBuilder()
131 : {
132 0 : Reference<XDocumentBuilder> xBuilder(DocumentBuilder::create(::comphelper::getProcessComponentContext()));
133 0 : return xBuilder;
134 : }
135 :
136 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|