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 : #include "TokenContext.hxx"
21 : #include <xmloff/xmltkmap.hxx>
22 : #include <xmloff/xmlimp.hxx>
23 : #include <xmloff/nmspmap.hxx>
24 : #include "xmloff/xmlerror.hxx"
25 :
26 : #include <tools/debug.hxx>
27 :
28 : using rtl::OUString;
29 : using com::sun::star::uno::Reference;
30 : using com::sun::star::xml::sax::XAttributeList;
31 :
32 :
33 : struct SvXMLTokenMapEntry aEmptyMap[1] =
34 : {
35 : XML_TOKEN_MAP_END
36 : };
37 :
38 :
39 0 : TokenContext::TokenContext( SvXMLImport& rImport,
40 : sal_uInt16 nPrefix,
41 : const OUString& rLocalName,
42 : const SvXMLTokenMapEntry* pAttributes,
43 : const SvXMLTokenMapEntry* pChildren )
44 : : SvXMLImportContext( rImport, nPrefix, rLocalName ),
45 : mpAttributes( pAttributes ),
46 0 : mpChildren( pChildren )
47 : {
48 0 : }
49 :
50 0 : TokenContext::~TokenContext()
51 : {
52 0 : }
53 :
54 0 : void TokenContext::StartElement(
55 : const Reference<XAttributeList>& xAttributeList )
56 : {
57 : // iterate over attributes
58 : // - if in map: call HandleAttribute
59 : // - xmlns:... : ignore
60 : // - other: warning
61 : DBG_ASSERT( mpAttributes != NULL, "no token map for attributes" );
62 0 : SvXMLTokenMap aMap( mpAttributes );
63 :
64 0 : sal_Int16 nCount = xAttributeList->getLength();
65 0 : for( sal_Int16 i = 0; i < nCount; i++ )
66 : {
67 : // get key/local-name pair from namespace map
68 0 : OUString sLocalName;
69 0 : sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
70 0 : GetKeyByAttrName( xAttributeList->getNameByIndex(i), &sLocalName );
71 :
72 : // get token from token map
73 0 : sal_uInt16 nToken = aMap.Get( nPrefix, sLocalName );
74 :
75 : // and the value...
76 0 : const OUString& rValue = xAttributeList->getValueByIndex(i);
77 :
78 0 : if( nToken != XML_TOK_UNKNOWN )
79 : {
80 0 : HandleAttribute( nToken, rValue );
81 : }
82 0 : else if( nPrefix != XML_NAMESPACE_XMLNS )
83 : {
84 : // error handling, for all attribute that are not
85 : // namespace declarations
86 0 : GetImport().SetError( XMLERROR_UNKNOWN_ATTRIBUTE,
87 0 : sLocalName, rValue);
88 : }
89 0 : }
90 0 : }
91 :
92 0 : SvXMLImportContext* TokenContext::CreateChildContext(
93 : sal_uInt16 nPrefix,
94 : const OUString& rLocalName,
95 : const Reference<XAttributeList>& xAttrList )
96 : {
97 : // call HandleChild for elements in token map. Ignore other content.
98 :
99 0 : SvXMLImportContext* pContext = NULL;
100 :
101 : DBG_ASSERT( mpChildren != NULL, "no token map for child elements" );
102 0 : SvXMLTokenMap aMap( mpChildren );
103 0 : sal_uInt16 nToken = aMap.Get( nPrefix, rLocalName );
104 0 : if( nToken != XML_TOK_UNKNOWN )
105 : {
106 : // call handle child, and pass down arguments
107 0 : pContext = HandleChild( nToken, nPrefix, rLocalName, xAttrList );
108 : }
109 :
110 : // error handling: create default context and generate warning
111 0 : if( pContext == NULL )
112 : {
113 0 : GetImport().SetError( XMLERROR_UNKNOWN_ELEMENT, rLocalName );
114 0 : pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
115 : }
116 0 : return pContext;
117 : }
118 :
119 0 : static bool lcl_IsWhiteSpace( sal_Unicode c )
120 : {
121 : return c == sal_Unicode( ' ' )
122 : || c == sal_Unicode( 0x09 )
123 : || c == sal_Unicode( 0x0A )
124 0 : || c == sal_Unicode( 0x0D );
125 : }
126 :
127 0 : void TokenContext::Characters( const ::rtl::OUString& rCharacters )
128 : {
129 : // get iterators for string data
130 0 : const sal_Unicode* pBegin = rCharacters.getStr();
131 0 : const sal_Unicode* pEnd = &( pBegin[ rCharacters.getLength() ] );
132 :
133 : // raise error if non-whitespace character is found
134 0 : if( ::std::find_if( pBegin, pEnd, ::std::not1(::std::ptr_fun(lcl_IsWhiteSpace)) ) != pEnd )
135 0 : GetImport().SetError( XMLERROR_UNKNOWN_CHARACTERS, rCharacters );
136 0 : }
137 :
138 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|