Skip to content

Commit

Permalink
Refactoring, add converter instantiation
Browse files Browse the repository at this point in the history
  • Loading branch information
rjyounes committed Oct 31, 2016
1 parent 3ce4ade commit eb13c2d
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 84 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package org.ld4l.bib2lod.clean.marcxml;

import org.ld4l.bib2lod.clean.BaseCleaner;
import org.ld4l.bib2lod.configuration.Configuration;

public class MarcxmlCleaner extends BaseCleaner {

public MarcxmlCleaner() {
// TODO Auto-generated constructor stub
}


}
83 changes: 64 additions & 19 deletions src/main/java/org/ld4l/bib2lod/configuration/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
import org.apache.commons.cli.ParseException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.ld4l.bib2lod.conversion.Converter;
import org.ld4l.bib2lod.uri.UriMinter;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.RuntimeJsonMappingException;

public class Configuration {
Expand All @@ -23,6 +26,11 @@ public class Configuration {
private String localNamespace;
private UriMinter uriMinter;
private List<File> input;

// TODO Convert to list later
// private List<Converter> converters;
private Converter converter;

// cleaner, parser, converters
// private Reader reader;
// private Writer writer;
Expand All @@ -44,29 +52,21 @@ public Configuration(String[] args) throws ClassNotFoundException,
// Get the configuration used to configure the application and create
// the services.

OptionsReader configurer = new OptionsReader(args);
JsonNode config = ((OptionsReader) configurer).getConfig();
OptionsReader optionsReader = new OptionsReader(args);
JsonNode config = optionsReader.configure();

LOGGER.debug(config.toString());

JsonNode services = config.get("services");

LOGGER.debug(services.toString());


String localNamespace = getJsonStringValue(config, "localNamespace");
setLocalNamespace(localNamespace);

makeUriMinter(getJsonStringValue(services, "uriMinter"));

// TODO Add same for other services...
buildServices(config);

// TODO Throw error if not defined
JsonNode inputNode = config.get("input");
String inputPath = getJsonStringValue(inputNode, "location");
String inputFormat = getJsonStringValue(inputNode, "format");
String fileExtension = getJsonStringValue(inputNode, "extension");
buildInputFileList(inputPath, inputFormat, fileExtension);
buildInputFileList(config);

// buildConverters(config);
buildConverter(config);

// TODO Add same for other config elements...

}
Expand All @@ -78,17 +78,35 @@ public String getLocalNamespace() {
public UriMinter getUriMinter() {
return uriMinter;
}


// TODO Or just return the input string from config file?
public List<File> getInput() {
return input;
}

// public List<Converter> getConverters() {
// return converters;
// }
public Converter getConverter() {
return converter;
}

protected void setLocalNamespace(String localNamespace) {
this.localNamespace = localNamespace;
}

protected void buildServices(JsonNode config)
throws ClassNotFoundException, ReflectiveOperationException {

JsonNode services = config.get("services");
LOGGER.debug(services.toString());

makeUriMinter(getJsonStringValue(services, "uriMinter"));

// TODO Add same for other services...

}

protected void makeUriMinter(String minterClassName)
throws ClassNotFoundException, ReflectiveOperationException {

Expand All @@ -106,8 +124,14 @@ protected void makeUriMinter(String minterClassName)
*/
// TODO Also pass in file type and make sure we get only the files of this
// type
protected void buildInputFileList(String inputPath, String fileFormat,
String fileExtension) throws FileNotFoundException {
protected void buildInputFileList(JsonNode config)
throws FileNotFoundException {

// TODO Throw error if not defined
JsonNode inputNode = config.get("input");
String inputPath = getJsonStringValue(inputNode, "location");
String inputFormat = getJsonStringValue(inputNode, "format");
String fileExtension = getJsonStringValue(inputNode, "extension");

this.input = new ArrayList<File>();

Expand All @@ -126,6 +150,27 @@ protected void buildInputFileList(String inputPath, String fileFormat,
}
}

protected void buildConverter(JsonNode config)
throws ClassNotFoundException, InstantiationException,
IllegalAccessException {

// ObjectMapper mapper = new ObjectMapper();

// JsonNode converterList = config.get("converters");
// TODO Should be an array of strings or a string. For now just handle
// an array.
// if string...convert to list
// else

// TODO Get this to work. Hard-coding as a single converter for now
// TypeReference ref = new TypeReference<List<Converter>>() {};
// converters = mapper.readValue(converterList, ref);
String converter = getJsonStringValue(config, "converter");
Class<?> c = Class.forName(converter);
this.converter = (Converter) c.newInstance();

}

private String getJsonStringValue(JsonNode node, String key) {

JsonNode value = node.get(key);
Expand Down
53 changes: 22 additions & 31 deletions src/main/java/org/ld4l/bib2lod/configuration/OptionsReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,35 +31,11 @@ public class OptionsReader {
public OptionsReader(String[] args) {
this.args = args;
}


/**
* Parse commandline options.
* @param options
* @param args
* @return
* @throws ParseException
*/
protected CommandLine getCommandLine(Options options, String[] args)
throws ParseException {

// Parse program arguments
CommandLineParser parser = new DefaultParser();

return parser.parse(options, args);
}

// /**
// * Print help text.
// * @param options
// */
// TODO Currently not used. What is best way to do this? If print here,
// need to catch exceptions and return null up the chain. If print from
// manager or elsewhere, need to supply a method to provide the help string.
// See HelpFormatter.renderOptions() or HelpFormatter.renderWrappedText().
// private void printHelp(Options options) {
//
/**
*
* Get configuration option values from config file or commandline;
* commandline overrides config file.
* @return
* @throws IOException
* @throws ParseException
Expand All @@ -69,9 +45,9 @@ protected CommandLine getCommandLine(Options options, String[] args)
// than using whatever json library they choose. However, if we are also
// using Jackson inside the core converter, it doesn't matter. Consider this
// later.
public JsonNode getConfig() throws IOException, ParseException {
public JsonNode configure() throws IOException, ParseException {

JsonNode jsonConfig = null;
JsonNode config = null;

Options options = buildOptions();

Expand All @@ -89,13 +65,13 @@ public JsonNode getConfig() throws IOException, ParseException {

try {
ObjectMapper mapper = new ObjectMapper();
jsonConfig = mapper.readTree(configFile);
config = mapper.readTree(configFile);

// Note: currently the only commandline option is the config file
// location. Later others may be supported, in which case this
// will method override the config file values with the commandline
// option values and return the result.
return jsonConfig;
return config;

} catch (JsonParseException e) {
throw new IOException(
Expand Down Expand Up @@ -133,4 +109,19 @@ private Options buildOptions() {
return options;
}

/**
* Parse commandline options.
* @param options
* @param args
* @return
* @throws ParseException
*/
protected CommandLine getCommandLine(Options options, String[] args)
throws ParseException {

// Parse program arguments
CommandLineParser parser = new DefaultParser();
return parser.parse(options, args);
}

}
15 changes: 15 additions & 0 deletions src/main/java/org/ld4l/bib2lod/conversion/BaseConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.ld4l.bib2lod.conversion;

import java.io.File;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.ld4l.bib2lod.configuration.Configuration;

// TODO Does this do anything? If not, remove
public abstract class BaseConverter implements Converter {

private static final Logger LOGGER =
LogManager.getLogger(BaseConverter.class);

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.ld4l.bib2lod.conversion;

public class MarcxmlToLd4lRdf {
public class BaseRecord implements Record {

public MarcxmlToLd4lRdf() {
public BaseRecord() {
// TODO Auto-generated constructor stub
}

Expand Down

This file was deleted.

12 changes: 12 additions & 0 deletions src/main/java/org/ld4l/bib2lod/conversion/Converter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.ld4l.bib2lod.conversion;

import java.io.File;

public interface Converter {

public String convertFile(String text);

public String convertFile(File file);

// public String convertRecord(Record record);
}
5 changes: 5 additions & 0 deletions src/main/java/org/ld4l/bib2lod/conversion/Record.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.ld4l.bib2lod.conversion;

public interface Record {

}
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
package org.ld4l.bib2lod.conversion;
package org.ld4l.bib2lod.conversion.to_rdf;

import java.io.File;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.ld4l.bib2lod.configuration.Configuration;
import org.ld4l.bib2lod.conversion.BaseConverter;

public class MarcxmlToLd4lRdf extends BaseConverter {

// TODO figure out whether we need an interface and/or base class
public class RecordConverter {

private static final Logger LOGGER =
LogManager.getLogger(RecordConverter.class);


public RecordConverter(Configuration config) {
// from config, get cleaner, parser, converters and instantiate all

}
LogManager.getLogger(MarcxmlToLd4lRdf.class);

@Override
public String convertFile(File file) {
// TODO Auto-generated method stub

// Loop through records. For each: clean, parse, and loop through
// converters to convert
// TODO Do we need a Record implementation?
return null;
}


@Override
public String convertFile(String text) {
// TODO Auto-generated method stub
return null;
}
}
21 changes: 14 additions & 7 deletions src/main/java/org/ld4l/bib2lod/manager/SimpleManager.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package org.ld4l.bib2lod.manager;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.ld4l.bib2lod.configuration.Configuration;
import org.ld4l.bib2lod.conversion.RecordConverter;

// TODO Put common methods (like getInputFiles() into a base class or a
// utility class?
public class SimpleManager {
public final class SimpleManager {

private static final Logger LOGGER =
LogManager.getLogger(SimpleManager.class);
Expand Down Expand Up @@ -38,15 +38,22 @@ public static void main(String[] args) {

/**
* Convert a list of input files
* @throws IOException
*/
private static void convertFiles(Configuration configuration) {

//RecordConverter recordConverter = new RecordConverter(configuration);
private static void convertFiles(Configuration configuration)
throws IOException {

List<File> inputFiles = configuration.getInput();

for (File file : inputFiles) {

//String converted = recordConverter.convertFile(file);
// TODO get back output and write it (use configuration writer)
// TODO Make work as a loop for a chain of converters?
// Then need to first convert to string, so each converter takes a
// string as input and returns a string, which is input to next
// converter.

configuration.getConverter().convertFile(file);

}
}

Expand Down

0 comments on commit eb13c2d

Please sign in to comment.