Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions constretto-api/src/main/java/org/constretto/model/CArray.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package org.constretto.model;

import org.apache.commons.lang.StringUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import java.util.*;
import org.apache.commons.lang.StringUtils;

/**
* @author <a href="mailto:kaare.nilsen@arktekk.no">Kaare Nilsen</a>
Expand All @@ -23,12 +26,13 @@ public List<CValue> data() {
}

@Override
public Set<String> referencedKeys() {
Set<String> referencedKeys = new HashSet<String>();
public Iterable<String> referencedKeys() {
List<Iterable<String>> referencedKeys = new ArrayList<Iterable<String>>();
for (CValue value : data) {
referencedKeys.addAll(value.referencedKeys());
referencedKeys.add(value.referencedKeys());
}
return referencedKeys;
return new Iter<String>(referencedKeys.toArray(new Iterable[0]));

}

@Override
Expand Down
15 changes: 8 additions & 7 deletions constretto-api/src/main/java/org/constretto/model/CObject.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package org.constretto.model;

import org.apache.commons.lang.StringUtils;

import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
* @author <a href="mailto:kaare.nilsen@arktekk.no">Kaare Nilsen</a>
Expand All @@ -23,12 +24,12 @@ public Map<String, CValue> data() {
}

@Override
public Set<String> referencedKeys() {
Set<String> referencedKeys = new HashSet<String>();
public Iterable<String> referencedKeys() {
List<Iterable<String>> referencedKeys = new ArrayList<Iterable<String>>();
for (CValue value : data.values()) {
referencedKeys.addAll(value.referencedKeys());
referencedKeys.add(value.referencedKeys());
}
return referencedKeys;
return new Iter<String>(referencedKeys.toArray(new Iterable[0]));
}

@Override
Expand Down
60 changes: 48 additions & 12 deletions constretto-api/src/main/java/org/constretto/model/CPrimitive.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package org.constretto.model;

import java.util.HashSet;
import java.util.Set;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* @author <a href="mailto:kaare.nilsen@arktekk.no">Kaare Nilsen</a>
*/
public class CPrimitive extends CValue {
private static final Pattern VARIABLE_PATTERN = Pattern.compile("#\\{(.*?)}");
private String value;

public CPrimitive(String value) {
Expand All @@ -25,14 +22,53 @@ public String value() {
}

@Override
public Set<String> referencedKeys() {
Set<String> referencedKeys = new HashSet<String>();
Matcher matcher = VARIABLE_PATTERN.matcher(value);
while (matcher.find()) {
String group = matcher.group(1);
referencedKeys.add(group);
}
return referencedKeys;
public Iterable<String> referencedKeys() {

final Iterator<String> iterator = new Iterator<String>() {

Boolean hasNext;
String s;

@Override
public boolean hasNext() {
if (hasNext != null) {
return hasNext;
}

String prefix = "#{", postfix = "}";

int startIndex = value.lastIndexOf(prefix);
if (startIndex < 0) {
hasNext = false;
return false;
}
int endIndex = value.indexOf(postfix, startIndex);
if (endIndex < 0) {
hasNext = false;
return false;
}
s = value.substring(startIndex + prefix.length(), endIndex);

return true;
}

@Override
public String next() {
return s;
}

@Override
public void remove() {
throw new UnsupportedOperationException("Remove not supported");
}
};

return new Iterable<String>() {
@Override
public Iterator<String> iterator() {
return iterator;
}
};
}

@Override
Expand Down
5 changes: 3 additions & 2 deletions constretto-api/src/main/java/org/constretto/model/CValue.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package org.constretto.model;

import java.util.Iterator;
import java.util.Set;

/**
* @author <a href="mailto:kaare.nilsen@arktekk.no">Kaare Nilsen</a>
*/
public abstract class CValue {

public abstract Set<String> referencedKeys();
public abstract Iterable<String> referencedKeys();

public boolean containsVariables() {
return !referencedKeys().isEmpty();
return referencedKeys().iterator().hasNext();
}

public boolean isArray(){
Expand Down
38 changes: 38 additions & 0 deletions constretto-api/src/main/java/org/constretto/model/Iter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.constretto.model;

import java.util.Iterator;

public class Iter<T> implements Iterable<T> {

private Iterable<T>[] input;

public Iter(Iterable<T>... input) {
this.input = input;
}

@Override
public Iterator<T> iterator() {
return new Iterator<T>() {

int index = 0;

@Override
public boolean hasNext() {
if (index >= input.length) {
return false;
}
return input[index].iterator().hasNext();
}

@Override
public T next() {
return input[index++].iterator().next();
}

@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;

import static org.junit.Assert.assertArrayEquals;
Expand Down Expand Up @@ -32,7 +33,7 @@ public void testData() throws Exception {

@Test
public void testReferencedKeys() throws Exception {
assertEquals(0, cArray.referencedKeys().size());
assertEquals(false, cArray.referencedKeys().iterator().hasNext());
}

@Test(expected = NullPointerException.class)
Expand All @@ -44,7 +45,13 @@ public void testNull() throws Exception {
@Test
public void testReplace() throws Exception {
final CArray arrayWithKey = new CArray(Arrays.<CValue>asList(new CPrimitive("#{key}")));
assertEquals(1, arrayWithKey.referencedKeys().size());

ArrayList<String> objects = new ArrayList<String>();
for (String s : arrayWithKey.referencedKeys()) {
objects.add(s);
}

assertEquals(1, objects.size());
arrayWithKey.replace("key", VALUE_ONE);
assertArrayEquals(new CValue[]{PRIMITIVE_ONE}, arrayWithKey.data().toArray(new CValue[]{}));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,18 @@ public void simpleLookupForKeyContainingReferencesToOtherKeys() {
assertEquals("it works when its at the end", constrettoConfiguration.evaluateToString("at-end"));
}

@Test
public void simpleLookupForKeyContainingReferencesToOtherKeyRecurive() {
ConstrettoConfiguration constrettoConfiguration = prepareTests();
assertEquals("recursive works at end", constrettoConfiguration.evaluateToString("recursive"));
}

@Test
public void simpleLookupForKeyContainingReferencesToOtherKeyNested() {
ConstrettoConfiguration constrettoConfiguration = prepareTests();
assertEquals("testuser", constrettoConfiguration.evaluateToString("nested"));
}

@Test
public void taggedLookupForKeyContainingReferencesToOtherKeys() {
ConstrettoConfiguration constrettoConfiguration = prepareTests();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
transitive=It should not work for #{transitive-circular} either
transitive-circular=#{transitive}

recursive=recursive works at #{#{end-value}-value}

environment=test
db.test.username=testuser
nested=#{db.#{environment}.username}


webservices-base-url=http://webservice
webservice.customer=#{webservices-base-url}/customer

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ multiple-replacements=#{start-value} at the beginning and in the #{middle-value}
circular=but when used in a #{circular}. It better throw an exception before giving a stack overflow :)
transitive=It should not work for #{transitive-circular} either
transitive-circular=#{transitive}
recursive=recursive works at #{#{end-value}-value}

environment=test
db.test.username=testuser
nested=#{db.#{environment}.username}

#
# Used for testing that variable resolving works well together
Expand Down