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