2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
6 package org
.openoffice
.xforms
;
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
;
20 private Model m_model
;
21 private XDocument m_domInstance
;
23 protected Instance( Model _model
, XDocument _domInstance
)
26 m_domInstance
= _domInstance
;
29 /** creates a new element in the instance
31 * The element will be inserted immediately below the root node of the instance.
34 * the name of the to-be-created element
36 * the node of the newly created element
37 * @throws com.sun.star.xml.dom.DOMException
39 public XNode
createElement( String _elementName
) throws DOMException
41 return createElement( m_domInstance
, _elementName
, null );
44 /** creates a new element in the instance
46 * The element will be inserted immediately below the root node of the instance.
49 * the name of the to-be-created element
50 * @param _initialNodeValue
51 * the initial value to set at the node. Might be null, in this case no value is set.
53 * the node of the newly created element
54 * @throws com.sun.star.xml.dom.DOMException
56 public XNode
createElement( String _elementName
, String _initialNodeValue
) throws DOMException
58 return createElement( m_domInstance
, _elementName
, _initialNodeValue
);
61 /** creates a new element in the instance
63 * The element will be inserted immediately below a given XNode.
65 * @param _parentElement
66 * the node whose child shall be created
68 * the name of the to-be-created element
70 * the node of the newly created element
71 * @throws com.sun.star.xml.dom.DOMException
73 public XNode
createElement( XNode _parentElement
, String _elementName
) throws DOMException
75 return createElement( _parentElement
, _elementName
, null );
78 /** creates a new element in the instance
80 * The element will be inserted immediately below a given XNode.
82 * @param _parentElement
83 * the node whose child shall be created
85 * the name of the to-be-created element
86 * @param _initialNodeValue
87 * the initial value to set at the node. Might be null, in this case no value is set.
89 * the node of the newly created element
90 * @throws com.sun.star.xml.dom.DOMException
92 public XNode
createElement( XNode _parentElement
, String _elementName
, String _initialNodeValue
) throws DOMException
94 XNode node
= _parentElement
.appendChild(
95 m_model
.getUIHelper().createElement( _parentElement
, _elementName
)
97 if ( _initialNodeValue
!= null )
98 node
.setNodeValue( _initialNodeValue
);
102 /** removes a child of the root-level node from the instance
104 * @param _elementName
105 * the name of the to-be-removed child
107 public XNode
removeNode( String _elementName
) throws DOMException
109 return removeNode( m_domInstance
, _elementName
);
112 /** removes a node from the instance
114 * @param _parentElement
115 * the node whose child is to be removed
116 * @param _elementName
117 * the name of the to-be-removed child
119 public XNode
removeNode( XNode _parentElement
, String _elementName
) throws DOMException
121 XNodeList nodes
= _parentElement
.getChildNodes();
122 for ( int i
=0; i
<nodes
.getLength(); ++i
)
124 XNode node
= nodes
.item(i
);
125 if ( node
.getLocalName().equals( _elementName
) )
127 _parentElement
.removeChild( node
);
131 throw new NoSuchElementException();
134 /** creates an attribute for the root node of the instance
137 * the name of the to-be-created attribute
139 * the DOM node, which has already been inserted into the DOM tree
140 * @throws com.sun.star.xml.dom.DOMException
142 public XNode
createAttribute( String _attribName
) throws DOMException
144 return createAttribute( m_domInstance
, _attribName
, null );
147 /** creates an attribute for the root node of the instance
150 * the name of the to-be-created attribute
151 * @param _initialNodeValue
152 * the initial value to set at the node. Might be null, in this case no value is set.
154 * the DOM node, which has already been inserted into the DOM tree
155 * @throws com.sun.star.xml.dom.DOMException
157 public XNode
createAttribute( String _attribName
, String _initialNodeValue
) throws DOMException
159 return createAttribute( m_domInstance
, _attribName
, _initialNodeValue
);
162 /** creates an attribute for the given node
164 * @param _parentElement
165 * the element at which the attribute should be created
167 * the name of the to-be-created attribute
169 * the DOM node, which has already been inserted into the DOM tree
170 * @throws com.sun.star.xml.dom.DOMException
172 public XNode
createAttribute( XNode _parentElement
, String _attribName
) throws DOMException
174 return createAttribute( _parentElement
, _attribName
, null );
177 /** creates an attribute for the given node
179 * @param _parentElement
180 * the element at which the attribute should be created
182 * the name of the to-be-created attribute
183 * @param _initialNodeValue
184 * the initial value to set at the node. Might be null, in this case no value is set.
186 * the DOM node, which has already been inserted into the DOM tree
187 * @throws com.sun.star.xml.dom.DOMException
189 public XNode
createAttribute( XNode _parentElement
, String _attribName
, String _initialNodeValue
) throws DOMException
191 XNode node
= _parentElement
.appendChild(
192 m_model
.getUIHelper().createAttribute( _parentElement
, _attribName
)
194 if ( _initialNodeValue
!= null )
195 node
.setNodeValue( _initialNodeValue
);