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

TextRange with Alien XML Attributes in Java

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
rbattistoni
General User
General User


Joined: 19 Sep 2005
Posts: 15

PostPosted: Tue Dec 20, 2005 2:50 am    Post subject: TextRange with Alien XML Attributes in Java Reply with quote

Hi,

I would like to add an alien XML attribute to a text portion. But I
don't understand very well how I can select only a text portion and add
the attribute only to this text.

Here is the Java code:

--------------------------------
.....

XTextRange xEnd = xText.getEnd();

xEnd.setString(", Now I continue with a new Text
Style...(T1)");
addAttributeToText(xEnd,"semantic-text:text1","N/A");
xEnd = xText.getEnd();
xEnd.setString("I continue again (T2)");
addAttributeToText(xEnd,"semantic-text:text2","N/A");
xText.insertControlCharacter(xTextCursor,
ControlCharacter.PARAGRAPH_BREAK, false);

.......

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

Where addAttributeToText method is:

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

private void addAttributeToText(XTextRange xTextRange,String
XMLAttrName,String XMLAttrVal) throws UnknownPropertyException,
PropertyVetoException, IllegalArgumentException, WrappedTargetException
{

XPropertySet xCursorProps = (XPropertySet)
UnoRuntime.queryInterface(XPropertySet.class, xTextRange);

XNameContainer uda =
createMyAttribute(xCursorProps,"TextUserDefinedAttributes","CDATA",XMLAttrName,XMLAttrVal);
xCursorProps.setPropertyValue("TextUserDefinedAttributes", uda);

}

....
private XNameContainercreateMyAttribute(XPropertySet xSet,
String propertyName, String XMLAttrType, String XMLAttrName,
String XMLAttrValue) throws UnknownPropertyException,
WrappedTargetException {
AttributeData attr = new AttributeData();
attr.Namespace = nameSpace;
attr.Type = XMLAttrType;
attr.Value = XMLAttrValue;

XNameContainer uda = null;
try {
uda = (XNameContainer) AnyConverter.toObject(new Type(
XNameContainer.class), xSet.getPropertyValue(propertyName));
uda.insertByName(XMLAttrName, attr);

} catch (com.sun.star.lang.IllegalArgumentException e) {
e.printStackTrace();
} catch (ElementExistException e) {
e.printStackTrace();
}
return uda;
}
--------------------------------

When I look into the content.xml I have this result:

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

<style:style style:name="T1" style:family="text">
<style:text-properties semantic-text:text1="N/A" />
</style:style>

<style:style style:name="T2" style:family="text">
<style:text-properties semantic-text:text1="N/A" semantic-text:text2="N/A"/>
</style:style>

....

<text:p text:style-name="P3">
This is the first Paragraph
<text:span text:style-name="T1">, Now I continue with a new Text Style...(T1)</text:span>
<text:span text:style-name="T2">I continue again(T2)</text:span>
</text:p>

<text:p text:style-name="P4">
<text:span text:style-name="T2">This is the second Paragraph</text:span>
</text:p>

<text:p text:style-name="Standard">
<text:span text:style-name="T2" />
</text:p>

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

But T2 is propagated onto next paragraphs too, why this?

XTextRange xEnd = xText.getEnd() should be the range relative to the
text portion added; so I thought that the alien attribute lived only in
this text portion and not in the whole text.

And why has T2 the alien attribute defined in T1 too. I would like to
have an OD content.xml like this:

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

<style:style style:name="T1" style:family="text">
<style:text-properties semantic-text:text1="N/A" />
</style:style>

<style:style style:name="T2" style:family="text">
<style:text-properties semantic-text:text2="N/A" />
</style:style>

<text:p text:style-name="P3">
This is the first Paragraph
<text:span text:style-name="T1">, Now I continue with a new Text Style...(T1)</text:span>
<text:span text:style-name="T2">I continue again (T2)</text:span>
</text:p>

<text:p text:style-name="P4">This is the second Paragraph</text:span></text:p>
<text:p text:style-name="Standard"> </text:p>

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

Thanks

Roberto
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 Macros and API 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