📄 htmlwriter.java
字号:
{ // part of the start tag + contents writeSection((Section) element, indent); return; } case Element.LIST: { List list = (List) element; // start tag addTabs(indent); if (list.isNumbered()) { writeStart(HtmlTags.ORDEREDLIST); } else { writeStart(HtmlTags.UNORDEREDLIST); } if (hasMarkupAttributes(list)) { writeMarkupAttributes((MarkupAttributes)list); } os.write(GT); // contents for (Iterator i = list.getItems().iterator(); i.hasNext(); ) { write((Element) i.next(), indent + 1); } // end tag addTabs(indent); if (list.isNumbered()) { writeEnd(HtmlTags.ORDEREDLIST); } else { writeEnd(HtmlTags.UNORDEREDLIST); } return; } case Element.LISTITEM: { ListItem listItem = (ListItem) element; styleAttributes = new Properties(); if (listItem.leadingDefined()) styleAttributes.setProperty(MarkupTags.CSS_LINEHEIGHT, String.valueOf(listItem.leading()) + "pt"); // start tag addTabs(indent); writeStart(HtmlTags.LISTITEM); if (hasMarkupAttributes(listItem)) { writeMarkupAttributes((MarkupAttributes)listItem); } write(listItem.font(), styleAttributes); os.write(GT); currentfont.push(listItem.font()); // contents for (Iterator i = listItem.iterator(); i.hasNext(); ) { write((Element) i.next(), indent + 1); } // end tag addTabs(indent); writeEnd(HtmlTags.LISTITEM); currentfont.pop(); return; } case Element.CELL: { Cell cell = (Cell) element; // start tag addTabs(indent); if (cell.header()) { writeStart(HtmlTags.HEADERCELL); } else { writeStart(HtmlTags.CELL); } if (hasMarkupAttributes(cell)) { writeMarkupAttributes((MarkupAttributes)cell); } if (cell.borderWidth() != Rectangle.UNDEFINED) { write(HtmlTags.BORDERWIDTH, String.valueOf(cell.borderWidth())); } if (cell.borderColor() != null) { write(HtmlTags.BORDERCOLOR, HtmlEncoder.encode(cell.borderColor())); } if (cell.backgroundColor() != null) { write(HtmlTags.BACKGROUNDCOLOR, HtmlEncoder.encode(cell.backgroundColor())); } String alignment = HtmlEncoder.getAlignment(cell.horizontalAlignment()); if (!"".equals(alignment)) { write(HtmlTags.HORIZONTALALIGN, alignment); } alignment = HtmlEncoder.getAlignment(cell.verticalAlignment()); if (!"".equals(alignment)) { write(HtmlTags.VERTICALALIGN, alignment); } if (cell.cellWidth() != null) { write(HtmlTags.WIDTH, cell.cellWidth()); } if (cell.colspan() != 1) { write(HtmlTags.COLSPAN, String.valueOf(cell.colspan())); } if (cell.rowspan() != 1) { write(HtmlTags.ROWSPAN, String.valueOf(cell.rowspan())); } if (cell.noWrap()) { write(HtmlTags.NOWRAP, String.valueOf(true)); } os.write(GT); // contents if (cell.isEmpty()) { write(NBSP); } else { for (Iterator i = cell.getElements(); i.hasNext(); ) { write((Element) i.next(), indent + 1); } } // end tag addTabs(indent); if (cell.header()) { writeEnd(HtmlTags.HEADERCELL); } else { writeEnd(HtmlTags.CELL); } return; } case Element.ROW: { Row row = (Row) element; // start tag addTabs(indent); writeStart(HtmlTags.ROW); if (hasMarkupAttributes(row)) { writeMarkupAttributes((MarkupAttributes)row); } os.write(GT); // contents Element cell; for (int i = 0; i < row.columns(); i++) { if ((cell = (Element)row.getCell(i)) != null) { write(cell, indent + 1); } } // end tag addTabs(indent); writeEnd(HtmlTags.ROW); return; } case Element.TABLE: { Table table = (Table) element; table.complete(); // start tag addTabs(indent); writeStart(HtmlTags.TABLE); if (hasMarkupAttributes(table)) { writeMarkupAttributes((MarkupAttributes)table); } os.write(SPACE); write(HtmlTags.WIDTH); os.write(EQUALS); os.write(QUOTE); if (! "".equals(table.absWidth())){ write(table.absWidth()); } else{ write(String.valueOf(table.widthPercentage())); write("%"); } os.write(QUOTE); String alignment = HtmlEncoder.getAlignment(table.alignment()); if (!"".equals(alignment)) { write(HtmlTags.ALIGN, alignment); } write(HtmlTags.CELLPADDING, String.valueOf(table.cellpadding())); write(HtmlTags.CELLSPACING, String.valueOf(table.cellspacing())); if (table.borderWidth() != Rectangle.UNDEFINED) { write(HtmlTags.BORDERWIDTH, String.valueOf(table.borderWidth())); } if (table.borderColor() != null) { write(HtmlTags.BORDERCOLOR, HtmlEncoder.encode(table.borderColor())); } if (table.backgroundColor() != null) { write(HtmlTags.BACKGROUNDCOLOR, HtmlEncoder.encode(table.backgroundColor())); } os.write(GT); // contents Row row; for (Iterator iterator = table.iterator(); iterator.hasNext(); ) { row = (Row) iterator.next(); write(row, indent + 1); } // end tag addTabs(indent); writeEnd(HtmlTags.TABLE); return; } case Element.ANNOTATION: { Annotation annotation = (Annotation) element; writeComment(annotation.title() + ": " + annotation.content()); if (hasMarkupAttributes(annotation)) { os.write(BEGINCOMMENT); writeMarkupAttributes((MarkupAttributes)annotation); os.write(ENDCOMMENT); } return; } case Element.GIF: case Element.JPEG: case Element.PNG: { Image image = (Image) element; if (image.url() == null) { return; } // start tag addTabs(indent); writeStart(HtmlTags.IMAGE); String path = image.url().toString(); if (imagepath != null) { if (path.indexOf("/") > 0) { path = imagepath + path.substring(path.lastIndexOf("/") + 1); } else { path = imagepath + path; } } write(HtmlTags.URL, path); if ((image.alignment() & Image.LEFT) > 0) { write(HtmlTags.ALIGN, HtmlTags.ALIGN_LEFT); } else if ((image.alignment() & Image.RIGHT) > 0) { write(HtmlTags.ALIGN, HtmlTags.ALIGN_RIGHT); } else if ((image.alignment() & Image.MIDDLE) > 0) { write(HtmlTags.ALIGN, HtmlTags.ALIGN_MIDDLE); } if (image.alt() != null) { write(HtmlTags.ALT, image.alt()); } write(HtmlTags.PLAINWIDTH, String.valueOf(image.scaledWidth())); write(HtmlTags.PLAINHEIGHT, String.valueOf(image.scaledHeight())); if (hasMarkupAttributes(image)){ writeMarkupAttributes((MarkupAttributes)image); } writeEnd(); return; } default: return; } } /** * Writes the HTML representation of a section. * * @param section the section to write * @param indent the indentation */ protected void writeSection(Section section, int indent) throws IOException { if (section.title() != null) { int depth = section.depth() - 1; if (depth > 5) { depth = 5; } Properties styleAttributes = new Properties(); if (section.title().leadingDefined()) styleAttributes.setProperty(MarkupTags.CSS_LINEHEIGHT, String.valueOf(section.title().leading()) + "pt"); // start tag addTabs(indent); writeStart(HtmlTags.H[depth]); write(section.title().font(), styleAttributes); String alignment = HtmlEncoder.getAlignment(section.title().alignment()); if (!"".equals(alignment)) { write(HtmlTags.ALIGN, alignment); } if (hasMarkupAttributes(section.title())) { writeMarkupAttributes((MarkupAttributes)section.title()); } os.write(GT); currentfont.push(section.title().font()); // contents for (Iterator i = section.title().iterator(); i.hasNext(); ) { write((Element)i.next(), indent + 1); } // end tag addTabs(indent); writeEnd(HtmlTags.H[depth]); currentfont.pop(); } for (Iterator i = section.iterator(); i.hasNext(); ) { write((Element) i.next(), indent); } } /** * Writes the representation of a <CODE>Font</CODE>. * * @param font a <CODE>Font</CODE> * @param styleAttributes the style of the font */ protected void write(Font font, Properties styleAttributes) throws IOException { if (font == null || !isOtherFont(font) || styleAttributes == null) return; write(" "); write(MarkupTags.STYLE); write("=\""); if (styleAttributes != null) { String key; for (Enumeration e = styleAttributes.propertyNames(); e.hasMoreElements(); ) { key = (String)e.nextElement(); writeCssProperty(key, styleAttributes.getProperty(key)); } } if (isOtherFont(font)) { writeCssProperty(MarkupTags.CSS_FONTFAMILY, font.getFamilyname()); if (font.size() != Font.UNDEFINED) { writeCssProperty(MarkupTags.CSS_FONTSIZE, String.valueOf(font.size()) + "pt"); } if (font.color() != null) { writeCssProperty(MarkupTags.CSS_COLOR, HtmlEncoder.encode(font.color())); } int fontstyle = font.style(); if (fontstyle != Font.UNDEFINED && fontstyle != Font.NORMAL) { switch (fontstyle & Font.BOLDITALIC) { case Font.BOLD: writeCssProperty(MarkupTags.CSS_FONTWEIGHT, MarkupTags.CSS_BOLD); break; case Font.ITALIC: writeCssProperty(MarkupTags.CSS_FONTSTYLE, MarkupTags.CSS_ITALIC); break; case Font.BOLDITALIC: writeCssProperty(MarkupTags.CSS_FONTWEIGHT, MarkupTags.CSS_BOLD); writeCssProperty(MarkupTags.CSS_FONTSTYLE, MarkupTags.CSS_ITALIC); break; } // CSS only supports one decoration tag so if both are specified // only one of the two will display if ((fontstyle & Font.UNDERLINE) > 0) { writeCssProperty(MarkupTags.CSS_TEXTDECORATION, MarkupTags.CSS_UNDERLINE); } if ((fontstyle & Font.STRIKETHRU) > 0) { writeCssProperty(MarkupTags.CSS_TEXTDECORATION, MarkupTags.CSS_LINETHROUGH); } } } write("\""); } /** * Writes out a CSS property. */ protected void writeCssProperty(String prop, String value) throws IOException { write(new StringBuffer(prop).append(": ").append(value).append("; ").toString()); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -