xref: /AOO41X/main/forms/qa/org/openoffice/xforms/Instance.java (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*
2  * To change this template, choose Tools | Templates
3  * and open the template in the editor.
4  */
5 
6 package org.openoffice.xforms;
7 
8 import com.sun.star.xml.dom.DOMException;
9 import com.sun.star.xml.dom.XDocument;
10 import com.sun.star.xml.dom.XNode;
11 import com.sun.star.xml.dom.XNodeList;
12 import java.util.NoSuchElementException;
13 
14 /**
15  *
16  * @author fs93730
17  */
18 public class Instance
19 {
20     private Model           m_model;
21     private XDocument       m_domInstance;
22 
23     protected Instance( Model _model, XDocument _domInstance )
24     {
25         m_model = _model;
26         m_domInstance = _domInstance;
27     }
28 
29     /** creates a new element in the instance
30      *
31      * The element will be inserted immediately below the root node of the instance.
32      *
33      * @param _elementName
34      *      the name of the to-be-created element
35      * @return
36      *      the node of the newly created element
37      * @throws com.sun.star.xml.dom.DOMException
38      */
39     public XNode createElement( String _elementName ) throws DOMException
40     {
41         return createElement( m_domInstance, _elementName, null );
42     }
43 
44     /** creates a new element in the instance
45      *
46      * The element will be inserted immediately below a given XNode.
47      *
48      * @param _parentElement
49      *      the node whose child shall be created
50      * @param _elementName
51      *      the name of the to-be-created element
52      * @return
53      *      the node of the newly created element
54      * @throws com.sun.star.xml.dom.DOMException
55      */
56     public XNode createElement( XNode _parentElement, String _elementName ) throws DOMException
57     {
58         return createElement( _parentElement, _elementName, null );
59     }
60 
61     /** creates a new element in the instance
62      *
63      * The element will be inserted immediately below a given XNode.
64      *
65      * @param _parentElement
66      *      the node whose child shall be created
67      * @param _elementName
68      *      the name of the to-be-created element
69      * @param _initialNodeValue
70      *      the initial value to set at the node. Might be null, in this case no value is set.
71      * @return
72      *      the node of the newly created element
73      * @throws com.sun.star.xml.dom.DOMException
74      */
75     public XNode createElement( XNode _parentElement, String _elementName, String _initialNodeValue ) throws DOMException
76     {
77         XNode node = _parentElement.appendChild(
78             m_model.getUIHelper().createElement( _parentElement, _elementName )
79         );
80         if ( _initialNodeValue != null )
81             node.setNodeValue( _initialNodeValue );
82         return node;
83     }
84 
85     /** removes a child of the root-level node from the instance
86      *
87      * @param _elementName
88      *  the name of the to-be-removed child
89      */
90     public XNode removeNode( String _elementName ) throws DOMException
91     {
92         return removeNode( m_domInstance, _elementName );
93     }
94 
95     /** removes a node from the instance
96      *
97      * @param _parentElement
98      *  the node whose child is to be removed
99      * @param _elementName
100      *  the name of the to-be-removed child
101      */
102     public XNode removeNode( XNode _parentElement, String _elementName ) throws DOMException
103     {
104         XNodeList nodes = _parentElement.getChildNodes();
105         for ( int i=0; i<nodes.getLength(); ++i )
106         {
107             XNode node = nodes.item(i);
108             if ( node.getLocalName().equals( _elementName ) )
109             {
110                 _parentElement.removeChild( node );
111                 return node;
112             }
113         }
114         throw new NoSuchElementException();
115     }
116 
117     /** creates an attribute for the root node of the instance
118      *
119      * @param _attribName
120      *      the name of the to-be-created attribute
121      * @return
122      *      the DOM node, which has already been inserted into the DOM tree
123      * @throws com.sun.star.xml.dom.DOMException
124      */
125     public XNode createAttribute( String _attribName ) throws DOMException
126     {
127         return createAttribute( m_domInstance, _attribName, null );
128     }
129 
130     /** creates an attribute for the root node of the instance
131      *
132      * @param _attribName
133      *      the name of the to-be-created attribute
134      * @param _initialNodeValue
135      *      the initial value to set at the node. Might be null, in this case no value is set.
136      * @return
137      *      the DOM node, which has already been inserted into the DOM tree
138      * @throws com.sun.star.xml.dom.DOMException
139      */
140     public XNode createAttribute( String _attribName, String _initialNodeValue ) throws DOMException
141     {
142         return createAttribute( m_domInstance, _attribName, _initialNodeValue );
143     }
144 
145     /** creates an attribute for the given node
146      *
147      * @param _parentElement
148      *      the element at which the attribute should be created
149      * @param _attribName
150      *      the name of the to-be-created attribute
151      * @return
152      *      the DOM node, which has already been inserted into the DOM tree
153      * @throws com.sun.star.xml.dom.DOMException
154      */
155     public XNode createAttribute( XNode _parentElement, String _attribName ) throws DOMException
156     {
157         return createAttribute( _parentElement, _attribName, null );
158     }
159 
160     /** creates an attribute for the given node
161      *
162      * @param _parentElement
163      *      the element at which the attribute should be created
164      * @param _attribName
165      *      the name of the to-be-created attribute
166      * @param _initialNodeValue
167      *      the initial value to set at the node. Might be null, in this case no value is set.
168      * @return
169      *      the DOM node, which has already been inserted into the DOM tree
170      * @throws com.sun.star.xml.dom.DOMException
171      */
172     public XNode createAttribute( XNode _parentElement, String _attribName, String _initialNodeValue ) throws DOMException
173     {
174         XNode node = _parentElement.appendChild(
175             m_model.getUIHelper().createAttribute( _parentElement, _attribName )
176         );
177         if ( _initialNodeValue != null )
178             node.setNodeValue( _initialNodeValue );
179         return node;
180     }
181 }
182