xref: /aoo41x/main/forms/source/xforms/xmlhelper.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_forms.hxx"
30 
31 #include "xmlhelper.hxx"
32 
33 #include "unohelper.hxx"
34 #include <rtl/ustring.hxx>
35 #include <com/sun/star/uno/Reference.hxx>
36 #include <com/sun/star/xml/dom/XDocumentBuilder.hpp>
37 
38 using rtl::OUString;
39 using com::sun::star::uno::Reference;
40 using com::sun::star::uno::UNO_QUERY_THROW;
41 using com::sun::star::container::XNameContainer;
42 using com::sun::star::xml::dom::XDocumentBuilder;
43 
44 
45 //
46 // determine valid XML name
47 //
48 
49 // character class:
50 // 1: NameStartChar
51 // 2: NameChar
52 // 4: NCNameStartChar
53 // 8: NCNameChar
54 inline sal_uInt8 lcl_getCharClass( sal_Unicode c )
55 {
56     sal_uInt8 nClass = 0;
57 
58     // NameStartChar
59     if( (c >= 'A' && c <= 'Z')
60         || c == '_'
61         || (c >=    'a' && c <=    'z')
62         || (c >= 0x00C0 && c <= 0x00D6)
63         || (c >= 0x00D8 && c <= 0x00F6)
64         || (c >= 0x00F8 && c <= 0x02FF)
65         || (c >= 0x0370 && c <= 0x037D)
66         || (c >= 0x037F && c <= 0x1FFF)
67         || (c >= 0x200C && c <= 0x200D)
68         || (c >= 0x2070 && c <= 0x218F)
69         || (c >= 0x2C00 && c <= 0x2FEF)
70         || (c >= 0x3001 && c <= 0xD7FF)
71         || (c >= 0xF900 && c <= 0xFDCF)
72         || (c >= 0xFDF0 && c <= 0xFFFD)
73 
74         // surrogates
75         || (c >= 0xD800 && c <= 0xDBFF)
76         || (c >= 0xDC00 && c <= 0xDFFF) )
77     {
78         nClass = 15;
79     }
80     else if( c == '-'
81              || c == '.'
82              || (c >= '0' && c <= '9')
83              || (c == 0x00B7)
84              || (c >= 0x0300 && c <= 0x036F)
85              || (c >= 0x203F && c <= 0x2040) )
86     {
87         nClass = 10;
88     }
89     else if( c == ':' )
90     {
91         nClass = 3;
92     }
93 
94     return nClass;
95 }
96 
97 bool isValidQName( const OUString& sName,
98                    const Reference<XNameContainer>& /*xNamespaces*/ )
99 {
100     sal_Int32 nLength = sName.getLength();
101     const sal_Unicode* pName = sName.getStr();
102 
103     bool bRet = false;
104     sal_Int32 nColon = 0;
105     if( nLength > 0 )
106     {
107         bRet = ( ( lcl_getCharClass( pName[0] ) & 4 ) != 0 );
108         for( sal_Int32 n = 1; n < nLength; n++ )
109         {
110             sal_uInt8 nClass = lcl_getCharClass( pName[n] );
111             bRet &= ( ( nClass & 2 ) != 0 );
112             if( nClass == 3 )
113                 nColon++;
114         }
115     }
116     if( nColon > 1 )
117         bRet = sal_False;
118 
119     return bRet;
120 }
121 
122 bool isValidPrefixName( const OUString& sName,
123                         const Reference<XNameContainer>& /*xNamespaces*/ )
124 {
125     sal_Int32 nLength = sName.getLength();
126     const sal_Unicode* pName = sName.getStr();
127     bool bRet = false;
128 
129     if( nLength > 0 )
130     {
131         bRet = ( ( lcl_getCharClass( pName[0] ) & 4 ) != 0 );
132         for( sal_Int32 n = 1; n < nLength; n++ )
133             bRet &= ( ( lcl_getCharClass( pName[n] ) & 8 ) != 0 );
134     }
135 
136     return bRet;
137 }
138 
139 Reference<XDocumentBuilder> getDocumentBuilder()
140 {
141     Reference<XDocumentBuilder> xBuilder(
142         xforms::createInstance(
143             OUSTRING("com.sun.star.xml.dom.DocumentBuilder") ),
144         UNO_QUERY_THROW );
145     OSL_ENSURE( xBuilder.is(), "no document builder?" );
146     return xBuilder;
147 }
148 
149