Skip to content

Commit

Permalink
Begin refactoring EMLParser constructors.
Browse files Browse the repository at this point in the history
Partially complete, mostly consolidated duplicated code, and began setting up for the namespace comparisons.
  • Loading branch information
mbjones committed Feb 1, 2019
1 parent d2d2137 commit 13f28fc
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 44 deletions.
85 changes: 41 additions & 44 deletions src/main/java/org/ecoinformatics/eml/EMLParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -101,35 +102,27 @@ public class EMLParser {

/**
* parses an eml file
* @param xml the eml input stream to parse
* @param xml the eml file to parse
*/
public EMLParser(File xml) {
this.xml = xml;
URL configFile = getClass().getResource("/config.xml");
try {

config = new ConfigXML(configFile.openStream());
} catch(Exception e) {
throw new EMLParserException("Config file not found: " + e.getMessage());
}

parseConfig();
parseKeys();
parseKeyrefs();
this(xml, null);
}

/**
* parses an eml file
* parses an eml file with an alternate configuration
* @param xml the eml file to parse
* @param configFile the alternate config file to use
*/
public EMLParser(File xml, File configFile)
throws EMLParserException {
public EMLParser(File xml, File configFile) throws EMLParserException {
this.xml = xml;
try {
config = new ConfigXML(configFile.getAbsolutePath());
} catch(Exception e) {
throw new EMLParserException("Config file not found: " + e.getMessage());
if (configFile == null) {
config = getDefaultConfig();
} else {
try {
config = new ConfigXML(configFile.getAbsolutePath());
} catch(Exception e) {
throw new EMLParserException("Config file not found: " + e.getMessage());
}
}

parseConfig();
Expand All @@ -139,28 +132,43 @@ public EMLParser(File xml, File configFile)


/**
* parses an eml reader
* @param xmlReader the xml need to parse
* @param configFile the alternate config file to use
* parses an eml document as a String
* @param xmlString the xml to parse
*/
public EMLParser(String xmlString)
throws EMLParserException, IOException {
public EMLParser(String xmlString) throws EMLParserException, IOException {
if (xmlString == null || xmlString.equals("")) {
throw new EMLParserException("The string need to be parse is null");
throw new EMLParserException("The EML string to be parsed is null or empty.");
}
config = getDefaultConfig();

parseConfig();
parseKeys(xmlString);
parseKeyrefs(xmlString);
}


private ConfigXML getDefaultConfig() throws EMLParserException {
URL configFile = getClass().getResource("/config.xml");
ConfigXML defaultConfig = null;
try {

config = new ConfigXML(configFile.openStream());
defaultConfig = new ConfigXML(configFile.openStream());
} catch(Exception e) {
throw new EMLParserException("Config file not found: " + e.getMessage());
}
// catch the String reader
parseConfig();
parseKeys(xmlString);
parseKeyrefs(xmlString);
return(defaultConfig);
}

public void validateRecent(File xml) throws FileNotFoundException, IOException {
FileReader reader = new FileReader(xml);
String namespace= EMLParserServlet.findNamespace(reader);
reader.close();
String version = namespace.split("\\-")[1];
SemVersion nsVersion = new SemVersion(version);
SemVersion v220 = new SemVersion("2.2.0");
if (nsVersion.compareTo(v220) >= 0) {
System.out.println("Use EMLValidator!");
}
}

/**
* make sure all ids are unique and hash the keys
Expand Down Expand Up @@ -403,18 +411,7 @@ public static NodeList getPathContent(InputStream is, String xpath)
public static NodeList getPathContent(StringReader read, String xpath)
throws Exception {
InputSource in = new InputSource(read);
DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
dfactory.setNamespaceAware(false);
Document doc = dfactory.newDocumentBuilder().parse(in);

// Set up an identity transformer to use as serializer.
Transformer serializer = TransformerFactory.newInstance().newTransformer();
serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");

// Use the simple XPath API to select a nodeIterator.
NodeList nl = XPathAPI.selectNodeList(doc, xpath);
return nl;
//return getPathContent(in, xpath);
return getPathContent(in, xpath);
}

private static NodeList getPathContent(InputSource in, String xpath)
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/org/ecoinformatics/eml/SemVersion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.ecoinformatics.eml;

public class SemVersion implements Comparable<SemVersion> {
public final int[] numbers;

public SemVersion(String version) {
final String split[] = version.split("\\-")[0].split("\\.");
numbers = new int[split.length];
for (int i = 0; i < split.length; i++) {
numbers[i] = Integer.valueOf(split[i]);
}
}

@Override
public int compareTo(SemVersion another) {
final int maxLength = Math.max(numbers.length, another.numbers.length);
for (int i = 0; i < maxLength; i++) {
final int left = i < numbers.length ? numbers[i] : 0;
final int right = i < another.numbers.length ? another.numbers[i] : 0;
if (left != right) {
return left < right ? -1 : 1;
}
}
return 0;
}
}

0 comments on commit 13f28fc

Please sign in to comment.