1 /*
2  * Hunt - A xml library for D programming language.
3  *
4  * Copyright (C) 2006, 2009 Marcin Kalicinski (For C++ Version 1.13)
5  * Copyright (C) 2018-2019 HuntLabs ( For D Language Version)
6  *
7  * Website: https://www.huntlabs.net
8  *
9  * Licensed under the Apache-2.0 License.
10  *
11  */
12 
13 module hunt.xml.Node;
14 
15 import hunt.xml.Common;
16 import hunt.xml.Element;
17 
18 /**
19  * <code>Node</code> defines the polymorphic behavior for all XML nodes in a
20  * dom tree.
21  *
22  * A node can be output as its XML format, can be detached from its position in
23  * a document and can have XPath expressions evaluated on itself.
24  *
25  * A node may optionally support the parent relationship and may be read only.
26  *
27  */
28 class Node {
29     protected NodeType m_type;
30     protected string m_name;
31     protected Element m_parent;
32 
33     this() {
34 
35     }
36     
37     this(NodeType type) {
38         m_type = type;
39     }
40 
41     this(string name, NodeType type) {
42         m_name = name;
43         m_type = type;
44     }
45 
46     /**
47      * Returns the code according to the type of node. This makes processing
48      * nodes polymorphically much easier as the switch statement can be used
49      * instead of multiple if (instanceof) statements.
50      */
51     NodeType getType() {
52         return m_type;
53     }
54 
55     /**
56      * <p>
57      * <code>getName</code> returns the name of this node. This is the XML
58      * local name of the element, attribute, entity or processing instruction.
59      * For CDATA and Text nodes this method will return null.
60      * </p>
61      * 
62      * @return the XML name of this node
63      */
64     string getName() {
65         return m_name;
66     }
67 
68     /**
69      * <p>
70      * Sets the text data of this node or this method will throw an
71      * <code>UnsupportedOperationException</code> if it is read-only.
72      * </p>
73      * 
74      * @param name
75      *            is the new name of this node
76      */
77     void setName(string name) {
78         m_name = name;
79     }
80 
81     /**
82      * <p>
83      * <code>supportsParent</code> returns true if this node supports the
84      * parent relationship.
85      * </p>
86      * 
87      * <p>
88      * Some XML tree implementations are singly linked and only support downward
89      * navigation through children relationships. The default case is that both
90      * parent and children relationships are supported though for memory and
91      * performance reasons the parent relationship may not be supported.
92      * </p>
93      * 
94      * @return true if this node supports the parent relationship or false it is
95      *         not supported
96      */
97     // bool supportsParent();
98 
99     /**
100      * <p>
101      * <code>getParent</code> returns the parent <code>Element</code> if
102      * this node supports the parent relationship or null if it is the root
103      * element or does not support the parent relationship.
104      * </p>
105      * 
106      * <p>
107      * This method is an optional feature and may not be supported for all
108      * <code>Node</code> implementations.
109      * </p>
110      * 
111      * @return the parent of this node or null if it is the root of the tree or
112      *         the parent relationship is not supported.
113      */
114     Element getParent() {
115         return cast(Element) m_parent;
116     }
117 
118     /**
119      * <p>
120      * <code>setParent</code> sets the parent relationship of this node if the
121      * parent relationship is supported or does nothing if the parent
122      * relationship is not supported.
123      * </p>
124      * 
125      * <p>
126      * This method should only be called from inside an <code>Element</code>
127      * implementation method and is not intended for general use.
128      * </p>
129      * 
130      * @param parent
131      *            is the new parent of this node.
132      */
133     void setParent(Element parent) {
134         m_parent = parent;
135     }
136 
137 }