org.semantictools.context.view.DefaultDocumentPrinter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of semantictools-context-renderer Show documentation
Show all versions of semantictools-context-renderer Show documentation
A library used to generate documentation for media types associated with a JSON-LD context
The newest version!
/*******************************************************************************
* Copyright 2012 Pearson Education
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package org.semantictools.context.view;
import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.semantictools.context.renderer.model.BibliographicReference;
import org.semantictools.context.renderer.model.DocumentMetadata;
import org.semantictools.context.renderer.model.Person;
import org.semantictools.context.renderer.model.ReferenceManager;
import org.semantictools.frame.api.LinkManager;
public class DefaultDocumentPrinter extends PrintEngine implements DocumentPrinter {
protected static final String TOC_MARKER = "";
protected DocumentMetadata metadata;
private ClassificationPrinter classificationPrinter;
private CaptionManager captionManager;
private Set forwardReferenceList;
private StringWriter body;
private Heading topHeading;
private Heading currentHeading;
private int h2;
private int h3;
private int figureNumber;
private int tableNumber;
public DefaultDocumentPrinter() {
this(null);
}
public DefaultDocumentPrinter(PrintContext context) {
super(context);
clear();
}
@Override
public void clear() {
topHeading = new Heading(Level.H1, "", "");
currentHeading = topHeading;
body = new StringWriter();
if (context == null) {
context = new PrintContext();
}
context.setWriter(new PrintWriter(body));
forwardReferenceList = new HashSet();
captionManager = new CaptionManager();
}
static class CaptionManager {
Map uri2FigureCaption = new HashMap();
public void add(Caption caption) {
if (caption.getUri() == null) return;
switch (caption.getType()) {
case Figure :
uri2FigureCaption.put(caption.getUri(), caption);
break;
}
}
public Caption getFigureCaptionByURI(String uri) {
return uri2FigureCaption.get(uri);
}
}
public ClassificationPrinter getClassificationPrinter() {
return classificationPrinter;
}
public void setClassificationPrinter(ClassificationPrinter classificationPrinter) {
this.classificationPrinter = classificationPrinter;
}
@Override
public PrintContext getPrintContext() {
return context;
}
@Override
public void setPrintContext(PrintContext context) {
this.context = context;
}
@Override
public void setMetadata(DocumentMetadata metadata) {
this.metadata = metadata;
}
@Override
public void printTitlePage() {
printStatus();
printLogo();
printTitle();
printSubtitle();
printClassifiers();
printDateIssued();
printLatestVersion();
printVersionHistory();
printTitlePageEditors();
printTitlePageAuthors();
printLegalNotice();
indent().println("
");
}
protected void printStatus() {
String status = metadata.getStatus();
if (status == null) return;
print("");
print(status);
println("");
}
protected void printTitlePageAuthors() {
printAuthors();
}
protected void printAuthors() {
List authorList = metadata.getAuthors();
if (authorList != null && !authorList.isEmpty()) {
indent().print("Authors");
for (Person author : authorList) {
indent().print("").print(author.getPersonName());
if (author.getOrgName() != null) {
print(", ").print(author.getOrgName());
}
println("");
}
}
}
protected void printTitlePageEditors() {
printEditors();
}
protected void printEditors() {
List editorList = metadata.getEditors();
if (editorList != null && !editorList.isEmpty()) {
indent().print("Editors");
for (Person editor : editorList) {
indent().print("").print(editor.getPersonName());
if (editor.getOrgName()!=null) {
print(", ").print(editor.getOrgName());
}
println("");
}
}
}
protected void printVersionHistory() {
Boolean historyLink = metadata.hasHistoryLink();
if (historyLink!=null && historyLink) {
indent().print("See Also: Version History");
}
}
/**
* Print document classifiers; i.e. metadata that classifies this document
* using some specialized vocabulary.
*/
protected void printClassifiers() {
if (classificationPrinter != null) {
classificationPrinter.printClassifiers();
}
}
protected void printLegalNotice() {
String legalNotice = metadata.getLegalNotice();
if (legalNotice != null) {
print(legalNotice);
}
}
protected void printLatestVersion() {
String latestVersionURI = metadata.getLatestVersionURI();
if (latestVersionURI == null) return;
indent().print("Latest version: ");
print("");
print(latestVersionURI);
println("");
}
protected void printDateIssued() {
// By default, the date issued appears in the
// subtitle. We print a separate date issued property only if
// the subtitle is declared explicitly.
if (metadata.getSubtitle() != null) {
String date = metadata.getDate();
if (date == null) return;
indent().print("Date Issued: ").print(date);
println("");
}
}
protected void printLogo() {
String logo = metadata.getLogo();
if (logo == null) return;
print("");
}
protected void printSubtitle() {
String subtitle = metadata.getSubtitle();
if (subtitle == null) {
String status = metadata.getStatus();
String date = metadata.getDate();
subtitle =
(status !=null && date !=null) ? status + " " + date :
(status != null) ? status :
(date != null) ? date :
null;
}
if (subtitle != null) {
indent().print("").print(subtitle).print("");
}
}
protected void printTitle() {
String title = metadata.getTitle();
indent().print("");
print(title);
println("
");
}
@Override
public void printFooter() {
String footer = metadata.getFooter();
if (footer == null) return;
print(footer);
}
@Override
public Heading createHeading(String text) {
text = text.trim();
if (currentHeading == null) {
currentHeading = new Heading(Level.H1, text, text.replace(' ', '_') );
return currentHeading;
}
Level level = currentHeading.getLevel().getNextLevel();
Heading result = new Heading(level, text, text.replace(' ', '_'));
currentHeading.add(result);
return result;
}
@Override
public void print(Heading heading) {
boolean showNumber = heading.isShowNumber();
Level level = heading.getLevel();
String className = heading.getClassName();
String number = null;
switch (level) {
case H2:
h2++;
number = h2 + ".";
break;
case H3:
h3++;
number = h2 + "." + h3;
break;
}
heading.setHeadingNumber(number);
indent().print("<" + level);
printAttr("id", heading.getHeadingId());
if (className != null) {
printAttr("class", className);
}
print(">");
if (showNumber) {
print(number);
print(" ");
}
print(heading.getHeadingText());
println("" + level + ">");
}
@Override
public void beginSection(Heading heading) {
print(heading);
currentHeading = heading;
}
@Override
public void endSection() {
switch (currentHeading.getLevel()) {
case H2:
h3 = 0;
break;
case H1:
h2 = 0;
}
currentHeading = currentHeading.getParent();
}
@Override
public void printTableOfContentsMarker() {
println(TOC_MARKER);
}
protected void printHeadings(List toc) {
if (toc == null)
return;
indent().print("");
pushIndent();
for (Heading heading : toc) {
if (!heading.isInToc()) continue;
String number = heading.getHeadingNumber();
String text = heading.getHeadingText();
String href = "#" + heading.getHeadingId();
indent().print("- ");
if (heading.isShowNumber()) {
print("");
print(number);
print("");
}
print(" ").print(text).print("");
println("
");
printHeadings(heading.getChildren());
}
popIndent();
println("
");
}
@Override
public void printReferences() {
printReferences(null);
}
private DefaultDocumentPrinter printReferenceAnchor(BibliographicReference r) {
String id = r.getId();
String label = r.htmlLabel();
print("");
print(label);
print("");
return this;
}
private void linkToBibliography(List list, String text) {
for (BibliographicReference ref : list) {
String id = ref.getId();
String htmlLabel = ref.htmlLabel();
String textLabel = ref.textLabel();
String link = citationLink(htmlLabel, id);
text = text.replace(htmlLabel, link);
if (!textLabel.equals(htmlLabel)) {
text = text.replace(textLabel, link);
}
}
print(text);
}
private String citationLink(String label, String id) {
StringBuilder builder = new StringBuilder();
builder.append("");
builder.append(label);
builder.append("");
return builder.toString();
}
@Override
public String getText() {
body.flush();
String text = body.toString();
text = updateReferences(text);
body.getBuffer().setLength(0);
indent().println("Table of Contents
");
printHeadings(topHeading.getChildren());
String toc = body.toString();
return text.replace(TOC_MARKER, toc);
}
@Override
public String popText() {
String text = body.toString();
body.getBuffer().setLength(0);
return text;
}
@Override
public void insertTableOfContents() {
body.flush();
String text = popText();
text = updateReferences(text);
indent().println("Table of Contents
");
printHeadings(topHeading.getChildren());
String toc = popText();
print( text.replace(TOC_MARKER, toc) );
}
private String updateReferences(String text) {
for (Caption caption : forwardReferenceList) {
String ref = createForwardRef(caption);
text = text.replace(ref, createLink(caption));
}
return text;
}
private String createForwardRef(Caption caption) {
return "";
}
@Override
public void printForwardRef(Caption caption) {
print(createForwardRef(caption));
forwardReferenceList.add(caption);
}
private String createLink(Caption caption) {
return "" + caption.getNumber()
+ "";
}
@Override
public void printLink(Caption caption) {
print(createLink(caption));
}
@Override
public Heading getCurrentHeading() {
return currentHeading;
}
@Override
public void printCaption(Caption caption) {
captionManager.add(caption);
indent().print("");
print(caption.getNumber());
print(".  ");
print(caption.getText());
indent().println("");
}
@Override
public void printFigure(String src, Caption caption) {
indent().print("");
pushIndent();
indent().print("");
if (caption != null) {
printCaption(caption);
}
popIndent();
indent().println("");
}
@Override
public void assignNumber(Caption caption) {
String number = null;
switch (caption.getType()) {
case Figure:
figureNumber++;
number = "Figure " + figureNumber;
break;
case Table:
tableNumber++;
number = "Table " + tableNumber;
}
caption.setNumber(number);
}
@Override
public void beginTable(String className) {
indent().print("");
pushIndent();
}
@Override
public void endTable() {
popIndent();
indent().println("
");
}
@Override
public void beginRow() {
indent().println("");
pushIndent();
}
@Override
public void endRow() {
popIndent();
indent().println(" ");
}
@Override
public void printTH(String value) {
indent().print("").print(value).println(" ");
}
@Override
public void printTD(String className, String text) {
indent().print("").print(text).println(" ");
}
@Override
public void printTD(String value) {
indent().print("").print(value).println(" ");
}
@Override
public void printAnchor(String href, String text) {
print("").print(text).print("");
}
@Override
public void beginDiv(String className) {
indent().print("");
pushIndent();
}
@Override
public void beginDiv(String className, String id) {
indent().print("");
pushIndent();
}
@Override
public void endDiv() {
popIndent();
indent().println("");
}
@Override
public void beginCodeSnippet() {
indent().print("");
println("");
}
@Override
public void endCodeSnippet() {
println("
");
indent().println("");
}
@Override
public void printDefinition(String termName, String description) {
indent().print("").print(termName).println(" ");
pushIndent();
indent().print("").print(description).println(" ");
popIndent();
}
@Override
public void printParagraph(String text) {
indent().print("").print(text).println("
");
}
@Override
public void beginParagraph() {
indent().print("");
}
@Override
public void endParagraph() {
println("
");
}
@Override
public void printListItem(String text) {
if (text != null) {
indent().print("").print(text).println(" ");
}
}
@Override
public void beginHTML() {
println("");
println("");
pushIndent();
printStyleSheetLink();
popIndent();
println("");
println("");
}
@Override
public void endHTML() {
println("");
println("");
}
protected void printStyleSheetLink() {
String href = metadata.getCss();
indent().print("");
}
@Override
public Heading createHeading(String text, String id) {
text = text.trim();
Level level = currentHeading.getLevel().getNextLevel();
Heading result = new Heading(level, text, id);
currentHeading.add(result);
return result;
}
@Override
public Heading createHeading(Level level, String text, String id) {
Heading heading = new Heading(level, text, id);
currentHeading.add(heading);
return heading;
}
@Override
public void printReferences(HeadingPrinter headingPrinter) {
ReferenceManager manager = metadata.getReferenceManager();
if (manager == null) {
return;
}
String text = popText();
List targetList = new ArrayList();
List list = manager.listReferences();
Collections.sort(list);
for (BibliographicReference ref : list) {
String htmlLabel = ref.htmlLabel();
String textLabel = ref.textLabel();
if (text.contains(htmlLabel) || text.contains(textLabel)) {
targetList.add(ref);
}
}
if (targetList.isEmpty()) {
print(text);
return;
}
linkToBibliography(targetList, text);
Heading heading = createHeading(Level.H2, "References", "References");
if (headingPrinter == null) {
print(heading);
} else {
headingPrinter.print(heading);
}
indent().print("");
File thisFile = metadata.getLocalFile();
LinkManager linkManager = thisFile==null ? null : new LinkManager(thisFile);
for (BibliographicReference r : targetList) {
File otherFile = r.getLocalFile();
if (otherFile != null && thisFile !=null) {
String uri = linkManager.relativize(otherFile);
r.setUri(uri);
}
String html = r.htmlText();
indent().print("- ");
printReferenceAnchor(r).println("
");
indent().print("- ").print(html).println("
");
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy