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;
}
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());
}
}
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());
}
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;
}
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());
}
Aggregations