2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package org
.openoffice
.xforms
;
21 import com
.sun
.star
.xml
.dom
.DOMException
;
22 import com
.sun
.star
.xml
.dom
.XDocument
;
23 import com
.sun
.star
.xml
.dom
.XNode
;
24 import com
.sun
.star
.xml
.dom
.XNodeList
;
25 import java
.util
.NoSuchElementException
;
29 private final Model m_model
;
30 private final XDocument m_domInstance
;
32 protected Instance( Model _model
, XDocument _domInstance
)
35 m_domInstance
= _domInstance
;
38 /** creates a new element in the instance
40 * The element will be inserted immediately below the root node of the instance.
42 * @param _elementName the name of the to-be-created element
43 * @return the node of the newly created element
45 public XNode
createElement( String _elementName
) throws DOMException
47 return createElement( m_domInstance
, _elementName
, null );
52 /** creates a new element in the instance
54 * The element will be inserted immediately below a given XNode.
56 * @param _parentElement
57 * the node whose child shall be created
59 * the name of the to-be-created element
60 * @param _initialNodeValue
61 * the initial value to set at the node. Might be null, in this case no value is set.
63 * the node of the newly created element
65 private XNode
createElement( XNode _parentElement
, String _elementName
, String _initialNodeValue
) throws DOMException
67 XNode node
= _parentElement
.appendChild(
68 m_model
.getUIHelper().createElement( _parentElement
, _elementName
)
70 if ( _initialNodeValue
!= null )
71 node
.setNodeValue( _initialNodeValue
);
75 /** removes a child of the root-level node from the instance
78 * the name of the to-be-removed child
80 public XNode
removeNode( String _elementName
) throws DOMException
82 return removeNode( m_domInstance
, _elementName
);
85 /** removes a node from the instance
87 * @param _parentElement
88 * the node whose child is to be removed
90 * the name of the to-be-removed child
92 private XNode
removeNode( XNode _parentElement
, String _elementName
) throws DOMException
94 XNodeList nodes
= _parentElement
.getChildNodes();
95 for ( int i
=0; i
<nodes
.getLength(); ++i
)
97 XNode node
= nodes
.item(i
);
98 if ( node
.getLocalName().equals( _elementName
) )
100 _parentElement
.removeChild( node
);
104 throw new NoSuchElementException();
111 /** creates an attribute for the given node
113 * @param _parentElement
114 * the element at which the attribute should be created
116 * the name of the to-be-created attribute
118 * the DOM node, which has already been inserted into the DOM tree
120 public XNode
createAttribute( XNode _parentElement
, String _attribName
) throws DOMException
122 return createAttribute( _parentElement
, _attribName
, null );
125 /** creates an attribute for the given node
127 * @param _parentElement
128 * the element at which the attribute should be created
130 * the name of the to-be-created attribute
131 * @param _initialNodeValue
132 * the initial value to set at the node. Might be null, in this case no value is set.
134 * the DOM node, which has already been inserted into the DOM tree
136 public XNode
createAttribute( XNode _parentElement
, String _attribName
, String _initialNodeValue
) throws DOMException
138 XNode node
= _parentElement
.appendChild(
139 m_model
.getUIHelper().createAttribute( _parentElement
, _attribName
)
141 if ( _initialNodeValue
!= null )
142 node
.setNodeValue( _initialNodeValue
);