summaryrefslogtreecommitdiffstats
path: root/tags/cpp-1.0-incubating-M2-final/sca/tools/scagen/src/org/apache/tuscany/sca/cpp/tools/services/GenericDomNodeHandler.java
blob: 1ec393959d3a0287f633796a726d3e2096b1dfa0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/**
 *
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 */

/* @version $Rev$ $Date$ */

package org.apache.tuscany.sca.cpp.tools.services;

import java.io.File;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import org.apache.tuscany.sca.cpp.tools.common.Utils;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * 
 * 
 * 
 *  
 */
public class GenericDomNodeHandler implements DomNodeHandler {
    /**
     * This method parses the DOM attributes into name value pairs in the
     * parameter map each valued keyed by its XPath.
     * 
     * @param node
     *            the DOM node being processed
     * @param contextPath
     *            the XPath to the DOM node being processed
     * @param parameters
     *            a scratchpad map of name value pairs
     */
    public void transferAttrsToParameters(Node node, String contextPath,
            Map parameters) {

        if (node == null || contextPath == null || parameters == null)
            return;

        NamedNodeMap attrs = node.getAttributes();
        if (attrs != null) {
            for (int i = 0; i < attrs.getLength(); i++) {
                Node attr = attrs.item(i);
                parameters.put(contextPath + "/" + "@" + attr.getNodeName(),
                        attr.getNodeValue());

                // Report the library name to the deployment tool if required.
                if ("library".equals(attr.getNodeName())) {
                    //This is a path relative to the composite root.
                    //so we need to add it in.
                    File mod = (File) parameters.get("compositeOrFragmentFile");
                    if (null != mod) {
                        String separatorForRegex = File.separator.replaceAll(
                                "\\\\", "\\\\\\\\");

                        String end = attr.getNodeValue().replaceAll("[/\\\\]+",
                                separatorForRegex);

                        if (!end.startsWith(File.separator)) {
                            end = File.separator + end;
                        }

                        String fullPath = mod.getParentFile().getAbsolutePath()
                                + end;
                        Utils.postEvent(Utils.DEPLOYMENT_ARTEFACT_ENCOUNTERED,
                                fullPath);
                    }

                }

            }
        }
        return;
    }

    /**
     * This method will iterate through the DOM node's children and call the
     * appropriate handler for each one.
     * 
     * @param node
     *            the DOM node being processed
     * @param contextPath
     *            the XPath to the DOM node being processed
     * @param handlers
     *            a map of element name to handler objects
     * @param parameters
     *            a scratchpad map of name value pairs
     */
    public void handleChildElements(Node node, String contextPath,
            Map handlers, Map parameters) {

        if (node == null || contextPath == null)
            return;

        NodeList children = node.getChildNodes();
        if (children != null) {
            for (int i = 0; i < children.getLength(); i++) {
                Node child = children.item(i);
                mapNodeToHandlerAndHandle(child, contextPath + "/"
                        + child.getNodeName(), handlers, parameters);
            }
        }
        return;
    }

    /**
     * This method will resolve any registered handler for a particular DOM
     * element and call the handleNode method on it. If the handlers map has a
     * mapping from "entity" to an instance of EntityHandler which implements
     * the DomNodeHandler interface then any node that looks like <entity>
     * <any_other/><stuff/></entity>
     * 
     * Will be passed to the EntityHandler to process.
     * 
     * @param node
     *            the DOM node being processed
     * @param contextPath
     *            the XPath to the DOM node being processed
     * @param handlers
     *            a map of element name to handler objects
     * @param parameters
     *            a scratchpad map of name value pairs
     */
    public static void mapNodeToHandlerAndHandle(Node node, String contextPath,
            Map handlers, Map parameters) {
        if (node != null && node.getNodeType() == DomNodeHandler.ELEMENT) {
            String nodeName = node.getNodeName();
            if (nodeName != null && nodeName.length() > 0) {
                DomNodeHandler handler = (DomNodeHandler) handlers
                        .get(nodeName);
                if (handler != null) {
                    handler.handleNode(node, contextPath, handlers, parameters);
                }
            }
        }
    }

    /**
     * This method will place the attributes in this node into the parameter map
     * keyed by the XPath and recursively continue processing for any
     * sub-elements of the node.
     * 
     * @param node
     *            The DOM node being consumed
     * @param contextXPath
     *            The XPath to this node
     * @param handlers
     *            The map from element name to node handler
     * @param parameters
     *            A map of parameters - this is often used by a handler to place
     *            a name-value pair, the name is often an Xpath representation
     *            of the location of the data in the DOM but handlers are free
     *            to use whatever they like - the contextXpath is generated as
     *            an Xpath prefix for those handlers that wish to use it.
     */
    public void handleNode(Node node, String contextXPath, Map handlers,
            Map parameters) {

        clearParametersAtThisXPath(contextXPath, parameters);
        transferAttrsToParameters(node, contextXPath, parameters);
        handleChildElements(node, contextXPath, handlers, parameters);
    }

    /**
     * @param contextXPath
     * @param parameters
     */
    private void clearParametersAtThisXPath(String contextXPath, Map parameters) {

        // TODO: Slow but works, improve parameters mechanism overall
        // to make this unecessary
        if (contextXPath == null || parameters == null) {
            return;
        }

        // We want to clear both /compositeFragment and /composite
        // subtrees when we come across the root of either..
        if (contextXPath.equals("/compositeFragment")) {
            //clear both this and "composite"
            contextXPath = "/composite";
        }

        Set parms = parameters.entrySet();
        if (null != parms) {
            Iterator iter = parms.iterator();
            Map.Entry item = null;
            String thisKey;

            while (iter.hasNext()) {
                item = (Entry) iter.next();
                thisKey = (String) item.getKey();
                if (thisKey.startsWith(contextXPath)) {
                    //System.out.println(" removing " + thisKey + " for " +
                    // item.getValue().toString() + " against " + contextXPath);
                    iter.remove();
                }
            }
        }
    }
}