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/connectionsfragment.hxx"
29 
30 #include "oox/helper/attributelist.hxx"
31 #include "oox/xls/biffhelper.hxx"
32 #include "oox/xls/connectionsbuffer.hxx"
33 
34 namespace oox {
35 namespace xls {
36 
37 // ============================================================================
38 
39 using namespace ::oox::core;
40 
41 using ::rtl::OUString;
42 
43 // ============================================================================
44 
45 ConnectionContext::ConnectionContext( WorkbookFragmentBase& rParent, Connection& rConnection ) :
46     WorkbookContextBase( rParent ),
47     mrConnection( rConnection )
48 {
49 }
50 
51 ContextHandlerRef ConnectionContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
52 {
53     switch( getCurrentElement() )
54     {
55         case XLS_TOKEN( connection ):
56             if( nElement == XLS_TOKEN( webPr ) )
57             {
58                 mrConnection.importWebPr( rAttribs );
59                 return this;
60             }
61         break;
62 
63         case XLS_TOKEN( webPr ):
64             if( nElement == XLS_TOKEN( tables ) )
65             {
66                 mrConnection.importTables( rAttribs );
67                 return this;
68             }
69         break;
70 
71         case XLS_TOKEN( tables ):
72             mrConnection.importTable( rAttribs, nElement );
73         break;
74     }
75     return 0;
76 }
77 
78 void ConnectionContext::onStartElement( const AttributeList& rAttribs )
79 {
80     if( getCurrentElement() == XLS_TOKEN( connection ) )
81         mrConnection.importConnection( rAttribs );
82 }
83 
84 ContextHandlerRef ConnectionContext::onCreateRecordContext( sal_Int32 nRecId, SequenceInputStream& rStrm )
85 {
86     switch( getCurrentElement() )
87     {
88         case BIFF12_ID_CONNECTION:
89             if( nRecId == BIFF12_ID_WEBPR )
90             {
91                 mrConnection.importWebPr( rStrm );
92                 return this;
93             }
94         break;
95 
96         case BIFF12_ID_WEBPR:
97             if( nRecId == BIFF12_ID_WEBPRTABLES )
98             {
99                 mrConnection.importWebPrTables( rStrm );
100                 return this;
101             }
102         break;
103 
104         case BIFF12_ID_WEBPRTABLES:
105             mrConnection.importWebPrTable( rStrm, nRecId );
106         break;
107     }
108     return 0;
109 }
110 
111 void ConnectionContext::onStartRecord( SequenceInputStream& rStrm )
112 {
113     if( getCurrentElement() == BIFF12_ID_CONNECTION )
114         mrConnection.importConnection( rStrm );
115 }
116 
117 // ============================================================================
118 
119 ConnectionsFragment::ConnectionsFragment( const WorkbookHelper& rHelper, const OUString& rFragmentPath ) :
120     WorkbookFragmentBase( rHelper, rFragmentPath )
121 {
122 }
123 
124 ContextHandlerRef ConnectionsFragment::onCreateContext( sal_Int32 nElement, const AttributeList& /*rAttribs*/ )
125 {
126     switch( getCurrentElement() )
127     {
128         case XML_ROOT_CONTEXT:
129             if( nElement == XLS_TOKEN( connections ) )
130                 return this;
131         break;
132 
133         case XLS_TOKEN( connections ):
134             if( nElement == XLS_TOKEN( connection ) )
135                 return new ConnectionContext( *this, getConnections().createConnection() );
136         break;
137     }
138     return 0;
139 }
140 
141 ContextHandlerRef ConnectionsFragment::onCreateRecordContext( sal_Int32 nRecId, SequenceInputStream& /*rStrm*/ )
142 {
143     switch( getCurrentElement() )
144     {
145         case XML_ROOT_CONTEXT:
146             if( nRecId == BIFF12_ID_CONNECTIONS )
147                 return this;
148         break;
149 
150         case BIFF12_ID_CONNECTIONS:
151             if( nRecId == BIFF12_ID_CONNECTION )
152                 return new ConnectionContext( *this, getConnections().createConnection() );
153         break;
154     }
155     return 0;
156 }
157 
158 const RecordInfo* ConnectionsFragment::getRecordInfos() const
159 {
160     static const RecordInfo spRecInfos[] =
161     {
162         { BIFF12_ID_CONNECTIONS,    BIFF12_ID_CONNECTIONS + 1   },
163         { BIFF12_ID_CONNECTION,     BIFF12_ID_CONNECTION + 1    },
164         { BIFF12_ID_WEBPR,          BIFF12_ID_WEBPR + 1         },
165         { BIFF12_ID_WEBPRTABLES,    BIFF12_ID_WEBPRTABLES + 1   },
166         { -1,                       -1                          }
167     };
168     return spRecInfos;
169 }
170 
171 void ConnectionsFragment::finalizeImport()
172 {
173     getConnections().finalizeImport();
174 }
175 
176 // ============================================================================
177 
178 } // namespace xls
179 } // namespace oox
180