001: package JSci.mathml;
002:
003: import org.w3c.dom.*;
004: import org.w3c.dom.mathml.*;
005: import org.apache.xerces.dom.*;
006:
007: /**
008: * Implements a MathML <code>matrix</code> element.
009: * @version 1.0
010: * @author Mark Hale
011: */
012: public class MathMLMatrixElementImpl extends MathMLElementImpl
013: implements MathMLMatrixElement {
014: /**
015: * Constructs a MathML <code>matrix</code> element.
016: */
017: public MathMLMatrixElementImpl(MathMLDocumentImpl owner,
018: String qualifiedName) {
019: super (owner, qualifiedName);
020: }
021:
022: public int getNrows() {
023: return getRowsGetLength();
024: }
025:
026: public int getNcols() {
027: return getRow(1).getNEntries();
028: }
029:
030: public MathMLNodeList getRows() {
031: return new MathMLNodeList() {
032: public int getLength() {
033: return getRowsGetLength();
034: }
035:
036: public Node item(int index) {
037: return getRowsItem(index);
038: }
039: };
040: }
041:
042: public MathMLMatrixrowElement getRow(int index) throws DOMException {
043: Node row = getRowsItem(index - 1);
044: if (row == null) {
045: throw new DOMException(DOMException.INDEX_SIZE_ERR,
046: "Index out of bounds");
047: }
048: return (MathMLMatrixrowElement) row;
049: }
050:
051: public MathMLMatrixrowElement setRow(MathMLMatrixrowElement newRow,
052: int index) throws DOMException {
053: final int rowsLength = getRowsGetLength();
054:
055: if ((index < 1) || (index > rowsLength + 1)) {
056: throw new DOMException(DOMException.INDEX_SIZE_ERR,
057: "Index out of bounds");
058: }
059: if (index == rowsLength + 1) {
060: return (MathMLMatrixrowElement) appendChild(newRow);
061: } else {
062: return (MathMLMatrixrowElement) replaceChild(newRow,
063: getRowsItem(index - 1));
064: }
065: }
066:
067: public MathMLMatrixrowElement insertRow(
068: MathMLMatrixrowElement newRow, int index)
069: throws DOMException {
070: final int rowsLength = getRowsGetLength();
071:
072: if ((index < 0) || (index > rowsLength + 1)) {
073: throw new DOMException(DOMException.INDEX_SIZE_ERR,
074: "Index out of bounds");
075: }
076: if ((index == 0) || (index == rowsLength + 1)) {
077: return (MathMLMatrixrowElement) appendChild(newRow);
078: } else {
079: return (MathMLMatrixrowElement) insertBefore(newRow,
080: getRowsItem(index - 1));
081: }
082: }
083:
084: public MathMLMatrixrowElement removeRow(int index)
085: throws DOMException {
086: Node row = getRowsItem(index - 1);
087: if (row == null) {
088: throw new DOMException(DOMException.INDEX_SIZE_ERR,
089: "Index out of bounds");
090: }
091: return (MathMLMatrixrowElement) removeChild(row);
092: }
093:
094: public void deleteRow(int index) throws DOMException {
095: removeRow(index);
096: }
097:
098: private int getRowsGetLength() {
099: final int length = getLength();
100: int numRows = 0;
101:
102: for (int i = 0; i < length; i++) {
103: if (item(i) instanceof MathMLMatrixrowElement) {
104: numRows++;
105: }
106: }
107: return numRows;
108: }
109:
110: private Node getRowsItem(int index) {
111: final int rowsLength = getRowsGetLength();
112:
113: if ((index < 0) || (index >= rowsLength))
114: return null;
115:
116: Node node = null;
117: int n = -1;
118: for (int i = 0; n < index; i++) {
119: node = item(i);
120: if (node instanceof MathMLMatrixrowElement) {
121: n++;
122: }
123: }
124: return node;
125: }
126: }
|