use of de.knowwe.kdom.defaultMarkup.DefaultMarkupType in project d3web-KnowWE by denkbares.
the class SparqlContentRenderer method render.
private void render(Section<?> section, UserContext user, RenderResult result, boolean renderPreview) {
Section<SparqlType> sparqlTypeSection = Sections.cast(section, SparqlType.class);
Section<DefaultMarkupType> markupSection = Sections.ancestor(section, DefaultMarkupType.class);
Rdf2GoCore core = Rdf2GoUtils.getRdf2GoCore(user, markupSection);
if (core == null) {
// we render an empty div, otherwise the ajax rerendering does not
// work properly
result.appendHtmlElement("div", "");
return;
}
/*
* Show query text above of query result
*/
String showQueryFlag = DefaultMarkupType.getAnnotation(markupSection, SparqlMarkupType.RENDER_QUERY);
if ("true".equalsIgnoreCase(showQueryFlag)) {
// we need an opening html element around all the content as for
// some reason the ajax insert only inserts one (the first) html
// element into the page
result.appendHtml("<div>");
// render query text
result.appendHtml("<span>");
DelegateRenderer.getInstance().render(section, user, result);
result.appendHtml("</span>");
}
String sparqlString = Rdf2GoUtils.createSparqlString(core, section.getText());
if (sparqlString.toLowerCase().startsWith("construct")) {
final Set<Statement> statementsFromCache = core.getStatementsFromCache(section);
if (!statementsFromCache.isEmpty()) {
final SparqlResultRenderer sparqlResultRenderer = SparqlResultRenderer.getInstance();
int limit = 20;
int count = 0;
result.append("(" + statementsFromCache.size() + " statements constructed)");
result.appendHtml("<table>");
for (Statement statement : statementsFromCache) {
count++;
if (count > limit) {
// TODO: implement pagination and remove limit
result.appendHtml("<tr>");
int moreStatements = statementsFromCache.size() - limit;
result.append("\n(" + moreStatements + " statements hidden)");
result.appendHtml("</tr>");
break;
}
result.appendHtml("<tr>");
result.appendHtml("<td>");
final Resource subject = statement.getSubject();
result.appendHtml(sparqlResultRenderer.renderNode(subject, "", false, user, core, HTML));
result.appendHtml("</td>");
result.appendHtml("<td>");
final IRI predicate = statement.getPredicate();
result.appendHtml(sparqlResultRenderer.renderNode(predicate, "", false, user, core, HTML));
result.appendHtml("</td>");
result.appendHtml("<td>");
final Value object = statement.getObject();
result.appendHtml(sparqlResultRenderer.renderNode(object, "", false, user, core, HTML));
result.appendHtml("</td>");
result.appendHtml("</tr>");
}
result.appendHtml("</table>");
} else {
result.append("(No statements constructed)");
}
} else if (sparqlString.toLowerCase().startsWith("ask")) {
// process sparql ask query
RenderOptions opts = sparqlTypeSection.get().getRenderOptions(sparqlTypeSection, user);
try {
String query = sparqlTypeSection.get().getSparqlQuery(sparqlTypeSection, user);
boolean askResult = opts.getRdf2GoCore().sparqlAsk(query, new Rdf2GoCore.Options().timeout(opts.getTimeout()));
result.appendHtml("<div class='sparqlAsk' sparqlSectionId='" + opts.getId() + "'>");
if (opts.isBorder())
result.appendHtml("<div class='border'>");
result.append(Boolean.valueOf(askResult).toString());
if (opts.isBorder())
result.appendHtml("</div>");
result.appendHtml("</div>");
} catch (RuntimeException e) {
SparqlResultRenderer.handleRuntimeException(sparqlTypeSection, user, result, e);
}
} else {
SparqlResultRenderer.getInstance().renderSparqlResult(sparqlTypeSection, user, result, renderPreview);
}
if ("true".equalsIgnoreCase(showQueryFlag)) {
// we need an opening html element around all the content as
// for some reason the ajax insert only inserts one (the
// first) html element into the page
result.appendHtml("</div>");
}
}
use of de.knowwe.kdom.defaultMarkup.DefaultMarkupType in project d3web-KnowWE by denkbares.
the class Plugins method addAnnotations.
public static void addAnnotations(Type type, Type[] path) {
if (type instanceof DefaultMarkupType) {
DefaultMarkupType markupType = (DefaultMarkupType) type;
List<Type> childrenTypes = markupType.getChildrenTypes();
DefaultMarkup markup = markupType.getMarkup();
Extension[] extensions = PluginManager.getInstance().getExtensions(EXTENDED_PLUGIN_ID, EXTENDED_POINT_Annotation);
for (Extension extension : ScopeUtils.getMatchingExtensions(extensions, path)) {
// add the annotation itself to the markup
String regex = extension.getParameter("regex");
String name = extension.getName();
if (Strings.isBlank(regex)) {
markup.addAnnotation(name, false);
} else {
markup.addAnnotation(name, false, regex);
}
// add the documentation from the plugin definition
markup.getAnnotation(name).setDocumentation(extension.getParameter("description"));
// add children type(s) defined as class attribute(s)
for (String subTypeClass : extension.getParameters("class")) {
Type subType = (Type) getNewInstance(extension, subTypeClass);
markup.addAnnotationContentType(name, subType);
}
// set renderer for the annotation
String renderer = extension.getParameter("renderer");
if (!Strings.isBlank(renderer)) {
Renderer pluggedRenderer = (Renderer) getNewInstance(extension, renderer);
markup.addAnnotationRenderer(name, pluggedRenderer);
}
for (Type childTyp : childrenTypes) {
if (childTyp.getClass().equals(UnknownAnnotationType.class)) {
Annotation annotation = markup.getAnnotation(name);
type.addChildType(new AnnotationType(annotation));
break;
}
}
}
}
}
use of de.knowwe.kdom.defaultMarkup.DefaultMarkupType in project d3web-KnowWE by denkbares.
the class TestCaseUtils method getTestCaseProviders.
public static List<ProviderTriple> getTestCaseProviders(String web, String... kbPackages) {
PackageManager packageManager = KnowWEUtils.getPackageManager(web);
List<ProviderTriple> providers = new LinkedList<>();
for (String kbPackage : kbPackages) {
Collection<Section<?>> sectionsInPackage = packageManager.getSectionsOfPackage(kbPackage);
for (Section<?> section : sectionsInPackage) {
if (!(section.get() instanceof DefaultMarkupType))
continue;
D3webCompiler compiler = D3webUtils.getCompiler(section);
TestCaseProviderStorage testCaseProviderStorage = getTestCaseProviderStorage(compiler, section);
if (testCaseProviderStorage == null)
continue;
for (TestCaseProvider testCaseProvider : testCaseProviderStorage.getTestCaseProviders()) {
providers.add(new ProviderTriple(testCaseProvider, section, compiler.getCompileSection()));
}
}
}
return providers;
}
use of de.knowwe.kdom.defaultMarkup.DefaultMarkupType in project d3web-KnowWE by denkbares.
the class MatchingAttachmentsRenderer method render.
@Override
public void render(Section<?> section, UserContext user, RenderResult string) {
Section<DefaultMarkupType> defaultMarkupSection;
if (section.get() instanceof DefaultMarkupType) {
defaultMarkupSection = Sections.cast(section, DefaultMarkupType.class);
} else {
defaultMarkupSection = Sections.ancestor(section, DefaultMarkupType.class);
}
String[] annotations = DefaultMarkupType.getAnnotations(defaultMarkupSection, ANNOTATION_FILE);
Set<String> attachments = new TreeSet<>();
for (String s : annotations) {
try {
for (WikiAttachment attachment : KnowWEUtils.getAttachments(section.getTitle(), s)) {
attachments.add(attachment.getPath());
}
} catch (IOException e) {
}
}
if (!attachments.isEmpty()) {
string.append("The following attachments match the specified regular expression(s): \n");
for (String s : attachments) {
string.append("# " + s + "\n");
}
string.append("\n");
}
}
use of de.knowwe.kdom.defaultMarkup.DefaultMarkupType in project d3web-KnowWE by denkbares.
the class ArticleHasMarkupErrorsTest method execute.
@Override
public Message execute(Article article, String[] args2, String[]... ignores) throws InterruptedException {
// list all ignored articles
List<String> ignoredArticles = Stream.of(ignores).flatMap(Stream::of).map(Strings::unquote).collect(Collectors.toList());
// when this one is ignored -> go on
if (ignoredArticles.contains(article.getTitle()))
return new Message(Message.Type.SUCCESS);
// collect messages for erroneous markups
Set<String> messages = new TreeSet<>(NumberAwareComparator.CASE_INSENSITIVE);
List<MessageObject> messageObjects = new ArrayList<>();
List<Section<?>> sections = article.getRootSection().getChildren();
for (Section<?> sec : sections) {
// Check for nested markups
messageObjects.add(new MessageObject(sec.getTitle(), sec.get().getClass()));
String text = sec.getText().trim();
text = text.replaceAll("%%\\(color:.+%%", "");
if (text.matches("%%[\\s\\S]+%%[\\s\\S]+")) {
messages.add("In article [" + article.getTitle() + "] the section '" + sec.get().getName() + "' seems to have a nested markup.");
continue;
}
// section is actually perceived as a markup -> it is correct and we can go to the next
if (sec.get() instanceof DefaultMarkupType)
continue;
// text is only a % -> this might be a mistake ?
if ("%".equals(text)) {
messages.add("In article [" + article.getTitle() + "] a lonely % is floating around. This may be a mistake");
continue;
}
// text is not a markup -> we can go to the next
if (!text.startsWith("%%") && !text.endsWith("%"))
continue;
if (text.startsWith(("@progress")))
continue;
// Markup is in one line, then no closing % is needed -> we can go to the next
if (text.startsWith("%%") && !text.contains(System.getProperty("line.separator")))
continue;
// Check for missing % at the beginning or end of the markup
if (!text.matches("%%[\\s\\S]+%")) {
messages.add("In article [" + article.getTitle() + "] the section '" + sec.get().getName() + "' seems to have a wrong %-Markup. Did you start with '%%' and close the Markup with '%'?");
}
}
return getMessage(messageObjects, messages, Message.Type.ERROR);
}
Aggregations