Source code: com/aendvari/tethys/context/message/MessageContextUtil.java
1 /*
2 * MessageContextUtil.java
3 *
4 * Copyright (c) 2001, 2002 Aendvari, Ltd. All Rights Reserved.
5 *
6 * See the file LICENSE for terms of use.
7 *
8 */
9
10 package com.aendvari.tethys.context.message;
11
12 import java.util.Collection;
13 import java.util.Iterator;
14
15 import com.aendvari.common.model.*;
16 import com.aendvari.common.osm.*;
17
18 import com.aendvari.tethys.context.*;
19
20
21 /**
22 * <p>Contains various utility methods for message context objects.</p>
23 *
24 * @author Trevor Milne
25 *
26 */
27
28 public class MessageContextUtil
29 {
30 /* Message Contexts */
31
32
33 /**
34 * Extracts all of the message context objects and places them under
35 * the supplied {@link ModelNode}.
36 *
37 * @param contexts The {@link ContextMap} containing the {@link MessageContext} objects.
38 * @param destination The {@link ModelNode} to place the contexts under.
39 *
40 */
41
42 public static void contextsToModel(ContextMap contexts, ModelNode destination)
43 {
44 // get the model tree
45 ModelTree modelTree = destination.getOwnerModelTree();
46
47 // get the root node
48 OsmNode root = contexts.getContexts();
49
50 // copy contexts
51 contextsToModel(modelTree, root, destination);
52 }
53
54 /**
55 * Helper method for #contextsToModel(ContextMap, ModelNode).
56 * Performs recurive conversion of the supplied {@link ModelNode}.
57 *
58 * @param modelTree The {@link ModelTree} of the output model.
59 * @param contextNode The {@link OsmNode} to translate.
60 * @param modelNode The {@link ModelNode} to node the contexts under.
61 *
62 */
63
64 protected static void contextsToModel(ModelTree modelTree, OsmNode contextNode, ModelNode modelNode)
65 {
66 Collection children = contextNode.getChildNodes();
67 Iterator childIterator = children.iterator();
68
69 // examine each child of context node
70 while (childIterator.hasNext())
71 {
72 OsmNode childNode = (OsmNode)childIterator.next();
73
74 // check for message context
75 if (childNode.getNodeValue() != null)
76 {
77 // get message context
78 MessageContext context = (MessageContext)childNode.getNodeValue();
79
80 // create context node
81 ModelNode dataNode = modelTree.createNode("context");
82
83 dataNode.appendChild(modelTree.createNode("name", childNode.getNodeName()));
84 dataNode.appendChild(modelTree.createNode("path", context.getContextPath()));
85
86 // add node to model
87 modelNode.appendChild(dataNode);
88 }
89
90 // check for children
91 if (childNode.hasChildNodes())
92 {
93 // add node for children
94 ModelNode subNode = modelTree.createNode(childNode.getNodeName());
95 modelNode.appendChild(subNode);
96
97 // recursively examine child node
98 contextsToModel(modelTree, childNode, subNode);
99 }
100 }
101 }
102
103
104 /* Message Mappings */
105
106
107 /**
108 * Extracts all of the message mapping objects and places them under
109 * the supplied {@link ModelNode}.
110 *
111 * @param mappings The {@link ContextMap} containing the {@link MessageMapping} objects.
112 * @param destination The {@link ModelNode} to place the mappings under.
113 *
114 */
115
116 public static void mappingsToModel(ContextMap mappings, ModelNode destination)
117 {
118 // get the model tree
119 ModelTree modelTree = destination.getOwnerModelTree();
120
121 // get the root node
122 OsmNode root = mappings.getContexts();
123
124 // copy mappings
125 mappingsToModel(modelTree, root, destination);
126 }
127
128 /**
129 * Helper method for #mappingsToModel(ContextMap, ModelNode).
130 * Performs recurive conversion of the supplied {@link ModelNode}.
131 *
132 * @param modelTree The {@link ModelTree} of the output model.
133 * @param mappingNode The {@link OsmNode} to translate.
134 * @param modelNode The {@link ModelNode} to node the mappings under.
135 *
136 */
137
138 protected static void mappingsToModel(ModelTree modelTree, OsmNode mappingNode, ModelNode modelNode)
139 {
140 Collection children = mappingNode.getChildNodes();
141 Iterator childIterator = children.iterator();
142
143 // examine each child of mapping node
144 while (childIterator.hasNext())
145 {
146 OsmNode childNode = (OsmNode)childIterator.next();
147
148 // check for message mapping
149 if (childNode.getNodeValue() != null)
150 {
151 // get message mapping
152 MessageMapping mapping = (MessageMapping)childNode.getNodeValue();
153
154 // create message node
155 ModelNode dataNode = modelTree.createNode("message");
156
157 dataNode.appendChild(modelTree.createNode("name", childNode.getNodeName()));
158 dataNode.appendChild(modelTree.createNode("topic", mapping.getTopic()));
159
160 // add node to model
161 modelNode.appendChild(dataNode);
162 }
163
164 // check for children
165 if (childNode.hasChildNodes())
166 {
167 // add node for children
168 ModelNode subNode = modelTree.createNode(childNode.getNodeName());
169 modelNode.appendChild(subNode);
170
171 // recursively examine child node
172 mappingsToModel(modelTree, childNode, subNode);
173 }
174 }
175 }
176 }
177