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,v bc1d5fde7b73 2009/06/01 14:40:54 maxberger $ */
018    
019    package net.sourceforge.jeuclid.elements.content.semantic;
020    
021    import net.sourceforge.jeuclid.elements.presentation.AbstractContainer;
022    
023    import org.apache.batik.dom.AbstractDocument;
024    import org.w3c.dom.Node;
025    import org.w3c.dom.mathml.MathMLElement;
026    import org.w3c.dom.mathml.MathMLSemanticsElement;
027    
028    /**
029     * This class represents a semantics element.
030     * 
031     * @version $Revision: bc1d5fde7b73 $
032     */
033    public final class Semantics extends AbstractContainer implements
034            MathMLSemanticsElement {
035    
036        /**
037         * The XML element from this class.
038         */
039        public static final String ELEMENT = "semantics";
040    
041        private static final long serialVersionUID = 1L;
042    
043        /**
044         * Default constructor. Sets MathML Namespace.
045         * 
046         * @param qname
047         *            Qualified name.
048         * @param odoc
049         *            Owner Document.
050         */
051        public Semantics(final String qname, final AbstractDocument odoc) {
052            super(qname, odoc);
053        }
054    
055        /** {@inheritDoc} */
056        @Override
057        protected Node newNode() {
058            return new Semantics(this.nodeName, this.ownerDocument);
059        }
060    
061        /** {@inheritDoc} */
062        public void deleteAnnotation(final int index) {
063            this.removeAnnotation(index);
064        }
065    
066        /** {@inheritDoc} */
067        public MathMLElement getAnnotation(final int index) {
068            // Index is 1-based!
069            return (MathMLElement) this.getChildNodes().item(index);
070        }
071    
072        /** {@inheritDoc} */
073        public MathMLElement getBody() {
074            return (MathMLElement) this.getFirstChild();
075        }
076    
077        /** {@inheritDoc} */
078        public int getNAnnotations() {
079            return Math.max(0, this.getChildNodes().getLength() - 1);
080        }
081    
082        /** {@inheritDoc} */
083        public MathMLElement insertAnnotation(final MathMLElement newAnnotation,
084                final int index) {
085            if (index == 0) {
086                if (this.getNAnnotations() == 0) {
087                    this.setAnnotation(newAnnotation, 1);
088                } else {
089                    this.addMathElement(newAnnotation);
090                }
091            } else {
092                final MathMLElement oldChild = this.getAnnotation(index);
093                if (oldChild == null) {
094                    this.setAnnotation(newAnnotation, index);
095                } else {
096                    this.insertBefore(newAnnotation, oldChild);
097                }
098            }
099            return newAnnotation;
100        }
101    
102        /** {@inheritDoc} */
103        public MathMLElement removeAnnotation(final int index) {
104            final MathMLElement oldChild = this.getAnnotation(index);
105            return (MathMLElement) this.removeChild(oldChild);
106        }
107    
108        /** {@inheritDoc} */
109        public MathMLElement setAnnotation(final MathMLElement newAnnotation,
110                final int index) {
111            // Index is 1-based!
112            this.setMathElement(index, newAnnotation);
113            return newAnnotation;
114        }
115    
116        /** {@inheritDoc} */
117        public void setBody(final MathMLElement body) {
118            this.setMathElement(0, body);
119        }
120    
121    }