summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoman Kennke <roman@kennke.org>2006-11-11 11:02:07 +0000
committerRoman Kennke <roman@kennke.org>2006-11-11 11:02:07 +0000
commitd6468560da4a79c4d09bbf7b62598ad2bf7d9b99 (patch)
tree21e62cde57cb8a6728c8a5f56da10deb1eb0847a
parent5a7231e58f2b64cd18389479181b89e8da7e1bae (diff)
downloadclasspath-d6468560da4a79c4d09bbf7b62598ad2bf7d9b99.tar.gz
2006-11-11 Roman Kennke <kennke@aicas.com>
* gnu/javax/swing/text/html/css/CSSColor.java (isValidColor): New helper method. Checks strings if they form a valid color value. * gnu/javax/swing/text/html/css/Length.java (Length): Catch number format exceptions. * javax/swing/text/html/CSS.java (addInternal): New method. Checks for shorthand CSS attributes and parses them. (parseBackgroundShorthand): New method. Parses the background shorthand attribute. * javax/swing/text/html/HTMLDocument.java (HTMLReader.LinkAction): Made class a subclass of HiddenAction. (HTMLReader.LinkAction.start): Implemented to load the linked stylesheet. (HTMLReader.LinkAction.end): Removed. This is not needed. * javax/swing/text/html/StyleSheet.java (CSSStyleSheetParserCallback.declaration): Push declaration through CSS.addInternal() to parse shorthand attributes. (addCSSAttribute): Push declaration through CSS.addInternal() to parse shorthand attributes. (importStyleSheet): Implemented. This adds a stylesheet from an URL. * javax/swing/text/html/TableView.java (calculateColumnRequirements): Increase column index for non CellView children to avoid endless loop. * javax/swing/text/CompositeView.java (setParent): Comparen with numChildren not with real arraylength.
-rw-r--r--ChangeLog30
-rw-r--r--gnu/javax/swing/text/html/css/CSSColor.java29
-rw-r--r--gnu/javax/swing/text/html/css/Length.java36
-rw-r--r--javax/swing/text/CompositeView.java2
-rw-r--r--javax/swing/text/html/CSS.java22
-rw-r--r--javax/swing/text/html/HTMLDocument.java69
-rw-r--r--javax/swing/text/html/StyleSheet.java24
-rw-r--r--javax/swing/text/html/TableView.java2
8 files changed, 184 insertions, 30 deletions
diff --git a/ChangeLog b/ChangeLog
index e004bfb85..efc184994 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,33 @@
+2006-11-11 Roman Kennke <kennke@aicas.com>
+
+ * gnu/javax/swing/text/html/css/CSSColor.java
+ (isValidColor): New helper method. Checks strings if they
+ form a valid color value.
+ * gnu/javax/swing/text/html/css/Length.java
+ (Length): Catch number format exceptions.
+ * javax/swing/text/html/CSS.java
+ (addInternal): New method. Checks for shorthand CSS attributes
+ and parses them.
+ (parseBackgroundShorthand): New method. Parses the background
+ shorthand attribute.
+ * javax/swing/text/html/HTMLDocument.java
+ (HTMLReader.LinkAction): Made class a subclass of HiddenAction.
+ (HTMLReader.LinkAction.start): Implemented to load the linked
+ stylesheet.
+ (HTMLReader.LinkAction.end): Removed. This is not needed.
+ * javax/swing/text/html/StyleSheet.java
+ (CSSStyleSheetParserCallback.declaration): Push declaration
+ through CSS.addInternal() to parse shorthand attributes.
+ (addCSSAttribute): Push declaration through CSS.addInternal()
+ to parse shorthand attributes.
+ (importStyleSheet): Implemented. This adds a stylesheet from
+ an URL.
+ * javax/swing/text/html/TableView.java
+ (calculateColumnRequirements): Increase column index for
+ non CellView children to avoid endless loop.
+ * javax/swing/text/CompositeView.java
+ (setParent): Comparen with numChildren not with real arraylength.
+
2006-11-11 David Gilbert <david.gilbert@object-refinery.com>
* java/beans/beancontext/BeanContextSupport.java
diff --git a/gnu/javax/swing/text/html/css/CSSColor.java b/gnu/javax/swing/text/html/css/CSSColor.java
index 381bcd5ed..57230f12a 100644
--- a/gnu/javax/swing/text/html/css/CSSColor.java
+++ b/gnu/javax/swing/text/html/css/CSSColor.java
@@ -40,6 +40,8 @@ package gnu.javax.swing.text.html.css;
import java.awt.Color;
import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Set;
/**
* Converts CSS color values into AWT Color values.
@@ -131,4 +133,31 @@ public class CSSColor
{
return value;
}
+
+ /**
+ * Returns <code>true</code> if the specified value is a valid color value,
+ * <code>false</code> otherwise.
+ *
+ * @param val the value to check
+ *
+ * @return <code>true</code> if the specified value is a valid color value,
+ * <code>false</code> otherwise
+ */
+ public static boolean isValidColor(String val)
+ {
+ boolean ret = false;
+ if (val.charAt(0) == '#')
+ ret = true;
+ else
+ {
+ Set colors = COLOR_MAP.keySet();
+ for (Iterator i = colors.iterator(); i.hasNext() && ret == false;)
+ {
+ String color = (String) i.next();
+ if (color.equalsIgnoreCase(val))
+ ret = true;
+ }
+ }
+ return ret;
+ }
}
diff --git a/gnu/javax/swing/text/html/css/Length.java b/gnu/javax/swing/text/html/css/Length.java
index 11338d9f6..339e2a2e0 100644
--- a/gnu/javax/swing/text/html/css/Length.java
+++ b/gnu/javax/swing/text/html/css/Length.java
@@ -71,22 +71,30 @@ public class Length
value = val;
int i = value.indexOf("px");
int percent = value.indexOf("%");
- floatValue = 0.0F;
- if (i != -1)
+ try
{
- String sub = value.substring(0, i);
- floatValue = Float.parseFloat(sub);
+ floatValue = 0.0F;
+ if (i != -1)
+ {
+ String sub = value.substring(0, i);
+ floatValue = Float.parseFloat(sub);
+ }
+ else if (percent != -1)
+ {
+ isPercentage = true;
+ String sub = value.substring(0, percent);
+ floatValue = Float.parseFloat(sub) / 100;
+ }
+ else
+ {
+ // TODO: Implement other length options.
+ floatValue = Float.parseFloat(value);
+ }
}
- else if (percent != -1)
+ catch (NumberFormatException ex)
{
- isPercentage = true;
- String sub = value.substring(0, percent);
- floatValue = Float.parseFloat(sub) / 100;
- }
- else
- {
- // TODO: Implement other length options.
- floatValue = Float.parseFloat(value);
+ // Don't let such small problems interrupt CSS parsing.
+ System.err.println("couldn't parse: " + val);
}
}
@@ -104,7 +112,7 @@ public class Length
* Returns the absolute span for the case when this length value is
* a relative value.
*
- * @param span the target span
+ * @param available the target span
*
* @return the absolute span
*/
diff --git a/javax/swing/text/CompositeView.java b/javax/swing/text/CompositeView.java
index ab587a9e1..d4467f314 100644
--- a/javax/swing/text/CompositeView.java
+++ b/javax/swing/text/CompositeView.java
@@ -134,7 +134,7 @@ public abstract class CompositeView
public void setParent(View parent)
{
super.setParent(parent);
- if (parent != null && ((children == null) || children.length == 0))
+ if (parent != null && numChildren == 0)
loadChildren(getViewFactory());
}
diff --git a/javax/swing/text/html/CSS.java b/javax/swing/text/html/CSS.java
index a91394d4d..6461dca9a 100644
--- a/javax/swing/text/html/CSS.java
+++ b/javax/swing/text/html/CSS.java
@@ -46,6 +46,9 @@ import gnu.javax.swing.text.html.css.Length;
import java.io.Serializable;
import java.util.HashMap;
+import java.util.StringTokenizer;
+
+import javax.swing.text.MutableAttributeSet;
/**
* Provides CSS attributes to be used by the HTML view classes. The constants
@@ -524,4 +527,23 @@ public class CSS implements Serializable
o = v;
return o;
}
+
+ static void addInternal(MutableAttributeSet atts, Attribute a, String v)
+ {
+ if (a == Attribute.BACKGROUND)
+ parseBackgroundShorthand(atts, v);
+ }
+
+ private static void parseBackgroundShorthand(MutableAttributeSet atts,
+ String v)
+ {
+ StringTokenizer tokens = new StringTokenizer(v, " ");
+ while (tokens.hasMoreElements())
+ {
+ String token = tokens.nextToken();
+ if (CSSColor.isValidColor(token))
+ atts.addAttribute(Attribute.BACKGROUND_COLOR,
+ getValue(Attribute.BACKGROUND_COLOR, token));
+ }
+ }
}
diff --git a/javax/swing/text/html/HTMLDocument.java b/javax/swing/text/html/HTMLDocument.java
index 93397f2e5..418607309 100644
--- a/javax/swing/text/html/HTMLDocument.java
+++ b/javax/swing/text/html/HTMLDocument.java
@@ -42,6 +42,7 @@ import gnu.classpath.NotImplementedException;
import java.io.IOException;
import java.io.StringReader;
+import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
@@ -992,27 +993,71 @@ public class HTMLDocument extends DefaultStyledDocument
}
}
- class LinkAction extends TagAction
+ class LinkAction extends HiddenAction
{
/**
* This method is called when a start tag is seen for one of the types
* of tags associated with this Action.
*/
public void start(HTML.Tag t, MutableAttributeSet a)
- throws NotImplementedException
{
- // FIXME: Implement.
+ super.start(t, a);
+ String type = (String) a.getAttribute(HTML.Attribute.TYPE);
+ if (type == null)
+ type = "text/css";
+ if (type.equals("text/css"))
+ {
+ String rel = (String) a.getAttribute(HTML.Attribute.REL);
+ String media = (String) a.getAttribute(HTML.Attribute.MEDIA);
+ String title = (String) a.getAttribute(HTML.Attribute.TITLE);
+ if (media == null)
+ media = "all";
+ else
+ media = media.toLowerCase();
+ if (rel != null)
+ {
+ rel = rel.toLowerCase();
+ if ((media.indexOf("all") != -1
+ || media.indexOf("screen") != -1)
+ && (rel.equals("stylesheet")))
+ {
+ String href = (String) a.getAttribute(HTML.Attribute.HREF);
+ URL url = null;
+ try
+ {
+ url = new URL(baseURL, href);
+ }
+ catch (MalformedURLException ex)
+ {
+ try
+ {
+ url = new URL(href);
+ }
+ catch (MalformedURLException ex2)
+ {
+ url = null;
+ }
+ }
+ if (url != null)
+ {
+ try
+ {
+ getStyleSheet().importStyleSheet(url);
+ }
+ catch (Exception ex)
+ {
+ // Don't let exceptions and runtime exceptions
+ // in CSS parsing disprupt the HTML parsing
+ // process. But inform the user/developer
+ // on the console about it.
+ ex.printStackTrace();
+ }
+ }
+ }
+ }
+ }
}
- /**
- * Called when an end tag is seen for one of the types of tags associated
- * with this Action.
- */
- public void end(HTML.Tag t)
- throws NotImplementedException
- {
- // FIXME: Implement.
- }
}
class MapAction extends TagAction
diff --git a/javax/swing/text/html/StyleSheet.java b/javax/swing/text/html/StyleSheet.java
index 9f626e3f9..450682f68 100644
--- a/javax/swing/text/html/StyleSheet.java
+++ b/javax/swing/text/html/StyleSheet.java
@@ -50,7 +50,10 @@ import gnu.javax.swing.text.html.css.Selector;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
+import java.io.BufferedReader;
import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
import java.io.Reader;
import java.io.Serializable;
import java.io.StringReader;
@@ -154,6 +157,7 @@ public class StyleSheet extends StyleContext
{
CSS.Attribute cssAtt = CSS.getAttribute(property);
Object val = CSS.getValue(cssAtt, value);
+ CSS.addInternal(style, cssAtt, value);
if (cssAtt != null)
style.addAttribute(cssAtt, val);
}
@@ -596,13 +600,26 @@ public class StyleSheet extends StyleContext
/**
* Imports a style sheet from the url. The rules are directly added to the
- * receiver.
+ * receiver. This is usually called when a <link> tag is resolved in an
+ * HTML document.
*
- * @param url - the URL to import the StyleSheet from.
+ * @param url the URL to import the StyleSheet from
*/
public void importStyleSheet(URL url)
{
- // FIXME: Not implemented
+ try
+ {
+ InputStream in = url.openStream();
+ Reader r = new BufferedReader(new InputStreamReader(in));
+ CSSStyleSheetParserCallback cb =
+ new CSSStyleSheetParserCallback(CSSStyle.PREC_AUTHOR_NORMAL);
+ CSSParser parser = new CSSParser(r, cb);
+ parser.parse();
+ }
+ catch (IOException ex)
+ {
+ // We can't do anything about it I guess.
+ }
}
/**
@@ -638,6 +655,7 @@ public class StyleSheet extends StyleContext
String value)
{
Object val = CSS.getValue(key, value);
+ CSS.addInternal(attr, key, value);
attr.addAttribute(key, val);
}
diff --git a/javax/swing/text/html/TableView.java b/javax/swing/text/html/TableView.java
index ba959b589..2bd11ffcf 100644
--- a/javax/swing/text/html/TableView.java
+++ b/javax/swing/text/html/TableView.java
@@ -467,6 +467,8 @@ class TableView
}
c += colSpan;
}
+ else
+ c++;
}
// Update the total requirements as follows: