001: package JSci.mathml;
002:
003: import org.w3c.dom.*;
004: import org.w3c.dom.mathml.*;
005:
006: /**
007: * Implements a MathML <code>math</code> element.
008: * @version 1.0
009: * @author Mark Hale
010: */
011: public class MathMLMathElementImpl extends MathMLElementImpl implements
012: MathMLMathElement {
013: /**
014: * Constructs a MathML <code>math</code> element.
015: */
016: public MathMLMathElementImpl(MathMLDocumentImpl owner,
017: String qualifiedName) {
018: super (owner, qualifiedName);
019: }
020:
021: public String getMacros() {
022: return getAttribute("macros");
023: }
024:
025: public void setMacros(String macros) {
026: setAttribute("macros", macros);
027: }
028:
029: public String getDisplay() {
030: return getAttribute("display");
031: }
032:
033: public void setDisplay(String display) {
034: setAttribute("display", display);
035: }
036:
037: public int getNArguments() {
038: return getArgumentsGetLength();
039: }
040:
041: public MathMLNodeList getArguments() {
042: return new MathMLNodeList() {
043: public int getLength() {
044: return getArgumentsGetLength();
045: }
046:
047: public Node item(int index) {
048: return getArgumentsItem(index);
049: }
050: };
051: }
052:
053: public MathMLNodeList getDeclarations() {
054: return new MathMLNodeList() {
055: public int getLength() {
056: return getDeclarationsGetLength();
057: }
058:
059: public Node item(int index) {
060: return getDeclarationsItem(index);
061: }
062: };
063: }
064:
065: public MathMLElement getArgument(int index) throws DOMException {
066: Node arg = getArgumentsItem(index - 1);
067: if (arg == null) {
068: throw new DOMException(DOMException.INDEX_SIZE_ERR,
069: "Index out of bounds");
070: }
071: return (MathMLElement) arg;
072: }
073:
074: public MathMLElement setArgument(MathMLElement newArgument,
075: int index) throws DOMException {
076: final int argsLength = getArgumentsGetLength();
077:
078: if ((index < 1) || (index > argsLength + 1)) {
079: throw new DOMException(DOMException.INDEX_SIZE_ERR,
080: "Index out of bounds");
081: }
082: if (index == argsLength + 1) {
083: return (MathMLElement) appendChild(newArgument);
084: } else {
085: return (MathMLElement) replaceChild(newArgument,
086: getArgumentsItem(index - 1));
087: }
088: }
089:
090: public MathMLElement insertArgument(MathMLElement newArgument,
091: int index) throws DOMException {
092: final int argsLength = getArgumentsGetLength();
093:
094: if ((index < 0) || (index > argsLength + 1)) {
095: throw new DOMException(DOMException.INDEX_SIZE_ERR,
096: "Index out of bounds");
097: }
098: if ((index == 0) || (index == argsLength + 1)) {
099: return (MathMLElement) appendChild(newArgument);
100: } else {
101: return (MathMLElement) insertBefore(newArgument,
102: getArgumentsItem(index - 1));
103: }
104: }
105:
106: public MathMLElement removeArgument(int index) throws DOMException {
107: Node arg = getArgumentsItem(index - 1);
108: if (arg == null) {
109: throw new DOMException(DOMException.INDEX_SIZE_ERR,
110: "Index out of bounds");
111: }
112: return (MathMLElement) removeChild(arg);
113: }
114:
115: public void deleteArgument(int index) throws DOMException {
116: removeArgument(index);
117: }
118:
119: public MathMLDeclareElement getDeclaration(int index)
120: throws DOMException {
121: Node decl = getDeclarationsItem(index - 1);
122: if (decl == null) {
123: throw new DOMException(DOMException.INDEX_SIZE_ERR,
124: "Index out of bounds");
125: }
126: return (MathMLDeclareElement) decl;
127: }
128:
129: public MathMLDeclareElement setDeclaration(
130: MathMLDeclareElement newDeclaration, int index)
131: throws DOMException {
132: final int declsLength = getDeclarationsGetLength();
133:
134: if ((index < 1) || (index > declsLength + 1)) {
135: throw new DOMException(DOMException.INDEX_SIZE_ERR,
136: "Index out of bounds");
137: }
138: if (index == declsLength + 1) {
139: return (MathMLDeclareElement) appendChild(newDeclaration);
140: } else {
141: return (MathMLDeclareElement) replaceChild(newDeclaration,
142: getDeclarationsItem(index - 1));
143: }
144: }
145:
146: public MathMLDeclareElement insertDeclaration(
147: MathMLDeclareElement newDeclaration, int index)
148: throws DOMException {
149: final int declsLength = getDeclarationsGetLength();
150:
151: if ((index < 0) || (index > declsLength + 1)) {
152: throw new DOMException(DOMException.INDEX_SIZE_ERR,
153: "Index out of bounds");
154: }
155: if ((index == 0) || (index == declsLength + 1)) {
156: return (MathMLDeclareElement) appendChild(newDeclaration);
157: } else {
158: return (MathMLDeclareElement) insertBefore(newDeclaration,
159: getDeclarationsItem(index - 1));
160: }
161: }
162:
163: public MathMLDeclareElement removeDeclaration(int index)
164: throws DOMException {
165: Node decl = getDeclarationsItem(index - 1);
166: if (decl == null) {
167: throw new DOMException(DOMException.INDEX_SIZE_ERR,
168: "Index out of bounds");
169: }
170: return (MathMLDeclareElement) removeChild(decl);
171: }
172:
173: public void deleteDeclaration(int index) throws DOMException {
174: removeDeclaration(index);
175: }
176:
177: private int getArgumentsGetLength() {
178: final int length = getLength();
179: int numArgs = 0;
180:
181: for (int i = 0; i < length; i++) {
182: if (!(item(i) instanceof MathMLDeclareElement)) {
183: numArgs++;
184: }
185: }
186: return numArgs;
187: }
188:
189: private Node getArgumentsItem(int index) {
190: final int argsLength = getArgumentsGetLength();
191:
192: if ((index < 0) || (index >= argsLength))
193: return null;
194:
195: Node node = null;
196: int n = -1;
197: for (int i = 0; n < index; i++) {
198: node = item(i);
199: if (!(node instanceof MathMLDeclareElement)) {
200: n++;
201: }
202: }
203: return node;
204: }
205:
206: private int getDeclarationsGetLength() {
207: final int length = getLength();
208: int numDecls = 0;
209:
210: for (int i = 0; i < length; i++) {
211: if (item(i) instanceof MathMLDeclareElement) {
212: numDecls++;
213: }
214: }
215: return numDecls;
216: }
217:
218: private Node getDeclarationsItem(int index) {
219: final int declLength = getDeclarationsGetLength();
220:
221: if ((index < 0) || (index >= declLength))
222: return null;
223:
224: Node node = null;
225: int n = -1;
226: for (int i = 0; n < index; i++) {
227: node = item(i);
228: if (node instanceof MathMLDeclareElement) {
229: n++;
230: }
231: }
232: return node;
233: }
234: }
|