LibreOffice Module oox (master)  1
hyperlinkcontext.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 "hyperlinkcontext.hxx"
21 
22 #include <com/sun/star/xml/sax/XFastContextHandler.hpp>
23 
26 #include <oox/core/relations.hxx>
28 #include <oox/token/namespaces.hxx>
29 #include <oox/token/properties.hxx>
30 #include <oox/token/tokens.hxx>
31 #include <o3tl/string_view.hxx>
32 
33 using namespace ::oox::core;
34 using namespace ::com::sun::star::uno;
35 using namespace ::com::sun::star::xml::sax;
36 
37 namespace oox::drawingml {
38 
40  const AttributeList& rAttribs, PropertyMap& aProperties )
41  : ContextHandler2( rParent )
42  , maProperties(aProperties)
43 {
44  OUString sURL, sHref;
45  OUString aRelId = rAttribs.getString( R_TOKEN( id ) ).get();
46  if ( !aRelId.isEmpty() )
47  {
48  sHref = getRelations().getExternalTargetFromRelId( aRelId );
49  if( !sHref.isEmpty() )
50  {
51  sURL = getFilter().getAbsoluteUrl( sHref );
52  }
53  else
54  {
55  // not sure if we also need to set sHref to the internal target
56  sURL = getRelations().getInternalTargetFromRelId( aRelId );
57  }
58  }
59  OUString sTooltip = rAttribs.getString( R_TOKEN( tooltip ) ).get();
60  if ( !sTooltip.isEmpty() )
61  maProperties.setProperty(PROP_Representation, sTooltip);
62  OUString sFrame = rAttribs.getString( R_TOKEN( tgtFrame ) ).get();
63  if( !sFrame.isEmpty() )
64  maProperties.setProperty(PROP_TargetFrame, sFrame);
65  OUString aAction = rAttribs.getString( XML_action ).get();
66  if ( !aAction.isEmpty() )
67  {
68  // reserved values of the unrestricted string aAction:
69  // ppaction://customshow?id=SHOW_ID // custom presentation
70  // ppaction://hlinkfile // external file via r:id
71  // ppaction://hlinkpres?slideindex=SLIDE_NUM // external presentation via r:id
72  // ppaction://hlinkshowjump?jump=endshow
73  // ppaction://hlinkshowjump?jump=firstslide
74  // ppaction://hlinkshowjump?jump=lastslide
75  // ppaction://hlinkshowjump?jump=lastslideviewed
76  // ppaction://hlinkshowjump?jump=nextslide
77  // ppaction://hlinkshowjump?jump=previousslide
78  // ppaction://hlinksldjump
79  // ppaction://macro?name=MACRO_NAME
80  // ppaction://program
81 
82  static const OUStringLiteral sPPAction( u"ppaction://" );
83  if ( aAction.matchIgnoreAsciiCase( sPPAction ) )
84  {
85  OUString aPPAct( aAction.copy( sPPAction.getLength() ) );
86  sal_Int32 nIndex = aPPAct.indexOf( '?' );
87  OUString aPPAction( nIndex > 0 ? aPPAct.copy( 0, nIndex ) : aPPAct );
88 
89  if ( aPPAction.match( "hlinkshowjump" ) )
90  {
91  static const OUStringLiteral sJump( u"jump=" );
92  if ( aPPAct.match( sJump, nIndex + 1 ) )
93  {
94  std::u16string_view aDestination( aPPAct.subView( nIndex + 1 + sJump.getLength() ) );
95  sURL += OUString::Concat("#action?jump=") + aDestination;
96  }
97  }
98  else if ( aPPAction.match( "hlinksldjump" ) )
99  {
100  sHref = sURL;
101 
102  sal_Int32 nIndex2 = 0;
103  while ( nIndex2 < sHref.getLength() )
104  {
105  sal_Unicode nChar = sHref[ nIndex2 ];
106  if ( ( nChar >= '0' ) && ( nChar <= '9' ) )
107  break;
108  nIndex2++;
109  }
110  if ( nIndex2 && ( nIndex2 != sHref.getLength() ) )
111  {
112  sal_Int32 nLength = 1;
113  while( ( nIndex2 + nLength ) < sHref.getLength() )
114  {
115  sal_Unicode nChar = sHref[ nIndex2 + nLength ];
116  if ( ( nChar < '0' ) || ( nChar > '9' ) )
117  break;
118  nLength++;
119  }
120  sal_Int32 nPageNumber = o3tl::toInt32(sHref.subView( nIndex2, nLength ));
121  if ( nPageNumber )
122  {
123  const OUString aSlideType( sHref.copy( 0, nIndex2 ) );
124  if ( aSlideType.match( "slide" ) )
125  sURL = "#Slide " + OUString::number( nPageNumber );
126  else if ( aSlideType.match( "notesSlide" ) )
127  sURL = "#Notes " + OUString::number( nPageNumber );
128 // else: todo for other types such as notesMaster or slideMaster as they can't be referenced easily
129  }
130  }
131  }
132  }
133  }
134  if ( !sURL.isEmpty() )
135  maProperties.setProperty(PROP_URL, sURL);
136 
137  // TODO unhandled
138  // XML_invalidUrl
139  // XML_history
140  // XML_highlightClick
141  // XML_endSnd
142 }
143 
145 {
146 }
147 
149  ::sal_Int32 aElement, const AttributeList& )
150 {
151  switch( aElement )
152  {
153  case A_TOKEN( extLst ):
154  maProperties.setProperty(PROP_CharColor, XML_fillcolor);
155  break;
156  case A_TOKEN( snd ):
157  // TODO use getEmbeddedWAVAudioFile() here
158  break;
159  }
160 
161  return this;
162 }
163 
164 } // namespace oox::drawingml
165 
166 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OUString getInternalTargetFromRelId(const OUString &rRelId) const
Returns the internal target of the relation with the passed relation identifier.
Definition: relations.cxx:93
sal_Int32 nIndex
A helper that maps property identifiers to property values.
Definition: propertymap.hxx:51
virtual ::oox::core::ContextHandlerRef onCreateContext(::sal_Int32 Element, const ::oox::AttributeList &rAttribs) override
constexpr OUStringLiteral sFrame
OUString getExternalTargetFromRelId(const OUString &rRelId) const
Returns the external target of the relation with the passed relation identifier.
Definition: relations.cxx:87
bool setProperty(sal_Int32 nPropId, Type &&rValue)
Sets the specified property to the passed value.
Definition: propertymap.hxx:72
OptValue< OUString > getString(sal_Int32 nAttrToken) const
Returns the string value of the specified attribute.
sal_uInt16 sal_Unicode
const Type & get() const
Definition: helper.hxx:184
SvGenericNameContainerMapImpl maProperties
const Relations & getRelations() const
Returns the relations of the current fragment.
float u
sal_Int32 toInt32(std::u16string_view str, sal_Int16 radix=10)
Provides access to attribute values of an element.
XmlFilterBase & getFilter() const
Returns the filter instance.
HyperLinkContext(::oox::core::ContextHandler2Helper const &rParent, const ::oox::AttributeList &rAttribs, PropertyMap &aProperties)
OUString getAbsoluteUrl(const OUString &rUrl) const
Returns an absolute URL for the passed relative or absolute URL.
Definition: filterbase.cxx:268
sal_Int32 nLength
Helper class that provides a context stack.