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