KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > mathml > MathMLVectorElementImpl


1 package JSci.mathml;
2
3 import org.w3c.dom.*;
4 import org.w3c.dom.mathml.*;
5 import org.apache.xerces.dom.*;
6
7 /**
8  * Implements a MathML <code>vector</code> element.
9  * @version 1.0
10  * @author Mark Hale
11  */

12 public class MathMLVectorElementImpl extends MathMLElementImpl implements MathMLVectorElement {
13         /**
14          * Constructs a MathML <code>vector</code> element.
15          */

16         public MathMLVectorElementImpl(MathMLDocumentImpl owner, String JavaDoc qualifiedName) {
17                 super(owner, qualifiedName);
18         }
19
20         public int getNcomponents() {
21                 return getComponentsGetLength();
22         }
23
24         public MathMLContentElement getComponent(int index) throws DOMException {
25                 Node component = getComponentsItem(index-1);
26                 if (component == null) {
27                         throw new DOMException(DOMException.INDEX_SIZE_ERR, "Index out of bounds");
28                 }
29                 return (MathMLContentElement) component;
30         }
31         public MathMLContentElement setComponent(MathMLContentElement newComponent, int index) throws DOMException {
32                 final int componentsLength = getComponentsGetLength();
33
34                 if ((index < 1) || (index > componentsLength+1)) {
35                         throw new DOMException(DOMException.INDEX_SIZE_ERR, "Index out of bounds");
36                 }
37                 if (index == componentsLength+1) {
38                         return (MathMLContentElement) appendChild(newComponent);
39                 } else {
40                         return (MathMLContentElement) replaceChild(newComponent, getComponentsItem(index-1));
41                 }
42         }
43         public MathMLContentElement insertComponent(MathMLContentElement newComponent, int index) throws DOMException {
44                 final int componentsLength = getComponentsGetLength();
45
46                 if ((index < 0) || (index > componentsLength+1)) {
47                         throw new DOMException(DOMException.INDEX_SIZE_ERR, "Index out of bounds");
48                 }
49                 if ((index == 0) || (index == componentsLength+1)) {
50                         return (MathMLContentElement) appendChild(newComponent);
51                 } else {
52                         return (MathMLContentElement) insertBefore(newComponent, getComponentsItem(index-1));
53                 }
54         }
55         public MathMLContentElement removeComponent(int index) throws DOMException {
56                 Node component = getComponentsItem(index-1);
57                 if (component == null) {
58                         throw new DOMException(DOMException.INDEX_SIZE_ERR, "Index out of bounds");
59                 }
60                 return (MathMLContentElement) removeChild(component);
61         }
62         public void deleteComponent(int index) throws DOMException {
63                 removeComponent(index);
64         }
65
66         private int getComponentsGetLength() {
67                 final int length = getLength();
68                 int numComponents = 0;
69
70                 for (int i = 0; i < length; i++) {
71                         if (item(i) instanceof MathMLContentElement) {
72                                 numComponents++;
73                         }
74                 }
75                 return numComponents;
76         }
77         private Node getComponentsItem(int index) {
78                 final int componentsLength = getComponentsGetLength();
79
80                 if ((index < 0) || (index >= componentsLength))
81                         return null;
82
83                 Node node = null;
84                 int n = -1;
85                 for (int i = 0; n < index; i++) {
86                         node = item(i);
87                         if (node instanceof MathMLContentElement) {
88                                 n++;
89                         }
90                 }
91                 return node;
92         }
93 }
94
95
Popular Tags