Skip to content

Commit

Permalink
Merge branch 'release/28.x'
Browse files Browse the repository at this point in the history
* release/28.x:
  #1496 - Some spans are missing begin offset field
  #1511 - External recommender fails when CAS contains control characters
  #1496 - Some spans are missing begin offset field
  • Loading branch information
reckart committed May 30, 2023
2 parents c78eef6 + 61dfc54 commit 4800b99
Show file tree
Hide file tree
Showing 10 changed files with 371 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
</module>

<module name="TreeWalker">
<module name="SuppressionCommentFilter"/>

<module name="OuterTypeFilename"/>
<module name="IllegalTokenText">
<property name="tokens" value="STRING_LITERAL, CHAR_LITERAL"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
import de.tudarmstadt.ukp.inception.recommendation.imls.external.v1.model.Document;
import de.tudarmstadt.ukp.inception.recommendation.imls.external.v1.model.Metadata;
import de.tudarmstadt.ukp.inception.rendering.model.Range;
import de.tudarmstadt.ukp.inception.support.xml.sanitizer.IllegalXmlCharacterSanitizingContentHandler;

public class ExternalRecommender
extends RecommendationEngine
Expand Down Expand Up @@ -194,7 +195,7 @@ public Range predict(RecommenderContext aContext, CAS aCas, int aBegin, int aEnd
throw new RecommendationException("Error while deserializing CAS!", e);
}

return new Range(aCas);
return Range.rangeCoveringDocument(aCas);
}

private String serializeTypeSystem(CAS aCas) throws RecommendationException
Expand All @@ -210,13 +211,13 @@ private String serializeTypeSystem(CAS aCas) throws RecommendationException

private String serializeCas(CAS aCas) throws RecommendationException
{
try (StringWriter out = new StringWriter()) {
try (var out = new StringWriter()) {
// Passing "null" as the type system to the XmiCasSerializer means that we want
// to serialize all types (i.e. no filtering for a specific target type system).
XmiCasSerializer xmiCasSerializer = new XmiCasSerializer(null);
XMLSerializer sax2xml = new XMLSerializer(out, true);
xmiCasSerializer.serialize(getRealCas(aCas), sax2xml.getContentHandler(), null, null,
null);
var contentHandler = new XMLSerializer(out, true).getContentHandler();
contentHandler = new IllegalXmlCharacterSanitizingContentHandler(contentHandler);
xmiCasSerializer.serialize(getRealCas(aCas), contentHandler, null, null, null);
return out.toString();
}
catch (CASRuntimeException | SAXException | IOException e) {
Expand Down
4 changes: 4 additions & 0 deletions inception/inception-io-json/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,21 @@

import de.tudarmstadt.ukp.clarin.webanno.api.format.FormatSupport;
import de.tudarmstadt.ukp.clarin.webanno.model.Project;
import de.tudarmstadt.ukp.inception.io.jsoncas.config.LegacyUimaJsonCasFormatProperties;

public class LegacyUimaJsonFormatSupport
implements FormatSupport
{
public static final String ID = "json";
public static final String NAME = "UIMA CAS JSON (legacy)";

private final LegacyUimaJsonCasFormatProperties properties;

public LegacyUimaJsonFormatSupport(LegacyUimaJsonCasFormatProperties aProps)
{
properties = aProps;
}

@Override
public String getId()
{
Expand All @@ -57,6 +65,7 @@ public AnalysisEngineDescription getWriterDescription(Project aProject,
TypeSystemDescription aTSD, CAS aCAS)
throws ResourceInitializationException
{
return createEngineDescription(JsonWriter.class, aTSD);
return createEngineDescription(JsonWriter.class, aTSD, //
JsonWriter.PARAM_OMIT_DEFAULT_VALUES, properties.isOmitDefaultValues());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.jsoncas.config;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties("format.json-cas-legacy")
public class LegacyUimaJsonCasFormatProperties
{
private boolean omitDefaultValues = false;

public void setOmitDefaultValues(boolean aOmitDefaultValues)
{
omitDefaultValues = aOmitDefaultValues;
}

public boolean isOmitDefaultValues()
{
return omitDefaultValues;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package de.tudarmstadt.ukp.inception.io.jsoncas.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

Expand All @@ -26,14 +27,16 @@
import de.tudarmstadt.ukp.inception.io.jsoncas.UimaJsonCasFormatSupport;

@Configuration
@EnableConfigurationProperties(LegacyUimaJsonCasFormatProperties.class)
public class UimaJsonCasSupportAutoConfiguration
{
@ConditionalOnProperty(prefix = "format.json-cas-legacy", name = "enabled", //
havingValue = "true", matchIfMissing = false)
@Bean
public LegacyUimaJsonFormatSupport legacyUimaJsonFormatSupport()
public LegacyUimaJsonFormatSupport legacyUimaJsonFormatSupport(
LegacyUimaJsonCasFormatProperties aProps)
{
return new LegacyUimaJsonFormatSupport();
return new LegacyUimaJsonFormatSupport(aProps);
}

@ConditionalOnProperty(prefix = "format.json-cas", name = "enabled", //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ CAUTION: Legacy feature. To use this functionality, you need to enable it first
Support for this feature will be removed in a future version. The replacement is <<sect_formats_uimajson>>.
====


This is an old and deprecated UIMA CAS JSON format which can be exported but not imported.
It should no longer be used. Instead, one should turn to <<sect_formats_uimajson>>.

The format does support custom layers.

For more details on this format, please refer to the link:https://uima.apache.org/d/uimaj-current/references.html#ugr.ref.json[UIMA Reference Guide].

By default, the format writes all values to the JSON output, even if the values are the default values
in JSON (e.g. `0` for numbers or `false` for booleans). You can configure this behavior by setting
`format.json-cas-legacy.omit-default-values` to `true` or `false` (default) respectively.

[cols="2,1,1,1,3"]
|====
| Format | Read | Write | Custom Layers | Description
Expand All @@ -41,4 +44,3 @@ For more details on this format, please refer to the link:https://uima.apache.or
| yes
| UIMA CAS JSON (legacy)
|====

Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public class ContentHandlerAdapter

protected final ContentHandler delegate;

public ContentHandlerAdapter()
{
delegate = null;
}

public ContentHandlerAdapter(ContentHandler aDelegate)
{
delegate = aDelegate;
Expand All @@ -45,30 +50,50 @@ public ContentHandlerAdapter(ContentHandler aDelegate)
@Override
public void setDocumentLocator(Locator aLocator)
{
if (delegate == null) {
return;
}

delegate.setDocumentLocator(aLocator);
}

@Override
public void startDocument() throws SAXException
{
if (delegate == null) {
return;
}

delegate.startDocument();
}

@Override
public void endDocument() throws SAXException
{
if (delegate == null) {
return;
}

delegate.endDocument();
}

@Override
public void startPrefixMapping(String aPrefix, String aUri) throws SAXException
{
if (delegate == null) {
return;
}

delegate.startPrefixMapping(aPrefix, aUri);
}

@Override
public void endPrefixMapping(String aPrefix) throws SAXException
{
if (delegate == null) {
return;
}

delegate.endPrefixMapping(aPrefix);
}

Expand Down Expand Up @@ -117,6 +142,10 @@ public void startElement(String aLocalName, Map<String, String> aAttributes) thr
public void startElement(String aUri, String aLocalName, String aQName, Attributes aAtts)
throws SAXException
{
if (delegate == null) {
return;
}

delegate.startElement(aUri, aLocalName, aQName, aAtts);
}

Expand All @@ -133,6 +162,10 @@ public void endElement(QName aElement) throws SAXException
@Override
public void endElement(String aUri, String aLocalName, String aQName) throws SAXException
{
if (delegate == null) {
return;
}

delegate.endElement(aUri, aLocalName, aQName);
}

Expand All @@ -144,24 +177,40 @@ public void characters(String aString) throws SAXException
@Override
public void characters(char[] aCh, int aStart, int aLength) throws SAXException
{
if (delegate == null) {
return;
}

delegate.characters(aCh, aStart, aLength);
}

@Override
public void ignorableWhitespace(char[] aCh, int aStart, int aLength) throws SAXException
{
if (delegate == null) {
return;
}

delegate.ignorableWhitespace(aCh, aStart, aLength);
}

@Override
public void processingInstruction(String aTarget, String aData) throws SAXException
{
if (delegate == null) {
return;
}

delegate.processingInstruction(aTarget, aData);
}

@Override
public void skippedEntity(String aName) throws SAXException
{
if (delegate == null) {
return;
}

delegate.skippedEntity(aName);
}
}
Loading

0 comments on commit 4800b99

Please sign in to comment.