Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
move default parameter parser to the util project. new collection uti…
…ls method.
  • Loading branch information
chenson42 committed Jan 7, 2013
1 parent f3b3fff commit e52f819
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 23 deletions.
Expand Up @@ -25,9 +25,9 @@
import java.util.Set;
import java.util.TreeSet;

import org.jumpmind.properties.DefaultParameterParser;
import org.jumpmind.properties.DefaultParameterParser.ParameterMetaData;
import org.jumpmind.symmetric.service.IParameterService;
import org.jumpmind.symmetric.util.DefaultParameterParser;
import org.jumpmind.symmetric.util.DefaultParameterParser.ParameterMetaData;

/**
* Constants that represent parameters that can be retrieved or saved via the
Expand Down
Expand Up @@ -24,8 +24,9 @@

import junit.framework.Assert;

import org.jumpmind.properties.DefaultParameterParser;
import org.jumpmind.properties.DefaultParameterParser.ParameterMetaData;
import org.jumpmind.symmetric.common.ParameterConstants;
import org.jumpmind.symmetric.util.DefaultParameterParser.ParameterMetaData;
import org.junit.Test;

public class DefaultParameterParserTest {
Expand Down
Expand Up @@ -18,7 +18,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.jumpmind.symmetric.util;
package org.jumpmind.properties;

import java.io.IOException;
import java.io.InputStream;
Expand All @@ -41,24 +41,32 @@ public class DefaultParameterParser {
private static final String DATABASE_OVERRIDABLE = "DatabaseOverridable:";
private static final String TAGS = "Tags:";
private static final String TYPE = "Type:";

private String propertiesFilePath;


private InputStream inputStream;

final Logger log = LoggerFactory.getLogger(getClass());

public DefaultParameterParser(InputStream inputStream) {
this.inputStream = inputStream;
}

public DefaultParameterParser(String propertiesFilePath) {
this.propertiesFilePath = propertiesFilePath;
}

public Map<String, ParameterMetaData> parse() {
return parse(propertiesFilePath);
}

public Map<String, ParameterMetaData> parse(String fileName) {
Map<String, ParameterMetaData> metaData = new TreeMap<String, DefaultParameterParser.ParameterMetaData>();
try {
InputStream is = getClass().getResourceAsStream(fileName);
List<String> lines = IOUtils.readLines(is);
if (inputStream == null) {
inputStream = getClass().getResourceAsStream(fileName);
}
List<String> lines = IOUtils.readLines(inputStream);

ParameterMetaData currentMetaData = new ParameterMetaData();
for (String line : lines) {
Expand All @@ -69,15 +77,13 @@ public Map<String, ParameterMetaData> parse(String fileName) {
line.indexOf(DATABASE_OVERRIDABLE) + DATABASE_OVERRIDABLE.length())
.trim()));
} else if (line.contains(TAGS)) {
String[] tags = line.substring(
line.indexOf(TAGS) + TAGS.length())
.trim().split(",");
String[] tags = line.substring(line.indexOf(TAGS) + TAGS.length()).trim()
.split(",");
for (String tag : tags) {
currentMetaData.addTag(tag.trim());
}
} else if (line.contains(TYPE)) {
String type = line.substring(
line.indexOf(TYPE) + TYPE.length());
String type = line.substring(line.indexOf(TYPE) + TYPE.length());
currentMetaData.setType(type.trim());
} else {
currentMetaData.appendDescription(line);
Expand All @@ -95,7 +101,7 @@ public Map<String, ParameterMetaData> parse(String fileName) {
}
}
} catch (IOException e) {
log.error(e.getMessage(),e);
log.error(e.getMessage(), e);
}
return metaData;
}
Expand All @@ -105,19 +111,19 @@ public static class ParameterMetaData implements Serializable {
public static final String TYPE_BOOLEAN = "boolean";
public static final String TYPE_INT = "integer";
public static final String TYPE_TEXT_BOX = "textbox";

private static final long serialVersionUID = 1L;
private String key;
private String description;
private Set<String> tags = new HashSet<String>();
private boolean databaseOverridable;
private String defaultValue;
private String type = "";

public void setType(String type) {
this.type = type;
}

public String getType() {
return type;
}
Expand Down Expand Up @@ -169,20 +175,20 @@ public void appendDescription(String value) {
description = description + value;
}
}

public boolean isBooleanType() {
return type != null && type.equals(TYPE_BOOLEAN);
}

public boolean isIntType() {
return type != null && type.equals(TYPE_INT);
}

public boolean isTextBoxType() {
return type != null && type.equals(TYPE_TEXT_BOX);
}
public void addTag (String tag) {

public void addTag(String tag) {
tags.add(tag);
}
}
Expand Down
Expand Up @@ -23,10 +23,12 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.apache.commons.io.IOUtils;
import org.jumpmind.exception.IoException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -37,6 +39,16 @@ public class TypedProperties extends Properties {

protected static Logger log = LoggerFactory.getLogger(TypedProperties.class);

public TypedProperties(InputStream is) {
try {
load(is);
} catch (IOException ex) {
throw new IoException(ex);
} finally {
IOUtils.closeQuietly(is);
}
}

public TypedProperties() {
}

Expand Down
@@ -1,6 +1,7 @@
package org.jumpmind.util;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -96,6 +97,15 @@ public static <T,U> T[] copyOfRange(U[] original, int from, int to, Class<? exte
return copy;
}

public static <T> T[] add(T[] one, T[] two) {
int size = one.length + two.length;
T[] copy = Arrays.copyOf(one, size);
for (int i = one.length; i < size; i++) {
copy[i] = two[i-one.length];
}
return copy;
}

public static String toCommaSeparatedValues(Collection<?> list) {
StringBuilder csv = new StringBuilder();
if (list != null) {
Expand Down

0 comments on commit e52f819

Please sign in to comment.