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 #include "hyperlinkcontext.hxx"
25 
26 #include <com/sun/star/xml/sax/XFastContextHandler.hpp>
27 
28 #include "oox/helper/propertymap.hxx"
29 #include "oox/core/relations.hxx"
30 #include "oox/core/xmlfilterbase.hxx"
31 #include "oox/drawingml/embeddedwavaudiofile.hxx"
32 
33 using ::rtl::OUString;
34 using namespace ::oox::core;
35 using namespace ::com::sun::star::uno;
36 using namespace ::com::sun::star::xml::sax;
37 
38 namespace oox {
39 namespace drawingml {
40 
HyperLinkContext(ContextHandler & rParent,const Reference<XFastAttributeList> & xAttributes,PropertyMap & aProperties)41 HyperLinkContext::HyperLinkContext( ContextHandler& rParent,
42         const Reference< XFastAttributeList >& xAttributes, PropertyMap& aProperties )
43     : ContextHandler( rParent )
44     , maProperties(aProperties)
45 {
46 	OUString sURL, sHref;
47 	OUString aRelId = xAttributes->getOptionalValue( R_TOKEN( id ) );
48 	if ( aRelId.getLength() )
49 	{
50 		OSL_TRACE("OOX: URI rId %s", ::rtl::OUStringToOString (aRelId, RTL_TEXTENCODING_UTF8).pData->buffer);
51 		sHref = getRelations().getExternalTargetFromRelId( aRelId );
52 		if( sHref.getLength() > 0 )
53 		{
54 			OSL_TRACE("OOX: URI href %s", ::rtl::OUStringToOString (sHref, RTL_TEXTENCODING_UTF8).pData->buffer);
55 			sURL = getFilter().getAbsoluteUrl( sHref );
56 		}
57 	}
58 	OUString sTooltip = xAttributes->getOptionalValue( R_TOKEN( tooltip ) );
59 	if ( sTooltip.getLength() )
60 		maProperties[ PROP_Representation ] <<= sTooltip;
61 	OUString sFrame = xAttributes->getOptionalValue( R_TOKEN( tgtFrame ) );
62 	if( sFrame.getLength() )
63 		maProperties[ PROP_TargetFrame ] <<= sFrame;
64 	OUString aAction = xAttributes->getOptionalValue( XML_action );
65 	if ( aAction.getLength() )
66 	{
67 		// reserved values of the unrestricted string aAction:
68 		// ppaction://customshow?id=SHOW_ID				// custom presentation
69 		// ppaction://hlinkfile							// external file via r:id
70 		// ppaction://hlinkpres?slideindex=SLIDE_NUM	// external presentation via r:id
71 		// ppaction://hlinkshowjump?jump=endshow
72 		// ppaction://hlinkshowjump?jump=firstslide
73 		// ppaction://hlinkshowjump?jump=lastslide
74 		// ppaction://hlinkshowjump?jump=lastslideviewed
75 		// ppaction://hlinkshowjump?jump=nextslide
76 		// ppaction://hlinkshowjump?jump=previousslide
77 		// ppaction://hlinksldjump
78 		// ppaction://macro?name=MACRO_NAME
79 		// ppaction://program
80 
81 		const OUString sPPAction( CREATE_OUSTRING( "ppaction://" ) );
82 		if ( aAction.matchIgnoreAsciiCase( sPPAction, 0 ) )
83 		{
84 			OUString aPPAct( aAction.copy( sPPAction.getLength() ) );
85 			sal_Int32 nIndex = aPPAct.indexOf( '?', 0 );
86 			OUString aPPAction( nIndex > 0 ? aPPAct.copy( 0, nIndex ) : aPPAct );
87 
88 			const OUString sHlinkshowjump( CREATE_OUSTRING( "hlinkshowjump" ) );
89 			const OUString sHlinksldjump( CREATE_OUSTRING( "hlinksldjump" ) );
90 			if ( aPPAction.match( sHlinkshowjump ) )
91 			{
92 				const OUString sJump( CREATE_OUSTRING( "jump=" ) );
93 				if ( aPPAct.match( sJump, nIndex + 1 ) )
94 				{
95 					OUString aDestination( aPPAct.copy( nIndex + 1 + sJump.getLength() ) );
96 					sURL = sURL.concat( CREATE_OUSTRING( "#action?jump=" ) );
97 					sURL = sURL.concat( aDestination );
98 				}
99 			}
100 			else if ( aPPAction.match( sHlinksldjump ) )
101 			{
102 				sURL = OUString();
103 
104 				sal_Int32 nIndex2 = 0;
105 				while ( nIndex2 < sHref.getLength() )
106 				{
107 					sal_Unicode nChar = sHref[ nIndex2 ];
108 					if ( ( nChar >= '0' ) && ( nChar <= '9' ) )
109 						break;
110 					nIndex2++;
111 				}
112 				if ( nIndex2 && ( nIndex2 != sHref.getLength() ) )
113 				{
114 					sal_Int32 nLength = 1;
115 					while( ( nIndex2 + nLength ) < sHref.getLength() )
116 					{
117 						sal_Unicode nChar = sHref[ nIndex2 + nLength ];
118 						if ( ( nChar < '0' ) || ( nChar > '9' ) )
119 							break;
120 						nLength++;
121 					}
122 					sal_Int32 nPageNumber = sHref.copy( nIndex2, nLength ).toInt32();
123 					if ( nPageNumber )
124 					{
125 						const OUString sSlide( CREATE_OUSTRING( "slide" ) );
126 						const OUString sNotesSlide( CREATE_OUSTRING( "notesSlide" ) );
127 						const OUString aSlideType( sHref.copy( 0, nIndex2 ) );
128 						if ( aSlideType.match( sSlide ) )
129 							sURL = CREATE_OUSTRING( "#Slide " ).concat( rtl::OUString::valueOf( nPageNumber ) );
130 						else if ( aSlideType.match( sNotesSlide ) )
131 							sURL = CREATE_OUSTRING( "#Notes " ).concat( rtl::OUString::valueOf( nPageNumber ) );
132 //						else: todo for other types such as notesMaster or slideMaster as they can't be referenced easily
133 					}
134 				}
135 			}
136 		}
137 	}
138 	if ( sURL.getLength() )
139 		maProperties[ PROP_URL ] <<= sURL;
140 
141 	// TODO unhandled
142     // XML_invalidUrl
143     // XML_history
144     // XML_highlightClick
145     // XML_endSnd
146 }
147 
~HyperLinkContext()148 HyperLinkContext::~HyperLinkContext()
149 {
150 }
151 
createFastChildContext(::sal_Int32 aElement,const Reference<XFastAttributeList> & xAttribs)152 Reference< XFastContextHandler > HyperLinkContext::createFastChildContext(
153         ::sal_Int32 aElement, const Reference< XFastAttributeList >& xAttribs ) throw (SAXException, RuntimeException)
154 {
155     Reference< XFastContextHandler > xRet;
156     switch( aElement )
157     {
158     case A_TOKEN( extLst ):
159         return xRet;
160     case A_TOKEN( snd ):
161         EmbeddedWAVAudioFile aAudio;
162         getEmbeddedWAVAudioFile( getRelations(), xAttribs, aAudio );
163         break;
164     }
165     if ( !xRet.is() )
166         xRet.set( this );
167     return xRet;
168 }
169 
170 } // namespace drawingml
171 } // namespace oox
172 
173