Author: dlanglais Date: 2010-03-09 12:37:24 +0100 (Tue, 09 Mar 2010) New Revision: 201 Log: Export ?\195?\160 l'aide de XPP3 fait et test?\195?\169 Added: trunk/msm-fromtoXPP3/src/test/java/org/nuiton/mapstoragemanager/plugins/exporter/ToXMLXPP3Test.java Modified: trunk/msm-fromtoXPP3/ trunk/msm-fromtoXPP3/pom.xml trunk/msm-fromtoXPP3/src/main/java/org/nuiton/mapstoragemanager/plugins/exporter/ToXMLXPP3.java Property changes on: trunk/msm-fromtoXPP3 ___________________________________________________________________ Added: svn:ignore + target Modified: trunk/msm-fromtoXPP3/pom.xml =================================================================== --- trunk/msm-fromtoXPP3/pom.xml 2010-03-09 10:53:28 UTC (rev 200) +++ trunk/msm-fromtoXPP3/pom.xml 2010-03-09 11:37:24 UTC (rev 201) @@ -83,7 +83,8 @@ <name>MSM-FromToXPP3</name> - <description>Plugin for MapStorageManager to work with actuals HBase</description> + <description>Plugin to import/export database content from/to xml files + using XPP3.</description> <inceptionYear>2010</inceptionYear> <developers> Modified: trunk/msm-fromtoXPP3/src/main/java/org/nuiton/mapstoragemanager/plugins/exporter/ToXMLXPP3.java =================================================================== --- trunk/msm-fromtoXPP3/src/main/java/org/nuiton/mapstoragemanager/plugins/exporter/ToXMLXPP3.java 2010-03-09 10:53:28 UTC (rev 200) +++ trunk/msm-fromtoXPP3/src/main/java/org/nuiton/mapstoragemanager/plugins/exporter/ToXMLXPP3.java 2010-03-09 11:37:24 UTC (rev 201) @@ -1,15 +1,30 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ package org.nuiton.mapstoragemanager.plugins.exporter; -import org.nuiton.mapstoragemanager.plugins.importer.*; import java.io.File; +import java.io.FileOutputStream; +import java.io.FileWriter; +import java.io.IOException; import javax.swing.filechooser.FileFilter; import javax.swing.filechooser.FileNameExtensionFilter; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.jdom.Document; +import org.jdom.output.Format; +import org.jdom.output.XMLOutputter; +import org.nuiton.mapstoragemanager.plugins.Exporter; import org.nuiton.mapstoragemanager.plugins.BigTable; -import org.nuiton.mapstoragemanager.plugins.Exporter; + +import org.xmlpull.v1.XmlPullParserException; +import org.xmlpull.v1.XmlPullParserFactory; +import org.xmlpull.v1.XmlSerializer; + + /** - * A XML Parser - using pullparsing - to import database from xml files. + * A Class to export in xml files the content of Hbase database. * @author Dorian Langlais */ public class ToXMLXPP3 implements Exporter { @@ -18,46 +33,187 @@ * Logger. */ private static final Log LOG = LogFactory.getLog(ToXMLXPP3.class); - /** - * Parsing State. + * the jdom document. */ - private ParsingState state; + private static Document document; /** - * Current Table, needed when create the database to know in which table - * we are to add a new column. - */ - private String currentTable; - /** * the fileFilter. */ private static FileFilter fileFilter; /** + * NameSpace. + */ + private static final String NAMESPACE = ""; + + /** * Constructor. */ { fileFilter = new FileNameExtensionFilter("Fichiers XML", "xml"); } + /** + * {@inheritDoc} + */ + @Override + public final void exportTo(final BigTable bigTable, final File file) { + + XmlSerializer serializer = null; + /** + * Initialisation of the serializer. + */ + try { + XmlPullParserFactory factory = + XmlPullParserFactory.newInstance( + System.getProperty(XmlPullParserFactory.PROPERTY_NAME), null); + serializer = factory.newSerializer(); + } catch (XmlPullParserException ex) { + LOG.fatal(ex, ex); + } + + try { + + serializer.setOutput(new FileWriter(file)); + + serializer.startTag(NAMESPACE, "database"); + + { + this.describe(bigTable, serializer); + + this.insert(bigTable, serializer); + } + + serializer.endTag(NAMESPACE, "database"); + + serializer.endDocument(); + + } catch (IOException ex) { + LOG.fatal(ex, ex); + } catch (IllegalArgumentException ex) { + LOG.fatal(ex, ex); + } catch (IllegalStateException ex) { + LOG.fatal(ex, ex); + } + } + /** - * {@inheritDoc} + * Write the description of the database. + * @param bigTable the bigTable. + * @param serializer the xmlSerializer. */ - public void exportTo(BigTable bigTable, File file) { - throw new UnsupportedOperationException("Not supported yet."); + private void describe(final BigTable bigTable, final XmlSerializer + serializer) throws IllegalArgumentException, IllegalStateException, + IOException { + + serializer.startTag(NAMESPACE, "describe"); + + /** for each Table */ + for (String tableName : bigTable.getTablesNames()) { + + serializer.startTag(NAMESPACE, "table"); + serializer.attribute(NAMESPACE, "tableName", tableName); + + /** for each Column */ + for (String columnName : bigTable.getColumnsNames(tableName)) { + serializer.startTag(NAMESPACE, "column") + .attribute(null, "columnName", columnName) + .endTag(NAMESPACE, "column"); + + } + + serializer.endTag(NAMESPACE, "table"); + } + serializer.endTag(NAMESPACE, "describe"); } /** - * {@inheritDoc} + * Write the elements of the database. + * @param bigTable the bigTable. + * @param serializer the xmlSerializer. */ - public FileFilter getFileFilter() { - return fileFilter; + private void insert(final BigTable bigTable, final XmlSerializer + serializer) throws IllegalArgumentException, IllegalStateException, + IOException { + + String value; + + serializer.startTag(NAMESPACE, "insert"); + + /** for each Table */ + for (String tableName : bigTable.getTablesNames()) { + + /** for each Column */ + for (String columnName : bigTable.getColumnsNames(tableName)) { + + /** for each Key */ + for (String key : bigTable.getKeys(tableName)) { + value = bigTable.get(tableName, columnName, key); + + /** if not "void" */ + if (value != null && !"".equals(value)) { + + serializer.startTag(NAMESPACE, "element") + .attribute(NAMESPACE, "table", tableName) + .attribute(NAMESPACE, "column", columnName) + .attribute(NAMESPACE, "key", key) + .attribute(NAMESPACE, "value", value) + .endTag(NAMESPACE, "element"); + } + } + + } + } + serializer.endTag(NAMESPACE, "insert"); } /** + * 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 save the file. + * @param file the file in which we save the database. + */ + private static void save(final File file) { + long t1 = System.currentTimeMillis(); + try { + XMLOutputter output = new XMLOutputter(Format.getPrettyFormat()); + FileOutputStream fos = new FileOutputStream(file); +// GZIPOutputStream gzos = new GZIPOutputStream(fos); +// output.output(document, gzos); + output.output(document, fos); + fos.close(); + long t2 = System.currentTimeMillis(); + LOG.info("temps d'enregistrement : " + (t2 - t1) + " ms."); + } catch (IOException ex) { + LOG.error(ex, ex); + } + } + + /** * {@inheritDoc} */ - public String getPluginName() { + @Override + public final String getPluginName() { return this.getClass().getSimpleName(); } + + /** + * {@inheritDoc} + */ + @Override + public final FileFilter getFileFilter() { + return fileFilter; + } } Added: trunk/msm-fromtoXPP3/src/test/java/org/nuiton/mapstoragemanager/plugins/exporter/ToXMLXPP3Test.java =================================================================== --- trunk/msm-fromtoXPP3/src/test/java/org/nuiton/mapstoragemanager/plugins/exporter/ToXMLXPP3Test.java (rev 0) +++ trunk/msm-fromtoXPP3/src/test/java/org/nuiton/mapstoragemanager/plugins/exporter/ToXMLXPP3Test.java 2010-03-09 11:37:24 UTC (rev 201) @@ -0,0 +1,227 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package org.nuiton.mapstoragemanager.plugins.exporter; + +import java.io.File; +import junit.framework.TestCase; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.mapstoragemanager.plugins.BigTable; +import org.nuiton.mapstoragemanager.plugins.Exporter; +import org.nuiton.mapstoragemanager.plugins.Importer; +import org.nuiton.mapstoragemanager.plugins.bighashmapv2.BigHashMapV2; +import org.nuiton.mapstoragemanager.plugins.getFile; +import org.nuiton.mapstoragemanager.plugins.importer.FromXMLXPP3; + +/** + * + * @author E054030D + */ +public class ToXMLXPP3Test extends TestCase { + + /** + * Logger. + */ + private static final Log LOG = LogFactory.getLog(ToXMLXPP3Test.class); + + /** + * BigTable actual. + */ + BigTable actual; + /** + * BigTable expected. + */ + BigTable expected; + + + /** + * Test to export a bigTable database with empty Tables. + */ + public void testEmptyTables() { + + Exporter exporter = new ToXMLXPP3(); + + expected = new BigHashMapV2(); + actual = new BigHashMapV2(); + { + expected.createTable("table1"); + expected.createTable("table2"); + } + + File exportTest = + getFile.getTestFile("/src/test/resources/exportTest.xml"); + + exporter.exportTo(expected,exportTest); + + Importer importer = new FromXMLXPP3(); + importer.importFrom(actual, exportTest); + + /** + * Same tables. + */ + assertEquals(expected.getTablesNames(), + actual.getTablesNames()); + + exportTest.delete(); + } + + /** + * Test to export a bigTable database with empty columns. + */ + public void testEmptyColumns() { + + Exporter exporter = new ToXMLXPP3(); + + expected = new BigHashMapV2(); + actual = new BigHashMapV2(); + { + expected.createTable("table1"); + expected.createTable("table2"); + + expected.createColumn("table1", "column1"); + expected.createColumn("table1", "column2"); + expected.createColumn("table2", "column3"); + expected.createColumn("table2", "column4"); + expected.createColumn("table2", "column5"); + } + + File exportTest = + getFile.getTestFile("/src/test/resources/exportTest.xml"); + + exporter.exportTo(expected,exportTest); + + Importer importer = new FromXMLXPP3(); + importer.importFrom(actual, exportTest); + + /** + * Same tables. + */ + assertEquals(expected.getTablesNames(), + actual.getTablesNames()); + /** + * Same columns. + */ + for(String tableName : expected.getTablesNames()) { + assertEquals(expected.getColumnsNames(tableName), + actual.getColumnsNames(tableName)); + } + + exportTest.delete(); + } + + /** + * Test to export a bigTable database with empty columns. + */ + public void testWithElements() { + + Exporter exporter = new ToXMLXPP3(); + + expected = new BigHashMapV2(); + actual = new BigHashMapV2(); + { + expected.createTable("table1"); + expected.createTable("table2"); + + expected.createColumn("table1", "column1"); + expected.createColumn("table1", "column2"); + expected.createColumn("table2", "column3"); + expected.createColumn("table2", "column4"); + expected.createColumn("table2", "column5"); + + expected.put("table1", "column1", "1", "content1"); + expected.put("table1", "column1", "2", "content2"); + expected.put("table1", "column2", "3", "content3"); + expected.put("table1", "column2", "4", "content4"); + expected.put("table2", "column3", "5", "content5"); + expected.put("table2", "column3", "6", "content6"); + expected.put("table2", "column4", "7", "content7"); + expected.put("table2", "column4", "8", "content8"); + expected.put("table2", "column5", "9", "content9"); + expected.put("table2", "column5", "10", "content10"); + } + + File exportTest = + getFile.getTestFile("/src/test/resources/exportTest.xml"); + + exporter.exportTo(expected,exportTest); + + Importer importer = new FromXMLXPP3(); + importer.importFrom(actual, exportTest); + + /** + * Same tables. + */ + assertEquals(expected.getTablesNames(), + actual.getTablesNames()); + /** + * Same columns. + */ + for(String tableName : expected.getTablesNames()) { + assertEquals(expected.getColumnsNames(tableName), + actual.getColumnsNames(tableName)); + } + /** + * Same elements. + */ + String contentExpected, contentActual; + + for(String tableName : expected.getTablesNames()) { + + LOG.info("table : " + tableName); + for(String columnName : expected.getColumnsNames(tableName)) { + + LOG.info("column : " + columnName); + for(String key : expected.getKeys(tableName)) { + + LOG.info("key : " + key); + contentExpected = expected.get(tableName, columnName, key); + contentActual = actual.get(tableName, columnName, key); + + LOG.info("expected : " + contentExpected); + LOG.info("actual : " + contentActual); + if(contentActual!=null && !"".equals(contentActual)) { + assertEquals(contentExpected, contentActual); + } + } + } + } + + exportTest.delete(); + } + + public static void main(String[] args) { + + Exporter exporter = new ToXMLXPP3(); + + BigTable bigTable = new BigHashMapV2(); + { + bigTable.createTable("table1"); + bigTable.createTable("table2"); + bigTable.createColumn("table1", "column1"); + bigTable.createColumn("table1", "column2"); + bigTable.createColumn("table2", "column3"); + bigTable.createColumn("table2", "column4"); + bigTable.createColumn("table2", "column5"); + + bigTable.put("table1", "column1", "1", "content1"); + bigTable.put("table1", "column1", "2", "content2"); + bigTable.put("table1", "column2", "3", "content3"); + bigTable.put("table1", "column2", "4", "content4"); + + bigTable.put("table2", "column3", "5", "content5"); + bigTable.put("table2", "column3", "6", "content6"); + bigTable.put("table2", "column4", "7", "content7"); + bigTable.put("table2", "column4", "8", "content8"); + bigTable.put("table2", "column5", "9", "content9"); + bigTable.put("table2", "column5", "10", "content10"); + } + + File exportTest = + getFile.getTestFile("/src/test/resources/exportTest.xml"); + exporter.exportTo(bigTable,exportTest); + exportTest.delete(); + } +}