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: Mstyle.java,v bc1d5fde7b73 2009/06/01 14:40:54 maxberger $ */
18  
19  package net.sourceforge.jeuclid.elements.presentation.general;
20  
21  import net.sourceforge.jeuclid.LayoutContext;
22  import net.sourceforge.jeuclid.context.Display;
23  import net.sourceforge.jeuclid.context.Parameter;
24  import net.sourceforge.jeuclid.elements.presentation.AbstractContainer;
25  import net.sourceforge.jeuclid.elements.support.attributes.AttributesHelper;
26  
27  import org.apache.batik.dom.AbstractDocument;
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.w3c.dom.Node;
31  import org.w3c.dom.mathml.MathMLStyleElement;
32  
33  /**
34   * This class arrange an element lower to an other element.
35   * 
36   * @version $Revision: bc1d5fde7b73 $
37   */
38  public final class Mstyle extends AbstractContainer implements
39          MathMLStyleElement {
40  
41      /** Attribute for scriptminsize. */
42      public static final String ATTR_SCRIPTMINSIZE = "scriptminsize";
43  
44      /** Attribute for scriptlevel. */
45      public static final String ATTR_SCRIPTLEVEL = "scriptlevel";
46  
47      /** Attribute for scriptsizemultiplier. */
48      public static final String ATTR_SCRIPTSIZEMULTIPLIER = "scriptsizemultiplier";
49  
50      /** Attribute for displaystyle. */
51      public static final String ATTR_DISPLAYSTYLE = "displaystyle";
52  
53      /**
54       * The XML element from this class.
55       */
56      public static final String ELEMENT = "mstyle";
57  
58      /**
59       * Logger for this class
60       */
61      private static final Log LOGGER = LogFactory.getLog(Mstyle.class);
62  
63      private static final long serialVersionUID = 1L;
64  
65      /**
66       * Default constructor. Sets MathML Namespace.
67       * 
68       * @param qname
69       *            Qualified name.
70       * @param odoc
71       *            Owner Document.
72       */
73      public Mstyle(final String qname, final AbstractDocument odoc) {
74          super(qname, odoc);
75  
76          this.setDefaultMathAttribute(Mstyle.ATTR_DISPLAYSTYLE, "");
77      }
78  
79      /** {@inheritDoc} */
80      @Override
81      protected Node newNode() {
82          return new Mstyle(this.nodeName, this.ownerDocument);
83      }
84  
85      /**
86       * @return Script level
87       */
88      public String getScriptlevel() {
89          return this.getMathAttribute(Mstyle.ATTR_SCRIPTLEVEL);
90      }
91  
92      /**
93       * @param scriptlevel
94       *            script level
95       */
96      public void setScriptlevel(final String scriptlevel) {
97          this.setAttribute(Mstyle.ATTR_SCRIPTLEVEL, scriptlevel);
98      }
99  
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 }