/* Iterates through the element tree and prints out each element and its attributes. */ private void dumpTree() { Element elem; while (true) { if ((elem = next()) != null) { System.out.println("elem: " + elem.getName()); AttributeSet attr = elem.getAttributes(); String s = ""; Enumeration names = attr.getAttributeNames(); while (names.hasMoreElements()) { Object key = names.nextElement(); Object value = attr.getAttribute(key); if (value instanceof AttributeSet) { // don't go recursive s = s + key + "=**AttributeSet** "; } else { s = s + key + "=" + value + " "; } } System.out.println("attributes: " + s); } else { break; } } }
/** * Searches for embedded tags in the AttributeSet and writes them out. It also stores these tags * in a vector so that when appropriate the corresponding end tags can be written out. * * @exception IOException on any I/O error */ protected void writeEmbeddedTags(AttributeSet attr) throws IOException { // translate css attributes to html attr = convertToHTML(attr, oConvAttr); Enumeration names = attr.getAttributeNames(); while (names.hasMoreElements()) { Object name = names.nextElement(); if (name instanceof HTML.Tag) { HTML.Tag tag = (HTML.Tag) name; if (tag == HTML.Tag.FORM || tags.contains(tag)) { continue; } write('<'); write(tag.toString()); Object o = attr.getAttribute(tag); if (o != null && o instanceof AttributeSet) { writeAttributes((AttributeSet) o); } write('>'); tags.addElement(tag); tagValues.addElement(o); } } }
/** Print the given AttributeSet as a sequence of assignment-like strings, e.g. "key=value". */ protected void writeAttributes(AttributeSet attrs) throws IOException { Enumeration e = attrs.getAttributeNames(); while (e.hasMoreElements()) { Object name = e.nextElement(); Object val = attrs.getAttribute(name); write(name + "=" + val); writeLineSeparator(); } }
/** * Copies the given AttributeSet to a new set, converting any CSS attributes found to arguments of * an HTML style attribute. */ private static void convertToHTML40(AttributeSet from, MutableAttributeSet to) { Enumeration keys = from.getAttributeNames(); String value = ""; while (keys.hasMoreElements()) { Object key = keys.nextElement(); if (key instanceof CSS.Attribute) { value = value + " " + key + "=" + from.getAttribute(key) + ";"; } else { to.addAttribute(key, from.getAttribute(key)); } } if (value.length() > 0) { to.addAttribute(HTML.Attribute.STYLE, value); } }
/** * Create an older style of HTML attributes. This will convert character level attributes that * have a StyleConstants mapping over to an HTML tag/attribute. Other CSS attributes will be * placed in an HTML style attribute. */ private static void convertToHTML32(AttributeSet from, MutableAttributeSet to) { if (from == null) { return; } Enumeration keys = from.getAttributeNames(); String value = ""; while (keys.hasMoreElements()) { Object key = keys.nextElement(); if (key instanceof CSS.Attribute) { if ((key == CSS.Attribute.FONT_FAMILY) || (key == CSS.Attribute.FONT_SIZE) || (key == CSS.Attribute.COLOR)) { createFontAttribute((CSS.Attribute) key, from, to); } else if (key == CSS.Attribute.FONT_WEIGHT) { // add a bold tag is weight is bold CSS.FontWeight weightValue = (CSS.FontWeight) from.getAttribute(CSS.Attribute.FONT_WEIGHT); if ((weightValue != null) && (weightValue.getValue() > 400)) { addAttribute(to, HTML.Tag.B, SimpleAttributeSet.EMPTY); } } else if (key == CSS.Attribute.FONT_STYLE) { String s = from.getAttribute(key).toString(); if (s.indexOf("italic") >= 0) { addAttribute(to, HTML.Tag.I, SimpleAttributeSet.EMPTY); } } else if (key == CSS.Attribute.TEXT_DECORATION) { String decor = from.getAttribute(key).toString(); if (decor.indexOf("underline") >= 0) { addAttribute(to, HTML.Tag.U, SimpleAttributeSet.EMPTY); } if (decor.indexOf("line-through") >= 0) { addAttribute(to, HTML.Tag.STRIKE, SimpleAttributeSet.EMPTY); } } else if (key == CSS.Attribute.VERTICAL_ALIGN) { String vAlign = from.getAttribute(key).toString(); if (vAlign.indexOf("sup") >= 0) { addAttribute(to, HTML.Tag.SUP, SimpleAttributeSet.EMPTY); } if (vAlign.indexOf("sub") >= 0) { addAttribute(to, HTML.Tag.SUB, SimpleAttributeSet.EMPTY); } } else if (key == CSS.Attribute.TEXT_ALIGN) { addAttribute(to, HTML.Attribute.ALIGN, from.getAttribute(key).toString()); } else { // default is to store in a HTML style attribute if (value.length() > 0) { value = value + "; "; } value = value + key + ": " + from.getAttribute(key); } } else { Object attr = from.getAttribute(key); if (attr instanceof AttributeSet) { attr = ((AttributeSet) attr).copyAttributes(); } addAttribute(to, key, attr); } } if (value.length() > 0) { to.addAttribute(HTML.Attribute.STYLE, value); } }