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>vector</code> element.
009: * @version 1.0
010: * @author Mark Hale
011: */
012: public class MathMLVectorElementImpl extends MathMLElementImpl
013: implements MathMLVectorElement {
014: /**
015: * Constructs a MathML <code>vector</code> element.
016: */
017: public MathMLVectorElementImpl(MathMLDocumentImpl owner,
018: String qualifiedName) {
019: super (owner, qualifiedName);
020: }
021:
022: public int getNcomponents() {
023: return getComponentsGetLength();
024: }
025:
026: public MathMLContentElement getComponent(int index)
027: throws DOMException {
028: Node component = getComponentsItem(index - 1);
029: if (component == null) {
030: throw new DOMException(DOMException.INDEX_SIZE_ERR,
031: "Index out of bounds");
032: }
033: return (MathMLContentElement) component;
034: }
035:
036: public MathMLContentElement setComponent(
037: MathMLContentElement newComponent, int index)
038: throws DOMException {
039: final int componentsLength = getComponentsGetLength();
040:
041: if ((index < 1) || (index > componentsLength + 1)) {
042: throw new DOMException(DOMException.INDEX_SIZE_ERR,
043: "Index out of bounds");
044: }
045: if (index == componentsLength + 1) {
046: return (MathMLContentElement) appendChild(newComponent);
047: } else {
048: return (MathMLContentElement) replaceChild(newComponent,
049: getComponentsItem(index - 1));
050: }
051: }
052:
053: public MathMLContentElement insertComponent(
054: MathMLContentElement newComponent, int index)
055: throws DOMException {
056: final int componentsLength = getComponentsGetLength();
057:
058: if ((index < 0) || (index > componentsLength + 1)) {
059: throw new DOMException(DOMException.INDEX_SIZE_ERR,
060: "Index out of bounds");
061: }
062: if ((index == 0) || (index == componentsLength + 1)) {
063: return (MathMLContentElement) appendChild(newComponent);
064: } else {
065: return (MathMLContentElement) insertBefore(newComponent,
066: getComponentsItem(index - 1));
067: }
068: }
069:
070: public MathMLContentElement removeComponent(int index)
071: throws DOMException {
072: Node component = getComponentsItem(index - 1);
073: if (component == null) {
074: throw new DOMException(DOMException.INDEX_SIZE_ERR,
075: "Index out of bounds");
076: }
077: return (MathMLContentElement) removeChild(component);
078: }
079:
080: public void deleteComponent(int index) throws DOMException {
081: removeComponent(index);
082: }
083:
084: private int getComponentsGetLength() {
085: final int length = getLength();
086: int numComponents = 0;
087:
088: for (int i = 0; i < length; i++) {
089: if (item(i) instanceof MathMLContentElement) {
090: numComponents++;
091: }
092: }
093: return numComponents;
094: }
095:
096: private Node getComponentsItem(int index) {
097: final int componentsLength = getComponentsGetLength();
098:
099: if ((index < 0) || (index >= componentsLength))
100: return null;
101:
102: Node node = null;
103: int n = -1;
104: for (int i = 0; n < index; i++) {
105: node = item(i);
106: if (node instanceof MathMLContentElement) {
107: n++;
108: }
109: }
110: return node;
111: }
112: }
|