< prev index next >

src/java.desktop/share/classes/javax/swing/text/html/CSS.java

Print this page


   1 /*
   2  * Copyright (c) 1998, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 931      * specification.  This might be used to specify things
 932      * like brighter, more hue, etc.
 933      * This will return null if there is no value for <code>key</code>.
 934      *
 935      * @param key CSS.Attribute identifying where color is stored.
 936      * @param a the set of attributes
 937      * @return the color
 938      */
 939     Color getColor(AttributeSet a, CSS.Attribute key) {
 940         ColorValue cv = (ColorValue) a.getAttribute(key);
 941         if (cv != null) {
 942             return cv.getValue();
 943         }
 944         return null;
 945     }
 946 
 947     /**
 948      * Returns the size of a font from the passed in string.
 949      *
 950      * @param size CSS string describing font size
 951      * @param baseFontSize size to use for relative units.
 952      */
 953     float getPointSize(String size, StyleSheet ss) {
 954         int relSize, absSize, diff, index;
 955         ss = getStyleSheet(ss);
 956         if (size != null) {
 957             if (size.startsWith("+")) {
 958                 relSize = Integer.valueOf(size.substring(1)).intValue();
 959                 return getPointSize(baseFontSize + relSize, ss);
 960             } else if (size.startsWith("-")) {
 961                 relSize = -Integer.valueOf(size.substring(1)).intValue();
 962                 return getPointSize(baseFontSize + relSize, ss);
 963             } else {
 964                 absSize = Integer.valueOf(size).intValue();
 965                 return getPointSize(absSize, ss);
 966             }
 967         }
 968         return 0;
 969     }
 970 
 971     /**


1641          * map to 1 or more CCS.Attribute.
1642          */
1643         CSS.Attribute[] cssAttrList = getCssAttribute(key);
1644 
1645         if (cssAttrList == null || htmlAttrValue == null) {
1646             return;
1647         }
1648         for (Attribute cssAttr : cssAttrList) {
1649             Object o = getCssValue(cssAttr, htmlAttrValue);
1650             if (o != null) {
1651                 cssAttrSet.addAttribute(cssAttr , o);
1652             }
1653         }
1654     }
1655 
1656     /**
1657      * Given a CSS.Attribute object and its corresponding HTML.Attribute's
1658      * value, this method returns a CssValue object to associate with the
1659      * CSS attribute.
1660      *
1661      * @param the CSS.Attribute
1662      * @param a String containing the value associated HTML.Attribtue.
1663      */
1664     Object getCssValue(CSS.Attribute cssAttr, String htmlAttrValue) {
1665         CssValue value = (CssValue)valueConvertor.get(cssAttr);
1666         Object o = value.parseHtmlValue(htmlAttrValue);
1667         return o;
1668     }
1669 
1670     /**
1671      * Maps an HTML.Attribute object to its appropriate CSS.Attributes.
1672      *
1673      * @param HTML.Attribute
1674      * @return CSS.Attribute[]
1675      */
1676     private CSS.Attribute[] getCssAttribute(HTML.Attribute hAttr) {
1677         return htmlAttrToCssAttrMap.get(hAttr);
1678     }
1679 
1680     /**
1681      * Maps HTML.Attribute.ALIGN to either:
1682      *     CSS.Attribute.TEXT_ALIGN
1683      *     CSS.Attribute.FLOAT
1684      *     CSS.Attribute.VERTICAL_ALIGN
1685      * based on the tag associated with the attribute and the
1686      * value of the attribute.
1687      *
1688      * @param AttributeSet containing HTML attributes.
1689      * @return CSS.Attribute mapping for HTML.Attribute.ALIGN.
1690      */
1691     private CSS.Attribute getCssAlignAttribute(HTML.Tag tag,
1692                                                    AttributeSet htmlAttrSet) {
1693         return CSS.Attribute.TEXT_ALIGN;
1694 /*
1695         String htmlAttrValue = (String)htmlAttrSet.getAttribute(HTML.Attribute.ALIGN);
1696         CSS.Attribute cssAttr = CSS.Attribute.TEXT_ALIGN;
1697         if (htmlAttrValue != null && htmlAttrSet instanceof Element) {
1698             Element elem = (Element)htmlAttrSet;
1699             if (!elem.isLeaf() && tag.isBlock() && validTextAlignValue(htmlAttrValue)) {
1700                 return CSS.Attribute.TEXT_ALIGN;
1701             } else if (isFloater(htmlAttrValue)) {
1702                 return CSS.Attribute.FLOAT;
1703             } else if (elem.isLeaf()) {
1704                 return CSS.Attribute.VERTICAL_ALIGN;
1705             }
1706         }
1707         return null;
1708         */
1709     }
1710 
1711     /**
1712      * Fetches the tag associated with the HTML AttributeSet.
1713      *
1714      * @param  AttributeSet containing the HTML attributes.
1715      * @return HTML.Tag
1716      */
1717     private HTML.Tag getHTMLTag(AttributeSet htmlAttrSet) {
1718         Object o = htmlAttrSet.getAttribute(StyleConstants.NameAttribute);
1719         if (o instanceof HTML.Tag) {
1720             HTML.Tag tag = (HTML.Tag) o;
1721             return tag;
1722         }
1723         return null;
1724     }
1725 
1726 
1727     private boolean isHTMLFontTag(HTML.Tag tag) {
1728         return (tag != null && ((tag == HTML.Tag.FONT) || (tag == HTML.Tag.BASEFONT)));
1729     }
1730 
1731 
1732     private boolean isFloater(String alignValue) {
1733         return (alignValue.equals("left") || alignValue.equals("right"));
1734     }


   1 /*
   2  * Copyright (c) 1998, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 931      * specification.  This might be used to specify things
 932      * like brighter, more hue, etc.
 933      * This will return null if there is no value for <code>key</code>.
 934      *
 935      * @param key CSS.Attribute identifying where color is stored.
 936      * @param a the set of attributes
 937      * @return the color
 938      */
 939     Color getColor(AttributeSet a, CSS.Attribute key) {
 940         ColorValue cv = (ColorValue) a.getAttribute(key);
 941         if (cv != null) {
 942             return cv.getValue();
 943         }
 944         return null;
 945     }
 946 
 947     /**
 948      * Returns the size of a font from the passed in string.
 949      *
 950      * @param size CSS string describing font size

 951      */
 952     float getPointSize(String size, StyleSheet ss) {
 953         int relSize, absSize, diff, index;
 954         ss = getStyleSheet(ss);
 955         if (size != null) {
 956             if (size.startsWith("+")) {
 957                 relSize = Integer.valueOf(size.substring(1)).intValue();
 958                 return getPointSize(baseFontSize + relSize, ss);
 959             } else if (size.startsWith("-")) {
 960                 relSize = -Integer.valueOf(size.substring(1)).intValue();
 961                 return getPointSize(baseFontSize + relSize, ss);
 962             } else {
 963                 absSize = Integer.valueOf(size).intValue();
 964                 return getPointSize(absSize, ss);
 965             }
 966         }
 967         return 0;
 968     }
 969 
 970     /**


1640          * map to 1 or more CCS.Attribute.
1641          */
1642         CSS.Attribute[] cssAttrList = getCssAttribute(key);
1643 
1644         if (cssAttrList == null || htmlAttrValue == null) {
1645             return;
1646         }
1647         for (Attribute cssAttr : cssAttrList) {
1648             Object o = getCssValue(cssAttr, htmlAttrValue);
1649             if (o != null) {
1650                 cssAttrSet.addAttribute(cssAttr , o);
1651             }
1652         }
1653     }
1654 
1655     /**
1656      * Given a CSS.Attribute object and its corresponding HTML.Attribute's
1657      * value, this method returns a CssValue object to associate with the
1658      * CSS attribute.
1659      *
1660      * @param cssAttr the CSS.Attribute
1661      * @param htmlAttrValue a String containing the value associated HTML.Attribute.
1662      */
1663     Object getCssValue(CSS.Attribute cssAttr, String htmlAttrValue) {
1664         CssValue value = (CssValue)valueConvertor.get(cssAttr);
1665         Object o = value.parseHtmlValue(htmlAttrValue);
1666         return o;
1667     }
1668 
1669     /**
1670      * Maps an HTML.Attribute object to its appropriate CSS.Attributes.
1671      *
1672      * @param hAttr HTML.Attribute
1673      * @return CSS.Attribute[]
1674      */
1675     private CSS.Attribute[] getCssAttribute(HTML.Attribute hAttr) {
1676         return htmlAttrToCssAttrMap.get(hAttr);
1677     }
1678 
1679     /**
1680      * Maps HTML.Attribute.ALIGN to either:
1681      *     CSS.Attribute.TEXT_ALIGN
1682      *     CSS.Attribute.FLOAT
1683      *     CSS.Attribute.VERTICAL_ALIGN
1684      * based on the tag associated with the attribute and the
1685      * value of the attribute.
1686      *
1687      * @param tag the AttributeSet containing HTML attributes.
1688      * @return CSS.Attribute mapping for HTML.Attribute.ALIGN.
1689      */
1690     private CSS.Attribute getCssAlignAttribute(HTML.Tag tag,
1691                                                    AttributeSet htmlAttrSet) {
1692         return CSS.Attribute.TEXT_ALIGN;
1693 /*
1694         String htmlAttrValue = (String)htmlAttrSet.getAttribute(HTML.Attribute.ALIGN);
1695         CSS.Attribute cssAttr = CSS.Attribute.TEXT_ALIGN;
1696         if (htmlAttrValue != null && htmlAttrSet instanceof Element) {
1697             Element elem = (Element)htmlAttrSet;
1698             if (!elem.isLeaf() && tag.isBlock() && validTextAlignValue(htmlAttrValue)) {
1699                 return CSS.Attribute.TEXT_ALIGN;
1700             } else if (isFloater(htmlAttrValue)) {
1701                 return CSS.Attribute.FLOAT;
1702             } else if (elem.isLeaf()) {
1703                 return CSS.Attribute.VERTICAL_ALIGN;
1704             }
1705         }
1706         return null;
1707         */
1708     }
1709 
1710     /**
1711      * Fetches the tag associated with the HTML AttributeSet.
1712      *
1713      * @param  htmlAttrSet the AttributeSet containing the HTML attributes.
1714      * @return HTML.Tag
1715      */
1716     private HTML.Tag getHTMLTag(AttributeSet htmlAttrSet) {
1717         Object o = htmlAttrSet.getAttribute(StyleConstants.NameAttribute);
1718         if (o instanceof HTML.Tag) {
1719             HTML.Tag tag = (HTML.Tag) o;
1720             return tag;
1721         }
1722         return null;
1723     }
1724 
1725 
1726     private boolean isHTMLFontTag(HTML.Tag tag) {
1727         return (tag != null && ((tag == HTML.Tag.FONT) || (tag == HTML.Tag.BASEFONT)));
1728     }
1729 
1730 
1731     private boolean isFloater(String alignValue) {
1732         return (alignValue.equals("left") || alignValue.equals("right"));
1733     }


< prev index next >