openoffice中一个将java代码转换为c++代码的例子
2011-07-22 13:57
627 查看
java代码如下:
//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));
c++代码如下:
//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
// 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
// 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
// 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
// Don’t forget to add “com.sun.star.container.XNameContainer \” in the makefile
// Don’t forget to add : #include
// 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);
因为对any类型的理解不深入,我不知道如何直接使用any变量,所以我用了三个临时any变量,见最后三行
//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));
c++代码如下:
//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
// 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
// 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
// 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
// Don’t forget to add “com.sun.star.container.XNameContainer \” in the makefile
// Don’t forget to add : #include
// 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);
因为对any类型的理解不深入,我不知道如何直接使用any变量,所以我用了三个临时any变量,见最后三行
相关文章推荐
- JNI native层、C++异步回调JAVA代码。JNI数据类型转换
- java把字符串转化为二进制,再把二进制转换成字符串的一个小例子
- 一个很有意思的java代码例子
- 求一个JAVA代码,转换成OC 的代码,打印机上用。
- java/android 使用swig编译c/c++ 代码类型转换
- 用java写的jodconverter借用openoffice来转换office成pdf的示例代码
- 一个Android JAVA代码中的各显示单位转换工具
- NET2java 一个net代码向java 的代码转换工具
- 介绍一个 C/C++ 、C#、JAVA 代码美化工具
- 如何将一个C++代码工程转换为matlab的mex文件直接调用呢
- 一个性能还不错的DES加密库(转换自同事改写的一份C++代码)
- 反编译:java代码中含有同步和异常的反编译前后代码对照的一个例子(xiongjy)
- 一个使用自定义命名空间的Schema文件,xml文件和castor生成的java代码的例子
- Java8Map示例:一个略复杂的数据映射聚合例子及代码重构
- java也能写出点点算法-像C++一样去优化核心并发的代码例子1
- 一个jaxb的例子看java object和xml之间的转换
- 一个C++程序重构的例子——糟糕的代码
- Java8--传递代码:一个例子
- 一个jaxb的例子看java object和xml之间的转换
- 一个将数据文件转换成excel文件打印的java实现方法的代码片断(Struts+poi)