Skip to content

Commit

Permalink
Merge pull request #4309 from inception-project/feature/4308-Support-…
Browse files Browse the repository at this point in the history
…for-custom-XML-formats

#4308 - Support for custom XML formats
  • Loading branch information
reckart authored Nov 17, 2023
2 parents 5b65de5 + 0bf844d commit 64ee0f4
Show file tree
Hide file tree
Showing 10 changed files with 375 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
import org.apache.uima.fit.util.LifeCycleUtil;
import org.apache.uima.resource.ResourceInitializationException;
import org.apache.uima.resource.metadata.TypeSystemDescription;
import org.apache.wicket.request.resource.CssResourceReference;
import org.apache.wicket.request.resource.ResourceReference;
import org.dkpro.core.api.io.JCasFileWriter_ImplBase;
import org.dkpro.core.api.io.ResourceCollectionReaderBase;

Expand Down Expand Up @@ -159,7 +159,7 @@ public void close() throws IOException
/**
* @return format-specific CSS style-sheets that styleable editors should load.
*/
default List<CssResourceReference> getCssStylesheets()
default List<ResourceReference> getCssStylesheets()
{
return Collections.emptyList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import org.apache.uima.cas.CAS;
import org.apache.uima.resource.ResourceInitializationException;
import org.apache.uima.resource.metadata.TypeSystemDescription;
import org.apache.wicket.request.resource.CssResourceReference;
import org.apache.wicket.request.resource.ResourceReference;

import de.tudarmstadt.ukp.clarin.webanno.api.format.FormatSupport;
import de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument;
Expand Down Expand Up @@ -88,7 +88,7 @@ default Optional<FormatSupport> getFormatByName(String aFormatName)

FormatSupport getFallbackFormat();

default List<CssResourceReference> getFormatCssStylesheets(SourceDocument aDoc)
default List<ResourceReference> getFormatCssStylesheets(SourceDocument aDoc)
{
Optional<FormatSupport> maybeFormatSupport = getFormatById(aDoc.getFormat());
if (!maybeFormatSupport.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package de.tudarmstadt.ukp.inception.externaleditor.config;

import static de.tudarmstadt.ukp.clarin.webanno.support.JSONUtil.fromJsonStream;
import static de.tudarmstadt.ukp.clarin.webanno.support.logging.BaseLoggers.BOOT_LOG;
import static java.nio.file.Files.isDirectory;

import java.io.File;
Expand Down Expand Up @@ -91,7 +92,7 @@ public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry aRegistry)
private void registerEditorPlugin(BeanDefinitionRegistry aRegistry,
ExternalEditorPluginDescripion aDesc)
{
log.info("Loading editor plugin: {}", aDesc.getName());
BOOT_LOG.info("Loading editor plugin: {}", aDesc.getName());
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(
ExternalAnnotationEditorFactory.class,
() -> new ExternalAnnotationEditorFactory(aDesc));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public class ExternalEditorPluginDescripion
private String id;
private String factory;
private String name;
private String annotationFormat;
private String view;

private List<String> scripts = Collections.emptyList();
Expand Down Expand Up @@ -70,16 +69,6 @@ public void setName(String aName)
name = aName;
}

public String getAnnotationFormat()
{
return annotationFormat;
}

public void setAnnotationFormat(String aAnnotationFormat)
{
annotationFormat = aAnnotationFormat;
}

public String getView()
{
return view;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ for any type of editor plugin:
* `name`: the human-readable name for the editor
* `factory`: the JavaScript expression to access the annotation editor factory provided by the plugin

.Example `plugin.json` for external editor
[source,json]
----
{
"name": "My Editor (external)",
"factory": "MyEditor.factory()",
"view": "iframe:cas+xhtml+xml",
"scripts": [
"dist/MyEditor.min.js"
],
"stylesheets": [
"dist/MyEditor.min.css"
]
}
----
== Document-rendering editors

A document-rendering editor loads the document and annotation data from the backend and then renders
Expand All @@ -48,6 +63,16 @@ For document-rendering editors, the `plugin.json` file offers the following sett
followed by a path to the HTML file within the plugin. E.g. if a template file `editor.html` is next
to the `plugin.json` file in the same folder, use `plugin:editor.html`.

.Example `plugin.json` for document-rendering editor
[source,json]
----
{
"name": "My Editor (external)",
"factory": "MyEditor.factory()"
"view": "plugin:editor.html"
}
----

The external editor mechanism loads the template file within an IFrame that is embedded in the
annotation page. Any CSS or JavaScript files needed by the plugin must be referenced by the template
file using a relative location. For example let's assume a file `editor.html` which needs to load
Expand All @@ -62,16 +87,6 @@ a `editor.css` style sheet and an `editor.js` JavaScript file:
<div id="editor"/>
----

.Example `plugin.json` for document-rendering editors
[source,json]
----
{
"name": "My editor",
"factory": "MyEditor.default.factory()"
"view": "plugin:editor.html"
}
----

== Editors using server-side document views

Some annotation editors overlay their annotations on an already existing document view. For example,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.apache.uima.collection.CollectionReaderDescription;
import org.apache.uima.resource.ResourceInitializationException;
import org.apache.uima.resource.metadata.TypeSystemDescription;
import org.apache.wicket.request.resource.CssResourceReference;
import org.apache.wicket.request.resource.ResourceReference;

import de.tudarmstadt.ukp.clarin.webanno.api.format.FormatSupport;
import de.tudarmstadt.ukp.clarin.webanno.model.Project;
Expand Down Expand Up @@ -83,7 +83,7 @@ public CollectionReaderDescription getReaderDescription(Project aProject,
// }

@Override
public List<CssResourceReference> getCssStylesheets()
public List<ResourceReference> getCssStylesheets()
{
return asList(IntertextCssReference.get());
}
Expand Down
17 changes: 17 additions & 0 deletions inception/inception-io-xml/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
Expand Down Expand Up @@ -73,6 +77,19 @@
<groupId>org.dkpro.core</groupId>
<artifactId>dkpro-core-api-resources-asl</artifactId>
</dependency>

<dependency>
<groupId>org.apache.wicket</groupId>
<artifactId>wicket-core</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>

<dependency>
<groupId>commons-io</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* Licensed to the Technische Universität Darmstadt under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The Technische Universität Darmstadt
* licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.
*
* 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 de.tudarmstadt.ukp.inception.io.xml;

import static org.apache.uima.fit.factory.CollectionReaderFactory.createReaderDescription;

import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.apache.uima.collection.CollectionReaderDescription;
import org.apache.uima.resource.ResourceInitializationException;
import org.apache.uima.resource.metadata.TypeSystemDescription;
import org.apache.wicket.Application;
import org.apache.wicket.request.resource.ResourceReference;
import org.apache.wicket.resource.FileSystemResourceReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.tudarmstadt.ukp.clarin.webanno.api.format.FormatSupport;
import de.tudarmstadt.ukp.clarin.webanno.model.Project;
import de.tudarmstadt.ukp.clarin.webanno.support.WatchedResourceFile;
import de.tudarmstadt.ukp.inception.io.xml.dkprocore.XmlDocumentReader;
import de.tudarmstadt.ukp.inception.support.xml.sanitizer.PolicyCollection;
import de.tudarmstadt.ukp.inception.support.xml.sanitizer.PolicyCollectionIOUtils;

public class CustomXmlFormatFactory
implements FormatSupport
{
private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

private final CustomXmlFormatPluginDescripion description;
private final Application wicketApplication;
private final List<ResourceReference> stylesheetReferences;

private WatchedResourceFile<PolicyCollection> policyResource;

public CustomXmlFormatFactory(CustomXmlFormatPluginDescripion aDescription,
Application aWicketApplication)
{
description = aDescription;
wicketApplication = aWicketApplication;

var policyFile = description.getBasePath().resolve("policy.yaml");
try {
policyResource = new WatchedResourceFile<>(policyFile,
PolicyCollectionIOUtils::loadPolicies);
}
catch (IOException e) {
throw new IllegalStateException(e);
}

stylesheetReferences = new ArrayList<ResourceReference>();
for (var stylesheet : description.getStylesheets()) {
var stylesheetPath = description.getBasePath().resolve(stylesheet);
if (!stylesheetPath.startsWith(description.getBasePath())) {
LOG.warn("Stylesheet in custom XML format [{}] has illegal path [{}]",
description.getId(), stylesheet);
continue;
}
var ref = new FileSystemResourceReference(
CustomXmlFormatLoader.PLUGINS_XML_FORMAT_BASE_NAME + description.getId() + "/"
+ stylesheet,
stylesheetPath);
wicketApplication.getResourceReferenceRegistry().registerResourceReference(ref);
stylesheetReferences.add(ref);
}
}

@Override
public String getId()
{
return description.getId();
}

@Override
public String getName()
{
return description.getName();
}

@Override
public List<ResourceReference> getCssStylesheets()
{
return stylesheetReferences;
}

@Override
public Optional<PolicyCollection> getPolicy() throws IOException
{
return policyResource.get();
}

@Override
public CollectionReaderDescription getReaderDescription(Project aProject,
TypeSystemDescription aTSD)
throws ResourceInitializationException
{
return createReaderDescription(XmlDocumentReader.class, aTSD);
}

@Override
public boolean isReadable()
{
return true;
}
}
Loading

0 comments on commit 64ee0f4

Please sign in to comment.