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