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