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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
use crate::{Element, Error, NodeType, RefNode, Result};

// ------------------------------------------------------------------------------------------------
// Public Types
// ------------------------------------------------------------------------------------------------

///
/// An extended interface that provides access to namespace information for elements, including
/// the resolving of prefixes and namespaces in the hierarchy of the document.
///
/// The abstraction is of a hash map for each element that maps prefixes to namespace URIs. A
/// prefix is of type `Option<String>` so that the un-prefixed namespace can be represented as
/// the prefix `None`. URIs are simply stored as `String`s.
///
/// So, given the following XML:
///
/// ```xml
/// <element
///   xmlns="example.org/schema/common"
///   xmlns:p="example.org/schema/product"
///   xmlns:o="example.org/schema/order">
/// </element>
/// ```
///
/// we would get the following hash:
///
/// ```rust,ignore
/// {
///     Some(
///         "o",
///     ): "example.org/schema/order",
///     None: "example.org/schema/common",
///     Some(
///         "p",
///     ): "example.org/schema/product",
/// }
/// ```
///
pub trait Namespaced: Element {
    ///
    /// Returns `true` if this, and only this, element has a URI mapping for the provided `prefix`,
    /// `false` otherwise.
    ///
    fn contains(&self, prefix: Option<&str>) -> bool;
    ///
    /// Returns the namespace URI associated with the provided `prefix`, `None` if the prefix is not
    /// mapped to a URI for this, and only this, element.
    ///  
    fn get(&self, prefix: Option<&str>) -> Option<String>;
    ///
    /// Returns the namespace URI associated with the provided `prefix` for this element by looking
    /// up the DOM tree through `parent_node` links. Returns `None` if the prefix is not mapped to a
    /// URI on this, or any parent, element.
    ///  
    fn resolve(&self, prefix: Option<&str>) -> Option<String>;

    ///
    /// Returns `true` if this, and only this, element has a URI mapping for the provided
    /// `namespace_uri`, `false` otherwise.
    ///
    fn contains_namespace(&self, namespace_uri: &str) -> bool;
    ///
    /// Returns the prefix associated with the provided `namespace_uri`, `None` if the namespace
    /// URI is not mapped with a prefix for this, and only this, element.
    ///  
    fn get_prefix_for(&self, namespace_uri: &str) -> Option<Option<String>>;
    ///
    /// Returns the prefix associated with the provided `namespace_uri` for this element by looking
    /// up the DOM tree through `parent_node` links. Returns `None` if the namespace is not mapped
    /// with a prefix for this, or any parent, element.
    ///  
    fn resolve_prefix_for(&self, namespace_uri: &str) -> Option<Option<String>>;
}

#[doc(hidden)]
pub(crate) trait MutNamespaced: Namespaced {
    fn insert(&mut self, prefix: Option<&str>, namespace_uri: &str) -> Result<Option<String>>;
    fn remove(&mut self, prefix: Option<&str>) -> Result<Option<String>>;
}

// ------------------------------------------------------------------------------------------------
// Implementations
// ------------------------------------------------------------------------------------------------

impl Namespaced for RefNode {
    fn contains(&self, prefix: Option<&str>) -> bool {
        let ref_self = self.borrow();
        if ref_self.i_node_type == NodeType::Element {
            let current_scope = &ref_self.i_namespaces;
            current_scope.contains_key(&prefix.map(String::from))
        } else {
            false
        }
    }

    fn get(&self, prefix: Option<&str>) -> Option<String> {
        let ref_self = self.borrow();
        if ref_self.i_node_type == NodeType::Element {
            let current_scope = &ref_self.i_namespaces;
            let value = current_scope.get(&prefix.map(String::from));
            value.map(String::to_string)
        } else {
            None
        }
    }

    fn resolve(&self, prefix: Option<&str>) -> Option<String> {
        match self.get(prefix) {
            None => {
                let ref_self = self.borrow();
                match &ref_self.i_parent_node {
                    None => None,
                    Some(parent) => {
                        let parent = parent.clone();
                        let parent_node = parent.upgrade().expect("could not upgrade parent_node");
                        parent_node.resolve(prefix)
                    }
                }
            }
            found => found,
        }
    }

    fn contains_namespace(&self, namespace_uri: &str) -> bool {
        self.get_prefix_for(namespace_uri).is_some()
    }

    fn get_prefix_for(&self, namespace_uri: &str) -> Option<Option<String>> {
        let ref_self = self.borrow();
        if ref_self.i_node_type == NodeType::Element {
            let current_scope = &ref_self.i_namespaces;
            let ns = namespace_uri.to_string();
            let value = current_scope.iter().find(|(_, v)| **v == ns);
            match value {
                None => None,
                Some((k, _)) => Some(k.as_ref().map(String::from)),
            }
        } else {
            None
        }
    }

    fn resolve_prefix_for(&self, namespace_uri: &str) -> Option<Option<String>> {
        match self.get_prefix_for(namespace_uri) {
            None => {
                let ref_self = self.borrow();
                match &ref_self.i_parent_node {
                    None => None,
                    Some(parent) => {
                        let parent = parent.clone();
                        let parent_node = parent.upgrade().expect("could not upgrade parent_node");
                        parent_node.resolve_prefix_for(namespace_uri)
                    }
                }
            }
            found => found,
        }
    }
}

impl MutNamespaced for RefNode {
    fn insert(&mut self, prefix: Option<&str>, namespace_uri: &str) -> Result<Option<String>> {
        let mut mut_self = self.borrow_mut();
        if mut_self.i_node_type == NodeType::Element {
            let current_scope = &mut mut_self.i_namespaces;
            Ok(current_scope.insert(prefix.map(String::from), namespace_uri.to_string()))
        } else {
            Err(Error::InvalidState)
        }
    }

    fn remove(&mut self, prefix: Option<&str>) -> Result<Option<String>> {
        let mut mut_self = self.borrow_mut();
        if mut_self.i_node_type == NodeType::Element {
            let current_scope = &mut mut_self.i_namespaces;
            Ok(current_scope.remove(&prefix.map(String::from)))
        } else {
            Err(Error::InvalidState)
        }
    }
}

// ------------------------------------------------------------------------------------------------
// Unit Tests
// ------------------------------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use crate::convert::{
        as_element_namespaced, as_element_namespaced_mut, MutRefNamespaced, RefNamespaced,
    };
    use crate::node_impl::NodeImpl;
    use crate::{Name, RefNode};
    use std::str::FromStr;

    const HTML: &str = "http://www.w3.org/1999/xhtml";
    const XSD: &str = "http://www.w3.org/2001/XMLSchema";
    const XSLT: &str = "http://www.w3.org/1999/XSL/Transform";
    const EX: &str = "http://example.org/xmlns/example";

    fn make_node(name: &str) -> RefNode {
        let name = Name::from_str(name).unwrap();
        let node = NodeImpl::new_element(name);
        RefNode::new(node)
    }

    #[test]
    fn test_empty_element() {
        let ref_node = make_node("element");
        let namespaced = as_element_namespaced(&ref_node).unwrap();

        // prefix
        assert!(!namespaced.contains(None));
        assert!(!namespaced.contains(Some("s")));
        assert!(namespaced.get(None).is_none());
        assert!(namespaced.get(Some("s")).is_none());
        assert!(namespaced.resolve(None).is_none());
        assert!(namespaced.resolve(Some("s")).is_none());

        // namespace
        assert!(!namespaced.contains_namespace(HTML));
        assert!(namespaced.get_prefix_for(HTML).is_none());
        assert!(namespaced.resolve_prefix_for(HTML).is_none());
    }

    #[test]
    #[allow(unused_must_use)]
    fn test_this_element_string_prefix() {
        let mut ref_node = make_node("element");
        let namespaced = &mut ref_node as MutRefNamespaced<'_>;

        namespaced.insert(Some("xsd"), XSD);

        // prefix
        let ns_result = Some(XSD.to_string());

        assert_eq!(namespaced.contains(None), false);
        assert_eq!(namespaced.contains(Some("xsd")), true);
        assert_eq!(namespaced.get(None), None);
        assert_eq!(namespaced.get(Some("xsd")), ns_result);
        assert_eq!(namespaced.resolve(None), None);
        assert_eq!(namespaced.resolve(Some("xsd")), ns_result);

        // namespace
        let prefix_result = Some(Some("xsd".to_string()));

        assert_eq!(namespaced.contains_namespace(HTML), false);
        assert_eq!(namespaced.contains_namespace(XSD), true);
        assert_eq!(namespaced.get_prefix_for(XSD), prefix_result);
        assert_eq!(namespaced.resolve_prefix_for(XSD), prefix_result);
    }

    #[test]
    #[allow(unused_must_use)]
    fn test_this_element_none_prefix() {
        let mut ref_node = make_node("element");
        let namespaced = &mut ref_node as MutRefNamespaced<'_>;

        namespaced.insert(None, XSD);

        // prefix
        let ns_result = Some(XSD.to_string());

        assert_eq!(namespaced.contains(None), true);
        assert_eq!(namespaced.contains(Some("xsd")), false);
        assert_eq!(namespaced.get(None), ns_result);
        assert_eq!(namespaced.get(Some("xsd")), None);
        assert_eq!(namespaced.resolve(None), ns_result);
        assert_eq!(namespaced.resolve(Some("xsd")), None);

        // namespace
        let prefix_result = Some(None);

        assert_eq!(namespaced.contains_namespace(HTML), false);
        assert_eq!(namespaced.contains_namespace(XSD), true);
        assert_eq!(namespaced.get_prefix_for(XSD), prefix_result);
        assert_eq!(namespaced.resolve_prefix_for(XSD), prefix_result);
    }

    #[test]
    #[allow(unused_must_use)]
    fn test_tree_resolve() {
        //
        // Setup the tree
        //
        let mut ref_node = make_node("element");
        let ref_root = as_element_namespaced_mut(&mut ref_node).unwrap();
        ref_root.insert(Some("xsd"), XSD);

        let mut ref_teen_1 = make_node("teen1");
        {
            let ref_teen_ns = as_element_namespaced_mut(&mut ref_teen_1).unwrap();
            ref_teen_ns.insert(None, EX);
        }
        ref_root.append_child(ref_teen_1.clone());

        let mut ref_teen_2 = make_node("teen2");
        {
            let ref_teen_ns = as_element_namespaced_mut(&mut ref_teen_2).unwrap();
            ref_teen_ns.insert(None, HTML);
        }
        ref_root.append_child(ref_teen_2.clone());

        let mut ref_child = make_node("child");
        {
            let ref_child_ns = as_element_namespaced_mut(&mut ref_child).unwrap();
            ref_child_ns.insert(Some("xslt"), XSLT);
        }
        {
            let ref_teen = as_element_namespaced_mut(&mut ref_teen_2).unwrap();
            ref_teen.append_child(ref_child.clone());
        }

        let ns_child = &ref_child as RefNamespaced<'_>;

        //
        // Get
        //
        assert_eq!(ref_root.get(Some("xsd")), Some(XSD.to_string()));
        assert_eq!(ref_root.get(Some("xslt")), None);
        assert_eq!(ns_child.get(Some("xsd")), None);
        assert_eq!(ns_child.get(Some("xslt")), Some(XSLT.to_string()));

        //
        // Resolve
        //
        assert_eq!(ns_child.resolve(Some("xsd")), Some(XSD.to_string()));
        assert_eq!(ns_child.resolve(None), Some(HTML.to_string()));
        assert_eq!(ns_child.resolve(Some("xslt")), Some(XSLT.to_string()));

        //
        // Resolve by namespace
        //
        assert_eq!(
            ns_child.resolve_prefix_for(XSD),
            Some(Some("xsd".to_string()))
        );
        assert_eq!(ns_child.resolve_prefix_for(HTML), Some(None));
        assert_eq!(
            ns_child.resolve_prefix_for(XSLT),
            Some(Some("xslt".to_string()))
        );
    }
}