KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > mathml > MathMLPresentationContainerImpl


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

11 public class MathMLPresentationContainerImpl extends MathMLElementImpl implements MathMLPresentationContainer {
12         /**
13          * Constructs a MathML presentation container.
14          */

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