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