Skip to content

Commit

Permalink
Merge branch 'make-etikettMaker-configurable'
Browse files Browse the repository at this point in the history
Conflicts:
	src/main/resources/context.json
  • Loading branch information
jschnasse committed Apr 21, 2016
2 parents ba0bb2f + ac3f228 commit acc187b
Show file tree
Hide file tree
Showing 9 changed files with 409 additions and 88 deletions.
48 changes: 47 additions & 1 deletion src/main/java/de/hbz/lobid/helper/Etikett.java
Expand Up @@ -60,7 +60,11 @@ public class Etikett {
* Describes if the given is expected to occur as a \@set or a \@list. Can be
* null;
*/
public String container = null;
String container = null;

String comment = null;

String weight = null;

/**
* The jaxb needs this
Expand Down Expand Up @@ -147,6 +151,48 @@ public void setReferenceType(String referenceType) {
this.referenceType = referenceType;
}

/**
* @return json-ld container
*/
public String getContainer() {
return container;
}

/**
* @param container json-ld container
*/
public void setContainer(String container) {
this.container = container;
}

/**
* @return a comment that describes the usage of the etikett
*/
public String getComment() {
return comment;
}

/**
* @param comment a comment that describes the usage of the etikett
*/
public void setComment(String comment) {
this.comment = comment;
}

/**
* @return a weight to order a list of etiketts
*/
public String getWeight() {
return weight;
}

/**
* @param weight can be used to order a list of etiketts
*/
public void setWeight(String weight) {
this.weight = weight;
}

@Override
public String toString() {
try {
Expand Down
128 changes: 87 additions & 41 deletions src/main/java/de/hbz/lobid/helper/EtikettMaker.java
Expand Up @@ -19,6 +19,7 @@ License, or (at your option) any later version.

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -33,17 +34,21 @@ License, or (at your option) any later version.
* @author Jan Schnasse
*
*/
public class EtikettMaker {
public class EtikettMaker implements EtikettMakerInterface {

private static final String TYPE = "type";

private static final String ID = "id";

final static Logger logger = LoggerFactory.getLogger(EtikettMaker.class);

/**
* A map with URIs as key and labels,icons, shortnames as values
* A map with URIs as key
*/
Map<String, Etikett> pMap = new HashMap<>();

/**
* A map with Shortnames as key and labels,icons, uris as values
* A map with Shortnames as key
*/
Map<String, Etikett> nMap = new HashMap<>();

Expand All @@ -63,44 +68,50 @@ public class EtikettMaker {

/**
* The profile provides a json context an labels
*
* @param labelIn input stream to a labels file
*/
public EtikettMaker() {
public EtikettMaker(InputStream labelIn) {
initMaps(labelIn);
initContext();
initMaps();
}

/**
* @return a map with a json-ld context
/*
* (non-Javadoc)
*
* @see de.hbz.lobid.helper.EtikettMakerInterface#getContext()
*/
@Override
public Map<String, Object> getContext() {
return context;
}

/**
* @param key the uri
* @return an etikett object contains uri, icon, label, jsonname,
* referenceType
/*
* (non-Javadoc)
*
* @see de.hbz.lobid.helper.EtikettMakerInterface#getEtikett(java.lang.String)
*/
public Etikett getEtikett(String key) {
Etikett e = pMap.get(key);
@Override
public Etikett getEtikett(String uri) {
Etikett e = pMap.get(uri);
if (e == null) {
e = new Etikett(key);
e.name = getJsonName(key);
e = new Etikett(uri);
e.name = getJsonName(uri);
}
if (e.label == null) {
if (e.label == null || e.label.isEmpty()) {
e.label = e.uri;
}
logger.debug("Find name for " + key + " : " + e.name);
logger.debug("Find etikett for " + uri + " : " + e.name);
return e;
}

private void initContext() {
context = createContext("context.json");
context = createContext();
}

private void initMaps() {
private void initMaps(InputStream labelIn) {
try {
labels = createLabels("labels.json");
labels = createLabels(labelIn);

for (Etikett etikett : labels) {
pMap.put(etikett.uri, etikett);
Expand All @@ -112,11 +123,11 @@ private void initMaps() {

}

private static List<Etikett> createLabels(String fileName) {
private static List<Etikett> createLabels(InputStream labelIn) {
logger.info("Create labels....");
List<Etikett> result = new ArrayList<>();

result = loadFile(fileName, new ObjectMapper().getTypeFactory()
result = loadFile(labelIn, new ObjectMapper().getTypeFactory()
.constructCollectionType(List.class, Etikett.class));

if (result == null) {
Expand All @@ -127,28 +138,32 @@ private static List<Etikett> createLabels(String fileName) {
return result;
}

/**
* @return a Map representing additional information about the shortnames used
* in getLd
*/
Map<String, Object> createContext(String fileName) {
logger.info("Create context....");
Map<String, Object> result = new HashMap<>();

result = loadFile(fileName, new ObjectMapper().getTypeFactory()
.constructMapLikeType(HashMap.class, String.class, Object.class));

if (result == null) {
logger.info("...not succeeded!");
} else {
logger.info("...succeed!");
Map<String, Object> createContext() {
Map<String, Object> pmap;
Map<String, Object> cmap = new HashMap<>();
for (Etikett l : labels) {
if ("class".equals(l.referenceType) || l.referenceType == null
|| l.name == null)
continue;
pmap = new HashMap<>();
pmap.put("@id", l.uri);
if (!"String".equals(l.referenceType)) {
pmap.put("@type", l.referenceType);
}
if (l.container != null) {
pmap.put("@container", l.container);
}
cmap.put(l.name, pmap);
}
return result;
cmap.put(ID, "@id");
cmap.put(TYPE, "@type");
Map<String, Object> contextObject = new HashMap<>();
contextObject.put("@context", cmap);
return contextObject;
}

private static <T> T loadFile(String fileName, TypeBase type) {
try (InputStream in = Thread.currentThread().getContextClassLoader()
.getResourceAsStream(fileName)) {
private static <T> T loadFile(InputStream labelIn, TypeBase type) {
try (InputStream in = labelIn) {
return new ObjectMapper().readValue(in, type);
} catch (Exception e) {
throw new RuntimeException("Error during initialization!", e);
Expand All @@ -168,4 +183,35 @@ String getJsonName(String predicate) {
}
return e.name;
}

@Override
public Etikett getEtikettByName(String name) {
return nMap.get(name);
}

@Override
public Collection<Etikett> getValues() {
return pMap.values();
}

@Override
public boolean supportsLabelsForValues() {
return false;
}

@Override
public String getIdAlias() {
return ID;
}

@Override
public String getTypeAlias() {
return TYPE;
}

@Override
public String getLabelKey() {
return null;
}

}
75 changes: 75 additions & 0 deletions src/main/java/de/hbz/lobid/helper/EtikettMakerInterface.java
@@ -0,0 +1,75 @@
/*Copyright (c) 2016 "hbz"
This file is part of lobid-rdf-to-json.
etikett is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.hbz.lobid.helper;

import java.util.Collection;
import java.util.Map;

/**
* @author Jan Schnasse
*
*/
public interface EtikettMakerInterface {

/**
* @return a map with a json-ld context
*/
Map<String, Object> getContext();

/**
* @param uri the uri
* @return an etikett object contains uri, icon, label, jsonname,
* referenceType
*/
Etikett getEtikett(String uri);

/**
* @param name the json name
* @return an etikett object contains uri, icon, label, jsonname,
* referenceType
*/
Etikett getEtikettByName(String name);

/**
* @return a list of all Etiketts
*/
Collection<Etikett> getValues();

/**
* @return true if the implementor provides etiketts for all kind of values
* (uris on object position). false if the implementor provides
* etiketts for fields (uris in predicate position) only.
*/
boolean supportsLabelsForValues();

/**
* @return the idAlias is used to substitute all occurrences of "\@id"
*/
public String getIdAlias();

/**
* @return the typeAlias is used to substitute all occurrences of "\@type"
*/
public String getTypeAlias();

/**
* @return returns a json key that is used to store label info
*/
public String getLabelKey();

}
31 changes: 0 additions & 31 deletions src/main/java/de/hbz/lobid/helper/Globals.java

This file was deleted.

0 comments on commit acc187b

Please sign in to comment.