I see that no one has answered and it has been a few days, so i will
make a few comments.
1. Ask on the dev mailing list, because the developers can comment more
on these specific issues.
David Roe wrote:
I have read somewhere that the OOo API doesn't work properly when you have
more than one process using the API at the same time. Therefore I have been
trying to get a program written in Python to start up its own instance of
OOo. I have written a little function that finds a spare port and then
starts OOo listening on that port. I use the -env:UserInstallation switch to
point it at a temp directory for that instance, then I use UNO to connect to
it and edit documents.
This sounds like the correct solution to run multiple instances.
I have run into two problems when doing this. Firstly, I want to avoid
having to have an X server but when I start it with any combination of the
following flags -headless, -invisible or -nodefault, some of my code stops
working. The code in question is a function to search for a given string
inside a cursor. In most cases it does that by using
Historically, there has been code that is NOT initialized if the display
is not initialized. For example, certain things that only make sense if
there is a view. I have never tested this, but I will guess things such
as a view cursor, a current controller, selections, etc... I was also
under the impression that some of these limitations have been
systematically removed so that they can be used as you mention.
compareRegionStarts/compareRegionEnds but those don't work for matches
inside tables and generate an exception.
Really? I would expect that they would work regardless.
Remember:
The main document has a text object. Each cell and frame also has their
own text object.
A cursor is created by the text object, and is only useful in the
context of the text object used to create it. When you use the text
object to compare a region, it can only use a cursor or range created
from or somehow related to that range. You always use the main
document's text object.
Before calling compareRegionEnds, verify that the objects use the same
text object. I can not swear to it, but I think that you can use
EqualUnoObjects( oObj1, oObj2 )
I have not tested this, but you can probably use:
If EqualUnoObjects( cursor.text, match.text ) Then
'Compare the text regions here
Else
' they do not use the same text object
End If
When that happens I simply
enumerate all the things inside the cursor and check if any one of them is
the table associated with a match. Without the aformentioned startup flags,
the enumeration of a test document returns 4 items and one of them matches
the table I'm looking for. With the flags, the enumeration only returns 1
item and it's not the one I'm trying to search for. Is there a reason that
tables are ignored/missing from the enumeration when one of those flags is
given? Or is this a bug? The code for the search function is given below:
def findInCursor(self, searchDescriptor, cursor, callback):
"""Finds all text that matches a given search descriptor and for
every
match that lies within the specified cursor, the callback
function
is invoked. If there were matches then true is returned,
otherwise
false is returned"""
document = self.getUnoDocument()
text = document.Text
matches = document.findAll(searchDescriptor)
matched = False
# Iterate over matches
for i in xrange(0, matches.Count):
match = matches.getByIndex(i)
# Check if the match is within the given cursor
try:
if text.compareRegionStarts(match, cursor) <= 0 and \
text.compareRegionEnds(match, cursor) >= 0:
callback(match)
matched = True
except:
OK, this next part I do not really follow.
I expected to see something like:
If NOT IsEmpty(match.TextTable) Then
' The match is contained in a text table
End If
I did not know that None exists. Do you use "Option Explicit" at the top
of your modules? My guess is that if you do, then your code will
generate an error. If this does work, it is probably because "None" does
not exist, so Basic automatically generates a variable and assigns the
value Empty or something. Again, just a guess, and I did not try your code.
You want to see if a text table is contained inside of the cursor?
If you had access to the view cursor, you could use some tricks to place
the cursor before the text table, and then compare regions to see if it
was in the range. With no view, you can probably not do that. A shame,
since you can not simply compare a text table anchor (I wish that you
could).
Another trick that you can consider, is to check if the match is in a
text table. if it is, then start walking out until the text objects
match. I would probably write that as a function. After you have a text
table in the same text object, then you can worry about seeing if the
text table is in the same text object.
# If it's a table then we have to see if the table is in the
# text instead (note, this should probably be recursive)
if match.TextTable != None:
searchEnum = cursor.createEnumeration()
while searchEnum.hasMoreElements():
elem = searchEnum.nextElement()
if elem == match.TextTable:
callback(match)
matched = True
else:
raise
return matched
The second issue that I'm having is that despite running (what I thought
was) two seperate instances using different UserInstallation directories and
listening on different ports, when one copy of OOo exits, it causes the next
API operation on the other copy to generate a RuntimeException with message
"illegal object given!". How do I concurrently run more than one instance of
OOo, or have more than one connection to the API?
The second problem is more important to me as I can work around the first by
using something like Xvfb (although I'd rather not).
Potentially useful information:
OS: Fedora 9 x86_64
Python: 2.5.1
OpenOffice: 2.4.1 (RPMs from Fedora repository)
Thanks,
David Roe
--
Andrew Pitonyak
My Macro Document: http://www.pitonyak.org/AndrewMacro.odt
My Book: http://www.hentzenwerke.com/catalog/oome.htm
Info: http://www.pitonyak.org/oo.php
See Also: http://documentation.openoffice.org/HOW_TO/index.html
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]