xref: /trunk/main/forms/source/xforms/xmlhelper.cxx (revision 24acc546)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_forms.hxx"
26 
27 #include "xmlhelper.hxx"
28 
29 #include "unohelper.hxx"
30 #include <rtl/ustring.hxx>
31 #include <com/sun/star/uno/Reference.hxx>
32 #include <com/sun/star/xml/dom/XDocumentBuilder.hpp>
33 
34 using rtl::OUString;
35 using com::sun::star::uno::Reference;
36 using com::sun::star::uno::UNO_QUERY_THROW;
37 using com::sun::star::container::XNameContainer;
38 using com::sun::star::xml::dom::XDocumentBuilder;
39 
40 
41 //
42 // determine valid XML name
43 //
44 
45 // character class:
46 // 1: NameStartChar
47 // 2: NameChar
48 // 4: NCNameStartChar
49 // 8: NCNameChar
lcl_getCharClass(sal_Unicode c)50 inline sal_uInt8 lcl_getCharClass( sal_Unicode c )
51 {
52     sal_uInt8 nClass = 0;
53 
54     // NameStartChar
55     if( (c >= 'A' && c <= 'Z')
56         || c == '_'
57         || (c >=    'a' && c <=    'z')
58         || (c >= 0x00C0 && c <= 0x00D6)
59         || (c >= 0x00D8 && c <= 0x00F6)
60         || (c >= 0x00F8 && c <= 0x02FF)
61         || (c >= 0x0370 && c <= 0x037D)
62         || (c >= 0x037F && c <= 0x1FFF)
63         || (c >= 0x200C && c <= 0x200D)
64         || (c >= 0x2070 && c <= 0x218F)
65         || (c >= 0x2C00 && c <= 0x2FEF)
66         || (c >= 0x3001 && c <= 0xD7FF)
67         || (c >= 0xF900 && c <= 0xFDCF)
68         || (c >= 0xFDF0 && c <= 0xFFFD)
69 
70         // surrogates
71         || (c >= 0xD800 && c <= 0xDBFF)
72         || (c >= 0xDC00 && c <= 0xDFFF) )
73     {
74         nClass = 15;
75     }
76     else if( c == '-'
77              || c == '.'
78              || (c >= '0' && c <= '9')
79              || (c == 0x00B7)
80              || (c >= 0x0300 && c <= 0x036F)
81              || (c >= 0x203F && c <= 0x2040) )
82     {
83         nClass = 10;
84     }
85     else if( c == ':' )
86     {
87         nClass = 3;
88     }
89 
90     return nClass;
91 }
92 
isValidQName(const OUString & sName,const Reference<XNameContainer> &)93 bool isValidQName( const OUString& sName,
94                    const Reference<XNameContainer>& /*xNamespaces*/ )
95 {
96     sal_Int32 nLength = sName.getLength();
97     const sal_Unicode* pName = sName.getStr();
98 
99     bool bRet = false;
100     sal_Int32 nColon = 0;
101     if( nLength > 0 )
102     {
103         bRet = ( ( lcl_getCharClass( pName[0] ) & 4 ) != 0 );
104         for( sal_Int32 n = 1; n < nLength; n++ )
105         {
106             sal_uInt8 nClass = lcl_getCharClass( pName[n] );
107             bRet &= ( ( nClass & 2 ) != 0 );
108             if( nClass == 3 )
109                 nColon++;
110         }
111     }
112     if( nColon > 1 )
113         bRet = sal_False;
114 
115     return bRet;
116 }
117 
isValidPrefixName(const OUString & sName,const Reference<XNameContainer> &)118 bool isValidPrefixName( const OUString& sName,
119                         const Reference<XNameContainer>& /*xNamespaces*/ )
120 {
121     sal_Int32 nLength = sName.getLength();
122     const sal_Unicode* pName = sName.getStr();
123     bool bRet = false;
124 
125     if( nLength > 0 )
126     {
127         bRet = ( ( lcl_getCharClass( pName[0] ) & 4 ) != 0 );
128         for( sal_Int32 n = 1; n < nLength; n++ )
129             bRet &= ( ( lcl_getCharClass( pName[n] ) & 8 ) != 0 );
130     }
131 
132     return bRet;
133 }
134 
getDocumentBuilder()135 Reference<XDocumentBuilder> getDocumentBuilder()
136 {
137     Reference<XDocumentBuilder> xBuilder(
138         xforms::createInstance(
139             OUSTRING("com.sun.star.xml.dom.DocumentBuilder") ),
140         UNO_QUERY_THROW );
141     OSL_ENSURE( xBuilder.is(), "no document builder?" );
142     return xBuilder;
143 }
144 
145