1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include "oox/xls/commentsfragment.hxx"
29 
30 #include "oox/xls/richstringcontext.hxx"
31 
32 namespace oox {
33 namespace xls {
34 
35 // ============================================================================
36 
37 using namespace ::oox::core;
38 
39 using ::rtl::OUString;
40 
41 // ============================================================================
42 
43 CommentsFragment::CommentsFragment( const WorksheetHelper& rHelper, const OUString& rFragmentPath ) :
44     WorksheetFragmentBase( rHelper, rFragmentPath )
45 {
46 }
47 
48 ContextHandlerRef CommentsFragment::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
49 {
50     switch( getCurrentElement() )
51     {
52         case XML_ROOT_CONTEXT:
53             if( nElement == XLS_TOKEN( comments ) ) return this;
54         break;
55         case XLS_TOKEN( comments ):
56             if( nElement == XLS_TOKEN( authors ) ) return this;
57             if( nElement == XLS_TOKEN( commentList ) ) return this;
58         break;
59         case XLS_TOKEN( authors ):
60             if( nElement == XLS_TOKEN( author ) ) return this;  // collect author in onCharacters()
61         break;
62         case XLS_TOKEN( commentList ):
63             if( nElement == XLS_TOKEN( comment ) ) { importComment( rAttribs ); return this; }
64         break;
65         case XLS_TOKEN( comment ):
66             if( (nElement == XLS_TOKEN( text )) && mxComment.get() )
67                 return new RichStringContext( *this, mxComment->createText() );
68         break;
69     }
70     return 0;
71 }
72 
73 void CommentsFragment::onCharacters( const OUString& rChars )
74 {
75     if( isCurrentElement( XLS_TOKEN( author ) ) )
76         getComments().appendAuthor( rChars );
77 }
78 
79 void CommentsFragment::onEndElement()
80 {
81     if( isCurrentElement( XLS_TOKEN( comment ) ) )
82         mxComment.reset();
83 }
84 
85 ContextHandlerRef CommentsFragment::onCreateRecordContext( sal_Int32 nRecId, SequenceInputStream& rStrm )
86 {
87     switch( getCurrentElement() )
88     {
89         case XML_ROOT_CONTEXT:
90             if( nRecId == BIFF12_ID_COMMENTS ) return this;
91         break;
92         case BIFF12_ID_COMMENTS:
93             if( nRecId == BIFF12_ID_COMMENTAUTHORS ) return this;
94             if( nRecId == BIFF12_ID_COMMENTLIST ) return this;
95         break;
96         case BIFF12_ID_COMMENTAUTHORS:
97             if( nRecId == BIFF12_ID_COMMENTAUTHOR ) getComments().appendAuthor( BiffHelper::readString( rStrm ) );
98         break;
99         case BIFF12_ID_COMMENTLIST:
100             if( nRecId == BIFF12_ID_COMMENT ) { importComment( rStrm ); return this; }
101         break;
102         case BIFF12_ID_COMMENT:
103             if( (nRecId == BIFF12_ID_COMMENTTEXT) && mxComment.get() )
104                 mxComment->createText()->importString( rStrm, true );
105         break;
106     }
107     return 0;
108 }
109 
110 void CommentsFragment::onEndRecord()
111 {
112     if( isCurrentElement( BIFF12_ID_COMMENT ) )
113         mxComment.reset();
114 }
115 
116 const RecordInfo* CommentsFragment::getRecordInfos() const
117 {
118     static const RecordInfo spRecInfos[] =
119     {
120         { BIFF12_ID_COMMENT,        BIFF12_ID_COMMENT + 1           },
121         { BIFF12_ID_COMMENTAUTHORS, BIFF12_ID_COMMENTAUTHORS + 1    },
122         { BIFF12_ID_COMMENTLIST,    BIFF12_ID_COMMENTLIST + 1       },
123         { BIFF12_ID_COMMENTS,       BIFF12_ID_COMMENTS + 1          },
124         { -1,                       -1                              }
125     };
126     return spRecInfos;
127 }
128 
129 // private --------------------------------------------------------------------
130 
131 void CommentsFragment::importComment( const AttributeList& rAttribs )
132 {
133     mxComment = getComments().createComment();
134     mxComment->importComment( rAttribs );
135 }
136 
137 void CommentsFragment::importComment( SequenceInputStream& rStrm )
138 {
139     mxComment = getComments().createComment();
140     mxComment->importComment( rStrm );
141 }
142 
143 // ============================================================================
144 
145 } // namespace xls
146 } // namespace oox
147