* {@code
*
@@ -39,34 +40,71 @@ import org.jdom.Element;
* val2
* val3
* }
+ *
*
- *
+ *
+ * This can be also used in compounded to the PropertyXMLEncoder
+ * or other objects overriding the toString() method
+ *
+ * e.g.:
+ *
+ *
+ * {@code
+ *
+ *
+ *
+ * false
+ *
+ *
+ *
+ *
+ * true
+ * ele
+ * LIST
+ *
+ *
+ * }
+ *
+ *
+ *
* @author ETj (etj at geo-solutions.it)
+ * @author Carlo Cancellieri - carlo.cancellieri@geo-solutions.it
+ *
*/
-public class EntryKeyListEncoder {
+public class EntryKeyListEncoder extends XMLSerializer{
- private Map metadata = new LinkedHashMap();
- private final String listName;
+ private final Element root;
+
+ public EntryKeyListEncoder(String listName) {
+ root=new Element(listName);
+ }
- public EntryKeyListEncoder(String listName) {
- this.listName = listName;
- }
-
- public void add(String key, String value) {
- metadata.put(key, value);
- }
+ public void add(String key, T value) {
+ final Element entryElem = new Element("entry");
+ entryElem.setAttribute("key", key);
+ if (value instanceof String)
+ entryElem.setText((String)value);
+ else if (value instanceof Element)
+ entryElem.addContent((Element)value);
+ else if (value instanceof GSDimensionInfoEncoder)
+ entryElem.addContent(((GSDimensionInfoEncoder)value).getElement());
+ else
+ throw new IllegalArgumentException("Unable to add entry: unrecognized object");
+
+ root.addContent(entryElem);
+ }
+
- public void attachList(Element e) {
-
- if( ! metadata.isEmpty() ) {
- Element md = new Element(listName);
- for (Map.Entry entry : metadata.entrySet()) {
- Element entryeElem = new Element("entry")
- .setAttribute("key", entry.getKey())
- .setText(entry.getValue());
- md.addContent(entryeElem);
- }
- e.addContent(md);
- }
- }
+ /**
+ * add a node to the root
+ *
+ * @param el the node to add
+ */
+ public void addContent(final Element el){
+ root.addContent(el);
+ }
+
+ public Element getElement() {
+ return root;
+ }
}
diff --git a/src/main/java/it/geosolutions/geoserver/rest/encoder/utils/PropertyXMLEncoder.java b/src/main/java/it/geosolutions/geoserver/rest/encoder/utils/PropertyXMLEncoder.java
index dc77a2a..2c8b700 100644
--- a/src/main/java/it/geosolutions/geoserver/rest/encoder/utils/PropertyXMLEncoder.java
+++ b/src/main/java/it/geosolutions/geoserver/rest/encoder/utils/PropertyXMLEncoder.java
@@ -25,11 +25,7 @@
package it.geosolutions.geoserver.rest.encoder.utils;
-import java.util.HashMap;
-import java.util.Map;
import org.jdom.Element;
-import org.jdom.output.Format;
-import org.jdom.output.XMLOutputter;
/**
* Creates an XML document by mapping properties to XML nodes.
@@ -44,58 +40,69 @@ import org.jdom.output.XMLOutputter;
* {@code value }
*
* @author ETj (etj at geo-solutions.it)
+ * @author Carlo Cancellieri - carlo.cancellieri@geo-solutions.it
*/
-public class PropertyXMLEncoder {
+public class PropertyXMLEncoder extends XMLSerializer{
- private final static XMLOutputter OUTPUTTER = new XMLOutputter(Format.getCompactFormat());
- private final Map configElements = new HashMap();
- private final String rootName;
+// private final Map configElements = new HashMap();
+ private final Element root;
- public PropertyXMLEncoder(String rootName) {
- this.rootName = rootName;
+ public PropertyXMLEncoder(final String rootName) {
+ root=new Element(rootName);
}
-
+
+ /**
+ * add a node (as child) to the root node
+ *
+ * @param el the node to add
+ */
+ @Override
+ public void addContent(final Element el){
+ root.addContent(el);
+ }
+
+ /**
+ * @Deprecated use add()
+ */
+ @Deprecated
protected void setOrRemove(String key, String value) {
- if (value != null) {
- configElements.put(key, value);
+ final Element e=root.getChild(key);
+ if (value != null && e==null) {
+ add(root, key, value);
} else {
- configElements.remove(key);
+ root.removeChild(key);
}
}
-
- protected void set(String key, String value) {
- setOrRemove(key, value);
+
+ protected void add(String key, String value) {
+ final Element e=root.getChild(key);
+ if (value != null && e==null) {
+ add(root, key, value);
+ }
}
public boolean isEmpty() {
- return configElements.isEmpty();
+// return configElements.isEmpty();
+ return root.getChildren().isEmpty();
}
- /**
- * @return an xml document representing the stored properties.
- */
- public String encodeXml() {
-
- Element root = new Element(rootName);
- for (String key : configElements.keySet()) {
- final String value = configElements.get(key);
- add(root, key, value);
- }
-
- addNodesBeforeOutput(root);
- return OUTPUTTER.outputString(root);
+ public Element get(final String key){
+ return root.getChild(key);
+// return configElements.get(key);
}
+
/**
* Subclasses may need to override this method if some more info in the XML
* string are needed when calling {@link #encodeXml() encodeXml()}.
*
* @param root the root element that will be converted into String by encodeXml
*/
- protected void addNodesBeforeOutput(Element root) {
- // nothing to do, just override when needed.
+ @Override
+ public Element getElement(){
+ return root;
}
-
+
private void add(Element e, String key, String value) {
if( ! key.contains("/") ) {
e.addContent(new Element(key).setText(value));
@@ -114,4 +121,6 @@ public class PropertyXMLEncoder {
}
}
+
+
}
diff --git a/src/main/java/it/geosolutions/geoserver/rest/encoder/utils/TextNodeListEncoder.java b/src/main/java/it/geosolutions/geoserver/rest/encoder/utils/TextNodeListEncoder.java
index 1dbbea2..09ab87b 100644
--- a/src/main/java/it/geosolutions/geoserver/rest/encoder/utils/TextNodeListEncoder.java
+++ b/src/main/java/it/geosolutions/geoserver/rest/encoder/utils/TextNodeListEncoder.java
@@ -25,8 +25,6 @@
package it.geosolutions.geoserver.rest.encoder.utils;
-import java.util.ArrayList;
-import java.util.List;
import org.jdom.Element;
/**
@@ -42,39 +40,27 @@ import org.jdom.Element;
*
*
* @author ETj (etj at geo-solutions.it)
+ * @author Carlo Cancellieri - carlo.cancellieri@geo-solutions.it
*/
-public class TextNodeListEncoder {
-
- private List list = new ArrayList();
- private final String listName;
+public class TextNodeListEncoder extends XMLSerializer{
+ private final Element root;
public TextNodeListEncoder(String listName) {
- this.listName = listName;
+ root=new Element(listName);
}
public void add(String nodename, String nodetext) {
- list.add(new Pair(nodename, nodetext));
+ final Element el=new Element(nodename);
+ el.setText(nodetext);
+ root.addContent(el);
}
- public void attachList(Element e) {
-
- if( ! list.isEmpty() ) {
- Element elist = new Element(listName);
- for (Pair pair : list) {
- Element entry = new Element(pair.v1).setText(pair.v2);
- elist.addContent(entry);
- }
- e.addContent(elist);
- }
+ public Element getElement() {
+ return root;
}
- class Pair {
- String v1;
- String v2;
+ public void addContent(final Element el){
+ root.addContent(el);
+ }
- public Pair(String v1, String v2) {
- this.v1 = v1;
- this.v2 = v2;
- }
- }
}
diff --git a/src/main/java/it/geosolutions/geoserver/rest/encoder/utils/XMLSerializer.java b/src/main/java/it/geosolutions/geoserver/rest/encoder/utils/XMLSerializer.java
new file mode 100644
index 0000000..4abb0dc
--- /dev/null
+++ b/src/main/java/it/geosolutions/geoserver/rest/encoder/utils/XMLSerializer.java
@@ -0,0 +1,57 @@
+/*
+ * GeoServer-Manager - Simple Manager Library for GeoServer
+ *
+ * Copyright (C) 2007,2011 GeoSolutions S.A.S.
+ * http://www.geo-solutions.it
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+package it.geosolutions.geoserver.rest.encoder.utils;
+
+import org.jdom.Element;
+import org.jdom.output.Format;
+import org.jdom.output.XMLOutputter;
+
+/**
+ *
+ * @author Carlo Cancellieri - carlo.cancellieri@geo-solutions.it
+ *
+ */
+public abstract class XMLSerializer {
+ private final static XMLOutputter OUTPUTTER = new XMLOutputter(Format.getCompactFormat());
+
+ public XMLSerializer(){}
+
+ public abstract Element getElement();
+
+ public abstract void addContent(final Element el);
+
+ /**
+ * @return an xml String
+ */
+ @Override
+ public String toString() {
+ final Element root= getElement();
+ if (root!=null)
+ return OUTPUTTER.outputString(root);
+ else
+ return "";
+ }
+}
diff --git a/src/test/java/it/geosolutions/geoserver/rest/encoder/utils/EntryKeyListEncoderTest.java b/src/test/java/it/geosolutions/geoserver/rest/encoder/utils/EntryKeyListEncoderTest.java
index cbf66f9..eb49356 100644
--- a/src/test/java/it/geosolutions/geoserver/rest/encoder/utils/EntryKeyListEncoderTest.java
+++ b/src/test/java/it/geosolutions/geoserver/rest/encoder/utils/EntryKeyListEncoderTest.java
@@ -42,7 +42,7 @@ public class EntryKeyListEncoderTest extends TestCase {
ekle.add("k3", "v3");
Element root = new Element("root");
- ekle.attachList(root);
+ root.addContent(ekle.getElement());
assertEquals(1, root.getChildren().size());
assertNotNull(root.getChild("EKL"));
diff --git a/src/test/java/it/geosolutions/geoserver/rest/encoder/utils/GSCoverageEncoderTest.java b/src/test/java/it/geosolutions/geoserver/rest/encoder/utils/GSCoverageEncoderTest.java
new file mode 100644
index 0000000..bdb0913
--- /dev/null
+++ b/src/test/java/it/geosolutions/geoserver/rest/encoder/utils/GSCoverageEncoderTest.java
@@ -0,0 +1,65 @@
+/*
+ * Copyright (C) 2007 - 2011 GeoSolutions S.A.S.
+ * http://www.geo-solutions.it
+ *
+ * GPLv3 + Classpath exception
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
+package it.geosolutions.geoserver.rest.encoder.utils;
+
+import it.geosolutions.geoserver.rest.encoder.coverage.GSCoverageEncoder;
+import it.geosolutions.geoserver.rest.encoder.metadata.GSDimensionInfoEncoder;
+import it.geosolutions.geoserver.rest.encoder.metadata.GSDimensionInfoEncoder.Presentation;
+import junit.framework.TestCase;
+
+import org.apache.log4j.Logger;
+import org.junit.Test;
+
+/**
+ *
+ * @author ETj (etj at geo-solutions.it)
+ * @author Carlo Cancellieri - carlo.cancellieri@geo-solutions.it
+ */
+public class GSCoverageEncoderTest extends TestCase {
+
+ public GSCoverageEncoderTest() {
+ }
+
+ /**
+ * Default logger
+ */
+ protected final static Logger LOGGER = Logger.getLogger(GSCoverageEncoderTest.class);
+
+ @Test
+ public void testAll() {
+ GSCoverageEncoder encoder=new GSCoverageEncoder();
+ encoder.addKeyword("KEYWORD_1");
+ encoder.addKeyword("KEYWORD_2");
+ encoder.addKeyword("...");
+ encoder.addKeyword("KEYWORD_N");
+ GSDimensionInfoEncoder dim=new GSDimensionInfoEncoder(true);
+ dim.setPresentation(Presentation.CONTINUOUS_INTERVAL);
+ encoder.addMetadata("time", dim);
+ GSDimensionInfoEncoder dim2=new GSDimensionInfoEncoder(true);
+ dim2.setPresentation(Presentation.LIST);
+ encoder.addMetadata("elev", dim2);
+ encoder.setAllowMultithreading(true);
+
+ LOGGER.info(encoder.toString());
+// TODO TESTS
+
+
+ }
+}
diff --git a/src/test/java/it/geosolutions/geoserver/rest/encoder/utils/GSFeatureEncoderTest.java b/src/test/java/it/geosolutions/geoserver/rest/encoder/utils/GSFeatureEncoderTest.java
new file mode 100644
index 0000000..fcce197
--- /dev/null
+++ b/src/test/java/it/geosolutions/geoserver/rest/encoder/utils/GSFeatureEncoderTest.java
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2007 - 2011 GeoSolutions S.A.S.
+ * http://www.geo-solutions.it
+ *
+ * GPLv3 + Classpath exception
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
+package it.geosolutions.geoserver.rest.encoder.utils;
+
+import it.geosolutions.geoserver.rest.encoder.feature.GSFeatureDimensionInfoEncoder;
+import it.geosolutions.geoserver.rest.encoder.feature.GSFeatureTypeEncoder;
+import it.geosolutions.geoserver.rest.encoder.metadata.GSDimensionInfoEncoder.DiscretePresentation;
+
+import java.math.BigDecimal;
+
+import junit.framework.TestCase;
+
+import org.junit.Test;
+
+/**
+ *
+ * @author ETj (etj at geo-solutions.it)
+ * @author Carlo Cancellieri - carlo.cancellieri@geo-solutions.it
+ */
+public class GSFeatureEncoderTest extends TestCase {
+
+ public GSFeatureEncoderTest() {
+ }
+
+ @Test
+ public void testAll() {
+ GSFeatureTypeEncoder feature = new GSFeatureTypeEncoder();
+ feature.addKeyword("KEYWORD_1");
+ feature.addKeyword("KEYWORD_2");
+ feature.addKeyword("...");
+ feature.addKeyword("KEYWORD_N");
+ GSFeatureDimensionInfoEncoder dim2 = new GSFeatureDimensionInfoEncoder(
+ "ELE");
+ dim2.setPresentation(DiscretePresentation.DISCRETE_INTERVAL,
+ BigDecimal.valueOf(10));
+ feature.addMetadata("elevation", dim2);
+
+ // TODO TESTS
+
+ }
+}