| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
package net.sourceforge.jeuclid.elements.presentation.general; |
| 20 | |
|
| 21 | |
import java.util.ArrayList; |
| 22 | |
import java.util.List; |
| 23 | |
|
| 24 | |
import net.sourceforge.jeuclid.Constants; |
| 25 | |
import net.sourceforge.jeuclid.elements.AbstractElementWithDelegates; |
| 26 | |
import net.sourceforge.jeuclid.elements.AbstractJEuclidElement; |
| 27 | |
import net.sourceforge.jeuclid.elements.presentation.token.Mo; |
| 28 | |
import net.sourceforge.jeuclid.elements.support.operatordict.OperatorDictionary; |
| 29 | |
import net.sourceforge.jeuclid.layout.LayoutableNode; |
| 30 | |
|
| 31 | |
import org.apache.batik.dom.AbstractDocument; |
| 32 | |
import org.w3c.dom.Node; |
| 33 | |
import org.w3c.dom.mathml.MathMLFencedElement; |
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
public final class Mfenced extends AbstractElementWithDelegates implements |
| 41 | |
MathMLFencedElement { |
| 42 | |
|
| 43 | |
|
| 44 | |
public static final String ATTR_SEPARATORS = "separators"; |
| 45 | |
|
| 46 | |
|
| 47 | |
public static final String ATTR_CLOSE = "close"; |
| 48 | |
|
| 49 | |
|
| 50 | |
public static final String ATTR_OPEN = "open"; |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
public static final String ELEMENT = "mfenced"; |
| 56 | |
|
| 57 | |
private static final String FENCE_SPACE = "0.2em"; |
| 58 | |
|
| 59 | |
private static final long serialVersionUID = 1L; |
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
public Mfenced(final String qname, final AbstractDocument odoc) { |
| 70 | 1881 | super(qname, odoc); |
| 71 | 1881 | this.setDefaultMathAttribute(Mfenced.ATTR_OPEN, "("); |
| 72 | 1881 | this.setDefaultMathAttribute(Mfenced.ATTR_CLOSE, ")"); |
| 73 | 1881 | this.setDefaultMathAttribute(Mfenced.ATTR_SEPARATORS, ","); |
| 74 | 1881 | } |
| 75 | |
|
| 76 | |
|
| 77 | |
@Override |
| 78 | |
protected Node newNode() { |
| 79 | 0 | return new Mfenced(this.nodeName, this.ownerDocument); |
| 80 | |
} |
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
public String getOpen() { |
| 86 | 209 | return this.getMathAttribute(Mfenced.ATTR_OPEN); |
| 87 | |
} |
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
public void setOpen(final String open) { |
| 96 | 0 | this.setAttribute(Mfenced.ATTR_OPEN, open); |
| 97 | 0 | } |
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
public String getClose() { |
| 103 | 209 | return this.getMathAttribute(Mfenced.ATTR_CLOSE); |
| 104 | |
} |
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
public void setClose(final String close) { |
| 113 | 0 | this.setAttribute(Mfenced.ATTR_CLOSE, close); |
| 114 | 0 | } |
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
public String getSeparators() { |
| 122 | 1463 | final StringBuilder retVal = new StringBuilder(); |
| 123 | 1463 | final String attValue = this.getMathAttribute(Mfenced.ATTR_SEPARATORS); |
| 124 | 1463 | if (attValue != null) { |
| 125 | 3135 | for (int i = 0; i < attValue.length(); i++) { |
| 126 | 1672 | final char c = attValue.charAt(i); |
| 127 | 1672 | if (c > AbstractJEuclidElement.TRIVIAL_SPACE_MAX) { |
| 128 | 1463 | retVal.append(c); |
| 129 | |
} |
| 130 | |
} |
| 131 | |
} |
| 132 | 1463 | return retVal.toString(); |
| 133 | |
} |
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
public void setSeparators(final String separators) { |
| 142 | 0 | this.setAttribute(Mfenced.ATTR_SEPARATORS, separators); |
| 143 | 0 | } |
| 144 | |
|
| 145 | |
|
| 146 | |
@Override |
| 147 | |
protected List<LayoutableNode> createDelegates() { |
| 148 | 209 | final int contentCount = this.getMathElementCount(); |
| 149 | 209 | final List<LayoutableNode> retVal = new ArrayList<LayoutableNode>( |
| 150 | |
2 * contentCount + 1); |
| 151 | |
|
| 152 | 209 | final Mo opOpen = this.createFenceOperator(); |
| 153 | 209 | opOpen.setForm(OperatorDictionary.FORM_PREFIX); |
| 154 | 209 | opOpen.setTextContent(this.getOpen()); |
| 155 | |
|
| 156 | 209 | retVal.add(opOpen); |
| 157 | 209 | final String sep = this.getSeparators(); |
| 158 | 209 | final boolean haveSep = (sep != null) && (sep.length() > 0); |
| 159 | |
|
| 160 | 418 | for (int i = 0; i < contentCount; i++) { |
| 161 | 209 | retVal.add(this.getMathElement(i)); |
| 162 | |
|
| 163 | 209 | if (haveSep && (i < (contentCount - 1))) { |
| 164 | 0 | final Mo opSep = (Mo) this.getOwnerDocument().createElement( |
| 165 | |
Mo.ELEMENT); |
| 166 | 0 | opSep.setSeparator(Constants.TRUE); |
| 167 | 0 | if (i < sep.length()) { |
| 168 | 0 | opSep.setTextContent(String.valueOf(sep.charAt(i))); |
| 169 | |
} else { |
| 170 | 0 | opSep.setTextContent(String.valueOf(sep |
| 171 | |
.charAt(sep.length() - 1))); |
| 172 | |
} |
| 173 | 0 | retVal.add(opSep); |
| 174 | |
} |
| 175 | |
} |
| 176 | 209 | final Mo opClose = this.createFenceOperator(); |
| 177 | 209 | opClose.setForm(OperatorDictionary.FORM_POSTFIX); |
| 178 | 209 | opClose.setTextContent(this.getClose()); |
| 179 | 209 | retVal.add(opClose); |
| 180 | |
|
| 181 | 209 | return retVal; |
| 182 | |
} |
| 183 | |
|
| 184 | |
private Mo createFenceOperator() { |
| 185 | 418 | final Mo opOpen = (Mo) this.getOwnerDocument() |
| 186 | |
.createElement(Mo.ELEMENT); |
| 187 | 418 | opOpen.setFence(Constants.TRUE); |
| 188 | 418 | opOpen.setStretchy(Constants.TRUE); |
| 189 | 418 | opOpen.setRspace(Mfenced.FENCE_SPACE); |
| 190 | 418 | opOpen.setLspace(Mfenced.FENCE_SPACE); |
| 191 | 418 | opOpen.setSymmetric(Constants.FALSE); |
| 192 | 418 | return opOpen; |
| 193 | |
} |
| 194 | |
|
| 195 | |
} |