View Javadoc

1   /*
2    * Copyright 2002 - 2007 JEuclid, http://jeuclid.sf.net
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  /* $Id: MathImpl.java,v c169040ef426 2009/10/28 08:55:31 max $ */
18  
19  package net.sourceforge.jeuclid.elements.generic;
20  
21  import net.sourceforge.jeuclid.Constants;
22  import net.sourceforge.jeuclid.LayoutContext;
23  import net.sourceforge.jeuclid.context.Display;
24  import net.sourceforge.jeuclid.context.Parameter;
25  import net.sourceforge.jeuclid.elements.presentation.AbstractContainer;
26  
27  import org.apache.batik.dom.AbstractDocument;
28  import org.w3c.dom.Node;
29  import org.w3c.dom.mathml.MathMLMathElement;
30  
31  /**
32   * The root element for creating a MathElement tree.
33   * 
34   * @version $Revision: c169040ef426 $
35   */
36  public final class MathImpl extends AbstractContainer implements
37          MathMLMathElement {
38  
39      private final class ChildContext implements LayoutContext {
40          private final LayoutContext context;
41  
42          private ChildContext(final LayoutContext myContext) {
43              this.context = myContext;
44          }
45  
46          public Object getParameter(final Parameter which) {
47              Object retVal;
48              if (Parameter.DISPLAY.equals(which)) {
49                  if (MathImpl.DISPLAY_BLOCK.equals(MathImpl.this.getDisplay())) {
50                      retVal = Display.BLOCK;
51                  } else {
52                      retVal = Display.INLINE;
53                  }
54              } else {
55                  retVal = MathImpl.this.applyLocalAttributesToContext(
56                          this.context).getParameter(which);
57              }
58              retVal = this.getParamValueFromJEuclidExt(which, retVal);
59              return retVal;
60          }
61  
62          private Object getParamValueFromJEuclidExt(final Parameter which,
63                  final Object currentValue) {
64              Object retVal = currentValue;
65              final String s0 = MathImpl.this.getAttributeNS(
66                      Constants.NS_JEUCLID_EXT, which.getOptionName());
67              if ((s0 != null) && (s0.length() > 0)) {
68                  retVal = which.fromString(s0);
69              } else {
70                  retVal = this.getDeprecatedParamValuesFromJEuclidExt(which,
71                          retVal);
72              }
73              return retVal;
74          }
75  
76          private Object getDeprecatedParamValuesFromJEuclidExt(
77                  final Parameter which, final Object currentValue) {
78              // old namespace
79              Object retVal = currentValue;
80              final String s = MathImpl.this.getAttributeNS(
81                      Constants.NS_OLD_JEUCLID_EXT, which.getOptionName());
82              if ((s != null) && (s.length() > 0)) {
83                  retVal = which.fromString(s);
84              } else {
85                  // Support deprecated attributes
86                  final String s2 = MathImpl.this.getAttributeNS(
87                          Constants.NS_OLD_JEUCLID_EXT, which.toString());
88                  if ((s2 != null) && (s2.length() > 0)) {
89                      retVal = which.fromString(s2);
90                  }
91              }
92              return retVal;
93          }
94      }
95  
96      /** attribute for display. */
97      public static final String ATTR_DISPLAY = "display";
98  
99      /** attribute for macros. */
100     public static final String ATTR_MACROS = "macros";
101 
102     /**
103      * The XML element from this class.
104      */
105     public static final String ELEMENT = "math";
106 
107     private static final long serialVersionUID = 1L;
108 
109     /** attribute for mode. */
110     private static final String ATTR_MODE = "mode";
111 
112     private static final String DISPLAY_INLINE = "inline";
113 
114     private static final String DISPLAY_BLOCK = "block";
115 
116     // Happens to be display as well.
117     private static final String DEPRECATED_BLOCK_VALUE_FOR_MODE = MathImpl.ATTR_DISPLAY;
118 
119     /**
120      * Default constructor. Sets MathML Namespace.
121      * 
122      * @param qname
123      *            Qualified name.
124      * @param odoc
125      *            Owner Document.
126      */
127     public MathImpl(final String qname, final AbstractDocument odoc) {
128         super(qname, odoc);
129     }
130 
131     /** {@inheritDoc} */
132     @Override
133     protected Node newNode() {
134         return new MathImpl(this.nodeName, this.ownerDocument);
135     }
136 
137     /**
138      * Set the type of equation.
139      * 
140      * @param display
141      *            INLINE|BLOCK
142      */
143     public void setDisplay(final String display) {
144         this.setAttribute(MathImpl.ATTR_DISPLAY, display);
145     }
146 
147     /**
148      * Returns the display.
149      * 
150      * @return Display display
151      */
152     public String getDisplay() {
153         final String retVal;
154         final String attrDisplay = this.getMathAttribute(MathImpl.ATTR_DISPLAY);
155         if (attrDisplay == null) {
156             if (MathImpl.DEPRECATED_BLOCK_VALUE_FOR_MODE.equalsIgnoreCase(this
157                     .getMathAttribute(MathImpl.ATTR_MODE))) {
158                 retVal = MathImpl.DISPLAY_BLOCK;
159             } else {
160                 retVal = MathImpl.DISPLAY_INLINE;
161             }
162         } else {
163             if (MathImpl.DISPLAY_BLOCK.equalsIgnoreCase(attrDisplay)) {
164                 retVal = MathImpl.DISPLAY_BLOCK;
165             } else {
166                 retVal = MathImpl.DISPLAY_INLINE;
167             }
168         }
169         return retVal;
170     }
171 
172     /** {@inheritDoc} */
173     @Override
174     public LayoutContext getChildLayoutContext(final int childNum,
175             final LayoutContext context) {
176         return new ChildContext(context);
177     }
178 
179     /** {@inheritDoc} */
180     public String getMacros() {
181         return this.getMathAttribute(MathImpl.ATTR_MACROS);
182     }
183 
184     /** {@inheritDoc} */
185     public void setMacros(final String macros) {
186         this.setAttribute(MathImpl.ATTR_MACROS, macros);
187     }
188 
189 }