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: MathImpl.java,v c169040ef426 2009/10/28 08:55:31 max $ */ 018 019 package net.sourceforge.jeuclid.elements.generic; 020 021 import net.sourceforge.jeuclid.Constants; 022 import net.sourceforge.jeuclid.LayoutContext; 023 import net.sourceforge.jeuclid.context.Display; 024 import net.sourceforge.jeuclid.context.Parameter; 025 import net.sourceforge.jeuclid.elements.presentation.AbstractContainer; 026 027 import org.apache.batik.dom.AbstractDocument; 028 import org.w3c.dom.Node; 029 import org.w3c.dom.mathml.MathMLMathElement; 030 031 /** 032 * The root element for creating a MathElement tree. 033 * 034 * @version $Revision: c169040ef426 $ 035 */ 036 public final class MathImpl extends AbstractContainer implements 037 MathMLMathElement { 038 039 private final class ChildContext implements LayoutContext { 040 private final LayoutContext context; 041 042 private ChildContext(final LayoutContext myContext) { 043 this.context = myContext; 044 } 045 046 public Object getParameter(final Parameter which) { 047 Object retVal; 048 if (Parameter.DISPLAY.equals(which)) { 049 if (MathImpl.DISPLAY_BLOCK.equals(MathImpl.this.getDisplay())) { 050 retVal = Display.BLOCK; 051 } else { 052 retVal = Display.INLINE; 053 } 054 } else { 055 retVal = MathImpl.this.applyLocalAttributesToContext( 056 this.context).getParameter(which); 057 } 058 retVal = this.getParamValueFromJEuclidExt(which, retVal); 059 return retVal; 060 } 061 062 private Object getParamValueFromJEuclidExt(final Parameter which, 063 final Object currentValue) { 064 Object retVal = currentValue; 065 final String s0 = MathImpl.this.getAttributeNS( 066 Constants.NS_JEUCLID_EXT, which.getOptionName()); 067 if ((s0 != null) && (s0.length() > 0)) { 068 retVal = which.fromString(s0); 069 } else { 070 retVal = this.getDeprecatedParamValuesFromJEuclidExt(which, 071 retVal); 072 } 073 return retVal; 074 } 075 076 private Object getDeprecatedParamValuesFromJEuclidExt( 077 final Parameter which, final Object currentValue) { 078 // old namespace 079 Object retVal = currentValue; 080 final String s = MathImpl.this.getAttributeNS( 081 Constants.NS_OLD_JEUCLID_EXT, which.getOptionName()); 082 if ((s != null) && (s.length() > 0)) { 083 retVal = which.fromString(s); 084 } else { 085 // Support deprecated attributes 086 final String s2 = MathImpl.this.getAttributeNS( 087 Constants.NS_OLD_JEUCLID_EXT, which.toString()); 088 if ((s2 != null) && (s2.length() > 0)) { 089 retVal = which.fromString(s2); 090 } 091 } 092 return retVal; 093 } 094 } 095 096 /** attribute for display. */ 097 public static final String ATTR_DISPLAY = "display"; 098 099 /** 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 }