📄 althtmlwriter.java
字号:
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); } } } /** * Searches the attribute set for a tag, both of which * are passed in as a parameter. Returns true if no match is found * and false otherwise. */ private boolean noMatchForTagInAttributes(AttributeSet attr, HTML.Tag t, Object tagValue) { if (attr != null && attr.isDefined(t)) { Object newValue = attr.getAttribute(t); if ((tagValue == null) ? (newValue == null) : (newValue != null && tagValue.equals(newValue))) { return false; } } return true; } /** * Searches the attribute set and for each tag * that is stored in the tag vector. If the tag isnt found, * then the tag is removed from the vector and a corresponding * end tag is written out. * * @exception IOException on any I/O error */ protected void closeOutUnwantedEmbeddedTags(AttributeSet attr) throws IOException { tagsToRemove.removeAllElements(); // translate css attributes to html attr = convertToHTML(attr, null); HTML.Tag t; Object tValue; int firstIndex = -1; int size = tags.size(); // First, find all the tags that need to be removed. for (int i = size - 1; i >= 0; i--) { t = (HTML.Tag) tags.elementAt(i); tValue = tagValues.elementAt(i); if ((attr == null) || noMatchForTagInAttributes(attr, t, tValue)) { firstIndex = i; tagsToRemove.addElement(t); } } if (firstIndex != -1) { // Then close them out. boolean removeAll = ((size - firstIndex) == tagsToRemove.size()); for (int i = size - 1; i >= firstIndex; i--) { t = (HTML.Tag) tags.elementAt(i); if (removeAll || tagsToRemove.contains(t)) { tags.removeElementAt(i); tagValues.removeElementAt(i); } write('<'); write('/'); write(t.toString()); write('>'); } // Have to output any tags after firstIndex that still remaing, // as we closed them out, but they should remain open. size = tags.size(); for (int i = firstIndex; i < size; i++) { t = (HTML.Tag) tags.elementAt(i); write('<'); write(t.toString()); Object o = tagValues.elementAt(i); if (o != null && o instanceof AttributeSet) { writeAttributes((AttributeSet) o); } write('>'); } } } /** * Determines if the element associated with the attributeset * is a TEXTAREA or SELECT. If true, returns true else * false */ private boolean isFormElementWithContent(AttributeSet attr) { if (matchNameAttribute(attr, HTML.Tag.TEXTAREA) || matchNameAttribute(attr, HTML.Tag.SELECT)) { return true; } return false; } /** * Determines whether a the indentation needs to be * incremented. Basically, if next is a child of current, and * next is NOT a synthesized element, the indent level will be * incremented. If there is a parent-child relationship and "next" * is a synthesized element, then its children must be indented. * This state is maintained by the indentNext boolean. * * @return boolean that's true if indent level * needs incrementing. */ private boolean indentNext = false; private boolean indentNeedsIncrementing(Element current, Element next) { if ((next.getParentElement() == current) && !inPre) { if (indentNext) { indentNext = false; return true; } else if (synthesizedElement(next)) { indentNext = true; } else if (!synthesizedElement(current)) { return true; } } return false; } /** * Outputs the maps as elements. Maps are not stored as elements in * the document, and as such this is used to output them. */ void writeMaps(Enumeration maps) throws IOException { if (maps != null) { while (maps.hasMoreElements()) { Map map = (Map) maps.nextElement(); String name = map.getName(); incrIndent(); indent(); write("<map"); if (name != null) { write(" name=\""); write(name); write("\">"); } else { write('>'); } writeLineSeparator(); incrIndent(); // Output the areas AttributeSet[] areas = map.getAreas(); if (areas != null) { for (int counter = 0, maxCounter = areas.length; counter < maxCounter; counter++) { indent(); write("<area"); writeAttributes(areas[counter]); write("></area>"); writeLineSeparator(); } } decrIndent(); indent(); write("</map>"); writeLineSeparator(); decrIndent(); } } } /** * Outputs the styles as a single element. Styles are not stored as * elements, but part of the document. For the time being styles are * written out as a comment, inside a style tag. */ void writeStyles(StyleSheet sheet) throws IOException { if (sheet != null) { Enumeration styles = sheet.getStyleNames(); if (styles != null) { boolean outputStyle = false; while (styles.hasMoreElements()) { String name = (String) styles.nextElement(); // Don't write out the default style. if (!StyleContext.DEFAULT_STYLE.equals(name) && writeStyle(name, sheet.getStyle(name), outputStyle)) { outputStyle = true; } } if (outputStyle) { writeStyleEndTag(); } } } } /** * Outputs the named style. <code>outputStyle</code> indicates * whether or not a style has been output yet. This will return * true if a style is written. */ boolean writeStyle(String name, Style style, boolean outputStyle) throws IOException { boolean didOutputStyle = false; Enumeration attributes = style.getAttributeNames(); if (attributes != null) { while (attributes.hasMoreElements()) { Object attribute = attributes.nextElement(); if (attribute instanceof CSS.Attribute) { String value = style.getAttribute(attribute).toString(); if (value != null) { if (!outputStyle) { writeStyleStartTag(); outputStyle = true; } if (!didOutputStyle) { didOutputStyle = true; indent(); write(name); write(" {"); } else { write(";"); } write(' '); write(attribute.toString()); write(": "); write(value); } } } } if (didOutputStyle) { write(" }"); writeLineSeparator(); } return didOutputStyle; } void writeStyleStartTag() throws IOException { indent(); write("<style type=\"text/css\">"); incrIndent(); writeLineSeparator(); indent(); write("<!--"); incrIndent(); writeLineSeparator(); } void writeStyleEndTag() throws IOException { decrIndent(); indent(); write("-->"); writeLineSeparator(); decrIndent(); indent(); write("</style>"); writeLineSeparator(); indent(); } // --- conversion support --------------------------- /** * Convert the give set of attributes to be html for * the purpose of writing them out. Any keys that * have been converted will not appear in the resultant * set. Any keys not converted will appear in the * resultant set the same as the received set.<p> * This will put the converted values into <code>to</code>, unless * it is null in which case a temporary AttributeSet will be returned. */ AttributeSet convertToHTML(AttributeSet from, MutableAttributeSet to) { if (to == null) { to = convAttr; } to.removeAttributes(to); if (writeCSS) { convertToHTML40(from, to); } else { convertToHTML32(from, to); } return to; } /** * If true, the writer will emit CSS attributes in preference * to HTML tags/attributes (i.e. It will emit an HTML 4.0 * style). */ private boolean writeCSS = false; /** * Buffer for the purpose of attribute conversion */ private MutableAttributeSet convAttr = new SimpleAttributeSet(); /** * Buffer for the purpose of attribute conversion. This can be * used if convAttr is being used. */ private MutableAttributeSet oConvAttr = new SimpleAttributeSet(); /** * 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); String weightValue = from.getAttribute(CSS.Attribute.FONT_WEIGHT).toString(); if (weightValue != null) { int fweight; try { fweight = new Integer(weightValue).intValue(); } catch (Exception ex) { fweight = -1; } if ((weightValue.toLowerCase().equals("bold")) || (fweight > 400)) to.addAttribute(HTML.Tag.B, SimpleAttributeSet.EMPTY); } } else if (key == CSS.Attribute.FONT_STYLE) { String s = from.getAttribute(key).toString(); if (s.indexOf("italic") >= 0) { to.addAttribute(HTML.Tag.I, SimpleAttributeSet.EMPTY); } } else if (key == CSS.Attribute.TEXT_DECORATION) { String decor = from.getAttribute(key).toString(); if (decor.indexOf("underline") >= 0) { to.addAttribute(HTML.Tag.U, SimpleAttributeSet.EMPTY); } if (decor.indexOf("line-through") >= 0) { to.addAttribute(HTML.Tag.STRIKE, SimpleAttributeSet.EMPTY); } } else if (key == CSS.Attribute.VERTICAL_ALIGN) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -