xref: /trunk/main/oox/source/xls/stylesfragment.cxx (revision ca5ec200)
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/stylesfragment.hxx"
25 
26 #include "oox/helper/attributelist.hxx"
27 
28 namespace oox {
29 namespace xls {
30 
31 // ============================================================================
32 
33 using namespace ::oox::core;
34 
35 using ::rtl::OUString;
36 
37 // ============================================================================
38 
IndexedColorsContext(WorkbookFragmentBase & rFragment)39 IndexedColorsContext::IndexedColorsContext( WorkbookFragmentBase& rFragment ) :
40     WorkbookContextBase( rFragment )
41 {
42 }
43 
onCreateContext(sal_Int32 nElement,const AttributeList & rAttribs)44 ContextHandlerRef IndexedColorsContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
45 {
46     switch( getCurrentElement() )
47     {
48         case XLS_TOKEN( indexedColors ):
49             if( nElement == XLS_TOKEN( rgbColor ) ) getStyles().importPaletteColor( rAttribs );
50         break;
51     }
52     return 0;
53 }
54 
onCreateRecordContext(sal_Int32 nRecId,SequenceInputStream & rStrm)55 ContextHandlerRef IndexedColorsContext::onCreateRecordContext( sal_Int32 nRecId, SequenceInputStream& rStrm )
56 {
57     switch( getCurrentElement() )
58     {
59         case BIFF12_ID_INDEXEDCOLORS:
60             if( nRecId == BIFF12_ID_RGBCOLOR ) getStyles().importPaletteColor( rStrm );
61         break;
62     }
63     return 0;
64 }
65 
66 // ============================================================================
67 
onCreateContext(sal_Int32 nElement,const AttributeList & rAttribs)68 ContextHandlerRef FontContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
69 {
70     if( mxFont.get() )
71         mxFont->importAttribs( nElement, rAttribs );
72     return 0;
73 }
74 
75 // ============================================================================
76 
onStartElement(const AttributeList & rAttribs)77 void BorderContext::onStartElement( const AttributeList& rAttribs )
78 {
79     if( mxBorder.get() && (getCurrentElement() == XLS_TOKEN( border )) )
80         mxBorder->importBorder( rAttribs );
81 }
82 
onCreateContext(sal_Int32 nElement,const AttributeList & rAttribs)83 ContextHandlerRef BorderContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
84 {
85     if( mxBorder.get() ) switch( getCurrentElement() )
86     {
87         case XLS_TOKEN( border ):
88             mxBorder->importStyle( nElement, rAttribs );
89             return this;
90 
91         default:
92             if( nElement == XLS_TOKEN( color ) )
93                 mxBorder->importColor( getCurrentElement(), rAttribs );
94     }
95     return 0;
96 }
97 
98 // ============================================================================
99 
onCreateContext(sal_Int32 nElement,const AttributeList & rAttribs)100 ContextHandlerRef FillContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
101 {
102     if( mxFill.get() ) switch( getCurrentElement() )
103     {
104         case XLS_TOKEN( fill ):
105             switch( nElement )
106             {
107                 case XLS_TOKEN( patternFill ):  mxFill->importPatternFill( rAttribs );  return this;
108                 case XLS_TOKEN( gradientFill ): mxFill->importGradientFill( rAttribs ); return this;
109             }
110         break;
111         case XLS_TOKEN( patternFill ):
112             switch( nElement )
113             {
114                 case XLS_TOKEN( fgColor ):      mxFill->importFgColor( rAttribs );      break;
115                 case XLS_TOKEN( bgColor ):      mxFill->importBgColor( rAttribs );      break;
116             }
117         break;
118         case XLS_TOKEN( gradientFill ):
119             if( nElement == XLS_TOKEN( stop ) )
120             {
121                 mfGradPos = rAttribs.getDouble( XML_position, -1.0 );
122                 return this;
123             }
124         break;
125         case XLS_TOKEN( stop ):
126             if( nElement == XLS_TOKEN( color ) )
127                 mxFill->importColor( rAttribs, mfGradPos );
128         break;
129     }
130     return 0;
131 }
132 
133 // ============================================================================
134 
onStartElement(const AttributeList & rAttribs)135 void XfContext::onStartElement( const AttributeList& rAttribs )
136 {
137     if( mxXf.get() && (getCurrentElement() == XLS_TOKEN( xf )) )
138         mxXf->importXf( rAttribs, mbCellXf );
139 }
140 
onCreateContext(sal_Int32 nElement,const AttributeList & rAttribs)141 ContextHandlerRef XfContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
142 {
143     if( mxXf.get() ) switch( getCurrentElement() )
144     {
145         case XLS_TOKEN( xf ):
146             switch( nElement )
147             {
148                 case XLS_TOKEN( alignment ):    mxXf->importAlignment( rAttribs );  break;
149                 case XLS_TOKEN( protection ):   mxXf->importProtection( rAttribs ); break;
150             }
151         break;
152     }
153     return 0;
154 }
155 
156 // ============================================================================
157 
onCreateContext(sal_Int32 nElement,const AttributeList & rAttribs)158 ContextHandlerRef DxfContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
159 {
160     if( mxDxf.get() ) switch( getCurrentElement() )
161     {
162         case XLS_TOKEN( dxf ):
163             switch( nElement )
164             {
165                 case XLS_TOKEN( font ):         return new FontContext( *this, mxDxf->createFont() );
166                 case XLS_TOKEN( border ):       return new BorderContext( *this, mxDxf->createBorder() );
167                 case XLS_TOKEN( fill ):         return new FillContext( *this, mxDxf->createFill() );
168 
169                 case XLS_TOKEN( numFmt ):       mxDxf->importNumFmt( rAttribs );        break;
170 #if 0
171                 case XLS_TOKEN( alignment ):    mxDxf->importAlignment( rAttribs );     break;
172                 case XLS_TOKEN( protection ):   mxDxf->importProtection( rAttribs );    break;
173 #endif
174             }
175         break;
176     }
177     return 0;
178 }
179 
180 // ============================================================================
181 
StylesFragment(const WorkbookHelper & rHelper,const OUString & rFragmentPath)182 StylesFragment::StylesFragment( const WorkbookHelper& rHelper, const OUString& rFragmentPath ) :
183     WorkbookFragmentBase( rHelper, rFragmentPath )
184 {
185 }
186 
onCreateContext(sal_Int32 nElement,const AttributeList & rAttribs)187 ContextHandlerRef StylesFragment::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
188 {
189     switch( getCurrentElement() )
190     {
191         case XML_ROOT_CONTEXT:
192             if( nElement == XLS_TOKEN( styleSheet ) ) return this;
193         break;
194 
195         case XLS_TOKEN( styleSheet ):
196             switch( nElement )
197             {
198                 case XLS_TOKEN( colors ):
199                 case XLS_TOKEN( numFmts ):
200                 case XLS_TOKEN( fonts ):
201                 case XLS_TOKEN( borders ):
202                 case XLS_TOKEN( fills ):
203                 case XLS_TOKEN( cellXfs ):
204                 case XLS_TOKEN( cellStyleXfs ):
205                 case XLS_TOKEN( dxfs ):
206                 case XLS_TOKEN( cellStyles ):   return this;
207             }
208         break;
209 
210         case XLS_TOKEN( colors ):
211             if( nElement == XLS_TOKEN( indexedColors ) ) return new IndexedColorsContext( *this );
212         break;
213         case XLS_TOKEN( numFmts ):
214             if( nElement == XLS_TOKEN( numFmt ) ) getStyles().importNumFmt( rAttribs );
215         break;
216         case XLS_TOKEN( fonts ):
217             if( nElement == XLS_TOKEN( font ) ) return new FontContext( *this, getStyles().createFont() );
218         break;
219         case XLS_TOKEN( borders ):
220             if( nElement == XLS_TOKEN( border ) ) return new BorderContext( *this, getStyles().createBorder() );
221         break;
222         case XLS_TOKEN( fills ):
223             if( nElement == XLS_TOKEN( fill ) ) return new FillContext( *this, getStyles().createFill() );
224         break;
225         case XLS_TOKEN( cellXfs ):
226             if( nElement == XLS_TOKEN( xf ) ) return new XfContext( *this, getStyles().createCellXf(), true );
227         break;
228         case XLS_TOKEN( cellStyleXfs ):
229             if( nElement == XLS_TOKEN( xf ) ) return new XfContext( *this, getStyles().createStyleXf(), false );
230         break;
231         case XLS_TOKEN( dxfs ):
232             if( nElement == XLS_TOKEN( dxf ) ) return new DxfContext( *this, getStyles().createDxf() );
233         break;
234         case XLS_TOKEN( cellStyles ):
235             if( nElement == XLS_TOKEN( cellStyle ) ) getStyles().importCellStyle( rAttribs );
236         break;
237     }
238     return 0;
239 }
240 
onCreateRecordContext(sal_Int32 nRecId,SequenceInputStream & rStrm)241 ContextHandlerRef StylesFragment::onCreateRecordContext( sal_Int32 nRecId, SequenceInputStream& rStrm )
242 {
243     switch( getCurrentElement() )
244     {
245         case XML_ROOT_CONTEXT:
246             if( nRecId == BIFF12_ID_STYLESHEET ) return this;
247         break;
248 
249         case BIFF12_ID_STYLESHEET:
250             switch( nRecId )
251             {
252                 case BIFF12_ID_COLORS:
253                 case BIFF12_ID_NUMFMTS:
254                 case BIFF12_ID_FONTS:
255                 case BIFF12_ID_BORDERS:
256                 case BIFF12_ID_FILLS:
257                 case BIFF12_ID_CELLXFS:
258                 case BIFF12_ID_CELLSTYLEXFS:
259                 case BIFF12_ID_DXFS:
260                 case BIFF12_ID_CELLSTYLES:      return this;
261             }
262         break;
263 
264         case BIFF12_ID_COLORS:
265             if( nRecId == BIFF12_ID_INDEXEDCOLORS ) return new IndexedColorsContext( *this );
266         break;
267         case BIFF12_ID_NUMFMTS:
268             if( nRecId == BIFF12_ID_NUMFMT ) getStyles().importNumFmt( rStrm );
269         break;
270         case BIFF12_ID_FONTS:
271             if( nRecId == BIFF12_ID_FONT ) getStyles().createFont()->importFont( rStrm );
272         break;
273         case BIFF12_ID_BORDERS:
274             if( nRecId == BIFF12_ID_BORDER ) getStyles().createBorder()->importBorder( rStrm );
275         break;
276         case BIFF12_ID_FILLS:
277             if( nRecId == BIFF12_ID_FILL ) getStyles().createFill()->importFill( rStrm );
278         break;
279         case BIFF12_ID_CELLXFS:
280             if( nRecId == BIFF12_ID_XF ) getStyles().createCellXf()->importXf( rStrm, true );
281         break;
282         case BIFF12_ID_CELLSTYLEXFS:
283             if( nRecId == BIFF12_ID_XF ) getStyles().createStyleXf()->importXf( rStrm, false );
284         break;
285         case BIFF12_ID_DXFS:
286             if( nRecId == BIFF12_ID_DXF ) getStyles().createDxf()->importDxf( rStrm );
287         break;
288         case BIFF12_ID_CELLSTYLES:
289             if( nRecId == BIFF12_ID_CELLSTYLE ) getStyles().importCellStyle( rStrm );
290         break;
291     }
292     return 0;
293 }
294 
getRecordInfos() const295 const RecordInfo* StylesFragment::getRecordInfos() const
296 {
297     static const RecordInfo spRecInfos[] =
298     {
299         { BIFF12_ID_BORDERS,        BIFF12_ID_BORDERS + 1       },
300         { BIFF12_ID_CELLSTYLES,     BIFF12_ID_CELLSTYLES + 1    },
301         { BIFF12_ID_CELLSTYLEXFS,   BIFF12_ID_CELLSTYLEXFS + 1  },
302         { BIFF12_ID_CELLXFS,        BIFF12_ID_CELLXFS + 1       },
303         { BIFF12_ID_COLORS,         BIFF12_ID_COLORS + 1        },
304         { BIFF12_ID_DXFS,           BIFF12_ID_DXFS + 1          },
305         { BIFF12_ID_FILLS,          BIFF12_ID_FILLS + 1         },
306         { BIFF12_ID_FONTS,          BIFF12_ID_FONTS + 1         },
307         { BIFF12_ID_INDEXEDCOLORS,  BIFF12_ID_INDEXEDCOLORS + 1 },
308         { BIFF12_ID_MRUCOLORS,      BIFF12_ID_MRUCOLORS + 1     },
309         { BIFF12_ID_NUMFMTS,        BIFF12_ID_NUMFMTS + 1       },
310         { BIFF12_ID_STYLESHEET,     BIFF12_ID_STYLESHEET + 1    },
311         { BIFF12_ID_TABLESTYLES,    BIFF12_ID_TABLESTYLES + 1   },
312         { -1,                       -1                          }
313     };
314     return spRecInfos;
315 }
316 
finalizeImport()317 void StylesFragment::finalizeImport()
318 {
319     getStyles().finalizeImport();
320 }
321 
322 // ============================================================================
323 
324 } // namespace xls
325 } // namespace oox
326