Source code: com/aendvari/tethys/context/model/ModelContextUtil.java
1 /*
2 * ModelContextUtil.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.model;
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 model context objects.</p>
23 *
24 * @author Trevor Milne
25 *
26 */
27
28 public class ModelContextUtil
29 {
30 /* Model Contexts */
31
32
33 /**
34 * Extracts all of the model context objects and places them under
35 * the supplied {@link ModelNode}.
36 *
37 * @param contexts The {@link ContextMap} containing the {@link ModelContext} 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 model context
75 if (childNode.getNodeValue() != null)
76 {
77 // get model context
78 ModelContext context = (ModelContext)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.getModelPath()));
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