Search in sources :

Example 1 with DataSourcePropertiesInterface

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

the class SLDEditorFileHandler method readSLDEditorFile.

/**
 * Read sld editor file.
 *
 * @param file the file
 * @return the SLD data
 */
private SLDDataInterface readSLDEditorFile(File file) {
    SLDDataInterface sldData = null;
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder;
    try {
        documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.parse(file);
        String sldFile = extractTextData(document, SLDEditorFileHandler.SLD_ELEMENT);
        DataSourcePropertiesInterface dataSourceProperties = DataSourceProperties.decodeXML(document, SLDEditorFileHandler.DATASOURCE_ELEMENT);
        List<VersionData> vendorOptionList = extractVendorOptionData(document, SLDEditorFileHandler.VENDOR_OPTION_ELEMENT);
        File f = new File(sldFile);
        String sldContents = readFile(f, Charset.defaultCharset());
        sldData = new SLDData(new StyleWrapper(sldFile), sldContents);
        sldData.setDataSourceProperties(dataSourceProperties);
        sldData.setVendorOptionList(vendorOptionList);
        List<DataSourceAttributeData> fieldList = null;
        sldData.setFieldList(fieldList);
        sldData.setSLDFile(f);
        sldData.setReadOnly(false);
        sldData.setSldEditorFile(file);
        List<EnvVar> envVarList = extractEnvironmentVariables(document, SLDEditorFileHandler.ENVVAR_ELEMENT);
        sldData.setEnvVarList(envVarList);
        LegendOptionData legendOption = LegendOptionData.decodeXML(document, SLDEditorFileHandler.LEGEND_OPTION_ELEMENT);
        sldData.setLegendOptions(legendOption);
    } catch (ParserConfigurationException | SAXException | IOException e) {
        ConsoleManager.getInstance().exception(this, e);
    }
    return sldData;
}
Also used : SLDData(com.sldeditor.common.data.SLDData) DataSourceAttributeData(com.sldeditor.datasource.attribute.DataSourceAttributeData) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) IOException(java.io.IOException) Document(org.w3c.dom.Document) DataSourcePropertiesInterface(com.sldeditor.common.DataSourcePropertiesInterface) LegendOptionData(com.sldeditor.ui.legend.option.LegendOptionData) SAXException(org.xml.sax.SAXException) SLDDataInterface(com.sldeditor.common.SLDDataInterface) DocumentBuilder(javax.xml.parsers.DocumentBuilder) StyleWrapper(com.sldeditor.common.data.StyleWrapper) VersionData(com.sldeditor.common.vendoroption.VersionData) EnvVar(com.sldeditor.filter.v2.envvar.EnvVar) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SLDEditorFile(com.sldeditor.datasource.SLDEditorFile) File(java.io.File)

Example 2 with DataSourcePropertiesInterface

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

the class SLDFileEditorHandlerTest method testGetSLDContents.

/**
 * Test method for {@link
 * com.sldeditor.extension.filesystem.file.sldeditor.SLDEditorFileHandler#getSLDContents(com.sldeditor.common.NodeInterface)}.
 */
@Test
public void testGetSLDContents() {
    assertNull(new SLDEditorFileHandler().getSLDContents(null));
    URL url = SLDFileEditorHandlerTest.class.getResource("/point/sld");
    File parent = null;
    try {
        parent = new File(url.toURI());
    } catch (URISyntaxException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
    System.out.println(url.toString());
    try {
        FileTreeNode fileTreeNode = new FileTreeNode(parent, "point_attribute_sldeditor.sld");
        SLDFileHandler handler = new SLDFileHandler();
        List<SLDDataInterface> sldDataList = handler.getSLDContents(fileTreeNode);
        assertEquals(1, sldDataList.size());
        // Changes where the file is to be saved to
        File sldEditorFile = File.createTempFile(getClass().getSimpleName(), ".sldeditor");
        SLDData sldData = (SLDData) sldDataList.get(0);
        // Environment variables
        List<EnvVar> envVarList = new ArrayList<EnvVar>();
        envVarList.add(new EnvVar("notset", String.class, false));
        EnvVar envVar = new EnvVar("test", String.class, false);
        envVar.setValue("value");
        envVarList.add(envVar);
        EnvVar envVarBuiltIn = new EnvVar("wms_width", Integer.class, true);
        envVarBuiltIn.setValue(42);
        envVarList.add(envVarBuiltIn);
        sldData.setEnvVarList(envVarList);
        // Vendor options
        List<VersionData> vendorOptionList = new ArrayList<VersionData>();
        vendorOptionList.add(VersionData.decode(getClass(), "1.2.3"));
        sldData.setVendorOptionList(vendorOptionList);
        // Data sources
        DataSourcePropertiesInterface dataSourceProperties = DataSourceConnectorFactory.getDataSourceProperties("test.shp");
        sldData.setDataSourceProperties(dataSourceProperties);
        sldData.setSldEditorFile(sldEditorFile);
        SLDEditorFileHandler editorFileHandler = new SLDEditorFileHandler();
        assertFalse(editorFileHandler.save(null));
        assertTrue(editorFileHandler.save(sldData));
        SLDEditorFileHandler editorFileHandler2 = new SLDEditorFileHandler();
        List<SLDDataInterface> actualSldDataList = editorFileHandler2.open(sldEditorFile);
        assertEquals(1, actualSldDataList.size());
        FileTreeNode editorFileTreeNode = new FileTreeNode(sldEditorFile.getParentFile(), sldEditorFile.getName());
        List<SLDDataInterface> actualSldDataList2 = editorFileHandler2.getSLDContents(editorFileTreeNode);
        assertEquals(1, actualSldDataList2.size());
        sldEditorFile.delete();
    } catch (SecurityException e) {
        e.printStackTrace();
        fail(e.getMessage());
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        fail(e.getMessage());
    } catch (IOException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : SLDData(com.sldeditor.common.data.SLDData) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) SLDEditorFileHandler(com.sldeditor.extension.filesystem.file.sldeditor.SLDEditorFileHandler) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) DataSourcePropertiesInterface(com.sldeditor.common.DataSourcePropertiesInterface) URL(java.net.URL) FileTreeNode(com.sldeditor.datasource.extension.filesystem.node.file.FileTreeNode) SLDDataInterface(com.sldeditor.common.SLDDataInterface) VersionData(com.sldeditor.common.vendoroption.VersionData) EnvVar(com.sldeditor.filter.v2.envvar.EnvVar) SLDFileHandler(com.sldeditor.extension.filesystem.file.sld.SLDFileHandler) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 3 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.jupiter.api.Test)

Example 4 with DataSourcePropertiesInterface

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

the class VectorTool method importFile.

/**
 * Import file.
 *
 * @param fileTreeNode the file tree node
 */
protected boolean importFile(FileTreeNode fileTreeNode) {
    if (fileTreeNode != null) {
        File vectorFile = fileTreeNode.getFile();
        ConsoleManager.getInstance().information(this, String.format(MESSAGE_FORMAT, Localisation.getString(VectorTool.class, "VectorTool.createSymbol"), vectorFile.getAbsolutePath()));
        SLDDataInterface sldData = vectorReader.createVectorSLDData(vectorFile);
        DataSourcePropertiesInterface dsProperties = SLDEditorFile.getInstance().getDataSource();
        DataSourceConnectorInterface dsc = DataSourceConnectorFactory.getDataSource();
        try {
            String vectorFilename = vectorFile.toURI().toURL().toString();
            dsProperties = dsc.getDataSourceProperties(DataSourceProperties.encodeFilename(vectorFilename));
        } catch (MalformedURLException exceptionObj) {
            ConsoleManager.getInstance().exception(VectorTool.class, exceptionObj);
            return false;
        }
        loadSymbol(dsProperties, sldData, vectorFile);
    }
    return true;
}
Also used : MalformedURLException(java.net.MalformedURLException) SLDDataInterface(com.sldeditor.common.SLDDataInterface) DataSourceConnectorInterface(com.sldeditor.common.DataSourceConnectorInterface) DataSourcePropertiesInterface(com.sldeditor.common.DataSourcePropertiesInterface) SLDEditorFile(com.sldeditor.datasource.SLDEditorFile) File(java.io.File)

Example 5 with DataSourcePropertiesInterface

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

the class DataSourceConnectorFactoryTest method testGetDataSourcePropertiesMapOfStringString.

/**
 * Test method for {@link
 * com.sldeditor.datasource.connector.DataSourceConnectorFactory#getDataSourceProperties(java.util.Map)}.
 */
@Test
public void testGetDataSourcePropertiesMapOfStringString() {
    Map<String, Object> propertyMap = new HashMap<String, Object>();
    propertyMap.put("url", "filename.shp");
    DataSourcePropertiesInterface dsp = DataSourceConnectorFactory.getDataSourceProperties(propertyMap);
    assertEquals(DataSourceConnector.class, dsp.getDataSourceConnector().getClass());
    propertyMap.clear();
    propertyMap.put("host", "localhost");
    propertyMap.put("port", "5432");
    propertyMap.put("database", "testdb");
    propertyMap.put("user", "testuser");
    propertyMap.put("passwd", "pasword123");
    propertyMap.put("schema", "public");
    propertyMap.put("featureClass", "testfc");
    dsp = DataSourceConnectorFactory.getDataSourceProperties(propertyMap);
    assertEquals(DataSourceConnector.class, dsp.getDataSourceConnector().getClass());
}
Also used : HashMap(java.util.HashMap) DataSourcePropertiesInterface(com.sldeditor.common.DataSourcePropertiesInterface) Test(org.junit.jupiter.api.Test)

Aggregations

DataSourcePropertiesInterface (com.sldeditor.common.DataSourcePropertiesInterface)24 File (java.io.File)10 Test (org.junit.jupiter.api.Test)10 SLDEditorFile (com.sldeditor.datasource.SLDEditorFile)9 DataSourceConnectorInterface (com.sldeditor.common.DataSourceConnectorInterface)8 SLDDataInterface (com.sldeditor.common.SLDDataInterface)6 SLDData (com.sldeditor.common.data.SLDData)4 DataSourceInterface (com.sldeditor.datasource.DataSourceInterface)4 DataSourceProperties (com.sldeditor.datasource.impl.DataSourceProperties)4 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 VersionData (com.sldeditor.common.vendoroption.VersionData)3 DataSourceConnector (com.sldeditor.datasource.connector.instance.DataSourceConnector)3 FileTreeNode (com.sldeditor.datasource.extension.filesystem.node.file.FileTreeNode)3 EnvVar (com.sldeditor.filter.v2.envvar.EnvVar)3 MalformedURLException (java.net.MalformedURLException)3 ArrayList (java.util.ArrayList)3 DocumentBuilder (javax.xml.parsers.DocumentBuilder)3 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)3 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)3