1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include "oox/xls/autofiltercontext.hxx"
25 
26 #include "oox/xls/autofilterbuffer.hxx"
27 #include "oox/xls/biffinputstream.hxx"
28 
29 namespace oox {
30 namespace xls {
31 
32 using ::oox::core::ContextHandlerRef;
33 using ::rtl::OUString;
34 
35 // ============================================================================
36 
FilterSettingsContext(WorksheetContextBase & rParent,FilterSettingsBase & rFilterSettings)37 FilterSettingsContext::FilterSettingsContext( WorksheetContextBase& rParent, FilterSettingsBase& rFilterSettings ) :
38     WorksheetContextBase( rParent ),
39     mrFilterSettings( rFilterSettings )
40 {
41 }
42 
onCreateContext(sal_Int32 nElement,const AttributeList &)43 ContextHandlerRef FilterSettingsContext::onCreateContext( sal_Int32 nElement, const AttributeList& /*rAttribs*/ )
44 {
45     switch( getCurrentElement() )
46     {
47         case XLS_TOKEN( filters ):
48             if( nElement == XLS_TOKEN( filter ) ) return this;
49         break;
50         case XLS_TOKEN( customFilters ):
51             if( nElement == XLS_TOKEN( customFilter ) ) return this;
52         break;
53     }
54     return 0;
55 }
56 
onStartElement(const AttributeList & rAttribs)57 void FilterSettingsContext::onStartElement( const AttributeList& rAttribs )
58 {
59     mrFilterSettings.importAttribs( getCurrentElement(), rAttribs );
60 }
61 
onCreateRecordContext(sal_Int32 nRecId,SequenceInputStream &)62 ContextHandlerRef FilterSettingsContext::onCreateRecordContext( sal_Int32 nRecId, SequenceInputStream& /*rStrm*/ )
63 {
64     switch( getCurrentElement() )
65     {
66         case BIFF12_ID_DISCRETEFILTERS:
67             if( nRecId == BIFF12_ID_DISCRETEFILTER ) return this;
68         break;
69         case BIFF12_ID_CUSTOMFILTERS:
70             if( nRecId == BIFF12_ID_CUSTOMFILTER ) return this;
71         break;
72     }
73     return 0;
74 }
75 
onStartRecord(SequenceInputStream & rStrm)76 void FilterSettingsContext::onStartRecord( SequenceInputStream& rStrm )
77 {
78     mrFilterSettings.importRecord( getCurrentElement(), rStrm );
79 }
80 
81 // ============================================================================
82 
FilterColumnContext(WorksheetContextBase & rParent,FilterColumn & rFilterColumn)83 FilterColumnContext::FilterColumnContext( WorksheetContextBase& rParent, FilterColumn& rFilterColumn ) :
84     WorksheetContextBase( rParent ),
85     mrFilterColumn( rFilterColumn )
86 {
87 }
88 
onCreateContext(sal_Int32 nElement,const AttributeList &)89 ContextHandlerRef FilterColumnContext::onCreateContext( sal_Int32 nElement, const AttributeList& /*rAttribs*/ )
90 {
91     if( getCurrentElement() == XLS_TOKEN( filterColumn ) ) switch( nElement )
92     {
93         case XLS_TOKEN( filters ):
94             return new FilterSettingsContext( *this, mrFilterColumn.createFilterSettings< DiscreteFilter >() );
95         case XLS_TOKEN( top10 ):
96             return new FilterSettingsContext( *this, mrFilterColumn.createFilterSettings< Top10Filter >() );
97         case XLS_TOKEN( customFilters ):
98             return new FilterSettingsContext( *this, mrFilterColumn.createFilterSettings< CustomFilter >() );
99     }
100     return 0;
101 }
102 
onStartElement(const AttributeList & rAttribs)103 void FilterColumnContext::onStartElement( const AttributeList& rAttribs )
104 {
105     mrFilterColumn.importFilterColumn( rAttribs );
106 }
107 
onCreateRecordContext(sal_Int32 nRecId,SequenceInputStream &)108 ContextHandlerRef FilterColumnContext::onCreateRecordContext( sal_Int32 nRecId, SequenceInputStream& /*rStrm*/ )
109 {
110     if( getCurrentElement() == BIFF12_ID_FILTERCOLUMN ) switch( nRecId )
111     {
112         case BIFF12_ID_DISCRETEFILTERS:
113             return new FilterSettingsContext( *this, mrFilterColumn.createFilterSettings< DiscreteFilter >() );
114         case BIFF12_ID_TOP10FILTER:
115             return new FilterSettingsContext( *this, mrFilterColumn.createFilterSettings< Top10Filter >() );
116         case BIFF12_ID_CUSTOMFILTERS:
117             return new FilterSettingsContext( *this, mrFilterColumn.createFilterSettings< CustomFilter >() );
118     }
119     return 0;
120 }
121 
onStartRecord(SequenceInputStream & rStrm)122 void FilterColumnContext::onStartRecord( SequenceInputStream& rStrm )
123 {
124     mrFilterColumn.importFilterColumn( rStrm );
125 }
126 
127 // ============================================================================
128 
AutoFilterContext(WorksheetFragmentBase & rFragment,AutoFilter & rAutoFilter)129 AutoFilterContext::AutoFilterContext( WorksheetFragmentBase& rFragment, AutoFilter& rAutoFilter ) :
130     WorksheetContextBase( rFragment ),
131     mrAutoFilter( rAutoFilter )
132 {
133 }
134 
onCreateContext(sal_Int32 nElement,const AttributeList &)135 ContextHandlerRef AutoFilterContext::onCreateContext( sal_Int32 nElement, const AttributeList& /*rAttribs*/ )
136 {
137     if( (getCurrentElement() == XLS_TOKEN( autoFilter )) && (nElement == XLS_TOKEN( filterColumn )) )
138         return new FilterColumnContext( *this, mrAutoFilter.createFilterColumn() );
139     return 0;
140 }
141 
onStartElement(const AttributeList & rAttribs)142 void AutoFilterContext::onStartElement( const AttributeList& rAttribs )
143 {
144     mrAutoFilter.importAutoFilter( rAttribs, getSheetIndex() );
145 }
146 
onCreateRecordContext(sal_Int32 nRecId,SequenceInputStream &)147 ContextHandlerRef AutoFilterContext::onCreateRecordContext( sal_Int32 nRecId, SequenceInputStream& /*rStrm*/ )
148 {
149     if( (getCurrentElement() == BIFF12_ID_AUTOFILTER) && (nRecId == BIFF12_ID_FILTERCOLUMN) )
150         return new FilterColumnContext( *this, mrAutoFilter.createFilterColumn() );
151     return 0;
152 }
153 
onStartRecord(SequenceInputStream & rStrm)154 void AutoFilterContext::onStartRecord( SequenceInputStream& rStrm )
155 {
156     mrAutoFilter.importAutoFilter( rStrm, getSheetIndex() );
157 }
158 
159 // ============================================================================
160 
BiffAutoFilterContext(const WorksheetHelper & rHelper,AutoFilter & rAutoFilter)161 BiffAutoFilterContext::BiffAutoFilterContext( const WorksheetHelper& rHelper, AutoFilter& rAutoFilter ) :
162     BiffWorksheetContextBase( rHelper ),
163     mrAutoFilter( rAutoFilter )
164 {
165 }
166 
importRecord(BiffInputStream & rStrm)167 void BiffAutoFilterContext::importRecord( BiffInputStream& rStrm )
168 {
169     switch( rStrm.getRecId() )
170     {
171         // nothing to read for BIFF_ID_AUTOFILTER
172         case BIFF_ID_FILTERCOLUMN:  mrAutoFilter.createFilterColumn().importFilterColumn( rStrm );  break;
173     }
174 }
175 
176 // ============================================================================
177 
178 } // namespace xls
179 } // namespace oox
180