Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/aendvari/tethys/context/inset/InsetContextUtil.java


1   /*
2    * InsetContextUtil.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.inset;
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 inset context objects.</p>
23   *
24   * @author  Trevor Milne
25   *
26   */
27  
28  public class InsetContextUtil
29  {
30    /* Insets */
31  
32  
33    /**
34     * Extracts all of the inset objects and places them under
35     * the supplied {@link ModelNode}.
36     *
37     * @param    insets      The {@link ContextMap} containing the {@link Inset} objects.
38     * @param    destination    The {@link ModelNode} to place the insets under.
39     *
40     */
41  
42    public static void insetsToModel(ContextMap insets, ModelNode destination)
43    {
44      // get the model tree
45      ModelTree modelTree = destination.getOwnerModelTree();
46  
47      // get the root node
48      OsmNode root = insets.getContexts();
49  
50      // copy insets
51      insetsToModel(modelTree, root, destination);
52    }
53  
54    /**
55     * Helper method for #insetsToModel(ContextMap, ModelNode).
56     * Performs recurive conversion of the supplied {@link ModelNode}.
57     *
58     * @param    modelTree    The {@link ModelTree} of the output model.
59     * @param    insetNode    The {@link OsmNode} to translate.
60     * @param    modelNode    The {@link ModelNode} to node the insets under.
61     *
62     */
63  
64    protected static void insetsToModel(ModelTree modelTree, OsmNode insetNode, ModelNode modelNode)
65    {
66      Collection children = insetNode.getChildNodes();
67      Iterator childIterator = children.iterator();
68  
69      // examine each child of inset node
70      while (childIterator.hasNext())
71      {
72        OsmNode childNode = (OsmNode)childIterator.next();
73  
74        // check for inset content
75        if (childNode.getNodeValue() != null)
76        {
77          // get inset
78          Inset inset= (Inset)childNode.getNodeValue();
79  
80          // create inset node
81          ModelNode dataNode = modelTree.createNode("inset");
82  
83          dataNode.appendChild(modelTree.createNode("name", childNode.getNodeName()));
84          dataNode.appendChild(modelTree.createNode("content", inset.getContent()));
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          insetsToModel(modelTree, childNode, subNode);
99        }
100     }
101   }
102 }
103