1 package JSci.mathml; 2 3 import org.w3c.dom.*; 4 import org.w3c.dom.mathml.*; 5 6 11 public class MathMLContentContainerImpl extends MathMLElementImpl implements MathMLContentContainer { 12 15 public MathMLContentContainerImpl(MathMLDocumentImpl owner, String qualifiedName) { 16 super (owner, qualifiedName); 17 } 18 19 public MathMLConditionElement getCondition() { 20 return (MathMLConditionElement) getNodeByName("condition"); 21 } 22 public void setCondition(MathMLConditionElement condition) throws DOMException { 23 setNodeByName(condition, "condition"); 24 } 25 26 public MathMLElement getOpDegree() { 27 return (MathMLElement) getNodeByName("degree"); 28 } 29 public void setOpDegree(MathMLElement opDegree) throws DOMException { 30 setNodeByName(opDegree, "degree"); 31 } 32 33 public MathMLElement getDomainOfApplication() { 34 return (MathMLElement) getNodeByName("domainofapplication"); 35 } 36 public void setDomainOfApplication(MathMLElement domainOfApplication) throws DOMException { 37 setNodeByName(domainOfApplication, "domainofapplication"); 38 } 39 40 public MathMLElement getMomentAbout() { 41 return (MathMLElement) getNodeByName("momentabout"); 42 } 43 public void setMomentAbout(MathMLElement momentAbout) throws DOMException { 44 setNodeByName(momentAbout, "momentabout"); 45 } 46 47 public int getNBoundVariables() { 48 return getBoundVariablesGetLength(); 49 } 50 public MathMLBvarElement getBoundVariable(int index) { 51 Node bvar = getBoundVariablesItem(index-1); 52 53 return (MathMLBvarElement) bvar; 54 } 55 public MathMLBvarElement setBoundVariable(MathMLBvarElement newBvar, int index) throws DOMException { 56 final int bvarsLength = getBoundVariablesGetLength(); 57 58 return (MathMLBvarElement) replaceChild(newBvar, getBoundVariablesItem(index-1)); 59 } 60 public MathMLBvarElement insertBoundVariable(MathMLBvarElement newBvar, int index) throws DOMException { 61 final int bvarsLength = getBoundVariablesGetLength(); 62 63 if (index == 0) { 64 return (MathMLBvarElement) appendChild(newBvar); 65 } else { 66 return (MathMLBvarElement) insertBefore(newBvar, getBoundVariablesItem(index-1)); 67 } 68 } 69 public MathMLBvarElement removeBoundVariable(int index) { 70 Node bvar = getBoundVariablesItem(index-1); 71 72 return (MathMLBvarElement) removeChild(bvar); 73 } 74 public void deleteBoundVariable(int index) { 75 removeBoundVariable(index); 76 } 77 78 public int getNArguments() { 79 return getArgumentsGetLength(); 80 } 81 public MathMLNodeList getArguments() { 82 return new MathMLNodeList() { 83 public int getLength() { 84 return getArgumentsGetLength(); 85 } 86 public Node item(int index) { 87 return getArgumentsItem(index); 88 } 89 }; 90 } 91 public MathMLNodeList getDeclarations() { 92 return new MathMLNodeList() { 93 public int getLength() { 94 return getDeclarationsGetLength(); 95 } 96 public Node item(int index) { 97 return getDeclarationsItem(index); 98 } 99 }; 100 } 101 102 public MathMLElement getArgument(int index) throws DOMException { 103 Node arg = getArgumentsItem(index-1); 104 if (arg == null) { 105 throw new DOMException(DOMException.INDEX_SIZE_ERR, "Index out of bounds"); 106 } 107 return (MathMLElement) arg; 108 } 109 public MathMLElement setArgument(MathMLElement newArgument, int index) throws DOMException { 110 final int argsLength = getArgumentsGetLength(); 111 112 if ((index < 1) || (index > argsLength+1)) { 113 throw new DOMException(DOMException.INDEX_SIZE_ERR, "Index out of bounds"); 114 } 115 if (index == argsLength+1) { 116 return (MathMLElement) appendChild(newArgument); 117 } else { 118 return (MathMLElement) replaceChild(newArgument, getArgumentsItem(index-1)); 119 } 120 } 121 public MathMLElement insertArgument(MathMLElement newArgument, int index) throws DOMException { 122 final int argsLength = getArgumentsGetLength(); 123 124 if ((index < 0) || (index > argsLength+1)) { 125 throw new DOMException(DOMException.INDEX_SIZE_ERR, "Index out of bounds"); 126 } 127 if ((index == 0) || (index == argsLength+1)) { 128 return (MathMLElement) appendChild(newArgument); 129 } else { 130 return (MathMLElement) insertBefore(newArgument, getArgumentsItem(index-1)); 131 } 132 } 133 public MathMLElement removeArgument(int index) throws DOMException { 134 Node arg = getArgumentsItem(index-1); 135 if (arg == null) { 136 throw new DOMException(DOMException.INDEX_SIZE_ERR, "Index out of bounds"); 137 } 138 return (MathMLElement) removeChild(arg); 139 } 140 public void deleteArgument(int index) throws DOMException { 141 removeArgument(index); 142 } 143 144 public MathMLDeclareElement getDeclaration(int index) throws DOMException { 145 Node decl = getDeclarationsItem(index-1); 146 if (decl == null) { 147 throw new DOMException(DOMException.INDEX_SIZE_ERR, "Index out of bounds"); 148 } 149 return (MathMLDeclareElement) decl; 150 } 151 public MathMLDeclareElement setDeclaration(MathMLDeclareElement newDeclaration, int index) throws DOMException { 152 final int declsLength = getDeclarationsGetLength(); 153 154 if ((index < 1) || (index > declsLength+1)) { 155 throw new DOMException(DOMException.INDEX_SIZE_ERR, "Index out of bounds"); 156 } 157 if (index == declsLength+1) { 158 return (MathMLDeclareElement) appendChild(newDeclaration); 159 } else { 160 return (MathMLDeclareElement) replaceChild(newDeclaration, getDeclarationsItem(index-1)); 161 } 162 } 163 public MathMLDeclareElement insertDeclaration(MathMLDeclareElement newDeclaration, int index) throws DOMException { 164 final int declsLength = getDeclarationsGetLength(); 165 166 if ((index < 0) || (index > declsLength+1)) { 167 throw new DOMException(DOMException.INDEX_SIZE_ERR, "Index out of bounds"); 168 } 169 if ((index == 0) || (index == declsLength+1)) { 170 return (MathMLDeclareElement) appendChild(newDeclaration); 171 } else { 172 return (MathMLDeclareElement) insertBefore(newDeclaration, getDeclarationsItem(index-1)); 173 } 174 } 175 public MathMLDeclareElement removeDeclaration(int index) throws DOMException { 176 Node decl = getDeclarationsItem(index-1); 177 if (decl == null) { 178 throw new DOMException(DOMException.INDEX_SIZE_ERR, "Index out of bounds"); 179 } 180 return (MathMLDeclareElement) removeChild(decl); 181 } 182 public void deleteDeclaration(int index) throws DOMException { 183 removeDeclaration(index); 184 } 185 186 private int getBoundVariablesGetLength() { 187 final int length = getLength(); 188 int numBvars = 0; 189 190 for (int i = 0; i < length; i++) { 191 if (item(i) instanceof MathMLBvarElement) { 192 numBvars++; 193 } 194 } 195 return numBvars; 196 } 197 private Node getBoundVariablesItem(int index) { 198 final int bvarsLength = getBoundVariablesGetLength(); 199 200 if ((index < 0) || (index >= bvarsLength)) 201 return null; 202 203 Node node = null; 204 int n = -1; 205 for (int i = 0; n < index; i++) { 206 node = item(i); 207 if (node instanceof MathMLBvarElement) { 208 n++; 209 } 210 } 211 return node; 212 } 213 214 private int getArgumentsGetLength() { 215 final int length = getLength(); 216 int numArgs = 0; 217 218 for (int i = 0; i < length; i++) { 219 if (!(item(i) instanceof MathMLDeclareElement)) { 220 numArgs++; 221 } 222 } 223 return numArgs; 224 } 225 private Node getArgumentsItem(int index) { 226 final int argsLength = getArgumentsGetLength(); 227 228 if ((index < 0) || (index >= argsLength)) 229 return null; 230 231 Node node = null; 232 int n = -1; 233 for (int i = 0; n < index; i++) { 234 node = item(i); 235 if (!(node instanceof MathMLDeclareElement)) { 236 n++; 237 } 238 } 239 return node; 240 } 241 242 private int getDeclarationsGetLength() { 243 final int length = getLength(); 244 int numDecls = 0; 245 246 for (int i = 0; i < length; i++) { 247 if (item(i) instanceof MathMLDeclareElement) { 248 numDecls++; 249 } 250 } 251 return numDecls; 252 } 253 private Node getDeclarationsItem(int index) { 254 final int declLength = getDeclarationsGetLength(); 255 256 if ((index < 0) || (index >= declLength)) 257 return null; 258 259 Node node = null; 260 int n = -1; 261 for (int i = 0; n < index; i++) { 262 node = item(i); 263 if (node instanceof MathMLDeclareElement) { 264 n++; 265 } 266 } 267 return node; 268 } 269 270 protected Node getNodeByName(String name) { 271 final int length = getLength(); 272 Node node; 273 274 for (int i = 0; i < length; i++) { 275 node = item(i); 276 String localName = node.getLocalName(); 277 if (localName != null && localName.equals(name)) { 278 return node; 279 } 280 } 281 return null; 282 } 283 protected void setNodeByName(Node newNode, String name) { 284 final int length = getLength(); 285 Node node; 286 287 for (int i = 0; i < length; i++) { 288 node = item(i); 289 String localName = node.getLocalName(); 290 if (localName != null && localName.equals(name)) { 291 replaceChild(newNode, node); 292 return; 293 } 294 } 295 } 296 } 297 298 | Popular Tags |