001    /*
002     * Copyright 2002 - 2007 JEuclid, http://jeuclid.sf.net
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    /* $Id: Semantics.java 310 2007-05-18 20:26:36Z maxberger $ */
018    
019    package net.sourceforge.jeuclid.content.semantic;
020    
021    import net.sourceforge.jeuclid.MathBase;
022    import net.sourceforge.jeuclid.elements.presentation.general.AbstractRowLike;
023    
024    import org.w3c.dom.mathml.MathMLElement;
025    import org.w3c.dom.mathml.MathMLSemanticsElement;
026    
027    /**
028     * This class represents a semantics element.
029     * 
030     * @author Max Berger
031     * @version $Revision: 310 $
032     */
033    public class Semantics extends AbstractRowLike implements
034            MathMLSemanticsElement {
035    
036        /**
037         * The XML element from this class.
038         */
039        public static final String ELEMENT = "semantics";
040    
041        /**
042         * Creates a math element.
043         * 
044         * @param base
045         *            The base for the math element tree.
046         */
047        public Semantics(final MathBase base) {
048            super(base);
049        }
050    
051        /** {@inheritDoc} */
052        public String getTagName() {
053            return Semantics.ELEMENT;
054        }
055    
056        /** {@inheritDoc} */
057        public void deleteAnnotation(final int index) {
058            this.removeAnnotation(index);
059        }
060    
061        /** {@inheritDoc} */
062        public MathMLElement getAnnotation(final int index) {
063            // Index is 1-based!
064            return (MathMLElement) this.getChildNodes().item(index);
065        }
066    
067        /** {@inheritDoc} */
068        public MathMLElement getBody() {
069            return (MathMLElement) this.getFirstChild();
070        }
071    
072        /** {@inheritDoc} */
073        public int getNAnnotations() {
074            return Math.max(0, this.getChildNodes().getLength() - 1);
075        }
076    
077        /** {@inheritDoc} */
078        public MathMLElement insertAnnotation(final MathMLElement newAnnotation,
079                final int index) {
080            if (index == 0) {
081                if (this.getNAnnotations() == 0) {
082                    this.setAnnotation(newAnnotation, 1);
083                } else {
084                    this.addMathElement(newAnnotation);
085                }
086            } else {
087                final MathMLElement oldChild = this.getAnnotation(index);
088                if (oldChild != null) {
089                    this.insertBefore(newAnnotation, oldChild);
090                } else {
091                    this.setAnnotation(newAnnotation, index);
092                }
093            }
094            return newAnnotation;
095        }
096    
097        /** {@inheritDoc} */
098        public MathMLElement removeAnnotation(final int index) {
099            final MathMLElement oldChild = this.getAnnotation(index);
100            return (MathMLElement) this.removeChild(oldChild);
101        }
102    
103        /** {@inheritDoc} */
104        public MathMLElement setAnnotation(final MathMLElement newAnnotation,
105                final int index) {
106            // Index is 1-based!
107            this.setMathElement(index, newAnnotation);
108            return newAnnotation;
109        }
110    
111        /** {@inheritDoc} */
112        public void setBody(final MathMLElement body) {
113            this.setMathElement(0, body);
114        }
115    
116    }