定义服务

From Apache OpenOffice Wiki
Jump to: navigation, search


UNOIDL 服务将接口与属性结合,以指定一个特定功能。此外,老的样式服务可以包括其他服务。为此,服务规范中用到了 interfacepropertyservice 声明。通常,服务是对象实现的基础,虽然 OpenOffice.org API 中的老的样式服务仅作为其他服务的基础或附加项,但是并不表示要由它们自己来实现。


接下来我们将组合 ImageShrink 服务。我们的服务将从源目录读取图像文件,在目标目录中写入找到的图像的缩小版。XImageShrink 接口将提供所需的功能,还有 com.sun.star.document.XFilter 接口,它支持两个方法:

  boolean filter( [in] sequence< com::sun::star::beans::PropertyValue > aDescriptor)
  void cancel()


新样式服务只能包含一个接口,因此需要在一个多继承接口中将 XImageShrinkXFilter 结合起来:

  #ifndef __org_openoffice_test_XImageShrinkFilter_idl__
  #define __org_openoffice_test_XImageShrinkFilter_idl__
  #include <com/sun/star/document/XFilter.idl>
  #include <org/openoffice/test/XImageShrink.idl>
 
  module org { module openoffice { module test {
 
  interface XImageShrinkFilter {
      interface XImageShrink;
      interface com::sun::star::document::XFilter;
  };
 
  }; }; };
 
  #endif


下面代码显示 ImageShrink 服务规范:

  #ifndef __org_openoffice_test_ImageShrink_idl__
  #define __org_openoffice_test_ImageShrink_idl__
  #include <org/openoffice/test/XImageShrinkFilter.idl>
 
  module org { module openoffice { module test {
 
  service ImageShrink: XImageShrinkFilter;
 
  }; }; };
 
  #endif


使用 service 声明定义服务。新样式服务的开头是关健字 service,之后是服务名称、冒号和服务支持的接口名称,最后以分号结束。服务名称的第一个字母应为大写字母。


老的样式服务复杂得多。它的开头是关键字 service,之后是用花括号括起的服务名称和服务主体。service 指令以分号结尾。服务主体可以使用 interfaceservice 指令来引用接口和服务,还可以通过 [property] 指令识别服务支持的属性。


  • 服务主体中后面跟接口名称的 Interface 关键字表示服务支持这些接口。默认情况下,Interface将强制开发者实现这个接口。要向特定服务建议使用某个接口,可以使用 [optional] 标志,将其添加到关键字 interface 但这样将减弱权限的规范。可选的接口可以实现。可以为每个受支持的接口使用一个 interface 声明,或者使用以逗号分隔的接口列表给出由服务导出的接口。Interface 指令的结尾必须是分号。
  • service 指令可以包括其他服务。这样一来,其他服务的所有接口和属性定义都成为当前服务的一部分。要使用可选的服务引用,在 service 关键字前加上 [optional] 标志。可以使用一个声明规定一个服务,或者使用以逗号分隔的服务列表给出要引用的服务。service 声明的结尾是分号。
  • [optional] 声明描述可从外部通过特定名称和类型所达到的服务质量。与接口属性相反,这些质量并不是服务结构的有机部分。请参阅 专业 UNO 中的 UNO 概念 - 属性 以确定何时使用接口属性,以及何时在服务中引入属性。property 关键字必须用方括号括起,之后是已知类型和属性标识符。与服务和接口一样,要使属性成为非强制性的属性,可以使用 [property, optional]。除了 optional 外,还有许多其他标志可以与属性一起使用。下表显示了可与 [optional] 一起使用的所有标志:
属性标记 说明
optional 属性是非强制性的。
readonly 不能使用属性的设定方法,如 setPropertyValue(string name)
bound 值的更改将通知各个注册使用该组件的

com.sun.star.beans.XPropertyChangeListener

constrained 组件必须在更改值以前广播一个事件,侦听器可以否决。
maybeambiguous 某些情况下不能确定值,例如在多重选择时。
maybedefault 值可能来自一个样式或应用程序环境,而非来自对象自身。
maybevoid 属性类型将确定可能值的范围,但是有时可能没有可用信息。如果不定义每个类型的特殊值(即表示没有有意义的值),可以使用 UNO 类型 void。它的意义与关系数据库中的 null 相当。
removable 属性是可删除的。如果属性是可删除的,您必须在接口 com.sun.star.beans.XPropertySetInfo 使用 hasPropertyByName() 检查属性是否存在,并考虑使用 com.sun.star.beans.XPropertyContainer 提供添加或删除属性的功能。
transient 如果对象是序列化的(被持久化保存的),将不存储属性。
  • 可以在一个 property 声明中列出相同类型的几个属性。请记住在结尾添加分号。在您的服务中放入属性时,需要实现接口 com.sun.star.beans.XPropertySet,否则指定的属性将不能用于使用组件的其他程序。


Documentation note.png 一些根本不指定接口而只指定属性的老样式服务可用作 OpenOffice.org 中的 com.sun.star.beans.PropertyValue 的序列,例如 com.sun.star.document.MediaDescriptor


以下 UNOIDL 片断显示了在 UNOIDL 中定义的老样式服务 com.sun.star.text.TextDocument 支持的服务、接口和属性。请注意可选的接口以及可选且只读的属性。

  service TextDocument
  {
  service com::sun::star::document::OfficeDocument;
 
  interface com::sun::star::text::XTextDocument;
  interface com::sun::star::util::XSearchable;
  interface com::sun::star::util::XRefreshable;
  interface com::sun::star::util::XNumberFormatsSupplier;
 
  [optional] interface com::sun::star::text::XFootnotesSupplier;
  [optional] interface com::sun::star::text::XEndnotesSupplier;
  [optional] interface com::sun::star::util::XReplaceable;
  [optional] interface com::sun::star::text::XPagePrintable;
  [optional] interface com::sun::star::text::XReferenceMarksSupplier;
  [optional] interface com::sun::star::text::XLineNumberingSupplier;
  [optional] interface com::sun::star::text::XChapterNumberingSupplier;
  [optional] interface com::sun::star::beans::XPropertySet;
  [optional] interface com::sun::star::text::XTextGraphicObjectsSupplier;
  [optional] interface com::sun::star::text::XTextEmbeddedObjectsSupplier;
  [optional] interface com::sun::star::text::XTextTablesSupplier;
  [optional] interface com::sun::star::style::XStyleFamiliesSupplier;
 
  [optional, property] com::sun::star::lang::Locale CharLocale;
  [optional, property] string WordSeparator;
 
  [optional, readonly, property] long CharacterCount;
  [optional, readonly, property] long ParagraphCount;
  [optional, readonly, property] long WordCount;
 
  };
Documentation note.png 您可能会在老样式服务主体中遇到其他两个指令。关键字 observes 可以用在接口引用之前,表示给定的接口必须是“被观察的”。因为未使用 observes 指令,所以这里不作详细说明。

如果一个服务在引用之前使用 needs 关健字来引用另一个服务,此服务将取决于运行时所需服务的可用性。服务不应该使用 needs,因为这样会导致过分与实现相关。

Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages