OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

code snippet for searching and replacing text in hyperlinks

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets
View previous topic :: View next topic  
Author Message
amaramrahul
Newbie
Newbie


Joined: 25 Oct 2006
Posts: 3

PostPosted: Thu Nov 09, 2006 10:10 pm    Post subject: code snippet for searching and replacing text in hyperlinks Reply with quote

Code snippet for seaching and replacing "id=25" in all hyperlinks with "id=49" in file "c:\test.doc".

----------------------------------------------------------------------------------------------

// (the method getRemoteServiceManager is described in the chapter First Steps in Developer's Guide)
mxRemoteServiceManager = getRemoteServiceManager();

// retrieve the Desktop object, we need its XComponentLoader
Object desktop = mxRemoteServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", mxRemoteContext);

// query the XComponentLoader interface from the Desktop service
XComponentLoader xComponentLoader = (XComponentLoader)UnoRuntime.queryInterface(XComponentLoader.class, desktop);

// define load properties according to com.sun.star.document.MediaDescriptor
/* or simply create an empty array of com.sun.star.beans.PropertyValue structs:
PropertyValue[] loadProps = new PropertyValue[0] */

// the boolean property Hidden tells the office to open a file in hidden mode
PropertyValue[] loadProps = new PropertyValue[1];
loadProps[0] = new PropertyValue();
loadProps[0].Name = "Hidden";
loadProps[0].Value = new Boolean(true);

// load
mxDocComponent = xComponentLoader.loadComponentFromURL("file:///c:/test.doc", "_blank", 0, loadProps);

// Create a Search Descriptor
XTextDocument mxDoc = (XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, mxDocComponent);
XSearchable xSearchable = (XSearchable) UnoRuntime.queryInterface(XSearchable.class, mxDoc);
XSearchDescriptor xUrlSearchDesc = xSearchable.createSearchDescriptor();

// Add hyperlink as one of the search criteria
PropertyValue[] aSearchArgs = new PropertyValue[1];
aSearchArgs[0] = new PropertyValue();
aSearchArgs[0].Name = "HyperLinkURL";

XPropertyReplace xPropSear = (XPropertyReplace)UnoRuntime.queryInterface(XPropertyReplace.class, xUrlSearchDesc);
xPropSear.setSearchAttributes(aSearchArgs);
xPropSear.setValueSearch(false);

// Search for results
Object linkResult = xSearchable.findFirst(xUrlSearchDesc);
while(linkResult != null)
{
// Fetch the Property Set
XTextRange xTextRange = (XTextRange)UnoRuntime.queryInterface(XTextRange.class, linkResult);
XTextCursor xTextCursor = xTextRange.getText().createTextCursorByRange(xTextRange);
XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xTextCursor);

// Replace all occurrences of "id=25" with "id=49" in the hyperlink
xPropSet.setPropertyValue("HyperLinkURL", xPropSet.getPropertyValue("HyperLinkURL").toString().replaceAll("id=25", "id=49");

// Find next search result
linkResult = xSearchable.findNext(xTextRange.getEnd(), xUrlSearchDesc);
}
Back to top
View user's profile Send private message
SergeM
Super User
Super User


Joined: 09 Sep 2003
Posts: 3211
Location: Troyes France

PostPosted: Fri Nov 10, 2006 11:59 pm    Post subject: Reply with quote

amaramrahul,
thank you for this Java code.
_________________
Linux & Windows OOo3.0
UNO & C++ : WIKI
http://wiki.services.openoffice.org/wiki/Using_Cpp_with_the_OOo_SDK
In French
http://wiki.services.openoffice.org/wiki/Documentation/FR/Cpp_Guide
Back to top
View user's profile Send private message Visit poster's website
pfradua
Newbie
Newbie


Joined: 11 Jun 2008
Posts: 2

PostPosted: Thu Feb 26, 2009 2:21 am    Post subject: Reply with quote

Shocked

THANKS!!!
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group