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: Mstyle.java,v bc1d5fde7b73 2009/06/01 14:40:54 maxberger $ */
018    
019    package net.sourceforge.jeuclid.elements.presentation.general;
020    
021    import net.sourceforge.jeuclid.LayoutContext;
022    import net.sourceforge.jeuclid.context.Display;
023    import net.sourceforge.jeuclid.context.Parameter;
024    import net.sourceforge.jeuclid.elements.presentation.AbstractContainer;
025    import net.sourceforge.jeuclid.elements.support.attributes.AttributesHelper;
026    
027    import org.apache.batik.dom.AbstractDocument;
028    import org.apache.commons.logging.Log;
029    import org.apache.commons.logging.LogFactory;
030    import org.w3c.dom.Node;
031    import org.w3c.dom.mathml.MathMLStyleElement;
032    
033    /**
034     * This class arrange an element lower to an other element.
035     * 
036     * @version $Revision: bc1d5fde7b73 $
037     */
038    public final class Mstyle extends AbstractContainer implements
039            MathMLStyleElement {
040    
041        /** Attribute for scriptminsize. */
042        public static final String ATTR_SCRIPTMINSIZE = "scriptminsize";
043    
044        /** Attribute for scriptlevel. */
045        public static final String ATTR_SCRIPTLEVEL = "scriptlevel";
046    
047        /** Attribute for scriptsizemultiplier. */
048        public static final String ATTR_SCRIPTSIZEMULTIPLIER = "scriptsizemultiplier";
049    
050        /** Attribute for displaystyle. */
051        public static final String ATTR_DISPLAYSTYLE = "displaystyle";
052    
053        /**
054         * The XML element from this class.
055         */
056        public static final String ELEMENT = "mstyle";
057    
058        /**
059         * Logger for this class
060         */
061        private static final Log LOGGER = LogFactory.getLog(Mstyle.class);
062    
063        private static final long serialVersionUID = 1L;
064    
065        /**
066         * Default constructor. Sets MathML Namespace.
067         * 
068         * @param qname
069         *            Qualified name.
070         * @param odoc
071         *            Owner Document.
072         */
073        public Mstyle(final String qname, final AbstractDocument odoc) {
074            super(qname, odoc);
075    
076            this.setDefaultMathAttribute(Mstyle.ATTR_DISPLAYSTYLE, "");
077        }
078    
079        /** {@inheritDoc} */
080        @Override
081        protected Node newNode() {
082            return new Mstyle(this.nodeName, this.ownerDocument);
083        }
084    
085        /**
086         * @return Script level
087         */
088        public String getScriptlevel() {
089            return this.getMathAttribute(Mstyle.ATTR_SCRIPTLEVEL);
090        }
091    
092        /**
093         * @param scriptlevel
094         *            script level
095         */
096        public void setScriptlevel(final String scriptlevel) {
097            this.setAttribute(Mstyle.ATTR_SCRIPTLEVEL, scriptlevel);
098        }
099    
100        /**
101         * @return Minimum of script size
102         */
103        public String getScriptminsize() {
104            return this.getMathAttribute(Mstyle.ATTR_SCRIPTMINSIZE);
105        }
106    
107        /**
108         * @param scriptminsize
109         *            Minimum of script size
110         */
111        public void setScriptminsize(final String scriptminsize) {
112            this.setAttribute(Mstyle.ATTR_SCRIPTMINSIZE, scriptminsize);
113        }
114    
115        private class StyleLayoutContext implements LayoutContext {
116    
117            private final LayoutContext context;
118    
119            protected StyleLayoutContext(final LayoutContext parentContext) {
120                this.context = parentContext;
121            }
122    
123            public Object getParameter(final Parameter which) {
124                Object retVal = Mstyle.this.applyLocalAttributesToContext(
125                        this.context).getParameter(which);
126                if (Parameter.DISPLAY.equals(which)) {
127                    retVal = this.applyDisplay(retVal);
128                } else if (Parameter.SCRIPTLEVEL.equals(which)) {
129                    retVal = this.applyScriptlevel(retVal);
130                } else if (Parameter.SCRIPTMINSIZE.equals(which)) {
131                    retVal = this.applyScriptMinsize(retVal);
132                }
133                return retVal;
134            }
135    
136            private Object applyScriptMinsize(final Object parentLevel) {
137                final String newMinsize = Mstyle.this.getScriptminsize();
138                if ((newMinsize != null) && (newMinsize.length() > 0)) {
139                    return AttributesHelper.convertSizeToPt(newMinsize,
140                            this.context, AttributesHelper.PT);
141                } else {
142                    return parentLevel;
143                }
144            }
145    
146            private Object applyScriptlevel(final Object parentLevel) {
147                Object retVal = parentLevel;
148                String attr = Mstyle.this.getScriptlevel();
149                if (attr == null) {
150                    attr = "";
151                }
152                attr = attr.trim();
153                if (attr.length() > 0) {
154                    final char firstchar = attr.charAt(0);
155                    boolean relative = false;
156                    if (firstchar == '+') {
157                        relative = true;
158                        attr = attr.substring(1);
159                    } else if (firstchar == '-') {
160                        relative = true;
161                    }
162                    try {
163                        final int iValue = Integer.parseInt(attr);
164                        if (relative) {
165                            retVal = (Integer) retVal + iValue;
166                        } else {
167                            retVal = iValue;
168                        }
169                    } catch (final NumberFormatException e) {
170                        Mstyle.LOGGER
171                                .warn("Error in scriptlevel attribute for mstyle: "
172                                        + attr);
173                    }
174    
175                }
176                return retVal;
177            }
178    
179            private Object applyDisplay(final Object parentDisplay) {
180                Object retVal = parentDisplay;
181                final String displayStyle = Mstyle.this.getDisplaystyle();
182                if ("true".equalsIgnoreCase(displayStyle)) {
183                    retVal = Display.BLOCK;
184                }
185                if ("false".equalsIgnoreCase(displayStyle)) {
186                    retVal = Display.INLINE;
187                }
188                return retVal;
189            }
190        }
191    
192        /** {@inheritDoc} */
193        @Override
194        public LayoutContext getChildLayoutContext(final int childNum,
195                final LayoutContext context) {
196            return new Mstyle.StyleLayoutContext(context);
197        }
198    
199        /** {@inheritDoc} */
200        public String getBackground() {
201            return this.getMathbackground();
202        }
203    
204        /** {@inheritDoc} */
205        public String getColor() {
206            return this.getMathcolor();
207        }
208    
209        /** {@inheritDoc} */
210        public String getDisplaystyle() {
211            return this.getMathAttribute(Mstyle.ATTR_DISPLAYSTYLE);
212        }
213    
214        /** {@inheritDoc} */
215        public String getScriptsizemultiplier() {
216            return this.getMathAttribute(Mstyle.ATTR_SCRIPTSIZEMULTIPLIER);
217        }
218    
219        /** {@inheritDoc} */
220        public void setBackground(final String background) {
221            this.setMathbackground(background);
222        }
223    
224        /** {@inheritDoc} */
225        public void setColor(final String color) {
226            this.setMathcolor(color);
227        }
228    
229        /** {@inheritDoc} */
230        public void setDisplaystyle(final String displaystyle) {
231            this.setAttribute(Mstyle.ATTR_DISPLAYSTYLE, displaystyle);
232        }
233    
234        /** {@inheritDoc} */
235        public void setScriptsizemultiplier(final String scriptsizemultiplier) {
236            this.setAttribute(Mstyle.ATTR_SCRIPTSIZEMULTIPLIER,
237                    scriptsizemultiplier);
238        }
239    
240        /** {@inheritDoc} */
241        public String getMediummathspace() {
242            throw new UnsupportedOperationException();
243            // TODO Auto-generated method stub
244        }
245    
246        /** {@inheritDoc} */
247        public String getNegativemediummathspace() {
248            throw new UnsupportedOperationException();
249            // TODO Auto-generated method stub
250        }
251    
252        /** {@inheritDoc} */
253        public String getNegativethickmathspace() {
254            throw new UnsupportedOperationException();
255            // TODO Auto-generated method stub
256        }
257    
258        /** {@inheritDoc} */
259        public String getNegativethinmathspace() {
260            throw new UnsupportedOperationException();
261            // TODO Auto-generated method stub
262        }
263    
264        /** {@inheritDoc} */
265        public String getNegativeverythickmathspace() {
266            throw new UnsupportedOperationException();
267            // TODO Auto-generated method stub
268        }
269    
270        /** {@inheritDoc} */
271        public String getNegativeverythinmathspace() {
272            throw new UnsupportedOperationException();
273            // TODO Auto-generated method stub
274        }
275    
276        /** {@inheritDoc} */
277        public String getNegativeveryverythickmathspace() {
278            throw new UnsupportedOperationException();
279            // TODO Auto-generated method stub
280        }
281    
282        /** {@inheritDoc} */
283        public String getNegativeveryverythinmathspace() {
284            throw new UnsupportedOperationException();
285            // TODO Auto-generated method stub
286        }
287    
288        /** {@inheritDoc} */
289        public String getThickmathspace() {
290            throw new UnsupportedOperationException();
291            // TODO Auto-generated method stub
292        }
293    
294        /** {@inheritDoc} */
295        public String getThinmathspace() {
296            throw new UnsupportedOperationException();
297            // TODO Auto-generated method stub
298        }
299    
300        /** {@inheritDoc} */
301        public String getVerythickmathspace() {
302            throw new UnsupportedOperationException();
303            // TODO Auto-generated method stub
304        }
305    
306        /** {@inheritDoc} */
307        public String getVerythinmathspace() {
308            throw new UnsupportedOperationException();
309            // TODO Auto-generated method stub
310        }
311    
312        /** {@inheritDoc} */
313        public String getVeryverythickmathspace() {
314            throw new UnsupportedOperationException();
315            // TODO Auto-generated method stub
316        }
317    
318        /** {@inheritDoc} */
319        public String getVeryverythinmathspace() {
320            throw new UnsupportedOperationException();
321            // TODO Auto-generated method stub
322        }
323    
324        /** {@inheritDoc} */
325        public void setMediummathspace(final String mediummathspace) {
326            throw new UnsupportedOperationException();
327            // TODO Auto-generated method stub
328        }
329    
330        /** {@inheritDoc} */
331        public void setNegativemediummathspace(final String negativemediummathspace) {
332            throw new UnsupportedOperationException();
333            // TODO Auto-generated method stub
334        }
335    
336        /** {@inheritDoc} */
337        public void setNegativethickmathspace(final String negativethickmathspace) {
338            throw new UnsupportedOperationException();
339            // TODO Auto-generated method stub
340        }
341    
342        /** {@inheritDoc} */
343        public void setNegativethinmathspace(final String negativethinmathspace) {
344            throw new UnsupportedOperationException();
345            // TODO Auto-generated method stub
346        }
347    
348        /** {@inheritDoc} */
349        public void setNegativeverythickmathspace(
350                final String negativeverythickmathspace) {
351            throw new UnsupportedOperationException();
352            // TODO Auto-generated method stub
353        }
354    
355        /** {@inheritDoc} */
356        public void setNegativeverythinmathspace(
357                final String negativeverythinmathspace) {
358            throw new UnsupportedOperationException();
359            // TODO Auto-generated method stub
360        }
361    
362        /** {@inheritDoc} */
363        public void setNegativeveryverythickmathspace(
364                final String negativeveryverythickmathspace) {
365            throw new UnsupportedOperationException();
366            // TODO Auto-generated method stub
367        }
368    
369        /** {@inheritDoc} */
370        public void setNegativeveryverythinmathspace(
371                final String negativeveryverythinmathspace) {
372            throw new UnsupportedOperationException();
373            // TODO Auto-generated method stub
374        }
375    
376        /** {@inheritDoc} */
377        public void setThickmathspace(final String thickmathspace) {
378            throw new UnsupportedOperationException();
379            // TODO Auto-generated method stub
380        }
381    
382        /** {@inheritDoc} */
383        public void setThinmathspace(final String thinmathspace) {
384            throw new UnsupportedOperationException();
385            // TODO Auto-generated method stub
386        }
387    
388        /** {@inheritDoc} */
389        public void setVerythickmathspace(final String verythickmathspace) {
390            throw new UnsupportedOperationException();
391            // TODO Auto-generated method stub
392        }
393    
394        /** {@inheritDoc} */
395        public void setVerythinmathspace(final String verythinmathspace) {
396            throw new UnsupportedOperationException();
397            // TODO Auto-generated method stub
398        }
399    
400        /** {@inheritDoc} */
401        public void setVeryverythickmathspace(final String veryverythickmathspace) {
402            throw new UnsupportedOperationException();
403            // TODO Auto-generated method stub
404        }
405    
406        /** {@inheritDoc} */
407        public void setVeryverythinmathspace(final String veryverythinmathspace) {
408            throw new UnsupportedOperationException();
409            // TODO Auto-generated method stub
410        }
411    
412    }