LibreOffice Module sw (master)  1
ww8glsy.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <memory>
21 #include <tools/urlobj.hxx>
22 #include <svl/urihelper.hxx>
23 #include <ndtxt.hxx>
24 #include <pam.hxx>
25 #include <poolfmt.hxx>
26 #include <shellio.hxx>
27 #include <docsh.hxx>
28 #include <fmtanchr.hxx>
29 #include <frmfmt.hxx>
30 #include <doc.hxx>
32 #include <docary.hxx>
33 #include <frameformats.hxx>
34 #include "ww8glsy.hxx"
35 #include "ww8par.hxx"
36 
38  : rStrm(refStrm)
39  , xStg(pStg)
40  , nStrings(0)
41 {
42  refStrm->SetEndian(SvStreamEndian::LITTLE);
43  WW8Fib aWwFib(*refStrm, nVersion);
44 
45  if (aWwFib.m_nFibBack >= 0x6A) //Word97
46  {
48  aWwFib.m_fWhichTableStm ? SL::a1Table : SL::a0Table, StreamMode::STD_READ);
49 
50  if (xTableStream.is() && ERRCODE_NONE == xTableStream->GetError())
51  {
52  xTableStream->SetEndian(SvStreamEndian::LITTLE);
53  xGlossary = std::make_shared<WW8GlossaryFib>(*refStrm, nVersion, aWwFib);
54  }
55  }
56 }
57 
59 {
60  bool bRet=false;
61  for( sal_uInt16 nCnt = pDoc->GetSpzFrameFormats()->size(); nCnt; )
62  {
63  const SwFrameFormat* pFrameFormat = (*pDoc->GetSpzFrameFormats())[ --nCnt ];
64  if ( RES_FLYFRMFMT != pFrameFormat->Which() &&
65  RES_DRAWFRMFMT != pFrameFormat->Which() )
66  continue;
67  const SwFormatAnchor& rAnchor = pFrameFormat->GetAnchor();
68  SwPosition const*const pAPos = rAnchor.GetContentAnchor();
69  if (pAPos &&
70  ((RndStdIds::FLY_AT_PARA == rAnchor.GetAnchorId()) ||
71  (RndStdIds::FLY_AT_CHAR == rAnchor.GetAnchorId())) &&
72  rIdx == pAPos->nNode.GetIndex() )
73  {
74  bRet=true;
75  break;
76  }
77  }
78  return bRet;
79 }
80 
82  bool bSaveRelFile, const std::vector<OUString>& rStrings,
83  const std::vector<ww::bytes>& rExtra)
84 {
85  // this code will be called after reading all text into the
86  // empty sections
87  const OUString aOldURL( rBlocks.GetBaseURL() );
88  bool bRet=false;
89  if( bSaveRelFile )
90  {
91  rBlocks.SetBaseURL(
93  INetURLObject(), rBlocks.GetFileName(),
95  }
96  else
97  rBlocks.SetBaseURL( OUString() );
98 
99  SwNodeIndex aDocEnd( pD->GetNodes().GetEndOfContent() );
100  SwNodeIndex aStart( *aDocEnd.GetNode().StartOfSectionNode(), 1 );
101 
102  // search the first NormalStartNode
103  while( !( aStart.GetNode().IsStartNode() && SwNormalStartNode ==
104  aStart.GetNode().GetStartNode()->GetStartNodeType()) &&
105  aStart < aDocEnd )
106  ++aStart;
107 
108  if( aStart < aDocEnd )
109  {
111  (RES_POOLCOLL_STANDARD, false);
112  sal_uInt16 nGlosEntry = 0;
113  SwContentNode* pCNd = nullptr;
114  do {
115  SwPaM aPam( aStart );
116  {
117  SwNodeIndex& rIdx = aPam.GetPoint()->nNode;
118  ++rIdx;
119  if( nullptr == ( pCNd = rIdx.GetNode().GetTextNode() ) )
120  {
121  pCNd = pD->GetNodes().MakeTextNode( rIdx, pColl );
122  rIdx = *pCNd;
123  }
124  }
125  aPam.GetPoint()->nContent.Assign( pCNd, 0 );
126  aPam.SetMark();
127  {
128  SwNodeIndex& rIdx = aPam.GetPoint()->nNode;
129  rIdx = aStart.GetNode().EndOfSectionIndex() - 1;
130  if(( nullptr == ( pCNd = rIdx.GetNode().GetContentNode() ) )
131  || HasBareGraphicEnd(pD,rIdx))
132  {
133  ++rIdx;
134  pCNd = pD->GetNodes().MakeTextNode( rIdx, pColl );
135  rIdx = *pCNd;
136  }
137  }
138  aPam.GetPoint()->nContent.Assign( pCNd, pCNd->Len() );
139 
140  // now we have the right selection for one entry. Copy this to
141  // the defined TextBlock, but only if it is not an autocorrection
142  // entry (== -1) otherwise the group indicates the group in the
143  // sttbfglsystyle list that this entry belongs to. Unused at the
144  // moment
145  const ww::bytes &rData = rExtra[nGlosEntry];
146  sal_uInt16 n = SVBT16ToUInt16( &(rData[2]) );
147  if(n != 0xFFFF)
148  {
149  rBlocks.ClearDoc();
150  const OUString &rLNm = rStrings[nGlosEntry];
151 
152  OUString sShortcut = rLNm;
153 
154  // Need to check make sure the shortcut is not already being used
155  sal_Int32 nStart = 0;
156  sal_uInt16 nCurPos = rBlocks.GetIndex( sShortcut );
157  while( sal_uInt16(-1) != nCurPos )
158  {
159  sShortcut = rLNm + OUString::number(++nStart); // add a Number to it
160  nCurPos = rBlocks.GetIndex( sShortcut );
161  }
162 
163  if( rBlocks.BeginPutDoc( sShortcut, sShortcut )) // Make the shortcut and the name the same
164 
165  {
166  SwDoc* pGlDoc = rBlocks.GetDoc();
167  SwNodeIndex aIdx( pGlDoc->GetNodes().GetEndOfContent(),
168  -1 );
169  pCNd = aIdx.GetNode().GetContentNode();
170  SwPosition aPos(aIdx, SwIndex(pCNd, pCNd ? pCNd->Len() : 0));
172  rBlocks.PutDoc();
173  }
174  }
175  aStart = aStart.GetNode().EndOfSectionIndex() + 1;
176  ++nGlosEntry;
177  } while( aStart.GetNode().IsStartNode() &&
178  SwNormalStartNode == aStart.GetNode().
179  GetStartNode()->GetStartNodeType());
180  bRet=true;
181  }
182 
183 // this code will be called after reading all text into the empty sections
184 
185  rBlocks.SetBaseURL( aOldURL );
186  return bRet;
187 }
188 
189 bool WW8Glossary::Load( SwTextBlocks &rBlocks, bool bSaveRelFile )
190 {
191  bool bRet=false;
192  if (xGlossary && xGlossary->IsGlossaryFib() && rBlocks.StartPutMuchBlockEntries())
193  {
194  //read the names of the autotext entries
195  std::vector<OUString> aStrings;
196  std::vector<ww::bytes> aData;
197 
198  rtl_TextEncoding eStructCharSet =
199  WW8Fib::GetFIBCharset(xGlossary->m_chseTables, xGlossary->m_lid);
200 
201  WW8ReadSTTBF(true, *xTableStream, xGlossary->m_fcSttbfglsy,
202  xGlossary->m_lcbSttbfglsy, 0, eStructCharSet, aStrings, &aData );
203 
204  rStrm->Seek(0);
205 
206  if ( 0 != (nStrings = static_cast< sal_uInt16 >(aStrings.size())))
207  {
208  SfxObjectShellLock xDocSh(new SwDocShell(SfxObjectCreateMode::INTERNAL));
209  if (xDocSh->DoInitNew())
210  {
211  SwDoc *pD = static_cast<SwDocShell*>((&xDocSh))->GetDoc();
212 
213  SwNodeIndex aIdx(
215  if( !aIdx.GetNode().IsTextNode() )
216  {
217  OSL_ENSURE( false, "Where is the TextNode?" );
218  pD->GetNodes().GoNext( &aIdx );
219  }
220  SwPaM aPamo( aIdx );
221  aPamo.GetPoint()->nContent.Assign(aIdx.GetNode().GetContentNode(),
222  0);
223  std::unique_ptr<SwWW8ImplReader> xRdr(new SwWW8ImplReader(
224  xGlossary->m_nVersion, xStg.get(), rStrm.get(), *pD, rBlocks.GetBaseURL(),
225  true, false, *aPamo.GetPoint()));
226  xRdr->LoadDoc(this);
227  bRet = MakeEntries(pD, rBlocks, bSaveRelFile, aStrings, aData);
228  }
229  xDocSh->DoClose();
230  rBlocks.EndPutMuchBlockEntries();
231  }
232  }
233  return bRet;
234 }
235 
237 {
238  sal_uInt32 nGlossaryFibOffset = 0;
239  if ( rFib.m_fDot ) // it's a template
240  {
241  if ( rFib.m_pnNext )
242  nGlossaryFibOffset = ( rFib.m_pnNext * 512 );
243  }
244  return nGlossaryFibOffset;
245 }
246 
247 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
tools::SvRef< SotStorage > xStg
Definition: ww8glsy.hxx:78
bool is() const
virtual sal_Int32 Len() const
Definition: node.cxx:1181
Represents the style of a paragraph.
Definition: fmtcol.hxx:55
Marks a position in the document model.
Definition: pam.hxx:35
std::shared_ptr< WW8GlossaryFib > xGlossary
Definition: ww8glsy.hxx:75
sal_uInt16 PutDoc()
Definition: swblocks.cxx:396
const char aData[]
Definition: ww8scan.hxx:47
static bool HasBareGraphicEnd(SwDoc *pD, SwNodeIndex const &rIdx)
Definition: ww8glsy.cxx:58
SwNodeIndex nNode
Definition: pam.hxx:37
std::vector< sal_uInt8 > bytes
Definition: types.hxx:29
bool m_fDot
Definition: ww8scan.hxx:1139
sal_Int64 n
Definition: doc.hxx:186
const ContentProperties & rData
SwNode & GetNode() const
Definition: ndindex.hxx:119
WW8Glossary(tools::SvRef< SotStorageStream > &refStrm, sal_uInt8 nVersion, SotStorage *pStg)
Definition: ww8glsy.cxx:37
bool Load(SwTextBlocks &rBlocks, bool bSaveRelFile)
Definition: ww8glsy.cxx:189
tools::SvRef< SotStorageStream > xTableStream
Definition: ww8glsy.hxx:76
IDocumentContentOperations const & getIDocumentContentOperations() const
Definition: doc.cxx:314
const SwFrameFormats * GetSpzFrameFormats() const
Definition: doc.hxx:741
const char a1Table[]
Definition: ww8scan.hxx:45
SwIndex nContent
Definition: pam.hxx:38
static bool MakeEntries(SwDoc *pD, SwTextBlocks &rBlocks, bool bSaveRelFile, const std::vector< OUString > &rStrings, const std::vector< ww::bytes > &rExtra)
Definition: ww8glsy.cxx:81
IDocumentStylePoolAccess const & getIDocumentStylePoolAccess() const
Definition: doc.cxx:425
check if target position is in fly anchored at source range
sal_uLong GetIndex() const
Definition: ndindex.hxx:152
const char a0Table[]
Definition: ww8scan.hxx:46
SwNode & GetEndOfContent() const
Regular ContentSection (i.e. the BodyText).
Definition: ndarr.hxx:163
SwDoc * GetDoc()
returns Doc. But be careful!
Definition: docsh.hxx:203
PaM is Point and Mark: a selection of the document model.
Definition: pam.hxx:136
T * get() const
static SwNodePtr GetStartNode(SwOutlineNodes const *pOutlNds, int nOutlineLevel, SwOutlineNodes::size_type *nOutl)
Definition: docglbl.cxx:89
Style of a layout element.
Definition: frmfmt.hxx:57
void SetBaseURL(const OUString &rURL)
Definition: swblocks.cxx:563
const SwFormatAnchor & GetAnchor(bool=true) const
Definition: fmtanchr.hxx:81
SotStorageStream * OpenSotStream(const OUString &rEleName, StreamMode=StreamMode::STD_READWRITE)
const SwStartNode * StartOfSectionNode() const
Definition: node.hxx:131
const SwPosition * GetPoint() const
Definition: pam.hxx:207
virtual bool CopyRange(SwPaM &rPam, SwPosition &rPos, SwCopyFlags flags) const =0
Copy a selected content range to a position.
SwIndex & Assign(SwIndexReg *, sal_Int32)
Definition: index.cxx:206
RndStdIds GetAnchorId() const
Definition: fmtanchr.hxx:65
SVL_DLLPUBLIC Link< OUString *, bool > const & GetMaybeFileHdl()
const SwPosition * GetContentAnchor() const
Definition: fmtanchr.hxx:67
SwContentNode * GetContentNode()
Definition: node.hxx:615
FlyAnchors.
Definition: fmtanchr.hxx:34
Marks a character position inside a document model node.
Definition: index.hxx:37
void WW8ReadSTTBF(bool bVer8, SvStream &rStrm, sal_uInt32 nStart, sal_Int32 nLen, sal_uInt16 nExtraLen, rtl_TextEncoding eCS, std::vector< OUString > &rArray, std::vector< ww::bytes > *pExtraArray, std::vector< OUString > *pValueArray)
reads array of strings (see MS documentation: String Table stored in File) returns NOT the original p...
Definition: ww8scan.cxx:4121
void EndPutMuchBlockEntries()
Definition: swblocks.cxx:550
Marks a node in the document model.
Definition: ndindex.hxx:31
SwDoc * GetDoc()
Definition: swblocks.cxx:468
sal_uInt16 nStrings
Definition: ww8glsy.hxx:79
bool BeginPutDoc(const OUString &, const OUString &)
Definition: swblocks.cxx:372
sal_uInt16 Which() const
for Querying of Writer-functions.
Definition: format.hxx:78
sal_uLong EndOfSectionIndex() const
Definition: node.hxx:677
OUString GetBaseURL() const
Definition: swblocks.cxx:556
#define ERRCODE_NONE
unsigned char sal_uInt8
sal_uInt16 GetIndex(const OUString &) const
Definition: swblocks.cxx:258
bool DoInitNew(SfxMedium *pMedium=nullptr)
SwNodes & GetNodes()
Definition: doc.hxx:405
virtual SwTextFormatColl * GetTextCollFromPool(sal_uInt16 nId, bool bRegardLanguage=true)=0
Return "Auto-Collection with ID.
static sal_uInt32 FindGlossaryFibOffset(const WW8Fib &rFib)
Definition: ww8glsy.cxx:236
sal_uInt16 m_nFibBack
Definition: ww8scan.hxx:1160
bool m_fWhichTableStm
Definition: ww8scan.hxx:1145
FIB - the File Information Block.
Definition: ww8scan.hxx:1096
constexpr TypedWhichId< SwDrawFrameFormat > RES_DRAWFRMFMT(157)
WW8_PN m_pnNext
Definition: ww8scan.hxx:1137
void ClearDoc()
Definition: swblocks.cxx:475
constexpr TypedWhichId< SwFlyFrameFormat > RES_FLYFRMFMT(154)
size_t size() const
static rtl_TextEncoding GetFIBCharset(sal_uInt16 chs, LanguageType nLidLocale)
Definition: ww8scan.cxx:6592
virtual void SetMark()
Unless this is called, the getter method of Mark will return Point.
Definition: pam.cxx:475
tools::SvRef< SotStorageStream > & rStrm
Definition: ww8glsy.hxx:77
void SvStream & rStrm
SwContentNode * GoNext(SwNodeIndex *) const
Definition: nodes.cxx:1277
bool StartPutMuchBlockEntries()
Definition: swblocks.cxx:542
OUString const & GetFileName() const
Definition: swblocks.cxx:484
SwTextNode * GetTextNode()
Inline methods from Node.hxx.
Definition: ndtxt.hxx:836
SwTextNode * MakeTextNode(const SwNodeIndex &rWhere, SwTextFormatColl *pColl, bool bNewFrames=true)
Implementations of "Make...Node" are in the given .cxx-files.
Definition: ndtxt.cxx:104
SVL_DLLPUBLIC OUString SmartRel2Abs(INetURLObject const &rTheBaseURIRef, OUString const &rTheRelURIRef, Link< OUString *, bool > const &rMaybeFileHdl=Link< OUString *, bool >(), bool bCheckFileExists=true, bool bIgnoreFragment=false, INetURLObject::EncodeMechanism eEncodeMechanism=INetURLObject::EncodeMechanism::WasEncoded, INetURLObject::DecodeMechanism eDecodeMechanism=INetURLObject::DecodeMechanism::ToIUri, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8, FSysStyle eStyle=FSysStyle::Detect)