public int compare(Declaration d1, Declaration d2) { if (equals(d1, d2)) return 0; SourcePosition p1 = d1.getPosition(); SourcePosition p2 = d2.getPosition(); if (p1 == null && p2 != null) return 1; else if (p1 != null && p2 == null) return -1; else if (p1 == null && p2 == null) return compareEqualPosition(d1, d2); else { assert p1 != null && p2 != null; int fileComp = p1.file().compareTo(p2.file()); if (fileComp == 0) { long diff = (long) p1.line() - (long) p2.line(); if (diff == 0) { diff = Long.signum((long) p1.column() - (long) p2.column()); if (diff != 0) return (int) diff; else { // declarations may be two // compiler-generated members with the // same source position return compareEqualPosition(d1, d2); } } else return (diff < 0) ? -1 : 1; } else return fileComp; } }
/** * Convert the given Class to an HTML. * * @param cd the class to convert. * @param outputdir the name of the directory to output to. */ public void convertClass(ClassDoc cd, DocPath outputdir) { if (cd == null) { return; } try { SourcePosition sp = cd.position(); if (sp == null) return; Reader r; // temp hack until we can update SourcePosition API. if (sp instanceof SourcePositionImpl) { FileObject fo = ((SourcePositionImpl) sp).fileObject(); if (fo == null) return; r = fo.openReader(true); } else { File file = sp.file(); if (file == null) return; r = new FileReader(file); } int lineno = 1; String line; relativePath = DocPaths.SOURCE_OUTPUT.resolve(DocPath.forPackage(cd)).invert(); Content body = getHeader(); Content pre = new HtmlTree(HtmlTag.PRE); try (LineNumberReader reader = new LineNumberReader(r)) { while ((line = reader.readLine()) != null) { addLineNo(pre, lineno); addLine(pre, line, lineno); lineno++; } } addBlankLines(pre); Content div = HtmlTree.DIV(HtmlStyle.sourceContainer, pre); body.addContent((configuration.allowTag(HtmlTag.MAIN)) ? HtmlTree.MAIN(div) : div); writeToFile(body, outputdir.resolve(DocPath.forClass(cd))); } catch (IOException e) { e.printStackTrace(); } }