在复杂对象中查找对象

Find an object inside a complex object

我正在使用 Justinmind 的 API 开发插件。我需要的基本上是通过 ID 查找元素。但是,API 似乎没有像 findById() 这样的方法,因为我搜索了整个文档 here。我假设我必须创建一个方法。在这里,我将在 Justinmind 中解释问题的详细信息和一般结构,但问题并非特定于 Justinmind

假设我有以下页面布局:

我将在 Justinmind 中按照以下层次结构设计此页面:

Screen
----Image
----Group
--------Button
------------Image
------------Label
--------Button
------------Image
------------Label
--------Button
------------Image
------------Label
--------Button
------------Image
------------Label
--------Button
------------Image
------------Label
--------Button
------------Image
------------Label
----Text
----Text
----Text

在 Justinmind API 中,屏幕是一个 ICanvas 对象。我将屏幕存储在 myScreen 变量中。然后,getApiRoot() returns 页面的最顶层组件。之后,每个组件都是一个子组件,可以通过 getApiChildren() 方法访问。

List<IComponent> components = myScreen.getApiRoot().getApiChildren();

以上代码returns Image, Group and Texts,即一阶层次组件。例如,我可以按如下方式访问第一个按钮的标签 (OVERVIEW)

IComponent group = components.get(1); //Returns the Group
IComponent button1 = group.getApiChildren().get(0); //Returns the first button group, which is the first child of group
IComponent label1 = button1.getApiChildren().get(1); //Returns the label, which is the second child of the first button group

如您所见,我总是通过 getApiChildren() 方法访问组件。而这在大型动态页面中不太可行。因为我正在编写的插件,例如,当按钮组中缺少 a 标签时会发出警报。为了检查标签是否存在,我必须使用 getApiChildren() 方法遍历所有屏幕组件。

我可以用树来表示:

不过,这个结构总是会变的。我应该构建一棵树还是一个哈希图?在非常复杂的对象中查找对象的最佳方法是什么?

我不知道justinmind。但是如果你在 java 中有一个复杂对象的图表,你可以试试这个库 http://commons.apache.org/proper/commons-jxpath/ 并使用 XPATH syntaxe 来指定你的查询。