Author: dlanglais Date: 2010-03-01 19:25:21 +0100 (Mon, 01 Mar 2010) New Revision: 151 Added: trunk/msm-fromtoXML/src/main/java/org/nuiton/mapstoragemanager/plugins/importer/FromXML.java trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/getFile.java trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/importer/FromXMLTest.java trunk/msm-fromtoXML/src/test/resources/ trunk/msm-fromtoXML/src/test/resources/fiveTables.xml trunk/msm-fromtoXML/src/test/resources/twoTablesFiveColumns.xml Modified: trunk/msm-fromtoXML/src/main/java/org/nuiton/mapstoragemanager/plugins/exporter/ToXML.java trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/exporter/ToXMLTest.java Log: Commencement FromXML.java, et classe de test. Pour le moment import Tables et Colonnes : OK. Toujours pas de gestion des versions. Modified: trunk/msm-fromtoXML/src/main/java/org/nuiton/mapstoragemanager/plugins/exporter/ToXML.java =================================================================== --- trunk/msm-fromtoXML/src/main/java/org/nuiton/mapstoragemanager/plugins/exporter/ToXML.java 2010-02-28 17:58:02 UTC (rev 150) +++ trunk/msm-fromtoXML/src/main/java/org/nuiton/mapstoragemanager/plugins/exporter/ToXML.java 2010-03-01 18:25:21 UTC (rev 151) @@ -6,6 +6,7 @@ import java.io.File; import java.io.FileOutputStream; +import java.io.IOException; import java.util.NoSuchElementException; import java.util.Set; import org.apache.commons.logging.Log; @@ -28,27 +29,26 @@ * Logger. */ private static final Log LOG = LogFactory.getLog(ToXML.class); + /** + * the jdom document. + */ + private static Document document; - // the racine which is the database. - private static Element racine; - - // the jdom document. - private static Document document; - + /** + * {@inheritDoc} + */ @Override public void exportTo(NewBigTable bigTable, File file) { - racine = new Element("database"); - document = new Document(racine); + Element database = new Element("database"); + document = new Document(database); // Construction of the document. - forEachTable(bigTable, racine); + forEachTable(bigTable, database); - // the methods to print and to save the database in XML file. - //toSreen(); - if (file != null) { - save(file); - } + // print the jdom document. +// toSreen(); + save(file); } /** @@ -56,7 +56,7 @@ * @param bigTable the bigTable. * @param database the database Element. */ - private void forEachTable(NewBigTable bigTable, Element database) { + private void forEachTable(final NewBigTable bigTable, Element database) { // the tables name. Set<String> tablesNames = bigTable.getTablesNames(); @@ -80,25 +80,25 @@ * @param bigTable the bigTable. * @param table the table Element. */ - private void forEachColumn(NewBigTable bigTable, Element table) { + private void forEachColumn(final NewBigTable bigTable, Element table) { // the table name. String tableName = table.getAttributeValue("tableName"); // the columns name. Set<String> columnNames = bigTable.getColumnsNames(tableName); - for (String column : columnNames) { + for (String columnName : columnNames) { // new column Element. - Element columnElement = new Element("column"); + Element column = new Element("column"); // new attribute which contain the column name. - Attribute columnNameAttribute = new Attribute("columnName", column); - columnElement.setAttribute(columnNameAttribute); + Attribute columnNameAttr = new Attribute("columnName", columnName); + column.setAttribute(columnNameAttr); // we add the column to the table. - table.addContent(columnElement); + table.addContent(column); - // we search the columns of the table. - forEachCell(bigTable, columnElement); + // we search the cells of the table. + forEachCell(bigTable, column); } } @@ -107,7 +107,7 @@ * @param bigTable the bigTable. * @param column the column Element. */ - private void forEachCell(NewBigTable bigTable, Element column) { + private void forEachCell(final NewBigTable bigTable, Element column) { // the table name. String tableName = column.getParentElement().getAttributeValue("tableName"); @@ -124,27 +124,24 @@ if ( ! "".equals(content)) { // new cell Element. - Element cellElement = new Element("cell"); + Element cell = new Element("cell"); // new attribute which contain the cell key. - Attribute cellKeyAttribute = new Attribute("key", key); - cellElement.setAttribute(cellKeyAttribute); + Attribute cellKeyAttr = new Attribute("key", key); + cell.setAttribute(cellKeyAttr); // we add the cell to the column. - column.addContent(cellElement); + column.addContent(cell); // new value Element. - Element valueElement = new Element("value"); - // // new attribute which contain the value version. - // Attribute valueVersionAttribute = new Attribute("version", v); - // valueElement.setAttribute(valueVersionAttribute); - valueElement.setText(content); + Element value = new Element("value"); + value.setText(content); // we add the value to the cell. - cellElement.addContent(valueElement); + cell.addContent(value); } } catch (NoSuchElementException e) { -// LOG.warn(e, e); + LOG.trace(e, e); } } @@ -170,9 +167,11 @@ private static void save(File file) { try { XMLOutputter output = new XMLOutputter(Format.getPrettyFormat()); - output.output(document, new FileOutputStream(file)); - } catch (java.io.IOException e) { - LOG.error(e, e); + FileOutputStream fos = new FileOutputStream(file); + output.output(document, fos); + fos.close(); + } catch (IOException ex) { + LOG.error(ex, ex); } } } Added: trunk/msm-fromtoXML/src/main/java/org/nuiton/mapstoragemanager/plugins/importer/FromXML.java =================================================================== --- trunk/msm-fromtoXML/src/main/java/org/nuiton/mapstoragemanager/plugins/importer/FromXML.java (rev 0) +++ trunk/msm-fromtoXML/src/main/java/org/nuiton/mapstoragemanager/plugins/importer/FromXML.java 2010-03-01 18:25:21 UTC (rev 151) @@ -0,0 +1,172 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package org.nuiton.mapstoragemanager.plugins.importer; + +import java.io.File; +import java.io.IOException; +import java.util.List; +import java.util.NoSuchElementException; +import java.util.Set; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.jdom.Attribute; +import org.jdom.Document; +import org.jdom.Element; +import org.jdom.JDOMException; +import org.jdom.input.SAXBuilder; +import org.jdom.output.Format; +import org.jdom.output.XMLOutputter; +import org.nuiton.mapstoragemanager.plugins.Importer; +import org.nuiton.mapstoragemanager.plugins.NewBigTable; + +/** + * + * @author Dorian Langlais + */ +public class FromXML implements Importer { + + /** + * Logger. + */ + private static final Log LOG = LogFactory.getLog(FromXML.class); + /** + * the jdom document. + */ + private static Document document; + + /** + * {@inheritDoc} + */ + @Override + public void importFrom(NewBigTable bigTable, File file) { + try { + open(file); + + Element racine = document.getRootElement(); + + // Construction of the bigtable database. + forEachTable(bigTable, racine); + + // print the jdomDocument. +// toSreen(); + } catch (IOException ex) { + LOG.error(ex, ex); + } + } + + /** + * For eath table of the database, the document is constructed. + * @param bigTable the bigTable. + * @param database the database Element. + */ + private void forEachTable(NewBigTable bigTable, final Element database) { + // the tables name. + List<Element> tableElements = database.getChildren("table"); + + for (Element table : tableElements) { + // get table name and add to the bigTable. + String tableName = table.getAttributeValue("tableName"); + bigTable.createTable(tableName); + + // we search the columns of the table. + forEachColumn(bigTable, table); + } + } + + /** + * For eath column of the table, the document is constructed. + * @param bigTable the bigTable. + * @param table the table Element. + */ + private void forEachColumn(NewBigTable bigTable, final Element table) { + // the table name. + String tableName = table.getAttributeValue("tableName"); + + // the columns name. + List<Element> columnElements = table.getChildren("column"); + + for (Element column : columnElements) { + // get column name and add to the table. + String columnName = column.getAttributeValue("columnName"); + bigTable.createColumn(tableName, columnName); + + // we search the cells of the table. + forEachCell(bigTable, column); + } + } + + /** + * For eath cell of the column, the document is constructed. + * @param bigTable the bigTable. + * @param column the column Element. + */ + private void forEachCell(NewBigTable bigTable, final Element column) { + // the table name. + String tableName = + column.getParentElement().getAttributeValue("tableName"); + // the column name. + String columnName = column.getAttributeValue("columnName"); + + // the table keys. + Set<String> keys = bigTable.getKeys(tableName); + + String content = ""; + for (String key : keys) { + try{ + content = bigTable.get(tableName, columnName, key); + if ( ! "".equals(content)) { + + // new cell Element. + Element cell = new Element("cell"); + // new attribute which contain the cell key. + Attribute cellKeyAttr = new Attribute("key", key); + cell.setAttribute(cellKeyAttr); + + // we add the cell to the column. + column.addContent(cell); + + // new value Element. + Element value = new Element("value"); + value.setText(content); + + // we add the value to the cell. + cell.addContent(value); + } + } + catch (NoSuchElementException e) { + LOG.trace(e, e); + } + + } + } + + /** + * toSreen(). + * show the XML content on System.out + */ + static void toSreen() { + try { + XMLOutputter output = new XMLOutputter(Format.getPrettyFormat()); + output.output(document, System.out); + } catch (java.io.IOException e) { + LOG.error(e, e); + } + } + + /** + * Method to load the jdom document of the file. + * @param file the file from which the database is saved. + */ + private static void open(File file) throws IOException { + //On crée une instance de SAXBuilder + SAXBuilder saxBuilder = new SAXBuilder(); + try { + // we construct the jdom document from the file. + document = saxBuilder.build(file); + } catch (JDOMException ex) { + LOG.error(ex, ex); + } + } +} Modified: trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/exporter/ToXMLTest.java =================================================================== --- trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/exporter/ToXMLTest.java 2010-02-28 17:58:02 UTC (rev 150) +++ trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/exporter/ToXMLTest.java 2010-03-01 18:25:21 UTC (rev 151) @@ -6,8 +6,8 @@ package org.nuiton.mapstoragemanager.plugins.exporter; //import java.io.IOException; +import java.io.File; import java.lang.reflect.Field; -import java.util.List; import junit.framework.TestCase; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -20,6 +20,7 @@ import org.nuiton.mapstoragemanager.plugins.NewBigTable; import org.nuiton.mapstoragemanager.plugins.AssertJdomElement; import org.nuiton.mapstoragemanager.plugins.bighashmapv2.BigHashMap; +import org.nuiton.mapstoragemanager.plugins.getFile; /** * @@ -40,14 +41,13 @@ /** * Test to export a bigTable database in a xml format. */ - public void testToXML() { - // init actual et expected. + public void testExportTo() { createXMLDocument(); + // init actual et expected. Element actualRoot = actual.getRootElement(); Element expectedRoot = expected.getRootElement(); -// assertTrue(testEquals(expectedRoot, actualRoot)); AssertJdomElement.assertEquivalent(expectedRoot, actualRoot); } @@ -208,7 +208,10 @@ cell10.addContent(value10); Exporter exporter = new ToXML(); - exporter.exportTo(nbt, null); + File exportTest = + getFile.getTestFile("/src/test/resources/exportTest.xml"); + exporter.exportTo(nbt,exportTest); + exportTest.delete(); // expected.toString(); @@ -245,116 +248,9 @@ } /** - * Test if to Element are equal. - * @param expected expected element. - * @param actual actual element. - * @return true if expected and actual are equivalents. + * A main to test quickly. + * @param args args. */ - /*public static boolean testEquals(Element expected, Element actual) { - - boolean trace = true; -// String expectedContent = expected.getContent(); -// String actualContent = actual.getContent(); - - if(trace) { - System.out.println("---------------------------------------------"); - } -// System.out.println(expected.getContent()); -// System.out.println(actual.getContent()); - - String expectedElementName = expected.getName(); - String actualElementName = actual.getName(); - - if(!expectedElementName.equals(actualElementName)) { - return false; - } - - List<Attribute> expectedAttributes = expected.getAttributes(); - List<Attribute> actualAttributes = actual.getAttributes(); - - List<Element> expectedChildren = expected.getChildren(); - List<Element> actualChildren = actual.getChildren(); - - String expectedValue = expected.getValue(); - String actualValue = actual.getValue(); - - - // we compare attributes. - if (expectedAttributes.size() != actualAttributes.size()) { - return false; - } else if (expectedAttributes.size() == 1) { - // case with only one attribute. - String expectedAttribute = expectedAttributes.toString(); - String actualAttribute = actualAttributes.toString(); - if (! expectedAttribute.equals(actualAttribute)) { - return false; - } - } else { - // case with only many attribute. - // TODO -> we have only one attribute for instance. - } - - if(trace) { - System.out.println("Attribues"); - System.out.println(expectedAttributes); - System.out.println(actualAttributes); - -// System.out.println("Children"); -// System.out.println(expectedChildren); -// System.out.println(actualChildren); - -// System.out.println("Value"); -// System.out.println(expectedValue); -// System.out.println(actualValue); - } - - if (expectedChildren.size() != actualChildren.size()) { - return false; - } else if (expectedChildren.isEmpty()) { - if(trace) { - System.out.println(expectedValue); - System.out.println(actualValue); - } - return expected.getValue().equals(actual.getValue()); - } -// System.out.println(expectedChildren); -// System.out.println(actualChildren); - - boolean TF; - // same elements. - for (Element expectedChild : expectedChildren) { - TF = false; - for (Element actualChild : actualChildren) { - if(actualChild.getChildren().size() == - expectedChild.getChildren().size()) { - if (testEquals(expectedChild,actualChild)) { -// actualChildren.remove(actualChild); - TF = true; - if(trace) { - System.out.println("EQUIVALENT"); - System.out.println(expectedChild.getValue()); - System.out.println(actualChild.getValue()); - } - } else { - if(trace) { - System.out.println("NOT EQUIVALENT"); - System.out.println(expectedChild.getValue()); - System.out.println(actualChild.getValue()); - } - } - } - } - if (TF == false) { - if(trace) { - System.out.println("RETURN FALSE"); - } - return false; - } - } - - return true; - }*/ - public static void main(String[] args) { NewBigTable nbt = new BigHashMap(); nbt.createTable("table1"); @@ -380,6 +276,9 @@ nbt.put("table2", "column5", "10", "content10"); Exporter exporter = new ToXML(); - exporter.exportTo(nbt, null); + File exportTest = + getFile.getTestFile("/src/test/resources/exportTest.xml"); + exporter.exportTo(nbt,exportTest); + exportTest.delete(); } } Added: trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/getFile.java =================================================================== --- trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/getFile.java (rev 0) +++ trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/getFile.java 2010-03-01 18:25:21 UTC (rev 151) @@ -0,0 +1,52 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package org.nuiton.mapstoragemanager.plugins; + +import java.io.File; + +/** + * + * @author Dorian Langlais + */ +public class getFile { + + private static String basedir; + + public static File getTestFile(String path) { + return new File(getBasedir(), path); + } + + public static File getTestFile(String basedir, String path) { + File basedirFile = new File(basedir); + + if (!basedirFile.isAbsolute()) { + basedirFile = getTestFile(basedir); + } + + return new File(basedirFile, path); + } + + public static String getTestPath(String path) { + return getTestFile(path).getAbsolutePath(); + } + + public static String getTestPath(String basedir, String path) { + return getTestFile(basedir, path).getAbsolutePath(); + } + + public static String getBasedir() { + if (basedir != null) { + return basedir; + } + + basedir = System.getProperty("basedir"); + + if (basedir == null) { + basedir = new File("").getAbsolutePath(); + } + + return basedir; + } +} Added: trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/importer/FromXMLTest.java =================================================================== --- trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/importer/FromXMLTest.java (rev 0) +++ trunk/msm-fromtoXML/src/test/java/org/nuiton/mapstoragemanager/plugins/importer/FromXMLTest.java 2010-03-01 18:25:21 UTC (rev 151) @@ -0,0 +1,111 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package org.nuiton.mapstoragemanager.plugins.importer; + +import java.io.File; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import junit.framework.TestCase; +import org.nuiton.mapstoragemanager.plugins.Importer; +import org.nuiton.mapstoragemanager.plugins.NewBigTable; +import org.nuiton.mapstoragemanager.plugins.bighashmapv2.BigHashMap; +import org.nuiton.mapstoragemanager.plugins.getFile; + +/** + * + * @author Dorian Langlais + */ +public class FromXMLTest extends TestCase { + +// public void testImportFrom() { +// Assert.fail(); +// } + + /** + * Import the database from the file fiveTables and verify the content. + */ + public void testImportFromFiveTables() { + + File fiveTables = + getFile.getTestFile("/src/test/resources/fiveTables.xml"); + + // import fiveTables. + Importer importer = new FromXML(); + NewBigTable bigTable = new BigHashMap(); + + importer.importFrom(bigTable, fiveTables); + + Set<String> tableNamesExpected; + { + /** + * Init tableNames. + */ + tableNamesExpected = new HashSet<String>(); + tableNamesExpected.add("table1"); + tableNamesExpected.add("table2"); + tableNamesExpected.add("table3"); + tableNamesExpected.add("table4"); + tableNamesExpected.add("table5"); + } + Set<String> tableNamesActual = bigTable.getTablesNames(); + assertEquals(tableNamesExpected, tableNamesActual); + + + for (String tableName : tableNamesActual) { + Set<String> columnNamesExpected = new HashSet<String>(); + assertEquals(columnNamesExpected, + bigTable.getColumnsNames(tableName)); + } + } + + public void testImportFromTwoTablesFiveColumns() { + + File twoTablesFiveColumns = + getFile.getTestFile("/src/test/resources/twoTablesFiveColumns.xml"); + + // import fiveTables. + Importer importer = new FromXML(); + NewBigTable bigTable = new BigHashMap(); + + importer.importFrom(bigTable, twoTablesFiveColumns); + + Set<String> tableNamesExpected; + { + /** + * Init tableNames. + */ + tableNamesExpected = new HashSet<String>(); + tableNamesExpected.add("table1"); + tableNamesExpected.add("table2"); + } + Set<String> tableNamesActual = bigTable.getTablesNames(); + assertEquals(tableNamesExpected, tableNamesActual); + + Map<String, Set<String>> columnNamesExpected; + { + /** + * Init columnNames. + */ + columnNamesExpected = new HashMap<String, Set<String>>(); + Set<String> table1ColumnNames = new HashSet<String>(); + table1ColumnNames.add("column1"); + table1ColumnNames.add("column2"); + Set<String> table2ColumnNames = new HashSet<String>(); + table2ColumnNames.add("column3"); + table2ColumnNames.add("column4"); + table2ColumnNames.add("column5"); + columnNamesExpected.put("table1", table1ColumnNames); + columnNamesExpected.put("table2", table2ColumnNames); + } + + Set<String> columnNamesActual; + for (String tableName : tableNamesActual) { + columnNamesActual = bigTable.getColumnsNames(tableName); + assertEquals(columnNamesExpected.get(tableName), columnNamesActual); + } + } +} Added: trunk/msm-fromtoXML/src/test/resources/fiveTables.xml =================================================================== --- trunk/msm-fromtoXML/src/test/resources/fiveTables.xml (rev 0) +++ trunk/msm-fromtoXML/src/test/resources/fiveTables.xml 2010-03-01 18:25:21 UTC (rev 151) @@ -0,0 +1,9 @@ +<?xml version="1.0" encoding="UTF-8"?> +<database> + <table tableName="table1" /> + <table tableName="table2" /> + <table tableName="table3" /> + <table tableName="table4" /> + <table tableName="table5" /> +</database> + Added: trunk/msm-fromtoXML/src/test/resources/twoTablesFiveColumns.xml =================================================================== --- trunk/msm-fromtoXML/src/test/resources/twoTablesFiveColumns.xml (rev 0) +++ trunk/msm-fromtoXML/src/test/resources/twoTablesFiveColumns.xml 2010-03-01 18:25:21 UTC (rev 151) @@ -0,0 +1,13 @@ +<?xml version="1.0" encoding="UTF-8"?> +<database> + <table tableName="table1"> + <column columnName="column1" /> + <column columnName="column2" /> + </table> + <table tableName="table2"> + <column columnName="column3" /> + <column columnName="column4" /> + <column columnName="column5" /> + </table> +</database> +