KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > mathml > MathMLMathElementImpl


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

11 public class MathMLMathElementImpl extends MathMLElementImpl implements MathMLMathElement {
12         /**
13          * Constructs a MathML <code>math</code> element.
14          */

15         public MathMLMathElementImpl(MathMLDocumentImpl owner, String JavaDoc qualifiedName) {
16                 super (owner, qualifiedName);
17         }
18
19         public String JavaDoc getMacros() {
20                 return getAttribute("macros");
21         }
22         public void setMacros(String JavaDoc macros) {
23                 setAttribute("macros", macros);
24         }
25
26         public String JavaDoc getDisplay() {
27                 return getAttribute("display");
28         }
29         public void setDisplay(String JavaDoc display) {
30                 setAttribute("display", display);
31         }
32
33         public int getNArguments() {
34                 return getArgumentsGetLength();
35         }
36         public MathMLNodeList getArguments() {
37                 return new MathMLNodeList() {
38                         public int getLength() {
39                                 return getArgumentsGetLength();
40                         }
41                         public Node item(int index) {
42                                 return getArgumentsItem(index);
43                         }
44                 };
45         }
46         public MathMLNodeList getDeclarations() {
47                 return new MathMLNodeList() {
48                         public int getLength() {
49                                 return getDeclarationsGetLength();
50                         }
51                         public Node item(int index) {
52                                 return getDeclarationsItem(index);
53                         }
54                 };
55         }
56
57         public MathMLElement getArgument(int index) throws DOMException {
58                 Node arg = getArgumentsItem(index-1);
59                 if (arg == null) {
60                         throw new DOMException(DOMException.INDEX_SIZE_ERR, "Index out of bounds");
61                 }
62                 return (MathMLElement) arg;
63         }
64         public MathMLElement setArgument(MathMLElement newArgument, int index) throws DOMException {
65                 final int argsLength = getArgumentsGetLength();
66
67                 if ((index < 1) || (index > argsLength+1)) {
68                         throw new DOMException(DOMException.INDEX_SIZE_ERR, "Index out of bounds");
69                 }
70                 if (index == argsLength+1) {
71                         return (MathMLElement) appendChild(newArgument);
72                 } else {
73                         return (MathMLElement) replaceChild(newArgument, getArgumentsItem(index-1));
74                 }
75         }
76         public MathMLElement insertArgument(MathMLElement newArgument, int index) throws DOMException {
77                 final int argsLength = getArgumentsGetLength();
78
79                 if ((index < 0) || (index > argsLength+1)) {
80                         throw new DOMException(DOMException.INDEX_SIZE_ERR, "Index out of bounds");
81                 }
82                 if ((index == 0) || (index == argsLength+1)) {
83                         return (MathMLElement) appendChild(newArgument);
84                 } else {
85                         return (MathMLElement) insertBefore(newArgument, getArgumentsItem(index-1));
86                 }
87         }
88         public MathMLElement removeArgument(int index) throws DOMException {
89                 Node arg = getArgumentsItem(index-1);
90                 if (arg == null) {
91                         throw new DOMException(DOMException.INDEX_SIZE_ERR, "Index out of bounds");
92                 }
93                 return (MathMLElement) removeChild(arg);
94         }
95         public void deleteArgument(int index) throws DOMException {
96                 removeArgument(index);
97         }
98
99         public MathMLDeclareElement getDeclaration(int index) throws DOMException {
100                 Node decl = getDeclarationsItem(index-1);
101                 if (decl == null) {
102                         throw new DOMException(DOMException.INDEX_SIZE_ERR, "Index out of bounds");
103                 }
104                 return (MathMLDeclareElement) decl;
105         }
106         public MathMLDeclareElement setDeclaration(MathMLDeclareElement newDeclaration, int index) throws DOMException {
107                 final int declsLength = getDeclarationsGetLength();
108
109                 if ((index < 1) || (index > declsLength+1)) {
110                         throw new DOMException(DOMException.INDEX_SIZE_ERR, "Index out of bounds");
111                 }
112                 if (index == declsLength+1) {
113                         return (MathMLDeclareElement) appendChild(newDeclaration);
114                 } else {
115                         return (MathMLDeclareElement) replaceChild(newDeclaration, getDeclarationsItem(index-1));
116                 }
117         }
118         public MathMLDeclareElement insertDeclaration(MathMLDeclareElement newDeclaration, int index) throws DOMException {
119                 final int declsLength = getDeclarationsGetLength();
120
121                 if ((index < 0) || (index > declsLength+1)) {
122                         throw new DOMException(DOMException.INDEX_SIZE_ERR, "Index out of bounds");
123                 }
124                 if ((index == 0) || (index == declsLength+1)) {
125                         return (MathMLDeclareElement) appendChild(newDeclaration);
126                 } else {
127                         return (MathMLDeclareElement) insertBefore(newDeclaration, getDeclarationsItem(index-1));
128                 }
129         }
130         public MathMLDeclareElement removeDeclaration(int index) throws DOMException {
131                 Node decl = getDeclarationsItem(index-1);
132                 if (decl == null) {
133                         throw new DOMException(DOMException.INDEX_SIZE_ERR, "Index out of bounds");
134                 }
135                 return (MathMLDeclareElement) removeChild(decl);
136         }
137         public void deleteDeclaration(int index) throws DOMException {
138                 removeDeclaration(index);
139         }
140
141         private int getArgumentsGetLength() {
142                 final int length = getLength();
143                 int numArgs = 0;
144
145                 for (int i = 0; i < length; i++) {
146                         if (!(item(i) instanceof MathMLDeclareElement)) {
147                                 numArgs++;
148                         }
149                 }
150                 return numArgs;
151         }
152         private Node getArgumentsItem(int index) {
153                 final int argsLength = getArgumentsGetLength();
154
155                 if ((index < 0) || (index >= argsLength))
156                         return null;
157
158                 Node node = null;
159                 int n = -1;
160                 for (int i = 0; n < index; i++) {
161                         node = item(i);
162                         if (!(node instanceof MathMLDeclareElement)) {
163                                 n++;
164                         }
165                 }
166                 return node;
167         }
168
169         private int getDeclarationsGetLength() {
170                 final int length = getLength();
171                 int numDecls = 0;
172
173                 for (int i = 0; i < length; i++) {
174                         if (item(i) instanceof MathMLDeclareElement) {
175                                 numDecls++;
176                         }
177                 }
178                 return numDecls;
179         }
180         private Node getDeclarationsItem(int index) {
181                 final int declLength = getDeclarationsGetLength();
182
183                 if ((index < 0) || (index >= declLength))
184                         return null;
185
186                 Node node = null;
187                 int n = -1;
188                 for (int i = 0; n < index; i++) {
189                         node = item(i);
190                         if (node instanceof MathMLDeclareElement) {
191                                 n++;
192                         }
193                 }
194                 return node;
195         }
196 }
197
198
Popular Tags