1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
3  *
4  *  The Contents of this file are made available subject to the terms of
5  *  either of the following licenses
6  *
7  *         - GNU Lesser General Public License Version 2.1
8  *         - Sun Industry Standards Source License Version 1.1
9  *
10  *  Sun Microsystems Inc., October, 2000
11  *
12  *  GNU Lesser General Public License Version 2.1
13  *  =============================================
14  *  Copyright 2000 by Sun Microsystems, Inc.
15  *  901 San Antonio Road, Palo Alto, CA 94303, USA
16  *
17  *  This library is free software; you can redistribute it and/or
18  *  modify it under the terms of the GNU Lesser General Public
19  *  License version 2.1, as published by the Free Software Foundation.
20  *
21  *  This library is distributed in the hope that it will be useful,
22  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
23  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24  *  Lesser General Public License for more details.
25  *
26  *  You should have received a copy of the GNU Lesser General Public
27  *  License along with this library; if not, write to the Free Software
28  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29  *  MA  02111-1307  USA
30  *
31  *
32  *  Sun Industry Standards Source License Version 1.1
33  *  =================================================
34  *  The contents of this file are subject to the Sun Industry Standards
35  *  Source License Version 1.1 (the "License"); You may not use this file
36  *  except in compliance with the License. You may obtain a copy of the
37  *  License at http://www.openoffice.org/license.html.
38  *
39  *  Software provided under this License is provided on an "AS IS" basis,
40  *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
41  *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
42  *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
43  *  See the License for the specific provisions governing your rights and
44  *  obligations concerning the Software.
45  *
46  *  The Initial Developer of the Original Code is: IBM Corporation
47  *
48  *  Copyright: 2008 by IBM Corporation
49  *
50  *  All Rights Reserved.
51  *
52  *  Contributor(s): _______________________________________
53  *
54  *
55  ************************************************************************/
56 
57 #include "lwp9reader.hxx"
58 #include <lwpglobalmgr.hxx>
59 #include "lwparrowstyles.hxx"
60 #include <lwpobjhdr.hxx>
61 #include "lwpdoc.hxx"
62 #include <xfilter/xfstylemanager.hxx>
63 #include <lwpdocdata.hxx>
64 #include <lwpbookmarkmgr.hxx>
65 #include <lwpchangemgr.hxx>
66 #include <tools/stream.hxx>
67 
Lwp9Reader(LwpSvStream * pInputStream,IXFStream * pStream)68 Lwp9Reader::Lwp9Reader (LwpSvStream* pInputStream, IXFStream* pStream)
69     : m_pDocStream(pInputStream)
70     , m_pStream(pStream)
71     , m_pObjMgr(nullptr)
72     , m_LwpFileHdr()
73 {}
74 
75 /**
76  * @descr   The entrance of Word Pro 9 import filter.
77  **/
Read()78 bool Lwp9Reader::Read()
79 {
80     bool bRet = true;
81     LwpGlobalMgr* pGlobal = LwpGlobalMgr::GetInstance(m_pDocStream);
82     try
83     {
84         m_pObjMgr = pGlobal->GetLwpObjFactory();
85 
86         //Does not support Word Pro 96 and previous versions
87         if (ReadFileHeader() && LwpFileHeader::m_nFileRevision>=0x000B)
88         {
89             ReadIndex();
90             bRet = ParseDocument();
91         }
92         else
93             bRet = false;
94     }
95     catch(...)
96     {
97         LwpGlobalMgr::DeleteInstance();
98         throw;
99     }
100     LwpGlobalMgr::DeleteInstance();
101     return bRet;
102 }
103 
104 /**
105  * @descr   Read the LWP7 object.
106  */
ReadFileHeader()107 bool Lwp9Reader::ReadFileHeader()
108 {
109     if (!m_pDocStream->CheckSeek(LwpSvStream::LWP_STREAM_BASE))
110         return false;
111 
112     //Remember to initialize the LwpFileHeader::m_nFileRevision first.
113     LwpFileHeader::m_nFileRevision = 0;
114 
115     LwpObjectHeader objHdr;
116     objHdr.Read(*m_pDocStream);
117     sal_Int64 pos = m_pDocStream->Tell();
118     m_LwpFileHdr.Read(m_pDocStream);
119     return m_pDocStream->CheckSeek(pos + objHdr.GetSize());
120 }
121 
122 /**
123  * @descr  Read the index objects at the end of the WordProData stream
124  */
ReadIndex()125 void Lwp9Reader::ReadIndex()
126 {
127     sal_Int64 oldpos = m_pDocStream->Tell();
128     sal_uInt32 rootoffset = m_LwpFileHdr.GetRootIndexOffset();
129     m_pDocStream->Seek(rootoffset + LwpSvStream::LWP_STREAM_BASE);
130     m_pObjMgr->ReadIndex(m_pDocStream);
131     m_pDocStream->Seek(oldpos);
132 }
133 
134 /**
135  * @descr       Parse all document content
136 */
ParseDocument()137 bool Lwp9Reader::ParseDocument()
138 {
139     WriteDocHeader();
140 
141     //Get root document
142     LwpDocument* doc = dynamic_cast<LwpDocument*> ( m_LwpFileHdr.GetDocID().obj().get() );
143 
144     if (!doc)
145         return false;
146 
147     //Parse Doc Data
148     LwpDocData *pDocData = dynamic_cast<LwpDocData*>(doc->GetDocData().obj().get());
149     if (pDocData!=nullptr)
150         pDocData->Parse(m_pStream);
151 
152     //Register Styles
153     RegisteArrowStyles();
154     doc->DoRegisterStyle();
155     XFStyleManager* pXFStyleManager = LwpGlobalMgr::GetInstance()->GetXFStyleManager();
156     pXFStyleManager->ToXml(m_pStream);
157 
158     //Parse document content
159     m_pStream->GetAttrList()->Clear();
160     m_pStream->StartElement( "office:body" );
161 
162     //Parse change list
163     LwpGlobalMgr* pGlobal = LwpGlobalMgr::GetInstance();
164     LwpChangeMgr* pChangeMgr = pGlobal->GetLwpChangeMgr();
165     pChangeMgr->ConvertAllChange(m_pStream);
166 
167     doc->Parse(m_pStream);
168     m_pStream->EndElement("office:body");
169 
170     WriteDocEnd();
171     return true;
172 }
173 
174 /**
175  * @descr   Write xml document header
176  */
WriteDocHeader()177 void Lwp9Reader::WriteDocHeader()
178 {
179     m_pStream->StartDocument();
180 
181     IXFAttrList *pAttrList = m_pStream->GetAttrList();
182 
183     pAttrList->AddAttribute( "xmlns:office", "http://openoffice.org/2000/office" );
184     pAttrList->AddAttribute( "xmlns:style", "http://openoffice.org/2000/style" );
185     pAttrList->AddAttribute( "xmlns:text", "http://openoffice.org/2000/text" );
186     pAttrList->AddAttribute( "xmlns:table", "http://openoffice.org/2000/table" );
187     pAttrList->AddAttribute( "xmlns:draw", "http://openoffice.org/2000/drawing" );
188 
189     pAttrList->AddAttribute( "xmlns:fo", "http://www.w3.org/1999/XSL/Format" );
190     pAttrList->AddAttribute( "xmlns:xlink", "http://www.w3.org/1999/xlink" );
191     pAttrList->AddAttribute( "xmlns:number", "http://openoffice.org/2000/datastyle" );
192     pAttrList->AddAttribute( "xmlns:svg", "http://www.w3.org/2000/svg" );
193     pAttrList->AddAttribute( "xmlns:chart", "http://openoffice.org/2000/chart" );
194 
195     pAttrList->AddAttribute( "xmlns:dr3d", "http://openoffice.org/2000/dr3d" );
196     pAttrList->AddAttribute( "xmlns:math", "http://www.w3.org/1998/Math/MathML" );
197     pAttrList->AddAttribute( "xmlns:form", "http://openoffice.org/2000/form" );
198     pAttrList->AddAttribute( "xmlns:script", "http://openoffice.org/2000/script" );
199     pAttrList->AddAttribute( "xmlns:dc", "http://purl.org/dc/elements/1.1/" );
200 
201     pAttrList->AddAttribute( "xmlns:meta", "http://openoffice.org/2000/meta" );
202     pAttrList->AddAttribute( "office:class", "text");
203     pAttrList->AddAttribute( "office:version", "1.0");
204 
205     m_pStream->StartElement( "office:document" );
206     pAttrList->Clear();
207 
208 }
209 /**
210  * @descr   Write xml document end
211  */
WriteDocEnd()212 void Lwp9Reader::WriteDocEnd()
213 {
214     m_pStream->EndElement("office:document");
215     m_pStream->EndDocument();
216 }
217 
218 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
219