use of annis.libgui.visualizers.VisualizerPlugin in project ANNIS by korpling.
the class AnnisBaseUI method initPlugins.
private void initPlugins() {
log.info("Adding plugins");
pluginManager = PluginManagerFactory.createPluginManager();
addCustomUIPlugins(pluginManager);
File baseDir = VaadinService.getCurrent().getBaseDirectory();
File builtin = new File(baseDir, "WEB-INF/lib/annis-visualizers-" + VersionInfo.getReleaseName() + ".jar");
if (builtin.canRead()) {
pluginManager.addPluginsFrom(builtin.toURI());
log.info("added built-in plugins from {}", builtin.getPath());
} else {
log.warn("could not find built-in plugin file {}", builtin.getPath());
}
File basicPlugins = new File(baseDir, "WEB-INF/plugins");
if (basicPlugins.isDirectory()) {
pluginManager.addPluginsFrom(basicPlugins.toURI());
log.info("added plugins from {}", basicPlugins.getPath());
}
String globalPlugins = System.getenv("ANNIS_PLUGINS");
if (globalPlugins != null) {
pluginManager.addPluginsFrom(new File(globalPlugins).toURI());
log.info("added plugins from {}", globalPlugins);
}
StringBuilder listOfPlugins = new StringBuilder();
listOfPlugins.append("loaded plugins:\n");
PluginManagerUtil util = new PluginManagerUtil(pluginManager);
for (Plugin p : util.getPlugins()) {
listOfPlugins.append(p.getClass().getName()).append("\n");
}
log.info(listOfPlugins.toString());
Collection<VisualizerPlugin> visualizers = util.getPlugins(VisualizerPlugin.class);
for (VisualizerPlugin vis : visualizers) {
visualizerRegistry.put(vis.getShortName(), vis);
resourceAddedDate.put(vis.getShortName(), new Date());
}
}
use of annis.libgui.visualizers.VisualizerPlugin in project ANNIS by korpling.
the class ShareSingleMatchGenerator method generatorURLForVisualizer.
private URI generatorURLForVisualizer(ResolverEntry entry) {
String appContext = Helper.getContext();
URI appURI = UI.getCurrent().getPage().getLocation();
UriBuilder result = UriBuilder.fromUri(appURI).replacePath(appContext).path("embeddedvis").path(Helper.encodeJersey(entry.getVisType())).fragment("");
if (entry.getNamespace() != null) {
result = result.queryParam("embedded_ns", Helper.encodeJersey(entry.getNamespace()));
}
// test if the request was made from a sub-instance
String nonContextPath = appURI.getPath().substring(appContext.length());
if (!nonContextPath.isEmpty()) {
if (nonContextPath.startsWith("/")) {
nonContextPath = nonContextPath.substring(1);
}
result = result.queryParam(EmbeddedVisUI.KEY_INSTANCE, nonContextPath);
}
UriBuilder serviceURL = UriBuilder.fromUri(Helper.getAnnisWebResource().path("query").getURI());
VisualizerPlugin visPlugin = ps.getVisualizer(entry.getVisType());
if (visPlugin != null && visPlugin.isUsingText()) {
// generate a service URL that gets the whole document
URI firstID = match.getSaltIDs().get(0);
String pathAsString = firstID.getRawPath();
List<String> path = Splitter.on('/').omitEmptyStrings().trimResults().splitToList(pathAsString);
String corpusName = path.get(0);
String documentName = path.get(path.size() - 1);
try {
corpusName = URLDecoder.decode(corpusName, "UTF-8");
documentName = URLDecoder.decode(documentName, "UTF-8");
;
} catch (UnsupportedEncodingException ex) {
log.warn("Could not decode URL", ex);
}
// apply any node annotation filters if possible
if (visPlugin instanceof FilteringVisualizerPlugin) {
List<String> visAnnos = ((FilteringVisualizerPlugin) visPlugin).getFilteredNodeAnnotationNames(corpusName, documentName, entry.getMappings());
if (visAnnos != null) {
Set<String> annos = new HashSet<>(visAnnos);
// always add the matched node annotation as well
for (String matchedAnno : match.getAnnos()) {
if (!matchedAnno.isEmpty()) {
annos.add(matchedAnno);
}
}
serviceURL = serviceURL.queryParam("filternodeanno", Joiner.on(",").join(annos));
}
}
serviceURL = serviceURL.path("graph").path(Helper.encodePath(corpusName)).path(Helper.encodePath(documentName));
// add the original match so the embedded visualizer can add it
// (since we use the graph query it will not be included in the Salt XMI itself)
result = result.queryParam(EmbeddedVisUI.KEY_MATCH, Helper.encodeJersey(match.toString()));
} else {
// default to the subgraph URL for this specific match
serviceURL = serviceURL.path("search").path("subgraph").queryParam("match", Helper.encodeJersey(match.toString())).queryParam("left", query.getLeftContext()).queryParam("right", query.getRightContext());
if (query.getSegmentation() != null) {
serviceURL = serviceURL.queryParam("segmentation", query.getSegmentation());
}
}
// add the URL where to fetch the graph from
result = result.queryParam(EmbeddedVisUI.KEY_SALT, Helper.encodeQueryParam(serviceURL.build().toASCIIString()));
// add the current view as "return back" parameter
result = result.queryParam(EmbeddedVisUI.KEY_SEARCH_INTERFACE, appURI.toASCIIString());
if (segmentation != null) {
result = result.queryParam(EmbeddedVisUI.KEY_BASE_TEXT, segmentation);
}
// add all mappings as parameter
for (String key : entry.getMappings().stringPropertyNames()) {
if (!key.startsWith(EmbeddedVisUI.KEY_PREFIX)) {
String value = Helper.encodeJersey(entry.getMappings().getProperty(key));
result = result.queryParam(key, value);
}
}
return result.build();
}
use of annis.libgui.visualizers.VisualizerPlugin in project ANNIS by korpling.
the class EmbeddedVisUI method generateVisFromRemoteURL.
private void generateVisFromRemoteURL(final String visName, final String rawUri, Map<String, String[]> args) {
try {
// find the matching visualizer
final VisualizerPlugin visPlugin = this.getVisualizer(visName);
if (visPlugin == null) {
displayMessage("Unknown visualizer \"" + visName + "\"", "This ANNIS instance does not know the given visualizer.");
return;
}
URI uri = new URI(rawUri);
// fetch content of the URI
Client client = null;
AnnisUser user = Helper.getUser();
if (user != null) {
client = user.getClient();
}
if (client == null) {
client = Helper.createRESTClient();
}
final WebResource saltRes = client.resource(uri);
displayLoadingIndicator();
// copy the arguments for using them later in the callback
final Map<String, String[]> argsCopy = new LinkedHashMap<>(args);
Background.runWithCallback(new Callable<SaltProject>() {
@Override
public SaltProject call() throws Exception {
return saltRes.get(SaltProject.class);
}
}, new FutureCallback<SaltProject>() {
@Override
public void onFailure(Throwable t) {
displayMessage("Could not query the result.", t.getMessage());
}
@Override
public void onSuccess(SaltProject p) {
// TODO: allow to display several visualizers when there is more than one document
SCorpusGraph firstCorpusGraph = null;
SDocument doc = null;
if (p.getCorpusGraphs() != null && !p.getCorpusGraphs().isEmpty()) {
firstCorpusGraph = p.getCorpusGraphs().get(0);
if (firstCorpusGraph.getDocuments() != null && !firstCorpusGraph.getDocuments().isEmpty()) {
doc = firstCorpusGraph.getDocuments().get(0);
}
}
if (doc == null) {
displayMessage("No documents found in provided URL.", "");
return;
}
if (argsCopy.containsKey(KEY_INSTANCE)) {
Map<String, InstanceConfig> allConfigs = loadInstanceConfig();
InstanceConfig newConfig = allConfigs.get(argsCopy.get(KEY_INSTANCE)[0]);
if (newConfig != null) {
setInstanceConfig(newConfig);
}
}
// now it is time to load the actual defined instance fonts
loadInstanceFonts();
// generate the visualizer
VisualizerInput visInput = new VisualizerInput();
visInput.setDocument(doc);
if (getInstanceConfig() != null && getInstanceConfig().getFont() != null) {
visInput.setFont(getInstanceFont());
}
Properties mappings = new Properties();
for (Map.Entry<String, String[]> e : argsCopy.entrySet()) {
if (!KEY_SALT.equals(e.getKey()) && e.getValue().length > 0) {
mappings.put(e.getKey(), e.getValue()[0]);
}
}
visInput.setMappings(mappings);
String[] namespace = argsCopy.get(KEY_NAMESPACE);
if (namespace != null && namespace.length > 0) {
visInput.setNamespace(namespace[0]);
} else {
visInput.setNamespace(null);
}
String baseText = null;
if (argsCopy.containsKey(KEY_BASE_TEXT)) {
String[] value = argsCopy.get(KEY_BASE_TEXT);
if (value.length > 0) {
baseText = value[0];
}
}
List<SNode> segNodes = CommonHelper.getSortedSegmentationNodes(baseText, doc.getDocumentGraph());
if (argsCopy.containsKey(KEY_MATCH)) {
String[] rawMatch = argsCopy.get(KEY_MATCH);
if (rawMatch.length > 0) {
// enhance the graph with match information from the arguments
Match match = Match.parseFromString(rawMatch[0]);
Helper.addMatchToDocumentGraph(match, doc);
}
}
Map<String, String> markedColorMap = new HashMap<>();
Map<String, String> exactMarkedMap = Helper.calculateColorsForMarkedExact(doc);
Map<String, Long> markedAndCovered = Helper.calculateMarkedAndCoveredIDs(doc, segNodes, baseText);
Helper.calulcateColorsForMarkedAndCovered(doc, markedAndCovered, markedColorMap);
visInput.setMarkedAndCovered(markedAndCovered);
visInput.setMarkableMap(markedColorMap);
visInput.setMarkableExactMap(exactMarkedMap);
visInput.setContextPath(Helper.getContext());
String template = Helper.getContext() + "/Resource/" + visName + "/%s";
visInput.setResourcePathTemplate(template);
visInput.setSegmentationName(baseText);
// TODO: which other thing do we have to provide?
Component c = visPlugin.createComponent(visInput, null);
// add the styles
c.addStyleName("corpus-font");
c.addStyleName("vis-content");
Link link = new Link();
link.setCaption("Show in ANNIS search interface");
link.setIcon(ANNISFontIcon.LOGO);
link.setVisible(false);
link.addStyleName("dontprint");
link.setTargetName("_blank");
if (argsCopy.containsKey(KEY_SEARCH_INTERFACE)) {
String[] interfaceLink = argsCopy.get(KEY_SEARCH_INTERFACE);
if (interfaceLink.length > 0) {
link.setResource(new ExternalResource(interfaceLink[0]));
link.setVisible(true);
}
}
VerticalLayout layout = new VerticalLayout(link, c);
layout.setComponentAlignment(link, Alignment.TOP_LEFT);
layout.setSpacing(true);
layout.setMargin(true);
setContent(layout);
IDGenerator.assignID(link);
}
});
} catch (URISyntaxException ex) {
displayMessage("Invalid URL", "The provided URL is malformed:<br />" + ex.getMessage());
} catch (LoginDataLostException ex) {
displayMessage("LoginData Lost", "No login data available any longer in the session:<br /> " + ex.getMessage());
} catch (UniformInterfaceException ex) {
if (ex.getResponse().getStatus() == Response.Status.FORBIDDEN.getStatusCode()) {
displayMessage("Corpus access forbidden", "You are not allowed to access this corpus. " + "Please login at the <a target=\"_blank\" href=\"" + Helper.getContext() + "\">main application</a> first and then reload this page.");
} else {
displayMessage("Service error", ex.getMessage());
}
} catch (ClientHandlerException ex) {
displayMessage("Could not generate the visualization because the ANNIS service reported an error.", ex.getMessage());
} catch (Throwable ex) {
displayMessage("Could not generate the visualization.", ex.getMessage() == null ? ("An unknown error of type " + ex.getClass().getSimpleName()) + " occured." : ex.getMessage());
}
}
Aggregations