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/commentsfragment.hxx"
25 
26 #include "oox/xls/richstringcontext.hxx"
27 
28 namespace oox {
29 namespace xls {
30 
31 // ============================================================================
32 
33 using namespace ::oox::core;
34 
35 using ::rtl::OUString;
36 
37 // ============================================================================
38 
CommentsFragment(const WorksheetHelper & rHelper,const OUString & rFragmentPath)39 CommentsFragment::CommentsFragment( const WorksheetHelper& rHelper, const OUString& rFragmentPath ) :
40     WorksheetFragmentBase( rHelper, rFragmentPath )
41 {
42 }
43 
onCreateContext(sal_Int32 nElement,const AttributeList & rAttribs)44 ContextHandlerRef CommentsFragment::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
45 {
46     switch( getCurrentElement() )
47     {
48         case XML_ROOT_CONTEXT:
49             if( nElement == XLS_TOKEN( comments ) ) return this;
50         break;
51         case XLS_TOKEN( comments ):
52             if( nElement == XLS_TOKEN( authors ) ) return this;
53             if( nElement == XLS_TOKEN( commentList ) ) return this;
54         break;
55         case XLS_TOKEN( authors ):
56             if( nElement == XLS_TOKEN( author ) ) return this;  // collect author in onCharacters()
57         break;
58         case XLS_TOKEN( commentList ):
59             if( nElement == XLS_TOKEN( comment ) ) { importComment( rAttribs ); return this; }
60         break;
61         case XLS_TOKEN( comment ):
62             if( (nElement == XLS_TOKEN( text )) && mxComment.get() )
63                 return new RichStringContext( *this, mxComment->createText() );
64         break;
65     }
66     return 0;
67 }
68 
onCharacters(const OUString & rChars)69 void CommentsFragment::onCharacters( const OUString& rChars )
70 {
71     if( isCurrentElement( XLS_TOKEN( author ) ) )
72         getComments().appendAuthor( rChars );
73 }
74 
onEndElement()75 void CommentsFragment::onEndElement()
76 {
77     if( isCurrentElement( XLS_TOKEN( comment ) ) )
78         mxComment.reset();
79 }
80 
onCreateRecordContext(sal_Int32 nRecId,SequenceInputStream & rStrm)81 ContextHandlerRef CommentsFragment::onCreateRecordContext( sal_Int32 nRecId, SequenceInputStream& rStrm )
82 {
83     switch( getCurrentElement() )
84     {
85         case XML_ROOT_CONTEXT:
86             if( nRecId == BIFF12_ID_COMMENTS ) return this;
87         break;
88         case BIFF12_ID_COMMENTS:
89             if( nRecId == BIFF12_ID_COMMENTAUTHORS ) return this;
90             if( nRecId == BIFF12_ID_COMMENTLIST ) return this;
91         break;
92         case BIFF12_ID_COMMENTAUTHORS:
93             if( nRecId == BIFF12_ID_COMMENTAUTHOR ) getComments().appendAuthor( BiffHelper::readString( rStrm ) );
94         break;
95         case BIFF12_ID_COMMENTLIST:
96             if( nRecId == BIFF12_ID_COMMENT ) { importComment( rStrm ); return this; }
97         break;
98         case BIFF12_ID_COMMENT:
99             if( (nRecId == BIFF12_ID_COMMENTTEXT) && mxComment.get() )
100                 mxComment->createText()->importString( rStrm, true );
101         break;
102     }
103     return 0;
104 }
105 
onEndRecord()106 void CommentsFragment::onEndRecord()
107 {
108     if( isCurrentElement( BIFF12_ID_COMMENT ) )
109         mxComment.reset();
110 }
111 
getRecordInfos() const112 const RecordInfo* CommentsFragment::getRecordInfos() const
113 {
114     static const RecordInfo spRecInfos[] =
115     {
116         { BIFF12_ID_COMMENT,        BIFF12_ID_COMMENT + 1           },
117         { BIFF12_ID_COMMENTAUTHORS, BIFF12_ID_COMMENTAUTHORS + 1    },
118         { BIFF12_ID_COMMENTLIST,    BIFF12_ID_COMMENTLIST + 1       },
119         { BIFF12_ID_COMMENTS,       BIFF12_ID_COMMENTS + 1          },
120         { -1,                       -1                              }
121     };
122     return spRecInfos;
123 }
124 
125 // private --------------------------------------------------------------------
126 
importComment(const AttributeList & rAttribs)127 void CommentsFragment::importComment( const AttributeList& rAttribs )
128 {
129     mxComment = getComments().createComment();
130     mxComment->importComment( rAttribs );
131 }
132 
importComment(SequenceInputStream & rStrm)133 void CommentsFragment::importComment( SequenceInputStream& rStrm )
134 {
135     mxComment = getComments().createComment();
136     mxComment->importComment( rStrm );
137 }
138 
139 // ============================================================================
140 
141 } // namespace xls
142 } // namespace oox
143