Skip to content

Commit

Permalink
Wikidata#437: WIP: half-assed attempt to overcome limitations of curr…
Browse files Browse the repository at this point in the history
…ent WDTK around Lexemes API
  • Loading branch information
62mkv committed Oct 29, 2020
1 parent 8400cdd commit b8d639c
Show file tree
Hide file tree
Showing 21 changed files with 687 additions and 366 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -5,7 +5,7 @@

<groupId>org.wikidata.wdtk</groupId>
<artifactId>wdtk-parent</artifactId>
<version>0.11.1</version>
<version>0.11.1-mkv</version>
<packaging>pom</packaging>

<name>Wikidata Toolkit</name>
Expand Down
2 changes: 1 addition & 1 deletion wdtk-datamodel/pom.xml
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.wikidata.wdtk</groupId>
<artifactId>wdtk-parent</artifactId>
<version>0.11.1</version>
<version>0.11.1-mkv</version>
</parent>

<artifactId>wdtk-datamodel</artifactId>
Expand Down
Expand Up @@ -23,16 +23,17 @@
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Collections;

import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
import com.fasterxml.jackson.databind.ser.PropertyFilter;
import com.fasterxml.jackson.databind.ser.PropertyWriter;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wikidata.wdtk.datamodel.interfaces.EntityDocument;
import org.wikidata.wdtk.datamodel.interfaces.EntityDocumentDumpProcessor;
import org.wikidata.wdtk.datamodel.interfaces.ItemDocument;
import org.wikidata.wdtk.datamodel.interfaces.LexemeDocument;
import org.wikidata.wdtk.datamodel.interfaces.MediaInfoDocument;
import org.wikidata.wdtk.datamodel.interfaces.PropertyDocument;
import org.wikidata.wdtk.datamodel.interfaces.Statement;
import org.wikidata.wdtk.datamodel.interfaces.*;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
Expand Down Expand Up @@ -71,6 +72,40 @@ public class JsonSerializer implements EntityDocumentDumpProcessor {
protected static final ObjectMapper mapper = new ObjectMapper();
static {
mapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
mapper.setFilterProvider(new SimpleFilterProvider(Collections.singletonMap("formDocumentJsonFilter", buildFormDocumentJsonFilter())));
}

private static PropertyFilter buildFormDocumentJsonFilter() {
return new SimpleBeanPropertyFilter() {
@Override
protected boolean include(BeanPropertyWriter writer) {
return true;
}

@Override
protected boolean include(PropertyWriter writer) {
return true;
}

@Override
public void serializeAsField(Object pojo, JsonGenerator jgen, SerializerProvider provider, PropertyWriter writer) throws Exception {
if (include(writer)) {
if (!writer.getName().equals("add")) {
writer.serializeAsField(pojo, jgen, provider);
} else {
FormDocument formDocument = (FormDocument) pojo;
if (FormIdValue.NULL.equals(formDocument.getEntityId())) {
writer.serializeAsField(pojo, jgen, provider);
} else {
if (!jgen.canOmitFields()) {
writer.serializeAsOmittedField(pojo, jgen, provider);
}
}
}

}
}
};
}

/**
Expand Down Expand Up @@ -184,6 +219,10 @@ public static String getJsonString(ItemDocument itemDocument) {
return jacksonObjectToString(itemDocument);
}

public static String getJsonString(LexemeDocument lexemeDocument) {
return jacksonObjectToString(lexemeDocument);
}

/**
* Serializes the given object in JSON and returns the resulting string. In
* case of errors, null is returned.
Expand Down
Expand Up @@ -9,9 +9,9 @@
* 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.
Expand All @@ -38,6 +38,7 @@
*/
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonTypeInfo(use = JsonTypeInfo.Id.NONE)
@JsonFilter("formDocumentJsonFilter")
public class FormDocumentImpl extends StatementDocumentImpl implements FormDocument {

private final List<ItemIdValue> grammaticalFeatures;
Expand Down Expand Up @@ -189,7 +190,7 @@ public boolean equals(Object obj) {
public String toString() {
return ToString.toString(this);
}

@Override
public FormDocument withRevisionId(long newRevisionId) {
return new FormDocumentImpl(getEntityId(),
Expand Down Expand Up @@ -229,4 +230,8 @@ public FormDocument withoutStatementIds(Set<String> statementIds) {
representations, grammaticalFeatures,
newGroups, revisionId);
}

public String getAdd() {
return "";
}
}
Expand Up @@ -44,7 +44,7 @@
@JsonDeserialize()
public class FormIdValueImpl extends ValueImpl implements FormIdValue {

private static final Pattern PATTERN = Pattern.compile("^L[1-9]\\d*-F[1-9]\\d*$");
private static final Pattern PATTERN = Pattern.compile("^L[0-9]\\d*-F[0-9]\\d*$");

private final String id;
private final String siteIri;
Expand Down

0 comments on commit b8d639c

Please sign in to comment.