Search in sources :

Example 11 with DataSourcePropertiesInterface

use of com.sldeditor.common.DataSourcePropertiesInterface in project sldeditor by robward-scisys.

the class DataSourceConnectorTest method testGetConnectionProperties.

/**
 * Test method for
 * {@link com.sldeditor.datasource.connector.instance.DataSourceConnector#getConnectionProperties(com.sldeditor.DataSourcePropertiesInterface)}.
 */
@Test
public void testGetConnectionProperties() {
    DataSourceConnector dsc = new DataSourceConnector();
    assertNull(dsc.getConnectionProperties(null));
    Map<String, Object> expectedPropertyMap = new HashMap<String, Object>();
    expectedPropertyMap.put("server", "localhost");
    expectedPropertyMap.put("port", "5432");
    expectedPropertyMap.put("database", "testdb");
    expectedPropertyMap.put("user", "testuser");
    expectedPropertyMap.put("password", "pasword123");
    DataSourcePropertiesInterface dataSource = new DataSourceProperties(dsc);
    dataSource.setPropertyMap(expectedPropertyMap);
    assertEquals(expectedPropertyMap, dsc.getConnectionProperties(dataSource));
}
Also used : DataSourceConnector(com.sldeditor.datasource.connector.instance.DataSourceConnector) HashMap(java.util.HashMap) DataSourcePropertiesInterface(com.sldeditor.common.DataSourcePropertiesInterface) DataSourceProperties(com.sldeditor.datasource.impl.DataSourceProperties) Test(org.junit.Test)

Example 12 with DataSourcePropertiesInterface

use of com.sldeditor.common.DataSourcePropertiesInterface in project sldeditor by robward-scisys.

the class DataSourcePropertiesTest method testEncodeDecodeXML.

/**
 * Test method for {@link com.sldeditor.datasource.impl.DataSourceProperties#encodeXML(org.w3c.dom.Document, org.w3c.dom.Element, java.lang.String)}.
 * Test method for {@link com.sldeditor.datasource.impl.DataSourceProperties#decodeXML(org.w3c.dom.Document, java.lang.String)}.
 */
@Test
public void testEncodeDecodeXML() {
    Map<String, Object> propertyMap = new HashMap<String, Object>();
    propertyMap.put("field1", "value1");
    propertyMap.put("field2", "value2");
    propertyMap.put("field3", "value3");
    DataSourceConnectorInterface dsc = new DataSourceConnector();
    DataSourceProperties dsp = new DataSourceProperties(dsc);
    dsp.setPropertyMap(propertyMap);
    dsp.setFilename("testfilename.shp");
    dsp.setPassword("top secret password");
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = null;
    try {
        documentBuilder = documentBuilderFactory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
    Document doc = documentBuilder.newDocument();
    String elementName = "test_data_source_properties";
    Element root = doc.createElement("test");
    doc.appendChild(root);
    dsp.encodeXML(doc, root, elementName);
    // Try null parameters
    dsp.encodeXML(null, null, null);
    assertNull(DataSourceProperties.decodeXML(null, null));
    // Now decode XML
    DataSourcePropertiesInterface decodeDSP = DataSourceProperties.decodeXML(doc, elementName);
    assertEquals(dsp.getConnectionProperties(), decodeDSP.getConnectionProperties());
}
Also used : DataSourceConnector(com.sldeditor.datasource.connector.instance.DataSourceConnector) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) HashMap(java.util.HashMap) Element(org.w3c.dom.Element) DataSourceProperties(com.sldeditor.datasource.impl.DataSourceProperties) Document(org.w3c.dom.Document) DataSourcePropertiesInterface(com.sldeditor.common.DataSourcePropertiesInterface) DocumentBuilder(javax.xml.parsers.DocumentBuilder) DataSourceConnectorInterface(com.sldeditor.common.DataSourceConnectorInterface) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Test(org.junit.Test)

Example 13 with DataSourcePropertiesInterface

use of com.sldeditor.common.DataSourcePropertiesInterface in project sldeditor by robward-scisys.

the class SLDEditorFileHandler method save.

/*
     * (non-Javadoc)
     * 
     * @see com.sldeditor.extension.input.file.FileHandlerInterface#save(com.sldeditor.ui.iface.SLDDataInterface)
     */
@Override
public boolean save(SLDDataInterface sldData) {
    if (sldData == null) {
        return false;
    }
    File fileToSave = sldData.getSldEditorFile();
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder;
    try {
        documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document doc = documentBuilder.newDocument();
        Element root = doc.createElement(SLDEditorFileHandler.ROOT_ELEMENT);
        doc.appendChild(root);
        // SLD contents
        Element sldElement = doc.createElement(SLDEditorFileHandler.SLD_ELEMENT);
        sldElement.appendChild(doc.createTextNode(sldData.getSLDFile().getAbsolutePath()));
        root.appendChild(sldElement);
        // Vendor options
        List<VersionData> vendorOptionList = sldData.getVendorOptionList();
        if (vendorOptionList != null) {
            for (VersionData versionData : vendorOptionList) {
                Element vendorOptionElement = doc.createElement(SLDEditorFileHandler.VENDOR_OPTION_ELEMENT);
                VendorOptionVersion vendorOption = new VendorOptionVersion(versionData.getVendorOptionType(), versionData);
                vendorOptionElement.appendChild(doc.createTextNode(vendorOption.toString()));
                root.appendChild(vendorOptionElement);
            }
        }
        // Write out the data source connector
        DataSourcePropertiesInterface dataSourceProperties = sldData.getDataSourceProperties();
        if (dataSourceProperties != null) {
            dataSourceProperties.encodeXML(doc, root, SLDEditorFileHandler.DATASOURCE_ELEMENT);
        }
        // Environment variables
        Element envVarListElement = doc.createElement(SLDEditorFileHandler.ENVVARLIST_ELEMENT);
        root.appendChild(envVarListElement);
        List<EnvVar> envVarList = sldData.getEnvVarList();
        if (envVarList != null) {
            for (EnvVar envVar : envVarList) {
                Element envVarElement = doc.createElement(SLDEditorFileHandler.ENVVAR_ELEMENT);
                envVarElement.setAttribute(SLDEditorFileHandler.ENVVAR_NAME_ATTRIBUTE, envVar.getName());
                envVarElement.setAttribute(SLDEditorFileHandler.ENVVAR_TYPE_ATTRIBUTE, envVar.getType().getName());
                if (envVar.getValue() != null) {
                    String value = envVar.getValue().toString();
                    envVarElement.setAttribute(SLDEditorFileHandler.ENVVAR_VALUE_ATTRIBUTE, value);
                }
                envVarListElement.appendChild(envVarElement);
            }
        }
        // Write out the legend options
        LegendOptionData legendOptions = sldData.getLegendOptions();
        if (legendOptions != null) {
            legendOptions.encodeXML(doc, root, SLDEditorFileHandler.LEGEND_OPTION_ELEMENT);
        }
        // Output the XML file contents
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, SLDEditorFileHandler.YES);
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, SLDEditorFileHandler.YES);
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        StringWriter stringWriter = new StringWriter();
        transformer.transform(new DOMSource(doc), new StreamResult(stringWriter));
        String outputXML = stringWriter.getBuffer().toString();
        BufferedWriter writer = null;
        try {
            writer = new BufferedWriter(new FileWriter(fileToSave));
            writer.write(outputXML);
        } catch (IOException e) {
        // Do nothing
        } finally {
            try {
                if (writer != null) {
                    writer.close();
                }
            } catch (IOException e) {
            // Do nothing
            }
        }
    } catch (ParserConfigurationException e) {
        ConsoleManager.getInstance().exception(this, e);
    } catch (TransformerConfigurationException e) {
        ConsoleManager.getInstance().exception(this, e);
    } catch (TransformerFactoryConfigurationError e) {
        ConsoleManager.getInstance().exception(this, e.getException());
    } catch (TransformerException e) {
        ConsoleManager.getInstance().exception(this, e);
    }
    return true;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Transformer(javax.xml.transform.Transformer) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) Element(org.w3c.dom.Element) FileWriter(java.io.FileWriter) Document(org.w3c.dom.Document) BufferedWriter(java.io.BufferedWriter) StringWriter(java.io.StringWriter) EnvVar(com.sldeditor.filter.v2.envvar.EnvVar) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) TransformerException(javax.xml.transform.TransformerException) TransformerFactoryConfigurationError(javax.xml.transform.TransformerFactoryConfigurationError) TransformerFactory(javax.xml.transform.TransformerFactory) StreamResult(javax.xml.transform.stream.StreamResult) IOException(java.io.IOException) DataSourcePropertiesInterface(com.sldeditor.common.DataSourcePropertiesInterface) LegendOptionData(com.sldeditor.ui.legend.option.LegendOptionData) DocumentBuilder(javax.xml.parsers.DocumentBuilder) VersionData(com.sldeditor.common.vendoroption.VersionData) VendorOptionVersion(com.sldeditor.common.vendoroption.VendorOptionVersion) SLDEditorFile(com.sldeditor.datasource.SLDEditorFile) File(java.io.File)

Example 14 with DataSourcePropertiesInterface

use of com.sldeditor.common.DataSourcePropertiesInterface in project sldeditor by robward-scisys.

the class SLDDataTest method testDataSourceProperties.

/**
 * Test data source properties.
 */
@Test
public void testDataSourceProperties() {
    DataSourcePropertiesInterface dataSourceProperties = new DummyDataSourceProperties();
    SLDData data = new SLDData(null, null);
    data.setDataSourceProperties(dataSourceProperties);
    assertEquals(dataSourceProperties, data.getDataSourceProperties());
    // Increase code coverage
    data.setDataSourceProperties(dataSourceProperties);
    assertEquals(dataSourceProperties, data.getDataSourceProperties());
}
Also used : SLDData(com.sldeditor.common.data.SLDData) DataSourcePropertiesInterface(com.sldeditor.common.DataSourcePropertiesInterface) Test(org.junit.Test)

Example 15 with DataSourcePropertiesInterface

use of com.sldeditor.common.DataSourcePropertiesInterface in project sldeditor by robward-scisys.

the class DataSourceConnectorEmptyTest method testGetConnectionProperties.

/**
 * Test method for {@link com.sldeditor.datasource.connector.instance.DataSourceConnectorEmpty#getConnectionProperties(com.sldeditor.DataSourcePropertiesInterface)}.
 */
@Test
public void testGetConnectionProperties() {
    DataSourceConnectorEmpty dsc = new DataSourceConnectorEmpty();
    assertNull(dsc.getConnectionProperties(null));
    DataSourcePropertiesInterface noDataSource = DataSourceConnectorFactory.getNoDataSource();
    Map<String, String> expectedPropertyMap = new HashMap<String, String>();
    assertEquals(expectedPropertyMap, dsc.getConnectionProperties(noDataSource));
}
Also used : DataSourceConnectorEmpty(com.sldeditor.datasource.connector.instance.DataSourceConnectorEmpty) HashMap(java.util.HashMap) DataSourcePropertiesInterface(com.sldeditor.common.DataSourcePropertiesInterface) Test(org.junit.Test)

Aggregations

DataSourcePropertiesInterface (com.sldeditor.common.DataSourcePropertiesInterface)21 Test (org.junit.Test)9 File (java.io.File)8 DataSourceConnectorInterface (com.sldeditor.common.DataSourceConnectorInterface)7 SLDEditorFile (com.sldeditor.datasource.SLDEditorFile)7 SLDDataInterface (com.sldeditor.common.SLDDataInterface)5 DataSourceConnector (com.sldeditor.datasource.connector.instance.DataSourceConnector)4 DataSourceProperties (com.sldeditor.datasource.impl.DataSourceProperties)4 HashMap (java.util.HashMap)4 SLDData (com.sldeditor.common.data.SLDData)3 DataSourceInterface (com.sldeditor.datasource.DataSourceInterface)3 IOException (java.io.IOException)3 MalformedURLException (java.net.MalformedURLException)3 DocumentBuilder (javax.xml.parsers.DocumentBuilder)3 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)3 StyleWrapper (com.sldeditor.common.data.StyleWrapper)2 VersionData (com.sldeditor.common.vendoroption.VersionData)2 DatabaseClientInterface (com.sldeditor.extension.filesystem.database.client.DatabaseClientInterface)2 EnvVar (com.sldeditor.filter.v2.envvar.EnvVar)2 LegendOptionData (com.sldeditor.ui.legend.option.LegendOptionData)2