Translating Java Code in cpp

From Apache OpenOffice Wiki
Jump to: navigation, search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Writer Icon.png

Writer Project

Please view the guidelines
before contributing.

Popular Subcategories: The DPL extension (version 2.3.0) produced a SQL statement which lead to a Database error.
The reason may be an internal error of DPL or an error which you made,
especially when using DPL options like titleregexp.
Query text is:
SELECT DISTINCT `page`.page_namespace AS page_namespace,`page`.page_title AS page_title,`page`.page_id AS page_id, rev_user, rev_user_text, rev_comment, rev_timestamp FROM `revision` AS rev, `page` WHERE 1=1 AND `page`.page_namespace IN (14) AND `page`.page_is_redirect=0 AND `page`.page_id=rev.rev_page AND rev.rev_timestamp=( SELECT MAX(rev_aux.rev_timestamp) FROM `revision` AS rev_aux WHERE rev_aux.rev_page=rev.rev_page ) ORDER BY rev_timestamp DESC LIMIT 5 OFFSET 0

Error message is:
Unknown column 'rev_user' in 'field list' (127.0.0.1)


Internal Documentation: The DPL extension (version 2.3.0) produced a SQL statement which lead to a Database error.
The reason may be an internal error of DPL or an error which you made,
especially when using DPL options like titleregexp.
Query text is:
SELECT DISTINCT `page`.page_namespace AS page_namespace,`page`.page_title AS page_title,`page`.page_id AS page_id, rev_user, rev_user_text, rev_comment, rev_timestamp FROM `revision` AS rev, `page` WHERE 1=1 AND `page`.page_namespace IN (0) AND `page`.page_is_redirect=0 AND `page`.page_id=rev.rev_page AND rev.rev_timestamp=( SELECT MAX(rev_aux.rev_timestamp) FROM `revision` AS rev_aux WHERE rev_aux.rev_page=rev.rev_page ) ORDER BY rev_timestamp DESC LIMIT 5 OFFSET 0

Error message is:
Unknown column 'rev_user' in 'field list' (127.0.0.1)


API Documentation:

Ongoing Efforts: The DPL extension (version 2.3.0) produced a SQL statement which lead to a Database error.
The reason may be an internal error of DPL or an error which you made,
especially when using DPL options like titleregexp.
Query text is:
SELECT DISTINCT `page`.page_namespace AS page_namespace,`page`.page_title AS page_title,`page`.page_id AS page_id, rev_user, rev_user_text, rev_comment, rev_timestamp FROM `revision` AS rev, `page` WHERE 1=1 AND `page`.page_namespace IN (0) AND `page`.page_is_redirect=0 AND `page`.page_id=rev.rev_page AND rev.rev_timestamp=( SELECT MAX(rev_aux.rev_timestamp) FROM `revision` AS rev_aux WHERE rev_aux.rev_page=rev.rev_page ) ORDER BY rev_timestamp DESC LIMIT 5 OFFSET 0

Error message is:
Unknown column 'rev_user' in 'field list' (127.0.0.1)


Sw.OpenOffice.org


Edit-find-replace.png This article should be checked for accuracy and conformity to style.

We start with an example found in OOoForum under the title : Setting the page properties/margins directly from java

Here is the Java code :

//Listing 33 A Java Code Example
// Java
// create new writer document and get text, then manipulate text 
 XComponent xWriterComponent = newDocComponent("swriter"); 
 XTextDocument xTextDocument = (XTextDocument)UnoRuntime.queryInterface(XTextDocument.class,xWriterComponent); 
 // Access the text document's multi service factory, which we will need for most of the 
 // following examples 
 mxDocFactory = (XMultiServiceFactory) UnoRuntime.queryInterface
		(XMultiServiceFactory.class,xTextDocument); 
 XText xText = xTextDocument.getText(); 
 // create a text cursor from the cells XText interface 
 XTextCursor xTextCursor = xText.createTextCursor(); 
 // Get the property set of the cell's TextCursor 
 XPropertySet xTextCursorProps = (XPropertySet)UnoRuntime.queryInterface
		(XPropertySet.class,xTextCursor); 
 // Page Style name 
 String pageStyleName= xTextCursorProps.getPropertyValue("PageStyleName").toString(); 
 // Get the StyleFamiliesSupplier interface of the document 
 XStyleFamiliesSupplier xSupplier = (XStyleFamiliesSupplier) UnoRuntime.queryInterface
		(XStyleFamiliesSupplier.class,xTextDocument); 
 // Use the StyleFamiliesSupplier interface to get the XNameAccess interface of the 
 // actual style families 
 XNameAccess xFamilies = (XNameAccess)UnoRuntime.queryInterface(XNameAccess.class,
		 xSupplier.getStyleFamilies()); 
 // Access the 'PageStyles' Family 
 XNameContainer xFamily = (XNameContainer) UnoRuntime.queryInterface
		(XNameContainer.class,xFamilies.getByName("PageStyles")); 
 // Insert the newly created style into the PageStyles family 
 XStyle xStyle= (XStyle) UnoRuntime.queryInterface(XStyle.class,xFamily.getByName
		(pageStyleName)); 
 // Get the property set of the TextCursor 
 XPropertySet xStyleProps = (XPropertySet)UnoRuntime.queryInterface
		(XPropertySet.class,xStyle); 
 xStyleProps.setPropertyValue("LeftMargin",new Short((short)1200)); 
 xStyleProps.setPropertyValue("RightMargin",new Short((short)1200)); 
 xStyleProps.setPropertyValue("BottomMargin",new Short((short)1200));

Have a look at com.sun.star.text.XTextDocument, com.sun.star.beans.XPropertySet, com.sun.star.style.XStyleFamiliesSupplier, com.sun.star.container.XNameContainer et com.sun.star.style.XStyle interfaces before the C++ translation which gives :

//Listing 34 The corresponding C++ Translation
// C++
// create new writer document and get text, then manipulate text 

// Don't forget to add : using namespace com::sun::star::text;
// Don't forget to add : #include <com/sun/star/text/XTextDocument.hpp>
// Don't forget to add "com.sun.star.text.XTextDocument \" in the makefile

// Don't forget to add : using namespace com::sun::star::beans;
// Don't forget to add : #include <com/sun/star/beans/XPropertySet.hpp>
// Don't forget to add "com.sun.star.beans.XPropertySet \" in the makefile
// Don't forget to add : using namespace com::sun::star::style;
// Don't forget to add : #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
// Don't forget to add "com.sun.star.style.XStyleFamiliesSupplier \" in the makefile
// Don't forget to add : using namespace com::sun::star::container;

// Don't forget to add : #include <com/sun/star/container/XNameContainer.hpp>
// Don't forget to add "com.sun.star.container.XNameContainer \" in the makefile

// Don't forget to add : #include <com/sun/star/style/XStyle.hpp>
// Don't forget to add "com.sun.star.style.XStyle \" in the makefile

// the first line cannot be translated : already done in our main()
// XComponent xWriterComponent = newDocComponent("swriter"); 
 
	Reference < XTextDocument > xTextDocument (xWriterComponent,UNO_QUERY);
 // Access the text document's multi service factory, which we will need for most of the
 // following examples
	Reference< XMultiServiceFactory > mxDocFactory(xTextDocument,UNO_QUERY);
	Reference< XText > xText = xTextDocument->getText();
 // create a text cursor from the cells XText interface
	Reference< XTextCursor > xTextCursor = xText->createTextCursor(); 
 
 // Get the property set of the cell's TextCursor
	Reference< XPropertySet > xTextCursorProps(xTextCursor,UNO_QUERY); 
 
 // Page Style name 
 //*** I add a intermediate variable because of Any type returned by getPropertyValue 
	Any pageStyleName2 = xTextCursorProps->getPropertyValue
							(OUString::createFromAscii("PageStyleName"));
	OUString pageStyleName;
	pageStyleName2 >>= pageStyleName ;

  // Get the StyleFamiliesSupplier interface of the document
	Reference< XStyleFamiliesSupplier > xSupplier(xTextDocument,UNO_QUERY);

 // Use the StyleFamiliesSupplier interface to get the XNameAccess interface of the
 // actual style families
	Reference< XNameAccess > xFamilies(xSupplier->getStyleFamilies(),UNO_QUERY);

 // Access the 'PageStyles' Family 
	Reference< XNameContainer > xFamily(xFamilies->getByName
				(OUString::createFromAscii("PageStyles")),UNO_QUERY);
// Insert the newly created style into the PageStyles family
	Reference< XStyle > xStyle(xFamily->getByName(pageStyleName),UNO_QUERY);

 // Get the property set of the TextCursor 
	Reference< XPropertySet > xStyleProps(xStyle,UNO_QUERY);
	Any lm, rm, bm;
	lm<<=(short)1200; rm<<=(short)1200; bm<<=(short)1200;
	xStyleProps->setPropertyValue(OUString::createFromAscii("LeftMargin"),lm);
	xStyleProps->setPropertyValue(OUString::createFromAscii("RightMargin"),rm);
	xStyleProps->setPropertyValue(OUString::createFromAscii("BottomMargin"),bm);

My skills on Any type is not complete enough, then I don't find a straight forwarder way in using Any than using intermediate variables. See the last three lines for instance.

Go back to Programming OOoWriter


HomePageCpp.png Return to Document Home page

Personal tools