]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.webapp/src/main/webapp/argeo-ria-src/class/org/argeo/ria/util/Element.js
Set DEFAULT_NAMESPACE_MAP instead of passing it in each function call
[gpl/argeo-slc.git] / org.argeo.slc.webapp / src / main / webapp / argeo-ria-src / class / org / argeo / ria / util / Element.js
1 /**
2 * Cross browser XML Element API
3 *
4 * Overrides the Qooxdoo qx.xml.Element to handle the namespace prefixes
5 *
6 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk/html/81f3de54-3b79-46dc-8e01-73ca2d94cdb5.asp
7 * http://developer.mozilla.org/en/docs/Parsing_and_serializing_XML
8 */
9 qx.Class.define("org.argeo.ria.util.Element",
10 {
11
12 statics :
13 {
14
15 DEFAULT_NAMESPACE_MAP : null,
16
17 /**
18 * Selects the first XmlNode that matches the XPath expression.
19 *
20 * @param element {Element | Document} root element for the search
21 * @param query {String} XPath query
22 * @param NSMap (Object) A map matching namespace prefixes to namespace URIS;
23 * @return {Element} first matching element
24 * @signature function(element, query, NSMap)
25 */
26 selectSingleNode : qx.core.Variant.select("qx.client",
27 {
28 "mshtml|opera": function(element, query, NSMap) {
29 NSMap = NSMap || org.argeo.ria.util.Element.DEFAULT_NAMESPACE_MAP;
30 if(NSMap){
31 var namespaces = [];
32 var i=0;
33 for(var prefix in NSMap){
34 namespaces[i] = 'xmlns:'+prefix+'="'+NSMap[prefix]+'"';
35 i++;
36 }
37 var doc = element.ownerDocument || element;
38 doc.setProperty('SelectionNamespaces', namespaces.join(" "));
39 }
40 try{
41 return element.selectSingleNode(query);
42 }catch(err){}
43 },
44
45 "default": function(element, query, NSMap)
46 {
47 NSMap = NSMap || org.argeo.ria.util.Element.DEFAULT_NAMESPACE_MAP;
48 if(!this.__xpe) {
49 this.__xpe = new XPathEvaluator();
50 }
51
52 var xpe = this.__xpe;
53
54 try {
55 var resolver;
56 if(NSMap){
57 resolver = function(prefix){
58 return NSMap[prefix] || null;
59 }
60 }else{
61 resolver = xpe.createNSResolver(element);
62 }
63 //return xpe.evaluate(query, element, xpe.createNSResolver(element), XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
64 return xpe.evaluate(query, element, resolver, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
65 } catch(err) {
66 throw new Error("selectSingleNode: query: " + query + ", element: " + element + ", error: " + err);
67 }
68 }
69 }),
70
71
72 /**
73 * Selects a list of nodes matching the XPath expression.
74 *
75 * @param element {Element | Document} root element for the search
76 * @param query {String} XPath query
77 * @param NSMap {Map} Mapping between namespaces prefixes and URI.
78 * @return {Element[]} List of matching elements
79 * @signature function(element, query, NSMap)
80 */
81 selectNodes : qx.core.Variant.select("qx.client",
82 {
83 "mshtml|opera": function(element, query, NSMap) {
84 NSMap = NSMap || org.argeo.ria.util.Element.DEFAULT_NAMESPACE_MAP;
85 if(NSMap){
86 var namespaces = [];
87 var i=0;
88 for(var prefix in NSMap){
89 namespaces[i] = 'xmlns:'+prefix+'="'+NSMap[prefix]+'"';
90 i++;
91 }
92 var doc = element.ownerDocument || element;
93 doc.setProperty('SelectionNamespaces', namespaces.join(" "));
94 }
95 return element.selectNodes(query);
96 },
97
98 "default": function(element, query, NSMap)
99 {
100 NSMap = NSMap || org.argeo.ria.util.Element.DEFAULT_NAMESPACE_MAP;
101 var xpe = this.__xpe;
102
103 if(!xpe) {
104 this.__xpe = xpe = new XPathEvaluator();
105 }
106
107 try {
108 var resolver;
109 if(NSMap){
110 resolver = function(prefix){
111 return NSMap[prefix] || null;
112 }
113 }else{
114 resolver = xpe.createNSResolver(element);
115 }
116 //var result = xpe.evaluate(query, element, xpe.createNSResolver(element), XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
117 var result = xpe.evaluate(query, element, resolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
118 } catch(err) {
119 throw new Error("selectNodes: query: " + query + ", element: " + element + ", error: " + err);
120 }
121
122 var nodes = [];
123 for (var i=0; i<result.snapshotLength; i++) {
124 nodes[i] = result.snapshotItem(i);
125 }
126
127 return nodes;
128 }
129 }),
130
131
132 /**
133 * Returns a list of elements with the given tag name belonging to the given namespace (http://developer.mozilla.org/en/docs/DOM:element.getElementsByTagNameNS).
134 *
135 * @param element {Element | Document} the element from where the search should start.
136 * Note that only the descendants of this element are included in the search, not the node itself.
137 * @param namespaceURI {var} is the namespace URI of elements to look for . For example, if you need to look
138 * for XHTML elements, use the XHTML namespace URI, <tt>http://www.w3.org/1999/xhtml</tt>.
139 * @param tagname {String} the tagname to look for
140 * @return {Element[]} a list of found elements in the order they appear in the tree.
141 * @signature function(element, namespaceURI, tagname)
142 */
143 getElementsByTagNameNS : qx.core.Variant.select("qx.client",
144 {
145 "mshtml": function(element, namespaceURI, tagname)
146 {
147 var doc = element.ownerDocument || element;
148
149 doc.setProperty("SelectionLanguage", "XPath");
150 doc.setProperty("SelectionNamespaces", "xmlns:ns='" + namespaceURI + "'");
151
152 return qx.xml.Element.selectNodes(element, 'descendant-or-self::ns:' + tagname);
153 },
154
155 "default": function(element, namespaceURI, tagname) {
156 return element.getElementsByTagNameNS(namespaceURI, tagname);
157 }
158 }),
159
160
161 /**
162 * Selects the first XmlNode that matches the XPath expression and returns the text content of the element
163 *
164 * @param element {Element|Document} root element for the search
165 * @param query {String} XPath query
166 * @param NSMap {Object} Mapping between NS prefix / uri
167 * @return {String} the joined text content of the found element or null if not appropriate.
168 * @signature function(element, query)
169 */
170 getSingleNodeText : function(element, query, NSMap)
171 {
172 NSMap = NSMap || org.argeo.ria.util.Element.DEFAULT_NAMESPACE_MAP;
173 var node = org.argeo.ria.util.Element.selectSingleNode(element, query, NSMap);
174 return qx.dom.Node.getText(node);
175 }
176 }
177 });