/** * Helper method to create a Chapter/Section object. * @param attributes */ private static void setSectionParameters(Section section, Properties attributes) { String value; value = attributes.getProperty(ElementTags.NUMBERDEPTH); if (value != null) { section.setNumberDepth(Integer.parseInt(value)); } value = attributes.getProperty(ElementTags.INDENT); if (value != null) { section.setIndentation(Float.parseFloat(value + "f")); } value = attributes.getProperty(ElementTags.INDENTATIONLEFT); if (value != null) { section.setIndentationLeft(Float.parseFloat(value + "f")); } value = attributes.getProperty(ElementTags.INDENTATIONRIGHT); if (value != null) { section.setIndentationRight(Float.parseFloat(value + "f")); } }
@Override public void installApiDocs(List<ApiDoc> apiDocs) throws Exception { for (Chapter chapter : categoryChapters.keySet()) { ApiCategory apiCategory = categoryChapters.get(chapter); for (ApiDoc apiDoc : DocContainer.get().getApiDocQuery().queryByCategory(apiCategory.getCid())) { Section section = chapter.addSection(new Paragraph(secFont.process(apiDoc.getName()))); section.setIndentation(10); section.setIndentationLeft(10); section.setBookmarkOpen(false); section.setNumberStyle(Section.NUMBERSTYLE_DOTTED_WITHOUT_FINAL_DOT); //desc section.add(new Paragraph(textFont.process(apiDoc.getDesc()))); //summary addSummary(section, apiDoc); //parameters addParameters(section, apiDoc); //Result addResult(section, apiDoc); //ResultExample addResultExample(section, apiDoc); } document.add(chapter); } }
@Override public void installApiTypeDocs(List<ApiTypeDoc> apiTypeDocs) throws Exception { Paragraph paRefType = new Paragraph(chFont.process("引用类型参考\n")); Chapter chapter = new Chapter(paRefType, categoryChapters.size()+1); for (ApiTypeDoc apiTypeDoc : apiTypeDocs) { Phrase chunk = secFont.process(apiTypeDoc.getName()); ItextUtil.setLocalDestination(chunk, REFTYPE_LINK_PREFIX + apiTypeDoc.getName()); Section section = chapter.addSection(new Paragraph(chunk)); section.setIndentation(10); section.setIndentationLeft(10); section.setBookmarkOpen(false); section.setNumberStyle(Section.NUMBERSTYLE_DOTTED_WITHOUT_FINAL_DOT); //desc section.add(new Paragraph(textFont.process(apiTypeDoc.getDesc()))); //属性 addTypeAttrs(section, apiTypeDoc.getAttrs()); } document.add(chapter); }
private void addSummary(Section section, ApiDoc apiDoc) throws Exception { Paragraph summary = new Paragraph(itemFont.process("★简要信息")); summary.setSpacingBefore(6f); summary.setSpacingAfter(8f); List<String[]> rowDatas = new ArrayList<String[]>(); rowDatas.add(new String[]{apiDoc.getMapping()}); rowDatas.add(new String[]{apiDoc.getAuthor()}); rowDatas.add(new String[]{apiDoc.getVersion()}); PdfPTable table = DefaultPdfTable.get().create(TBDirection.Vertical, rowDatas.size(), new String[]{"映射地址", "作者", "版本"}, new int[]{1, 10}); DefaultPdfTable.get().setDataAlignments(new int[]{Element.ALIGN_LEFT}); DefaultPdfTable.get().setRowDatas(rowDatas); Paragraph wrapperTable = new Paragraph(); wrapperTable.setIndentationLeft(12f); wrapperTable.add(table); section.add(summary); section.add(wrapperTable); }
private void addParameters(Section section, ApiDoc apiDoc) throws Exception { Paragraph inputParams = new Paragraph(itemFont.process("★输入参数")); inputParams.setSpacingBefore(6f); inputParams.setSpacingAfter(8f); List<String[]> rowDatas = new ArrayList<String[]>(); for (ParamDoc paramDoc : apiDoc.getParams()) { String required = paramDoc.getRequired() ? "必须" : "可选"; rowDatas.add(new String[]{paramDoc.getName(), paramDoc.getType(), required, paramDoc.getExampleValue(), paramDoc.getDefaultValue(), paramDoc.getDesc()}); } PdfPTable paramTable = DefaultPdfTable.get().create(TBDirection.Horizontal, rowDatas.size()+1, new String[]{"名称", "类型", "是否必须", "示例值", "默认值", "描述"}, new int[]{15, 10, 10, 15, 10, 40}); DefaultPdfTable.get().setDataAlignments(new int[]{Element.ALIGN_CENTER, Element.ALIGN_CENTER, Element.ALIGN_CENTER, Element.ALIGN_CENTER, Element.ALIGN_CENTER, Element.ALIGN_LEFT}); DefaultPdfTable.get().setRowDatas(rowDatas); Paragraph wrapperTable = new Paragraph(); wrapperTable.setIndentationLeft(12f); wrapperTable.add(paramTable); section.add(inputParams); section.add(wrapperTable); }
private void addResultExample(Section section, ApiDoc apiDoc) throws Exception { Paragraph result = new Paragraph(itemFont.process("★返回示例")); // result.setLeading(6f); result.setSpacingBefore(6f); result.setSpacingAfter(8f); PdfPTable table = new PdfPTable(1); table.setWidthPercentage(95f); table.setHorizontalAlignment(Element.ALIGN_LEFT); PdfPCell pdfPCell = table.getDefaultCell(); pdfPCell.setHorizontalAlignment(Element.ALIGN_LEFT); pdfPCell.setBackgroundColor(Color.LIGHT_GRAY); if (apiDoc.getResultDoc() != null && apiDoc.getResultExample() != null) { pdfPCell.setPhrase(DefaultPdfTable.tableBSelector.process(apiDoc.getResultExample())); } table.addCell(pdfPCell); Paragraph wrapperTable = new Paragraph(); wrapperTable.setSpacingAfter(8f); wrapperTable.setIndentationLeft(12f); wrapperTable.add(table); section.add(result); section.add(wrapperTable); }
protected void addImage(Image img) throws EmptyStackException { // if there is an element on the stack... Object current = stack.pop(); // ...and it's a Chapter or a Section, the Image can be // added directly if (current instanceof Chapter || current instanceof Section || current instanceof Cell) { ((TextElementArray) current).add(img); stack.push(current); return; } // ...if not, we need to to a lot of stuff else { Stack newStack = new Stack(); while (!(current instanceof Chapter || current instanceof Section || current instanceof Cell)) { newStack.push(current); if (current instanceof Anchor) { img.setAnnotation(new Annotation(0, 0, 0, 0, ((Anchor) current).getReference())); } current = stack.pop(); } ((TextElementArray) current).add(img); stack.push(current); while (!newStack.empty()) { stack.push(newStack.pop()); } return; } }
/** * Writes the HTML representation of a section. * * @param section the section to write * @param indent the indentation * @throws IOException */ protected void writeSection(Section section, int indent) throws IOException { if (section.getTitle() != null) { int depth = section.getDepth() - 1; if (depth > 5) { depth = 5; } Properties styleAttributes = new Properties(); if (section.getTitle().hasLeading()) styleAttributes.setProperty(Markup.CSS_KEY_LINEHEIGHT, section.getTitle().getTotalLeading() + "pt"); // start tag addTabs(indent); writeStart(HtmlTags.H[depth]); write(section.getTitle().getFont(), styleAttributes); String alignment = HtmlEncoder.getAlignment(section.getTitle().getAlignment()); if (!"".equals(alignment)) { write(HtmlTags.ALIGN, alignment); } writeMarkupAttributes(markup); os.write(GT); currentfont.push(section.getTitle().getFont()); // contents for (Iterator i = section.getTitle().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 HTML representation of a section. * * @param section * the section to write * @param indent * the indentation * @throws IOException */ protected void writeSection(Section section, int indent) throws IOException { if (section.getTitle() != null) { int depth = section.getDepth() - 1; if (depth > 5) { depth = 5; } Properties styleAttributes = new Properties(); if (section.getTitle().hasLeading()) styleAttributes.setProperty(Markup.CSS_KEY_LINEHEIGHT, section.getTitle().getTotalLeading() + "pt"); // start tag addTabs(indent); writeStart(HtmlTags.H[depth]); write(section.getTitle().getFont(), styleAttributes); String alignment = HtmlEncoder.getAlignment(section.getTitle().getAlignment()); if (!"".equals(alignment)) { write(HtmlTags.ALIGN, alignment); } writeMarkupAttributes(markup); os.write(GT); currentfont.push(section.getTitle().getFont()); // contents for (Iterator i = section.getTitle().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); } }
/** * Creates a Section object based on a list of properties. * @param attributes * @return a Section */ public static Section getSection(Section parent, Properties attributes) { Section section = parent.addSection(""); setSectionParameters(section, attributes); return section; }
private void addResult(Section section, ApiDoc apiDoc) throws Exception { Paragraph result = new Paragraph(itemFont.process("★返回结果")); result.setSpacingBefore(6f); result.setSpacingAfter(8f); List<String[]> rowDatas = new ArrayList<String[]>(); if (apiDoc.getResultDoc() != null) { rowDatas.add(new String[]{null, null, apiDoc.getResultDoc().getExampleValue(), apiDoc.getResultDoc().getDesc()}); } PdfPTable resultTable = DefaultPdfTable.get().create(TBDirection.Horizontal, rowDatas.size()+1, new String[]{"类型", "引用类型", "示例值", "描述"}, new int[]{15, 30, 15, 40}); DefaultPdfTable.get().setDataAlignments(new int[]{Element.ALIGN_CENTER, Element.ALIGN_CENTER, Element.ALIGN_CENTER, Element.ALIGN_LEFT}); DefaultPdfTable.get().setRowDatas(rowDatas); if (apiDoc.getResultDoc() != null) { //type Phrase phraseType = DefaultPdfTable.tableBSelector.process(apiDoc.getResultDoc().getType()); if (!apiDoc.getResultDoc().getIsSimpleType()) { ItextUtil.setLocalGoto(phraseType, REFTYPE_LINK_PREFIX + apiDoc.getResultDoc().getType()); } DefaultPdfTable.get().getCell(resultTable, 1, 0).setPhrase(phraseType); //refType Phrase refTypePhrase = new Phrase(); for (RefTypeDoc refTypeDoc : apiDoc.getResultDoc().getRefTypes()) { Phrase chunk = DefaultPdfTable.tableBSelector.process(refTypeDoc.getName() + " "); if (!refTypeDoc.getIsSimpleType()) { ItextUtil.setLocalGoto(chunk, REFTYPE_LINK_PREFIX + refTypeDoc.getName()); } refTypePhrase.add(chunk); } DefaultPdfTable.get().getCell(resultTable, 1, 1).setPhrase(refTypePhrase); } Paragraph wrapperTable = new Paragraph(); wrapperTable.setIndentationLeft(12f); wrapperTable.add(resultTable); section.add(result); section.add(wrapperTable); }