Skip to content

Commit

Permalink
add some docs to getUriTemplateString
Browse files Browse the repository at this point in the history
  • Loading branch information
ghxiao committed Feb 24, 2016
1 parent c9c3aa0 commit 478c9d9
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 74 deletions.
149 changes: 75 additions & 74 deletions obdalib-core/src/main/java/it/unibz/krdb/obda/utils/URITemplates.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,112 +24,113 @@
import it.unibz.krdb.obda.exception.InvalidPrefixWritingException;
import it.unibz.krdb.obda.io.PrefixManager;
import it.unibz.krdb.obda.model.Function;
import it.unibz.krdb.obda.model.Term;
import it.unibz.krdb.obda.model.ValueConstant;
import it.unibz.krdb.obda.model.Variable;
import it.unibz.krdb.obda.model.impl.TermUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
* A utility class for URI templates
*
* @author xiao
*
* @author xiao
*/
public class URITemplates {

private static final String PLACE_HOLDER = "{}";
private static final int PLACE_HOLDER_LENGTH = PLACE_HOLDER.length();

/**
* This method instantiates the input uri template by arguments
*
* <p>
*
* Example:
* <p>
*
* If {@code args = ["A", 1]}, then
*
* {@code URITemplates.format("http://example.org/{}/{}", args)}
* results {@code "http://example.org/A/1" }
*
*
* @see #format(String, Object...)
*
* @param uriTemplate
* @param args
* @return
*/
public static String format(String uriTemplate, Collection<?> args) {

StringBuilder sb = new StringBuilder();

int beginIndex = 0;

for(Object arg : args){

int endIndex = uriTemplate.indexOf(PLACE_HOLDER, beginIndex);

sb.append(uriTemplate.subSequence(beginIndex, endIndex)).append(arg);

beginIndex = endIndex + PLACE_HOLDER_LENGTH;
}

sb.append(uriTemplate.substring(beginIndex));

return sb.toString();

}

/**
* This method instantiates the input uri template by arguments
*
* <p>
*
* Example:
* <p>
*
* {@code URITemplates.format("http://example.org/{}/{}", "A", 1)} results {@code "http://example.org/A/1" }
*
* @param uriTemplate
* @param args
* @return
*/
public static String format(String uriTemplate, Object... args) {
return format(uriTemplate, Arrays.asList(args));
}

private static final String PLACE_HOLDER = "{}";
private static final int PLACE_HOLDER_LENGTH = PLACE_HOLDER.length();

/**
* This method instantiates the input uri template by arguments
* <p>
* Example:
* <p>
* If {@code args = ["A", 1]}, then
* <p>
* {@code URITemplates.format("http://example.org/{}/{}", args)}
* results {@code "http://example.org/A/1" }
*
* @see #format(String, Object...)
*/
public static String format(String uriTemplate, Collection<?> args) {

StringBuilder sb = new StringBuilder();

int beginIndex = 0;

for (Object arg : args) {

int endIndex = uriTemplate.indexOf(PLACE_HOLDER, beginIndex);

sb.append(uriTemplate.subSequence(beginIndex, endIndex)).append(arg);

beginIndex = endIndex + PLACE_HOLDER_LENGTH;
}

sb.append(uriTemplate.substring(beginIndex));

return sb.toString();

}

/**
* This method instantiates the input uri template by arguments
* <p>
* <p>
* <p>
* Example:
* <p>
* <p>
* {@code URITemplates.format("http://example.org/{}/{}", "A", 1)} results {@code "http://example.org/A/1" }
*
* @param uriTemplate String with placeholder
* @param args args
* @return a formatted string
*/
public static String format(String uriTemplate, Object... args) {
return format(uriTemplate, Arrays.asList(args));
}


/**
* Converts a URI function to a URI template
* <p>
* For instance:
* <pre>
* URI("http://example.org/{}/{}/{}", X, Y, X) -> "http://example.org/{X}/{Y}/{X}"
* </pre>
*
* @param uriFunction URI Function
* @return a URI template with variable names inside the placeholders
*/
public static String getUriTemplateString(Function uriFunction) {
ValueConstant term = (ValueConstant) uriFunction.getTerm(0);
final String template = term.getValue();
List<Variable> varlist = new LinkedList<>();
TermUtils.addReferencedVariablesTo(varlist, uriFunction);
List<Variable> varList = new ArrayList<>();
TermUtils.addReferencedVariablesTo(varList, uriFunction);

List<String> splitParts = Splitter.on("{}").splitToList(template);
List<String> splitParts = Splitter.on(PLACE_HOLDER).splitToList(template);

StringBuilder templateWithVars = new StringBuilder();

int numVars = varlist.size();
int numVars = varList.size();
int numParts = splitParts.size();

if (splitParts.size() != numVars + 1 && splitParts.size() != numVars) {
if (numParts != numVars + 1 && numParts != numVars) {
throw new IllegalArgumentException("the number of place holders should be equal to the number of variables.");
}

for (int i = 0; i < numVars; i++) {
templateWithVars.append(splitParts.get(i))
.append("{")
.append(varlist.get(i))
.append(varList.get(i))
.append("}");
}

if (splitParts.size() == numVars + 1) {
if (numParts == numVars + 1) {
templateWithVars.append(splitParts.get(numVars));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,13 @@ public void testGetUriTemplateString4(){
FACTORY.getVariable("X"), FACTORY.getVariable("Y"));
assertEquals("http://example.org/{X}/{Y}/", URITemplates.getUriTemplateString(f1));
}

@Test
public void testGetUriTemplateString5(){
final OBDADataFactory FACTORY = OBDADataFactoryImpl.getInstance();

Function f1 = FACTORY.getUriTemplate(FACTORY.getConstantLiteral("http://example.org/{}/{}/{}"), //
FACTORY.getVariable("X"), FACTORY.getVariable("Y"),FACTORY.getVariable("X"));
assertEquals("http://example.org/{X}/{Y}/{X}", URITemplates.getUriTemplateString(f1));
}
}

0 comments on commit 478c9d9

Please sign in to comment.