amaramrahul Newbie

Joined: 25 Oct 2006 Posts: 3
|
Posted: Thu Nov 09, 2006 10:10 pm Post subject: code snippet for searching and replacing text in hyperlinks |
|
|
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);
} |
|