Skip to content

Commit

Permalink
Create specific class to manage a map of URI prefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian Mendez committed Jun 10, 2017
1 parent 7b276fb commit 26b3af6
Show file tree
Hide file tree
Showing 10 changed files with 217 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ public interface ParserConstant {
String LINE_CONTINUATION_SYMBOL = "\\";
String NEW_LINE = "\n";
String NEW_RECORD_TOKEN = "new";
String PREFIX_AMPERSAND = "&";
String PREFIX_MAP_TOKEN = "prefix";
String PREFIX_SEMICOLON = ";";
String PREFIX_SIGN = ":";
String SORTING_ORDER_DECLARATION_TOKEN = "order";
String SPACE = " ";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import de.tudresden.inf.lat.tabula.datatype.StringValue;
import de.tudresden.inf.lat.tabula.datatype.URIType;
import de.tudresden.inf.lat.tabula.datatype.URIValue;
import de.tudresden.inf.lat.tabula.table.PrefixMap;
import de.tudresden.inf.lat.tabula.table.PrefixMapImpl;
import de.tudresden.inf.lat.tabula.table.RecordImpl;
import de.tudresden.inf.lat.tabula.table.TableImpl;
import de.tudresden.inf.lat.tabula.table.TableMap;
Expand Down Expand Up @@ -121,8 +123,8 @@ private URI asUri(String uriStr, int lineCounter) {
}
}

public OptMap<URI, URI> parsePrefixMap(String line, int lineCounter) {
OptMap<URI, URI> ret = new OptMapImpl<>(new TreeMap<>());
public PrefixMap parsePrefixMap(String line, int lineCounter) {
PrefixMap ret = new PrefixMapImpl();
StringTokenizer stok = new StringTokenizer(getValue(line).get());
while (stok.hasMoreTokens()) {
String token = stok.nextToken();
Expand Down Expand Up @@ -177,24 +179,7 @@ public boolean isNewRecord(String line) {
return Objects.nonNull(line) && line.trim().startsWith(ParserConstant.NEW_RECORD_TOKEN);
}

public URIValue expandUri(URIValue value, OptMap<URI, URI> prefixMap, int lineCounter) {
URIValue ret = value;
String valueStr = value.render();
if (valueStr.startsWith(ParserConstant.PREFIX_AMPERSAND)) {
int pos = valueStr.indexOf(ParserConstant.PREFIX_SEMICOLON, ParserConstant.PREFIX_AMPERSAND.length());
if (pos != -1) {
URI prefix = asUri(valueStr.substring(ParserConstant.PREFIX_AMPERSAND.length(), pos), lineCounter);
Optional<URI> optExpansion = prefixMap.get(prefix);
if (optExpansion.isPresent()) {
ret = new URIValue(
optExpansion.get() + valueStr.substring(pos + ParserConstant.PREFIX_SEMICOLON.length()));
}
}
}
return ret;
}

private PrimitiveTypeValue getTypedValue(String key, String value, CompositeType type0, OptMap<URI, URI> prefixMap,
private PrimitiveTypeValue getTypedValue(String key, String value, CompositeType type0, PrefixMap prefixMap,
int lineCounter) {
if (Objects.isNull(key)) {
return new StringValue();
Expand All @@ -206,14 +191,14 @@ private PrimitiveTypeValue getTypedValue(String key, String value, CompositeType
PrimitiveTypeValue ret = (new PrimitiveTypeFactory()).newInstance(typeStr, value);
if (ret.getType().equals(new URIType())) {
URIValue uri = (URIValue) ret;
ret = expandUri(uri, prefixMap, lineCounter);
ret = new URIValue(prefixMap.getWithoutPrefix(uri.getUri()));
} else if (ret instanceof ParameterizedListValue) {
ParameterizedListValue list = (ParameterizedListValue) ret;
if (list.getParameter().equals(new URIType())) {
ParameterizedListValue newList = new ParameterizedListValue(new URIType());
list.forEach(elem -> {
URIValue uri = (URIValue) elem;
newList.add(expandUri(uri, prefixMap, lineCounter));
newList.add(new URIValue(prefixMap.getWithoutPrefix(uri.getUri())));
});
ret = newList;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,35 @@
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.TreeMap;

import de.tudresden.inf.lat.tabula.common.OptMap;
import de.tudresden.inf.lat.tabula.common.OptMapImpl;
import de.tudresden.inf.lat.tabula.datatype.ParameterizedListValue;
import de.tudresden.inf.lat.tabula.datatype.PrimitiveTypeValue;
import de.tudresden.inf.lat.tabula.datatype.Record;
import de.tudresden.inf.lat.tabula.datatype.URIType;
import de.tudresden.inf.lat.tabula.parser.ParserConstant;
import de.tudresden.inf.lat.tabula.table.PrefixMap;
import de.tudresden.inf.lat.tabula.table.PrefixMapImpl;

/**
* Renderer of records in simple format.
*/
public class SimpleFormatRecordRenderer implements RecordRenderer {

private Writer output = new OutputStreamWriter(System.out);
private OptMap<URI, URI> prefixMap = new OptMapImpl<>(new TreeMap<>());
private PrefixMap prefixMap = new PrefixMapImpl();

public SimpleFormatRecordRenderer(Writer output, OptMap<URI, URI> prefixMap) {
public SimpleFormatRecordRenderer(Writer output, PrefixMap prefixMap) {
Objects.requireNonNull(output);
this.output = output;
this.prefixMap = prefixMap;
}

public SimpleFormatRecordRenderer(UncheckedWriter output, OptMap<URI, URI> prefixMap) {
public SimpleFormatRecordRenderer(UncheckedWriter output, PrefixMap prefixMap) {
Objects.requireNonNull(output);
this.output = output.asWriter();
this.prefixMap = prefixMap;
}

public String renderWithPrefix(String uriStr) {
String[] ret = new String[1];
ret[0] = uriStr;
boolean[] found = new boolean[1];
found[0] = false;
prefixMap.keySet().forEach(key -> {
String expansion = prefixMap.get(key).get().toASCIIString();
if (!found[0] && uriStr.startsWith(expansion)) {
String keyStr = key.toASCIIString();
if (keyStr.isEmpty()) {
ret[0] = "";
} else {
ret[0] = ParserConstant.PREFIX_AMPERSAND + keyStr + ParserConstant.PREFIX_SEMICOLON;
}
ret[0] += uriStr.substring(expansion.length());
found[0] = true;
}
});
return ret[0];
}

public boolean writeIfNotEmpty(UncheckedWriter output, String field, PrimitiveTypeValue value) {
if (Objects.nonNull(field) && !field.trim().isEmpty() && Objects.nonNull(value) && !value.isEmpty()) {
output.write(ParserConstant.NEW_LINE);
Expand All @@ -75,7 +53,7 @@ public boolean writeIfNotEmpty(UncheckedWriter output, String field, PrimitiveTy
output.write(ParserConstant.NEW_LINE);
output.write(ParserConstant.SPACE);
if (hasUris[0]) {
output.write(renderWithPrefix(elem));
output.write(this.prefixMap.getWithPrefix(URI.create(elem)).toASCIIString());
} else {
output.write(elem.toString());
}
Expand All @@ -84,7 +62,7 @@ public boolean writeIfNotEmpty(UncheckedWriter output, String field, PrimitiveTy
} else {
output.write(ParserConstant.SPACE);
if (value.getType().equals(new URIType())) {
output.write(renderWithPrefix(value.toString()));
output.write(this.prefixMap.getWithPrefix(URI.create(value.toString())).toASCIIString());
} else {
output.write(value.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void renderPrefixMap(UncheckedWriter output, Table table) {
output.write(ParserConstant.PREFIX_MAP_TOKEN + ParserConstant.SPACE);
output.write(ParserConstant.EQUALS_SIGN);

table.getPrefixMap().keySet().forEach(prefix -> {
table.getPrefixMap().getKeysAsStream().forEach(prefix -> {
output.write(ParserConstant.SPACE + ParserConstant.LINE_CONTINUATION_SYMBOL);
output.write(ParserConstant.NEW_LINE);
output.write(ParserConstant.SPACE);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package de.tudresden.inf.lat.tabula.table;

import java.net.URI;
import java.util.Optional;
import java.util.stream.Stream;

/**
* This models a map of URI prefixes.
*/
public interface PrefixMap {

/**
* Start of the prefix.
*/
String PREFIX_AMPERSAND = "&";

/**
* End of the prefix.
*/
String PREFIX_SEMICOLON = ";";

/**
* Returns the expansion for the given prefix.
*
* @param key
* the prefix
* @return expansion for the given prefix
*/
Optional<URI> get(URI key);

/**
* Assigns a prefix to an expansion.
*
* @param key
* prefix
* @param value
* expansion
* @return an optional containing the previous value of the given key, or
* empty if there was no value for that key
*/
Optional<URI> put(URI key, URI value);

/**
* Returns a URI with the prefix, i.e. a shortened URI.
*
* @param uri
* URI
* @return a URI with the prefix
*/
URI getWithPrefix(URI uri);

/**
* Returns a URI without the prefix, i.e. with the prefix already expanded.
*
* @param uri
* URI
* @return a URI without the prefix
*/
URI getWithoutPrefix(URI uri);

/**
* Returns an optional with the prefix for the given URI, or empty if there
* is none.
*
* @param uri
* URI
* @return an optional with the prefix for the given URI, or empty if there
* is none
*/
Optional<URI> getPrefixFor(URI uri);

/**
* Returns a stream to iterate on the keys.
*
* @return a stream to iterate on the keys
*/
Stream<URI> getKeysAsStream();

/**
* Clears the content.
*/
void clear();

/**
* Returns the size of this prefix map.
*
* @return the size of this prefix map
*/
int size();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package de.tudresden.inf.lat.tabula.table;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.TreeMap;
import java.util.stream.Stream;

import de.tudresden.inf.lat.tabula.common.OptMap;
import de.tudresden.inf.lat.tabula.common.OptMapImpl;

/**
* An object of this class is a map of URI prefixes. This implementation
* iterates on the keys keeping the order in which they were added for the first
* time.
*
*/
public class PrefixMapImpl implements PrefixMap {

private OptMap<URI, URI> prefixMap = new OptMapImpl<URI, URI>(new TreeMap<>());
private List<URI> keyList = new ArrayList<URI>();

public Optional<URI> get(URI key) {
return this.prefixMap.get(key);
}

public Optional<URI> put(URI key, URI value) {
if (!this.prefixMap.containsKey(key)) {
this.keyList.add(key);
}
return this.prefixMap.put(key, value);
}

@Override
public Optional<URI> getPrefixFor(URI uri) {
String uriStr = uri.toASCIIString();
Optional<URI> key = prefixMap.keySet().stream()
.filter((URI e) -> uriStr.startsWith(prefixMap.get(e).get().toASCIIString())).findFirst();
if (key.isPresent()) {
return Optional.of(key.get());
} else {
return Optional.empty();
}
}

@Override
public URI getWithoutPrefix(URI uri) {
URI ret = uri;
String uriStr = uri.toASCIIString();
if (uriStr.startsWith(PREFIX_AMPERSAND)) {
int pos = uriStr.indexOf(PREFIX_SEMICOLON, PREFIX_AMPERSAND.length());
if (pos != -1) {
URI prefix = URI.create(uriStr.substring(PREFIX_AMPERSAND.length(), pos));
Optional<URI> optExpansion = this.prefixMap.get(prefix);
if (optExpansion.isPresent()) {
ret = URI.create(
optExpansion.get().toASCIIString() + uriStr.substring(pos + PREFIX_SEMICOLON.length()));
}
}
}
return ret;
}

@Override
public URI getWithPrefix(URI uri) {
URI ret = uri;
Optional<URI> optPrefix = getPrefixFor(uri);
if (optPrefix.isPresent()) {
String uriStr = uri.toASCIIString();
URI key = optPrefix.get();
String keyStr = key.toASCIIString();
Optional<URI> optExpansion = get(key);
String expansionStr = optExpansion.get().toASCIIString();
if (keyStr.isEmpty()) {
ret = URI.create(uriStr.substring(expansionStr.length()));
} else {
ret = URI.create(PREFIX_AMPERSAND + keyStr + PREFIX_SEMICOLON + uriStr.substring(expansionStr.length()));
}
}
return ret;
}

@Override
public Stream<URI> getKeysAsStream() {
return this.keyList.stream();
}

@Override
public void clear() {
this.prefixMap.clear();
this.keyList.clear();
}

public int size() {
return this.prefixMap.size();
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package de.tudresden.inf.lat.tabula.table;

import java.net.URI;
import java.util.List;
import java.util.Set;

import de.tudresden.inf.lat.tabula.common.OptMap;
import de.tudresden.inf.lat.tabula.datatype.CompositeTypeValue;

/**
Expand All @@ -18,15 +16,15 @@ public interface Table extends CompositeTypeValue {
*
* @return the map of URI prefixes
*/
OptMap<URI, URI> getPrefixMap();
PrefixMap getPrefixMap();

/**
* Sets the map of URI prefixes
*
* @param prefixMap
* map of URI prefixes
*/
void setPrefixMap(OptMap<URI, URI> prefixMap);
void setPrefixMap(PrefixMap prefixMap);

/**
* Returns the sorting order for the fields.
Expand Down
Loading

0 comments on commit 26b3af6

Please sign in to comment.