xref: /AOO42X/main/sw/source/filter/ww8/ww8glsy.cxx (revision b1c5455db1639c48e26c568e4fa7ee78ca5d60ee)
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 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sw.hxx"
26 
27 #include <tools/urlobj.hxx>
28 #include <svl/urihelper.hxx>
29 #include <rtl/tencinfo.h>
30 #include <swerror.h>
31 #include <ndtxt.hxx>
32 #include <pam.hxx>
33 #include <shellio.hxx>
34 #include <docsh.hxx>
35 #include <fmtanchr.hxx>
36 #include <frmfmt.hxx>
37 #include <doc.hxx>
38 #include <docary.hxx>
39 #include "ww8glsy.hxx"
40 #include "ww8par.hxx"
41 
42 
WW8Glossary(SvStorageStreamRef & refStrm,sal_uInt8 nVersion,SvStorage * pStg)43 WW8Glossary::WW8Glossary(SvStorageStreamRef &refStrm, sal_uInt8 nVersion,
44     SvStorage *pStg)
45     : pGlossary(0), rStrm(refStrm), xStg(pStg), nStrings(0)
46 {
47     refStrm->SetNumberFormatInt(NUMBERFORMAT_INT_LITTLEENDIAN);
48     WW8Fib aWwFib(*refStrm, nVersion);
49 
50     if (aWwFib.nFibBack >= 0x6A)   //Word97
51     {
52         xTableStream = pStg->OpenSotStream(String::CreateFromAscii(
53             aWwFib.fWhichTblStm ? SL::a1Table : SL::a0Table), STREAM_STD_READ);
54 
55         if (xTableStream.Is() && SVSTREAM_OK == xTableStream->GetError())
56         {
57             xTableStream->SetNumberFormatInt(NUMBERFORMAT_INT_LITTLEENDIAN);
58             pGlossary =
59                 new WW8GlossaryFib(*refStrm, nVersion, *xTableStream, aWwFib);
60         }
61     }
62 }
63 
HasBareGraphicEnd(SwDoc * pDoc,SwNodeIndex & rIdx)64 bool WW8Glossary::HasBareGraphicEnd(SwDoc *pDoc,SwNodeIndex &rIdx)
65 {
66     bool bRet=false;
67     for( sal_uInt16 nCnt = pDoc->GetSpzFrmFmts()->Count(); nCnt; )
68     {
69         SwFrmFmt* pFrmFmt = (*pDoc->GetSpzFrmFmts())[ --nCnt ];
70         if ( RES_FLYFRMFMT != pFrmFmt->Which() &&
71             RES_DRAWFRMFMT != pFrmFmt->Which() )
72                 continue;
73         const SwFmtAnchor& rAnchor = pFrmFmt->GetAnchor();
74         SwPosition const*const pAPos = rAnchor.GetCntntAnchor();
75         if (pAPos &&
76             ((FLY_AT_PARA == rAnchor.GetAnchorId()) ||
77              (FLY_AT_CHAR == rAnchor.GetAnchorId())) &&
78             rIdx == pAPos->nNode.GetIndex() )
79             {
80                 bRet=true;
81                 break;
82             }
83     }
84     return bRet;
85 }
86 
MakeEntries(SwDoc * pD,SwTextBlocks & rBlocks,bool bSaveRelFile,const std::vector<String> & rStrings,const std::vector<ww::bytes> & rExtra)87 bool WW8Glossary::MakeEntries(SwDoc *pD, SwTextBlocks &rBlocks,
88     bool bSaveRelFile, const std::vector<String>& rStrings,
89     const std::vector<ww::bytes>& rExtra)
90 {
91     // this code will be called after reading all text into the
92     // empty sections
93     const String aOldURL( rBlocks.GetBaseURL() );
94     bool bRet=false;
95     if( bSaveRelFile )
96     {
97         rBlocks.SetBaseURL(
98             URIHelper::SmartRel2Abs(
99                 INetURLObject(), rBlocks.GetFileName(),
100                 URIHelper::GetMaybeFileHdl()));
101     }
102     else
103         rBlocks.SetBaseURL( aEmptyStr );
104 
105     SwNodeIndex aDocEnd( pD->GetNodes().GetEndOfContent() );
106     SwNodeIndex aStart( *aDocEnd.GetNode().StartOfSectionNode(), 1 );
107 
108     // search the first NormalStartNode
109     while( !( aStart.GetNode().IsStartNode() && SwNormalStartNode ==
110            aStart.GetNode().GetStartNode()->GetStartNodeType()) &&
111             aStart < aDocEnd )
112         aStart++;
113 
114     if( aStart < aDocEnd )
115     {
116         SwTxtFmtColl* pColl = pD->GetTxtCollFromPool
117             (RES_POOLCOLL_STANDARD, false);
118         sal_uInt16 nGlosEntry = 0;
119         SwCntntNode* pCNd = 0;
120         do {
121             SwPaM aPam( aStart );
122             {
123                 SwNodeIndex& rIdx = aPam.GetPoint()->nNode;
124                 rIdx++;
125                 if( 0 == ( pCNd = rIdx.GetNode().GetTxtNode() ) )
126                 {
127                     pCNd = pD->GetNodes().MakeTxtNode( rIdx, pColl );
128                     rIdx = *pCNd;
129                 }
130             }
131             aPam.GetPoint()->nContent.Assign( pCNd, 0 );
132             aPam.SetMark();
133             {
134                 SwNodeIndex& rIdx = aPam.GetPoint()->nNode;
135                 rIdx = aStart.GetNode().EndOfSectionIndex() - 1;
136                 if(( 0 == ( pCNd = rIdx.GetNode().GetCntntNode() ) )
137                         || HasBareGraphicEnd(pD,rIdx))
138                 {
139                     rIdx++;
140                     pCNd = pD->GetNodes().MakeTxtNode( rIdx, pColl );
141                     rIdx = *pCNd;
142                 }
143             }
144             aPam.GetPoint()->nContent.Assign( pCNd, pCNd->Len() );
145 
146             // now we have the right selection for one entry.  Copy this to
147             // the definied TextBlock, but only if it is not an autocorrection
148             // entry (== -1) otherwise the group indicates the group in the
149             // sttbfglsystyle list that this entry belongs to. Unused at the
150             // moment
151             const ww::bytes &rData = rExtra[nGlosEntry];
152             sal_uInt16 n = SVBT16ToShort( &(rData[2]) );
153             if(n != 0xFFFF)
154             {
155                 rBlocks.ClearDoc();
156                 const String &rLNm = rStrings[nGlosEntry];
157 
158                 String sShortcut = rLNm;
159 
160                 // Need to check make sure the shortcut is not already being used
161                 xub_StrLen nStart = 0;
162                 sal_uInt16 nCurPos = rBlocks.GetIndex( sShortcut );
163                 xub_StrLen nLen = sShortcut.Len();
164                 while( (sal_uInt16)-1 != nCurPos )
165                 {
166                     sShortcut.Erase( nLen ) +=
167                         String::CreateFromInt32( ++nStart );    // add an Number to it
168                     nCurPos = rBlocks.GetIndex( sShortcut );
169                 }
170 
171                 if( rBlocks.BeginPutDoc( sShortcut, sShortcut ))    // Make the shortcut and the name the same
172 
173                 {
174                     SwDoc* pGlDoc = rBlocks.GetDoc();
175                     SwNodeIndex aIdx( pGlDoc->GetNodes().GetEndOfContent(),
176                         -1 );
177                     pCNd = aIdx.GetNode().GetCntntNode();
178                     SwPosition aPos( aIdx, SwIndex( pCNd, pCNd->Len() ));
179                     pD->CopyRange( aPam, aPos, false );
180                     rBlocks.PutDoc();
181                 }
182             }
183             aStart = aStart.GetNode().EndOfSectionIndex() + 1;
184             ++nGlosEntry;
185         } while( aStart.GetNode().IsStartNode() &&
186                 SwNormalStartNode == aStart.GetNode().
187                     GetStartNode()->GetStartNodeType());
188         bRet=true;
189     }
190 
191 // this code will be called after reading all text into the empty sections
192 
193     rBlocks.SetBaseURL( aOldURL );
194     return bRet;
195 }
196 
197 
Load(SwTextBlocks & rBlocks,bool bSaveRelFile)198 bool WW8Glossary::Load( SwTextBlocks &rBlocks, bool bSaveRelFile )
199 {
200     bool bRet=false;
201     if (pGlossary && pGlossary->IsGlossaryFib() && rBlocks.StartPutMuchBlockEntries())
202     {
203         //read the names of the autotext entries
204         std::vector<String> aStrings;
205         std::vector<ww::bytes> aData;
206 
207         rtl_TextEncoding eStructCharSet =
208             WW8Fib::GetFIBCharset(pGlossary->chseTables);
209 
210         WW8ReadSTTBF(true, *xTableStream, pGlossary->fcSttbfglsy,
211             pGlossary->lcbSttbfglsy, 0, eStructCharSet, aStrings, &aData );
212 
213         rStrm->Seek(0);
214 
215         if ( 0 != (nStrings = static_cast< sal_uInt16 >(aStrings.size())))
216         {
217             SfxObjectShellLock xDocSh(new SwDocShell(SFX_CREATE_MODE_INTERNAL));
218             if (xDocSh->DoInitNew(0))
219             {
220                 SwDoc *pD =  ((SwDocShell*)(&xDocSh))->GetDoc();
221                 SwWW8ImplReader* pRdr = new SwWW8ImplReader(pGlossary->nVersion,
222                     xStg, &rStrm, *pD, rBlocks.GetBaseURL(), true);
223 
224                 SwNodeIndex aIdx(
225                     *pD->GetNodes().GetEndOfContent().StartOfSectionNode(), 1);
226                 if( !aIdx.GetNode().IsTxtNode() )
227                 {
228                     ASSERT( sal_False, "where is the TextNode?" );
229                     pD->GetNodes().GoNext( &aIdx );
230                 }
231                 SwPaM aPamo( aIdx );
232                 aPamo.GetPoint()->nContent.Assign(aIdx.GetNode().GetCntntNode(),
233                     0);
234                 pRdr->LoadDoc(aPamo,this);
235 
236                 bRet = MakeEntries(pD, rBlocks, bSaveRelFile, aStrings, aData);
237 
238                 delete pRdr;
239             }
240             xDocSh->DoClose();
241             rBlocks.EndPutMuchBlockEntries();
242         }
243     }
244     return bRet;
245 }
246 
247 
IsGlossaryFib()248 bool WW8GlossaryFib::IsGlossaryFib()
249 {
250     // fGlsy will indicate whether this has AutoText or not
251     return fGlsy;
252 }
253 
FindGlossaryFibOffset(SvStream &,SvStream &,const WW8Fib & rFib)254 sal_uInt32 WW8GlossaryFib::FindGlossaryFibOffset(SvStream & /* rTableStrm */,
255                                              SvStream & /* rStrm */,
256                                              const WW8Fib &rFib)
257 {
258     sal_uInt32 nGlossaryFibOffset = 0;
259     if ( rFib.fDot ) // its a template
260     {
261         if ( rFib.pnNext  )
262             nGlossaryFibOffset = ( rFib.pnNext * 512 );
263     }
264     return nGlossaryFibOffset;
265 }
266 
267 /* vi:set tabstop=4 shiftwidth=4 expandtab: */
268