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 "ww8glsy.hxx"
34 #include "ww8par.hxx"
35 
37  : rStrm(refStrm)
38  , xStg(pStg)
39  , nStrings(0)
40 {
41  refStrm->SetEndian(SvStreamEndian::LITTLE);
42  WW8Fib aWwFib(*refStrm, nVersion);
43 
44  if (aWwFib.m_nFibBack >= 0x6A) //Word97
45  {
47  aWwFib.m_fWhichTableStm ? SL::a1Table : SL::a0Table, StreamMode::STD_READ);
48 
49  if (xTableStream.is() && ERRCODE_NONE == xTableStream->GetError())
50  {
51  xTableStream->SetEndian(SvStreamEndian::LITTLE);
52  xGlossary.reset(new WW8GlossaryFib(*refStrm, nVersion, aWwFib));
53  }
54  }
55 }
56 
58 {
59  bool bRet=false;
60  for( sal_uInt16 nCnt = pDoc->GetSpzFrameFormats()->size(); nCnt; )
61  {
62  const SwFrameFormat* pFrameFormat = (*pDoc->GetSpzFrameFormats())[ --nCnt ];
63  if ( RES_FLYFRMFMT != pFrameFormat->Which() &&
64  RES_DRAWFRMFMT != pFrameFormat->Which() )
65  continue;
66  const SwFormatAnchor& rAnchor = pFrameFormat->GetAnchor();
67  SwPosition const*const pAPos = rAnchor.GetContentAnchor();
68  if (pAPos &&
69  ((RndStdIds::FLY_AT_PARA == rAnchor.GetAnchorId()) ||
70  (RndStdIds::FLY_AT_CHAR == rAnchor.GetAnchorId())) &&
71  rIdx == pAPos->nNode.GetIndex() )
72  {
73  bRet=true;
74  break;
75  }
76  }
77  return bRet;
78 }
79 
81  bool bSaveRelFile, const std::vector<OUString>& rStrings,
82  const std::vector<ww::bytes>& rExtra)
83 {
84  // this code will be called after reading all text into the
85  // empty sections
86  const OUString aOldURL( rBlocks.GetBaseURL() );
87  bool bRet=false;
88  if( bSaveRelFile )
89  {
90  rBlocks.SetBaseURL(
92  INetURLObject(), rBlocks.GetFileName(),
94  }
95  else
96  rBlocks.SetBaseURL( OUString() );
97 
98  SwNodeIndex aDocEnd( pD->GetNodes().GetEndOfContent() );
99  SwNodeIndex aStart( *aDocEnd.GetNode().StartOfSectionNode(), 1 );
100 
101  // search the first NormalStartNode
102  while( !( aStart.GetNode().IsStartNode() && SwNormalStartNode ==
103  aStart.GetNode().GetStartNode()->GetStartNodeType()) &&
104  aStart < aDocEnd )
105  ++aStart;
106 
107  if( aStart < aDocEnd )
108  {
110  (RES_POOLCOLL_STANDARD, false);
111  sal_uInt16 nGlosEntry = 0;
112  SwContentNode* pCNd = nullptr;
113  do {
114  SwPaM aPam( aStart );
115  {
116  SwNodeIndex& rIdx = aPam.GetPoint()->nNode;
117  ++rIdx;
118  if( nullptr == ( pCNd = rIdx.GetNode().GetTextNode() ) )
119  {
120  pCNd = pD->GetNodes().MakeTextNode( rIdx, pColl );
121  rIdx = *pCNd;
122  }
123  }
124  aPam.GetPoint()->nContent.Assign( pCNd, 0 );
125  aPam.SetMark();
126  {
127  SwNodeIndex& rIdx = aPam.GetPoint()->nNode;
128  rIdx = aStart.GetNode().EndOfSectionIndex() - 1;
129  if(( nullptr == ( pCNd = rIdx.GetNode().GetContentNode() ) )
130  || HasBareGraphicEnd(pD,rIdx))
131  {
132  ++rIdx;
133  pCNd = pD->GetNodes().MakeTextNode( rIdx, pColl );
134  rIdx = *pCNd;
135  }
136  }
137  aPam.GetPoint()->nContent.Assign( pCNd, pCNd->Len() );
138 
139  // now we have the right selection for one entry. Copy this to
140  // the defined TextBlock, but only if it is not an autocorrection
141  // entry (== -1) otherwise the group indicates the group in the
142  // sttbfglsystyle list that this entry belongs to. Unused at the
143  // moment
144  const ww::bytes &rData = rExtra[nGlosEntry];
145  sal_uInt16 n = SVBT16ToUInt16( &(rData[2]) );
146  if(n != 0xFFFF)
147  {
148  rBlocks.ClearDoc();
149  const OUString &rLNm = rStrings[nGlosEntry];
150 
151  OUString sShortcut = rLNm;
152 
153  // Need to check make sure the shortcut is not already being used
154  sal_Int32 nStart = 0;
155  sal_uInt16 nCurPos = rBlocks.GetIndex( sShortcut );
156  while( sal_uInt16(-1) != nCurPos )
157  {
158  sShortcut = rLNm + OUString::number(++nStart); // add a Number to it
159  nCurPos = rBlocks.GetIndex( sShortcut );
160  }
161 
162  if( rBlocks.BeginPutDoc( sShortcut, sShortcut )) // Make the shortcut and the name the same
163 
164  {
165  SwDoc* pGlDoc = rBlocks.GetDoc();
166  SwNodeIndex aIdx( pGlDoc->GetNodes().GetEndOfContent(),
167  -1 );
168  pCNd = aIdx.GetNode().GetContentNode();
169  SwPosition aPos(aIdx, SwIndex(pCNd, pCNd ? pCNd->Len() : 0));
170  pD->getIDocumentContentOperations().CopyRange( aPam, aPos, /*bCopyAll=*/false, /*bCheckPos=*/true, /*bCopyText=*/false );
171  rBlocks.PutDoc();
172  }
173  }
174  aStart = aStart.GetNode().EndOfSectionIndex() + 1;
175  ++nGlosEntry;
176  } while( aStart.GetNode().IsStartNode() &&
177  SwNormalStartNode == aStart.GetNode().
178  GetStartNode()->GetStartNodeType());
179  bRet=true;
180  }
181 
182 // this code will be called after reading all text into the empty sections
183 
184  rBlocks.SetBaseURL( aOldURL );
185  return bRet;
186 }
187 
188 bool WW8Glossary::Load( SwTextBlocks &rBlocks, bool bSaveRelFile )
189 {
190  bool bRet=false;
191  if (xGlossary && xGlossary->IsGlossaryFib() && rBlocks.StartPutMuchBlockEntries())
192  {
193  //read the names of the autotext entries
194  std::vector<OUString> aStrings;
195  std::vector<ww::bytes> aData;
196 
197  rtl_TextEncoding eStructCharSet =
198  WW8Fib::GetFIBCharset(xGlossary->m_chseTables, xGlossary->m_lid);
199 
200  WW8ReadSTTBF(true, *xTableStream, xGlossary->m_fcSttbfglsy,
201  xGlossary->m_lcbSttbfglsy, 0, eStructCharSet, aStrings, &aData );
202 
203  rStrm->Seek(0);
204 
205  if ( 0 != (nStrings = static_cast< sal_uInt16 >(aStrings.size())))
206  {
207  SfxObjectShellLock xDocSh(new SwDocShell(SfxObjectCreateMode::INTERNAL));
208  if (xDocSh->DoInitNew())
209  {
210  SwDoc *pD = static_cast<SwDocShell*>((&xDocSh))->GetDoc();
211 
212  SwNodeIndex aIdx(
214  if( !aIdx.GetNode().IsTextNode() )
215  {
216  OSL_ENSURE( false, "Where is the TextNode?" );
217  pD->GetNodes().GoNext( &aIdx );
218  }
219  SwPaM aPamo( aIdx );
220  aPamo.GetPoint()->nContent.Assign(aIdx.GetNode().GetContentNode(),
221  0);
222  std::unique_ptr<SwWW8ImplReader> xRdr(new SwWW8ImplReader(
223  xGlossary->m_nVersion, xStg.get(), rStrm.get(), *pD, rBlocks.GetBaseURL(),
224  true, false, *aPamo.GetPoint()));
225  xRdr->LoadDoc(this);
226  bRet = MakeEntries(pD, rBlocks, bSaveRelFile, aStrings, aData);
227  }
228  xDocSh->DoClose();
229  rBlocks.EndPutMuchBlockEntries();
230  }
231  }
232  return bRet;
233 }
234 
236 {
237  sal_uInt32 nGlossaryFibOffset = 0;
238  if ( rFib.m_fDot ) // it's a template
239  {
240  if ( rFib.m_pnNext )
241  nGlossaryFibOffset = ( rFib.m_pnNext * 512 );
242  }
243  return nGlossaryFibOffset;
244 }
245 
246 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool is() const
virtual sal_Int32 Len() const
Definition: node.cxx:1180
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:57
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:185
const ContentProperties & rData
SwNode & GetNode() const
Definition: ndindex.hxx:119
WW8Glossary(tools::SvRef< SotStorageStream > &refStrm, sal_uInt8 nVersion, SotStorage *pStg)
Definition: ww8glsy.cxx:36
bool Load(SwTextBlocks &rBlocks, bool bSaveRelFile)
Definition: ww8glsy.cxx:188
virtual bool CopyRange(SwPaM &rPam, SwPosition &rPos, const bool bCopyAll, bool bCheckPos, bool bCopyText) const =0
Copy a selected content range to a position.
tools::SvRef< SotStorageStream > xTableStream
Definition: ww8glsy.hxx:76
IDocumentContentOperations const & getIDocumentContentOperations() const
Definition: doc.cxx:314
const SwFrameFormats * GetSpzFrameFormats() const
Definition: doc.hxx:738
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:80
IDocumentStylePoolAccess const & getIDocumentStylePoolAccess() const
Definition: doc.cxx:425
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
#define RES_FLYFRMFMT
Definition: hintids.hxx:369
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:87
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
SwIndex & Assign(SwIndexReg *, sal_Int32)
Definition: index.cxx:198
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 RES_DRAWFRMFMT
Definition: hintids.hxx:372
#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:403
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:235
sal_uInt16 m_nFibBack
Definition: ww8scan.hxx:1160
bool m_fWhichTableStm
Definition: ww8scan.hxx:1145
FIB - the File Information Block.
Definition: ww8scan.hxx:1096
WW8_PN m_pnNext
Definition: ww8scan.hxx:1137
void ClearDoc()
Definition: swblocks.cxx:475
size_t size() const
Definition: docary.hxx:225
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:469
tools::SvRef< SotStorageStream > & rStrm
Definition: ww8glsy.hxx:77
tools::SvRef< SotStorage > const xStg
Definition: ww8glsy.hxx:78
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:842
SwTextNode * MakeTextNode(const SwNodeIndex &rWhere, SwTextFormatColl *pColl, bool bNewFrames=true)
Implementations of "Make...Node" are in the given .cxx-files.
Definition: ndtxt.cxx:103
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)