KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > mathml > MathMLMatrixElementImpl


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>matrix</code> element.
9  * @version 1.0
10  * @author Mark Hale
11  */

12 public class MathMLMatrixElementImpl extends MathMLElementImpl implements MathMLMatrixElement {
13         /**
14          * Constructs a MathML <code>matrix</code> element.
15          */

16         public MathMLMatrixElementImpl(MathMLDocumentImpl owner, String JavaDoc qualifiedName) {
17                 super(owner, qualifiedName);
18         }
19
20         public int getNrows() {
21                 return getRowsGetLength();
22         }
23         public int getNcols() {
24                 return getRow(1).getNEntries();
25         }
26
27         public MathMLNodeList getRows() {
28                 return new MathMLNodeList() {
29                         public int getLength() {
30                                 return getRowsGetLength();
31                         }
32                         public Node item(int index) {
33                                 return getRowsItem(index);
34                         }
35                 };
36         }
37
38         public MathMLMatrixrowElement getRow(int index) throws DOMException {
39                 Node row = getRowsItem(index-1);
40                 if (row == null) {
41                         throw new DOMException(DOMException.INDEX_SIZE_ERR, "Index out of bounds");
42                 }
43                 return (MathMLMatrixrowElement) row;
44         }
45         public MathMLMatrixrowElement setRow(MathMLMatrixrowElement newRow, int index) throws DOMException {
46                 final int rowsLength = getRowsGetLength();
47
48                 if ((index < 1) || (index > rowsLength+1)) {
49                         throw new DOMException(DOMException.INDEX_SIZE_ERR, "Index out of bounds");
50                 }
51                 if (index == rowsLength+1) {
52                         return (MathMLMatrixrowElement) appendChild(newRow);
53                 } else {
54                         return (MathMLMatrixrowElement) replaceChild(newRow, getRowsItem(index-1));
55                 }
56         }
57         public MathMLMatrixrowElement insertRow(MathMLMatrixrowElement newRow, int index) throws DOMException {
58                 final int rowsLength = getRowsGetLength();
59
60                 if ((index < 0) || (index > rowsLength+1)) {
61                         throw new DOMException(DOMException.INDEX_SIZE_ERR, "Index out of bounds");
62                 }
63                 if ((index == 0) || (index == rowsLength+1)) {
64                         return (MathMLMatrixrowElement) appendChild(newRow);
65                 } else {
66                         return (MathMLMatrixrowElement) insertBefore(newRow, getRowsItem(index-1));
67                 }
68         }
69         public MathMLMatrixrowElement removeRow(int index) throws DOMException {
70                 Node row = getRowsItem(index-1);
71                 if (row == null) {
72                         throw new DOMException(DOMException.INDEX_SIZE_ERR, "Index out of bounds");
73                 }
74                 return (MathMLMatrixrowElement) removeChild(row);
75         }
76         public void deleteRow(int index) throws DOMException {
77                 removeRow(index);
78         }
79
80         private int getRowsGetLength() {
81                 final int length = getLength();
82                 int numRows = 0;
83
84                 for (int i = 0; i < length; i++) {
85                         if (item(i) instanceof MathMLMatrixrowElement) {
86                                 numRows++;
87                         }
88                 }
89                 return numRows;
90         }
91         private Node getRowsItem(int index) {
92                 final int rowsLength = getRowsGetLength();
93
94                 if ((index < 0) || (index >= rowsLength))
95                         return null;
96
97                 Node node = null;
98                 int n = -1;
99                 for (int i = 0; n < index; i++) {
100                         node = item(i);
101                         if (node instanceof MathMLMatrixrowElement) {
102                                 n++;
103                         }
104                 }
105                 return node;
106         }
107 }
108
109
Popular Tags