summaryrefslogtreecommitdiff
path: root/forms/qa/org/openoffice/xforms/Instance.java
blob: 40bf14946095220e261b0a67ad0e53b37d8704a0 (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
package org.openoffice.xforms;

import com.sun.star.xml.dom.DOMException;
import com.sun.star.xml.dom.XDocument;
import com.sun.star.xml.dom.XNode;
import com.sun.star.xml.dom.XNodeList;
import java.util.NoSuchElementException;

public class Instance
{
    private Model           m_model;
    private XDocument       m_domInstance;

    protected Instance( Model _model, XDocument _domInstance )
    {
        m_model = _model;
        m_domInstance = _domInstance;
    }

    /** creates a new element in the instance
     *
     * The element will be inserted immediately below the root node of the instance.
     *
     * @param _elementName
     *      the name of the to-be-created element
     * @return
     *      the node of the newly created element
     * @throws com.sun.star.xml.dom.DOMException
     */
    public XNode createElement( String _elementName ) throws DOMException
    {
        return createElement( m_domInstance, _elementName, null );
    }

    /** creates a new element in the instance
     *
     * The element will be inserted immediately below a given XNode.
     *
     * @param _parentElement
     *      the node whose child shall be created
     * @param _elementName
     *      the name of the to-be-created element
     * @return
     *      the node of the newly created element
     * @throws com.sun.star.xml.dom.DOMException
     */
    public XNode createElement( XNode _parentElement, String _elementName ) throws DOMException
    {
        return createElement( _parentElement, _elementName, null );
    }

    /** creates a new element in the instance
     *
     * The element will be inserted immediately below a given XNode.
     *
     * @param _parentElement
     *      the node whose child shall be created
     * @param _elementName
     *      the name of the to-be-created element
     * @param _initialNodeValue
     *      the initial value to set at the node. Might be null, in this case no value is set.
     * @return
     *      the node of the newly created element
     * @throws com.sun.star.xml.dom.DOMException
     */
    public XNode createElement( XNode _parentElement, String _elementName, String _initialNodeValue ) throws DOMException
    {
        XNode node = _parentElement.appendChild(
            m_model.getUIHelper().createElement( _parentElement, _elementName )
        );
        if ( _initialNodeValue != null )
            node.setNodeValue( _initialNodeValue );
        return node;
    }

    /** removes a child of the root-level node from the instance
     *
     * @param _elementName
     *  the name of the to-be-removed child
     */
    public XNode removeNode( String _elementName ) throws DOMException
    {
        return removeNode( m_domInstance, _elementName );
    }

    /** removes a node from the instance
     *
     * @param _parentElement
     *  the node whose child is to be removed
     * @param _elementName
     *  the name of the to-be-removed child
     */
    public XNode removeNode( XNode _parentElement, String _elementName ) throws DOMException
    {
        XNodeList nodes = _parentElement.getChildNodes();
        for ( int i=0; i<nodes.getLength(); ++i )
        {
            XNode node = nodes.item(i);
            if ( node.getLocalName().equals( _elementName ) )
            {
                _parentElement.removeChild( node );
                return node;
            }
        }
        throw new NoSuchElementException();
    }

    /** creates an attribute for the root node of the instance
     *
     * @param _attribName
     *      the name of the to-be-created attribute
     * @return
     *      the DOM node, which has already been inserted into the DOM tree
     * @throws com.sun.star.xml.dom.DOMException
     */
    public XNode createAttribute( String _attribName ) throws DOMException
    {
        return createAttribute( m_domInstance, _attribName, null );
    }

    /** creates an attribute for the root node of the instance
     *
     * @param _attribName
     *      the name of the to-be-created attribute
     * @param _initialNodeValue
     *      the initial value to set at the node. Might be null, in this case no value is set.
     * @return
     *      the DOM node, which has already been inserted into the DOM tree
     * @throws com.sun.star.xml.dom.DOMException
     */
    public XNode createAttribute( String _attribName, String _initialNodeValue ) throws DOMException
    {
        return createAttribute( m_domInstance, _attribName, _initialNodeValue );
    }

    /** creates an attribute for the given node
     *
     * @param _parentElement
     *      the element at which the attribute should be created
     * @param _attribName
     *      the name of the to-be-created attribute
     * @return
     *      the DOM node, which has already been inserted into the DOM tree
     * @throws com.sun.star.xml.dom.DOMException
     */
    public XNode createAttribute( XNode _parentElement, String _attribName ) throws DOMException
    {
        return createAttribute( _parentElement, _attribName, null );
    }

    /** creates an attribute for the given node
     *
     * @param _parentElement
     *      the element at which the attribute should be created
     * @param _attribName
     *      the name of the to-be-created attribute
     * @param _initialNodeValue
     *      the initial value to set at the node. Might be null, in this case no value is set.
     * @return
     *      the DOM node, which has already been inserted into the DOM tree
     * @throws com.sun.star.xml.dom.DOMException
     */
    public XNode createAttribute( XNode _parentElement, String _attribName, String _initialNodeValue ) throws DOMException
    {
        XNode node = _parentElement.appendChild(
            m_model.getUIHelper().createAttribute( _parentElement, _attribName )
        );
        if ( _initialNodeValue != null )
            node.setNodeValue( _initialNodeValue );
        return node;
    }
}