1b9b79128SAndrew Rist /**************************************************************
2b9b79128SAndrew Rist  *
3b9b79128SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4b9b79128SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5b9b79128SAndrew Rist  * distributed with this work for additional information
6b9b79128SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7b9b79128SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8b9b79128SAndrew Rist  * "License"); you may not use this file except in compliance
9b9b79128SAndrew Rist  * with the License.  You may obtain a copy of the License at
10b9b79128SAndrew Rist  *
11b9b79128SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12b9b79128SAndrew Rist  *
13b9b79128SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14b9b79128SAndrew Rist  * software distributed under the License is distributed on an
15b9b79128SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16b9b79128SAndrew Rist  * KIND, either express or implied.  See the License for the
17b9b79128SAndrew Rist  * specific language governing permissions and limitations
18b9b79128SAndrew Rist  * under the License.
19b9b79128SAndrew Rist  *
20b9b79128SAndrew Rist  *************************************************************/
21b9b79128SAndrew Rist 
22cdf0e10cSrcweir /*
23cdf0e10cSrcweir  * To change this template, choose Tools | Templates
24cdf0e10cSrcweir  * and open the template in the editor.
25cdf0e10cSrcweir  */
26cdf0e10cSrcweir 
27*cf2d78d1SDamjan Jovanovic package integration.xforms;
28cdf0e10cSrcweir 
29cdf0e10cSrcweir import com.sun.star.xml.dom.DOMException;
30cdf0e10cSrcweir import com.sun.star.xml.dom.XDocument;
31cdf0e10cSrcweir import com.sun.star.xml.dom.XNode;
32cdf0e10cSrcweir import com.sun.star.xml.dom.XNodeList;
33cdf0e10cSrcweir import java.util.NoSuchElementException;
34cdf0e10cSrcweir 
35cdf0e10cSrcweir /**
36cdf0e10cSrcweir  *
37cdf0e10cSrcweir  * @author fs93730
38cdf0e10cSrcweir  */
39cdf0e10cSrcweir public class Instance
40cdf0e10cSrcweir {
41cdf0e10cSrcweir     private Model           m_model;
42cdf0e10cSrcweir     private XDocument       m_domInstance;
43cdf0e10cSrcweir 
Instance( Model _model, XDocument _domInstance )44cdf0e10cSrcweir     protected Instance( Model _model, XDocument _domInstance )
45cdf0e10cSrcweir     {
46cdf0e10cSrcweir         m_model = _model;
47cdf0e10cSrcweir         m_domInstance = _domInstance;
48cdf0e10cSrcweir     }
49cdf0e10cSrcweir 
50cdf0e10cSrcweir     /** creates a new element in the instance
51cdf0e10cSrcweir      *
52cdf0e10cSrcweir      * The element will be inserted immediately below the root node of the instance.
53cdf0e10cSrcweir      *
54cdf0e10cSrcweir      * @param _elementName
55cdf0e10cSrcweir      *      the name of the to-be-created element
56cdf0e10cSrcweir      * @return
57cdf0e10cSrcweir      *      the node of the newly created element
58cdf0e10cSrcweir      * @throws com.sun.star.xml.dom.DOMException
59cdf0e10cSrcweir      */
createElement( String _elementName )60cdf0e10cSrcweir     public XNode createElement( String _elementName ) throws DOMException
61cdf0e10cSrcweir     {
62cdf0e10cSrcweir         return createElement( m_domInstance, _elementName, null );
63cdf0e10cSrcweir     }
64cdf0e10cSrcweir 
65cdf0e10cSrcweir     /** creates a new element in the instance
66cdf0e10cSrcweir      *
67cdf0e10cSrcweir      * The element will be inserted immediately below a given XNode.
68cdf0e10cSrcweir      *
69cdf0e10cSrcweir      * @param _parentElement
70cdf0e10cSrcweir      *      the node whose child shall be created
71cdf0e10cSrcweir      * @param _elementName
72cdf0e10cSrcweir      *      the name of the to-be-created element
73cdf0e10cSrcweir      * @return
74cdf0e10cSrcweir      *      the node of the newly created element
75cdf0e10cSrcweir      * @throws com.sun.star.xml.dom.DOMException
76cdf0e10cSrcweir      */
createElement( XNode _parentElement, String _elementName )77cdf0e10cSrcweir     public XNode createElement( XNode _parentElement, String _elementName ) throws DOMException
78cdf0e10cSrcweir     {
79cdf0e10cSrcweir         return createElement( _parentElement, _elementName, null );
80cdf0e10cSrcweir     }
81cdf0e10cSrcweir 
82cdf0e10cSrcweir     /** creates a new element in the instance
83cdf0e10cSrcweir      *
84cdf0e10cSrcweir      * The element will be inserted immediately below a given XNode.
85cdf0e10cSrcweir      *
86cdf0e10cSrcweir      * @param _parentElement
87cdf0e10cSrcweir      *      the node whose child shall be created
88cdf0e10cSrcweir      * @param _elementName
89cdf0e10cSrcweir      *      the name of the to-be-created element
90cdf0e10cSrcweir      * @param _initialNodeValue
91cdf0e10cSrcweir      *      the initial value to set at the node. Might be null, in this case no value is set.
92cdf0e10cSrcweir      * @return
93cdf0e10cSrcweir      *      the node of the newly created element
94cdf0e10cSrcweir      * @throws com.sun.star.xml.dom.DOMException
95cdf0e10cSrcweir      */
createElement( XNode _parentElement, String _elementName, String _initialNodeValue )96cdf0e10cSrcweir     public XNode createElement( XNode _parentElement, String _elementName, String _initialNodeValue ) throws DOMException
97cdf0e10cSrcweir     {
98cdf0e10cSrcweir         XNode node = _parentElement.appendChild(
99cdf0e10cSrcweir             m_model.getUIHelper().createElement( _parentElement, _elementName )
100cdf0e10cSrcweir         );
101cdf0e10cSrcweir         if ( _initialNodeValue != null )
102cdf0e10cSrcweir             node.setNodeValue( _initialNodeValue );
103cdf0e10cSrcweir         return node;
104cdf0e10cSrcweir     }
105cdf0e10cSrcweir 
106cdf0e10cSrcweir     /** removes a child of the root-level node from the instance
107cdf0e10cSrcweir      *
108cdf0e10cSrcweir      * @param _elementName
109cdf0e10cSrcweir      *  the name of the to-be-removed child
110cdf0e10cSrcweir      */
removeNode( String _elementName )111cdf0e10cSrcweir     public XNode removeNode( String _elementName ) throws DOMException
112cdf0e10cSrcweir     {
113cdf0e10cSrcweir         return removeNode( m_domInstance, _elementName );
114cdf0e10cSrcweir     }
115cdf0e10cSrcweir 
116cdf0e10cSrcweir     /** removes a node from the instance
117cdf0e10cSrcweir      *
118cdf0e10cSrcweir      * @param _parentElement
119cdf0e10cSrcweir      *  the node whose child is to be removed
120cdf0e10cSrcweir      * @param _elementName
121cdf0e10cSrcweir      *  the name of the to-be-removed child
122cdf0e10cSrcweir      */
removeNode( XNode _parentElement, String _elementName )123cdf0e10cSrcweir     public XNode removeNode( XNode _parentElement, String _elementName ) throws DOMException
124cdf0e10cSrcweir     {
125cdf0e10cSrcweir         XNodeList nodes = _parentElement.getChildNodes();
126cdf0e10cSrcweir         for ( int i=0; i<nodes.getLength(); ++i )
127cdf0e10cSrcweir         {
128cdf0e10cSrcweir             XNode node = nodes.item(i);
129cdf0e10cSrcweir             if ( node.getLocalName().equals( _elementName ) )
130cdf0e10cSrcweir             {
131cdf0e10cSrcweir                 _parentElement.removeChild( node );
132cdf0e10cSrcweir                 return node;
133cdf0e10cSrcweir             }
134cdf0e10cSrcweir         }
135cdf0e10cSrcweir         throw new NoSuchElementException();
136cdf0e10cSrcweir     }
137cdf0e10cSrcweir 
138cdf0e10cSrcweir     /** creates an attribute for the root node of the instance
139cdf0e10cSrcweir      *
140cdf0e10cSrcweir      * @param _attribName
141cdf0e10cSrcweir      *      the name of the to-be-created attribute
142cdf0e10cSrcweir      * @return
143cdf0e10cSrcweir      *      the DOM node, which has already been inserted into the DOM tree
144cdf0e10cSrcweir      * @throws com.sun.star.xml.dom.DOMException
145cdf0e10cSrcweir      */
createAttribute( String _attribName )146cdf0e10cSrcweir     public XNode createAttribute( String _attribName ) throws DOMException
147cdf0e10cSrcweir     {
148cdf0e10cSrcweir         return createAttribute( m_domInstance, _attribName, null );
149cdf0e10cSrcweir     }
150cdf0e10cSrcweir 
151cdf0e10cSrcweir     /** creates an attribute for the root node of the instance
152cdf0e10cSrcweir      *
153cdf0e10cSrcweir      * @param _attribName
154cdf0e10cSrcweir      *      the name of the to-be-created attribute
155cdf0e10cSrcweir      * @param _initialNodeValue
156cdf0e10cSrcweir      *      the initial value to set at the node. Might be null, in this case no value is set.
157cdf0e10cSrcweir      * @return
158cdf0e10cSrcweir      *      the DOM node, which has already been inserted into the DOM tree
159cdf0e10cSrcweir      * @throws com.sun.star.xml.dom.DOMException
160cdf0e10cSrcweir      */
createAttribute( String _attribName, String _initialNodeValue )161cdf0e10cSrcweir     public XNode createAttribute( String _attribName, String _initialNodeValue ) throws DOMException
162cdf0e10cSrcweir     {
163cdf0e10cSrcweir         return createAttribute( m_domInstance, _attribName, _initialNodeValue );
164cdf0e10cSrcweir     }
165cdf0e10cSrcweir 
166cdf0e10cSrcweir     /** creates an attribute for the given node
167cdf0e10cSrcweir      *
168cdf0e10cSrcweir      * @param _parentElement
169cdf0e10cSrcweir      *      the element at which the attribute should be created
170cdf0e10cSrcweir      * @param _attribName
171cdf0e10cSrcweir      *      the name of the to-be-created attribute
172cdf0e10cSrcweir      * @return
173cdf0e10cSrcweir      *      the DOM node, which has already been inserted into the DOM tree
174cdf0e10cSrcweir      * @throws com.sun.star.xml.dom.DOMException
175cdf0e10cSrcweir      */
createAttribute( XNode _parentElement, String _attribName )176cdf0e10cSrcweir     public XNode createAttribute( XNode _parentElement, String _attribName ) throws DOMException
177cdf0e10cSrcweir     {
178cdf0e10cSrcweir         return createAttribute( _parentElement, _attribName, null );
179cdf0e10cSrcweir     }
180cdf0e10cSrcweir 
181cdf0e10cSrcweir     /** creates an attribute for the given node
182cdf0e10cSrcweir      *
183cdf0e10cSrcweir      * @param _parentElement
184cdf0e10cSrcweir      *      the element at which the attribute should be created
185cdf0e10cSrcweir      * @param _attribName
186cdf0e10cSrcweir      *      the name of the to-be-created attribute
187cdf0e10cSrcweir      * @param _initialNodeValue
188cdf0e10cSrcweir      *      the initial value to set at the node. Might be null, in this case no value is set.
189cdf0e10cSrcweir      * @return
190cdf0e10cSrcweir      *      the DOM node, which has already been inserted into the DOM tree
191cdf0e10cSrcweir      * @throws com.sun.star.xml.dom.DOMException
192cdf0e10cSrcweir      */
createAttribute( XNode _parentElement, String _attribName, String _initialNodeValue )193cdf0e10cSrcweir     public XNode createAttribute( XNode _parentElement, String _attribName, String _initialNodeValue ) throws DOMException
194cdf0e10cSrcweir     {
195cdf0e10cSrcweir         XNode node = _parentElement.appendChild(
196cdf0e10cSrcweir             m_model.getUIHelper().createAttribute( _parentElement, _attribName )
197cdf0e10cSrcweir         );
198cdf0e10cSrcweir         if ( _initialNodeValue != null )
199cdf0e10cSrcweir             node.setNodeValue( _initialNodeValue );
200cdf0e10cSrcweir         return node;
201cdf0e10cSrcweir     }
202cdf0e10cSrcweir }
203