diff --git a/src/main/java/org/ecoinformatics/eml/EMLParser.java b/src/main/java/org/ecoinformatics/eml/EMLParser.java deleted file mode 100644 index c8915cb1..00000000 --- a/src/main/java/org/ecoinformatics/eml/EMLParser.java +++ /dev/null @@ -1,737 +0,0 @@ -/** - * '$RCSfile: EMLParser.java,v $' - * Copyright: 1997-2002 Regents of the University of California, - * University of New Mexico, and - * Arizona State University - * Sponsors: National Center for Ecological Analysis and Synthesis and - * Partnership for Interdisciplinary Studies of Coastal Oceans, - * University of California Santa Barbara - * Long-Term Ecological Research Network Office, - * University of New Mexico - * Center for Environmental Studies, Arizona State University - * Other funding: National Science Foundation (see README for details) - * The David and Lucile Packard Foundation - * For Details: http://knb.ecoinformatics.org/ - * - * '$Author: walbridge $' - * '$Date: 2008-11-05 21:08:45 $' - * '$Revision: 1.16 $' - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ -package org.ecoinformatics.eml; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.StringReader; -import java.io.StringWriter; -import java.net.URL; -import java.util.Hashtable; -import java.util.Stack; -import java.util.StringTokenizer; - -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.transform.OutputKeys; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerFactory; - -import org.apache.xpath.XPathAPI; -import org.w3c.dom.Document; -import org.w3c.dom.NamedNodeMap; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; -import org.xml.sax.InputSource; - -import edu.ucsb.nceas.utilities.config.ConfigXML; - - -/** - * This is a SAX parser to validate EML packages. This parser will validate - * an EML package with references based on the following rules: - * - */ -public class EMLParser -{ - private String parserName; - private ConfigXML config; - private Key[] keys; - private Keyref[] keyrefs; - private Hashtable idHash = new Hashtable(); - private Hashtable idrefHash = new Hashtable(); - private File xml; - - /** - * parses an eml file - * @param xml the eml input stream 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(); - } - - /** - * parses an eml file - * @param xml the eml file to parse - * @param configFile the alternate config file to use - */ - 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()); - } - - parseConfig(); - parseKeys(); - parseKeyrefs(); - } - - - /** - * parses an eml reader - * @param xmlReader the xml need to parse - * @param configFile the alternate config file to use - */ - public EMLParser(String xmlString) - throws EMLParserException, IOException - { - if (xmlString == null || xmlString.equals("")) - { - throw new EMLParserException("The string need to be parse is null"); - } - URL configFile = getClass().getResource("/config.xml"); - try - { - - config = 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); - } - - - /** - * make sure all ids are unique and hash the keys - */ - private void parseKeys() - { - for(int i=0; i"); - s.push(node); - } - - while(!s.empty()) - { - String node = (String)s.pop(); - xml.append(""); - } - - return builder.parse(new InputSource(new StringReader(xml.toString()))); - } - catch(Exception e) - { - throw new EMLParserException("Error building document fragment: " + - e.getMessage()); - } - } - - private void resolveKeys() - { - - } - - /** - * Gets the content of a path in an xml file(form input stream) - */ - public static NodeList getPathContent(InputStream is, String xpath) - throws Exception - { - InputSource in = new InputSource(is); - return getPathContent(in, xpath); - } - - /** - * Gets the conten of a path in an xml document(from Reader) - */ - 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); - } - - private static NodeList getPathContent(InputSource in, String xpath) - throws Exception - { - 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; - } - - private void parseConfig() - { - try - { //parse the keys and keyrefs out of the config file - NodeList keyNL = config.getPathContent("//key"); - keys = new Key[keyNL.getLength()]; - NodeList keyrefNL = config.getPathContent("//keyref"); - keyrefs = new Keyref[keyrefNL.getLength()]; - - //get the keys - for(int i=0; i 0 && !args[0].equals("-q")) - { - System.out.println("EML Parser version 1.0"); - System.out.println("Note that this parser DOES NOT VALIDATE your eml file "); - System.out.println("agains the schema. It only validates the ids and "); - System.out.println("references. To validate your eml file against the "); - System.out.println("schema, use SAXValidate or another xml parser."); - System.out.println("Usage: java org.ecoinformatics.eml.EMLParser [-q] [] "); - System.out.println("-----------------------------------------------------------------------"); - } - - if(args.length > 3) - { - System.out.println("Invalid number of arguments."); - } - - String configfile = ""; - String emlfile = ""; - if(args.length == 3) - { - configfile = args[1]; - emlfile = args[2]; - System.out.println("emlfile: " + emlfile + " configfile: " + configfile); - } - else if(args.length == 2) - { - if(args[0].equals("-q")) - { - emlfile = args[1]; - } - else - { - configfile = args[0]; - emlfile = args[1]; - } - } - else if(args.length == 1) - { - emlfile = args[0]; - } - else if(args.length == 0) - { - - System.out.println("Usage: java org.ecoinformatics.eml.EMLParser [-q] [] "); - System.out.println(" -q = quiet mode, little or no output"); - System.out.println(" = use an alternate config file. The default is lib/config.xml"); - System.out.println(" = the EML file to parse"); - System.exit(0); - } - - try - { - if(configfile.equals("")) - { - EMLParser parser = new EMLParser(new File(emlfile)); - FileReader xmldoc = new FileReader(emlfile); - char [] ch = new char [4096]; - StringWriter writer = new StringWriter(); - int readNum = xmldoc.read(ch); - while (readNum != -1) - { - writer.write(ch, 0, readNum); - readNum = xmldoc.read(ch); - } - String str = writer.toString(); - EMLParser readerParser = new EMLParser(str); - - } - else - { - EMLParser parser = new EMLParser(new File(emlfile), new File(configfile)); - } - System.out.println(emlfile + " has valid ids and references."); - } - catch(Exception e) - { - System.out.println("Error: " + e.getMessage()); - } - } - - /** - * class to represent a key - */ - private class Key - { - protected String selector; //xpath expression for the selector - protected String field; //xpath expression for the field in the selector - protected String name; //name of the key - - Key(String name, String selector, String field) - { - this.name = name; - this.selector = selector; - this.field = field; - } - - public String toString() - { - String s = "name: " + name + " selector: " + selector + " field: " + field; - return s; - } - } - - /** - * class to represent a keyref - */ - private class Keyref - { - protected String name; //name of the keyref - protected String refer; //the key that we are refering to - protected String selector; //the selector for the keyref - protected String field; //the field in the selector - - Keyref(String name, String refer, String selector, String field) - { - this.name = name; - this.refer = refer; - this.selector = selector; - this.field = field; - } - - public String toString() - { - String s = "name: " + name + " refer: " + refer + " selector: " + - selector + " field: " + field; - return s; - } - } -} diff --git a/src/main/java/org/ecoinformatics/eml/EMLParserException.java b/src/main/java/org/ecoinformatics/eml/EMLParserException.java deleted file mode 100644 index b4c55d44..00000000 --- a/src/main/java/org/ecoinformatics/eml/EMLParserException.java +++ /dev/null @@ -1,44 +0,0 @@ -/** - * '$RCSfile: EMLParserException.java,v $' - * Copyright: 1997-2002 Regents of the University of California, - * University of New Mexico, and - * Arizona State University - * Sponsors: National Center for Ecological Analysis and Synthesis and - * Partnership for Interdisciplinary Studies of Coastal Oceans, - * University of California Santa Barbara - * Long-Term Ecological Research Network Office, - * University of New Mexico - * Center for Environmental Studies, Arizona State University - * Other funding: National Science Foundation (see README for details) - * The David and Lucile Packard Foundation - * For Details: http://knb.ecoinformatics.org/ - * - * '$Author: berkley $' - * '$Date: 2002-10-03 21:36:17 $' - * '$Revision: 1.3 $' - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -package org.ecoinformatics.eml; - -/** - * An exception for the EMLParser to throw at a high level. - */ -public class EMLParserException extends RuntimeException -{ - public EMLParserException(String msg) - { - super(msg); - } -} diff --git a/src/test/java/org/ecoinformatics/emltest/EMLParserTest.java b/src/test/java/org/ecoinformatics/emltest/EMLParserTest.java deleted file mode 100644 index 7e129458..00000000 --- a/src/test/java/org/ecoinformatics/emltest/EMLParserTest.java +++ /dev/null @@ -1,191 +0,0 @@ -/** - * '$RCSfile: EMLParserTest.java,v $' - * Copyright: 2000 Regents of the University of California and the - * National Center for Ecological Analysis and Synthesis - * - * '$Author: walbridge $' - * '$Date: 2008-11-05 23:00:46 $' - * '$Revision: 1.6 $' - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -package org.ecoinformatics.emltest; - -import java.io.File; -import java.util.Vector; - -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; - -import org.ecoinformatics.eml.EMLParser; - -/** - * A JUnit test for testing the EMLParser - * - * @author Chad Berkley - */ -public class EMLParserTest extends TestCase -{ - private final static String TEST_DIR = "./src/test/resources"; - private final static String INVALID_DIR = TEST_DIR + "/invalidEML"; - private final static String ERROR1 = INVALID_DIR + "/eml-error1.xml"; - private final static String ERROR3 = INVALID_DIR + "/eml-error3.xml"; - private final static String ERROR4 = INVALID_DIR + "/eml-error4.xml"; - - private EMLParser emlp; - - /** - * Constructor to build the test - * - * @param name the name of the test method - */ - public EMLParserTest(String name) - { - super(name); - } - - /** Establish a testing framework by initializing appropriate objects */ - public void setUp() - { - - } - - /** Release any objects after tests are complete */ - public void tearDown() - { - } - - /** - * Create a suite of tests to be run together - * - * @return The test suite - */ - public static Test suite() - { - TestSuite suite = new TestSuite(); - suite.addTest(new EMLParserTest("initialize")); - suite.addTest(new EMLParserTest("testParse")); - return suite; - } - - /** - * Check that the testing framework is functioning properly with a trivial - * assertion. - */ - public void initialize() - { - assertTrue(true); - } - - public void testParse() - { - - // all valid documents should validate - File testDir = new File(TEST_DIR); - Vector fileList = getXmlFiles(testDir); - for (int i=0; i < fileList.size(); i++) { - File testFile = (File)fileList.elementAt(i); - try { - System.err.println("Validating file: " + testFile.getName()); - emlp = new EMLParser(testFile); - } catch (Exception e) { - e.printStackTrace(System.err); - fail("Document NOT valid!\n\n" + e.getClass().getName() + - "(" + e.getMessage() + ")" ); - } - } - - // All of the invalid files should not validate - // NOTE: EMLParser does not validate against the schema (see SAXParserTest) - int failures = 0; - File invalidDir = new File(INVALID_DIR); - Vector invalidList = getXmlFiles(invalidDir); - int invalidFileCount = invalidList.size(); - System.err.println("Checking invalid files: " + invalidFileCount); - for (int i=0; i < invalidFileCount; i++) { - File invalidFile = (File)invalidList.elementAt(i); - System.err.println("Invalidating file: " + invalidFile.getName()); - try { - emlp = new EMLParser(invalidFile); - System.err.println(" Valid."); - } catch (Exception e) { - System.err.println(" Invalid."); - failures++; - assertTrue(e.getMessage() != null); - } - } - if (failures != invalidFileCount) { - System.err.println(failures + "/" + invalidFileCount + " failures in directory."); - fail("Error: An EMLParserException should have been thrown for all invalid files."); - } - - try { - File f = new File(ERROR1); - emlp = new EMLParser(f); - fail("Error 1. An EMLParserException should have been thrown."); - } catch(Exception e) { - assertTrue(e.getMessage().indexOf("Error in xml document. " + - "This EML document is not valid because the id 23445 occurs more " + - "than once. IDs must be unique.") != -1); - } - - try { - File f = new File(ERROR3); - emlp = new EMLParser(f); - fail("Error 3. An EMLParserException should have been thrown."); - } catch(Exception e) { - assertTrue(e.getMessage().equals("Error processing keyrefs: " + - "//references : Error in xml document. This EML instance is " + - "invalid because referenced id 23447 does not exist in the " + - "given keys.")); - } - - try { - File f = new File(ERROR4); - emlp = new EMLParser(f); - fail("Error 3. An EMLParserException should have been thrown."); - } catch(Exception e) { - //System.out.println(e.getMessage()); - assertTrue(e.getMessage().equals("Error processing keyrefs: " + - "//references : Error in xml document. This EML instance is invalid " + - "because this element has an id and it is being used in " + - "a keyref expression.")); - } - } - - /** - * Get the list of files in a directory. - * - * @param directory the directory to list - * @return a vector of File objects in the directory - */ - private Vector getXmlFiles(File directory) - { - String[] files = directory.list(); - Vector fileList = new Vector(); - - for (int i=0; i < files.length; i++) { - String filename = files[i]; - File currentFile = new File(directory, filename); - if (currentFile.isFile() && filename.endsWith(".xml")) { - fileList.addElement(currentFile); - } - } - return fileList; - } - -}