]> git.argeo.org Git - gpl/argeo-slc.git/commitdiff
Introduce XML un/marshallign for SLC Detached
authorMathieu Baudier <mbaudier@argeo.org>
Sun, 5 Oct 2008 12:33:11 +0000 (12:33 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Sun, 5 Oct 2008 12:33:11 +0000 (12:33 +0000)
git-svn-id: https://svn.argeo.org/slc/trunk@1698 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

org.argeo.slc.detached/.classpath
org.argeo.slc.detached/pom.xml
org.argeo.slc.detached/src/main/java/org/argeo/slc/detached/DetachedAnswer.java
org.argeo.slc.detached/src/main/java/org/argeo/slc/detached/DetachedXmlConverter.java [new file with mode: 0644]
org.argeo.slc.detached/src/main/java/org/argeo/slc/detached/DetachedXmlConverterCompat.java [new file with mode: 0644]
org.argeo.slc.detached/src/main/resources/org/argeo/slc/detached/slc-detached.xsd [new file with mode: 0644]

index 1aa73cdbf084050c2f4611c2f74041009d5307dc..7dc80e91b8c60e97306a2b3b1a48762a364cecfb 100644 (file)
@@ -2,7 +2,9 @@
 <classpath>
        <classpathentry kind="src" path="src/main/java"/>
        <classpathentry kind="src" path="src/main/resources"/>
+       <classpathentry kind="src" path="src/test/java"/>
+       <classpathentry kind="src" path="src/test/resources"/>
        <classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
        <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
-       <classpathentry kind="output" path="bin"/>
+       <classpathentry kind="output" path="target/classes"/>
 </classpath>
index 22dd847ef86840594de611c103d611a94e9b6365..1588b11631765ca2f5a48fe669ba62c716855e2e 100644 (file)
@@ -1,78 +1,96 @@
 <project xmlns="http://maven.apache.org/POM/4.0.0"
-       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-       xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
-       <modelVersion>4.0.0</modelVersion>
-       <parent>
-               <groupId>org.argeo.slc</groupId>
-               <artifactId>argeo-slc</artifactId>
-               <version>0.11.1-SNAPSHOT</version>
-               <relativePath>../org.argeo.slc</relativePath>
-       </parent>
-       <artifactId>org.argeo.slc.detached</artifactId>
-       <name>Argeo SLC Detached</name>
-       <packaging>bundle</packaging>
-       <build>
-               <plugins>
-                       <plugin>
-                               <groupId>org.apache.felix</groupId>
-                               <artifactId>maven-bundle-plugin</artifactId>
-                               <version>1.4.3</version>
-                               <extensions>true</extensions>
-                               <configuration>
-                                       <manifestLocation>
-                                               src/main/resources/META-INF
-                                       </manifestLocation>
-                                       <instructions>
-                                               <Bundle-SymbolicName>
-                                                       ${pom.artifactId}
-                                               </Bundle-SymbolicName>
-                        <Bundle-Activator>org.argeo.slc.detached.Activator</Bundle-Activator>
-                                               <Export-Package>
-                                                       org.argeo.slc.detached.*
-                                               </Export-Package>
-                                       </instructions>
-                               </configuration>
-                       </plugin>
-                       <plugin>
-                               <groupId>org.apache.maven.plugins</groupId>
-                               <artifactId>maven-compiler-plugin</artifactId>
-                               <configuration>
-                                       <source>1.3</source>
-                                       <target>1.3</target>
-                               </configuration>
-                       </plugin>
-                       <plugin>
-                               <groupId>org.apache.maven.plugins</groupId>
-                               <artifactId>maven-source-plugin</artifactId>
-                       </plugin>
-               </plugins>
-       </build>
-       <dependencies>
-               <dependency>
-                       <groupId>org.apache.felix</groupId>
-                       <artifactId>org.osgi.core</artifactId>
-               </dependency>
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <groupId>org.argeo.slc</groupId>
+        <artifactId>argeo-slc</artifactId>
+        <version>0.11.1-SNAPSHOT</version>
+        <relativePath>../org.argeo.slc</relativePath>
+    </parent>
+    <artifactId>org.argeo.slc.detached</artifactId>
+    <name>Argeo SLC Detached</name>
+    <packaging>bundle</packaging>
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.felix</groupId>
+                <artifactId>maven-bundle-plugin</artifactId>
+                <version>1.4.3</version>
+                <extensions>true</extensions>
+                <configuration>
+                    <manifestLocation>
+                        src/main/resources/META-INF
+                    </manifestLocation>
+                    <instructions>
+                        <Bundle-SymbolicName>
+                            ${pom.artifactId}
+                        </Bundle-SymbolicName>
+                        <Bundle-Activator>
+                            org.argeo.slc.detached.Activator
+                        </Bundle-Activator>
+                        <Export-Package>
+                            org.argeo.slc.detached.*
+                        </Export-Package>
+                    </instructions>
+                </configuration>
+            </plugin>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <configuration>
+                    <source>1.3</source>
+                    <target>1.3</target>
+                </configuration>
+            </plugin>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-source-plugin</artifactId>
+            </plugin>
+        </plugins>
+    </build>
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.osgi.core</artifactId>
+        </dependency>
 
-               <dependency>
-                       <groupId>org.springframework</groupId>
-                       <artifactId>spring-context</artifactId>
-                       <version>2.0.8</version>
-               </dependency>
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-context</artifactId>
+            <version>2.0.8</version>
+        </dependency>
 
-               <!-- 
-                       <dependency>
-                       <groupId>org.codehaus.castor</groupId>
-                       <artifactId>castor</artifactId>
-                       </dependency>
-               -->
-               <dependency>
-                       <groupId>junit</groupId>
-                       <artifactId>junit</artifactId>
-                       <scope>test</scope>
-               </dependency>
-               <dependency>
-                       <groupId>log4j</groupId>
-                       <artifactId>log4j</artifactId>
-               </dependency>
-       </dependencies>
+        <dependency>
+            <groupId>org.codehaus.castor</groupId>
+            <artifactId>castor</artifactId>
+            <version>1.1.2</version>
+            <exclusions>
+                <exclusion>
+                    <groupId>adaptx</groupId>
+                    <artifactId>adaptx</artifactId>
+                </exclusion>
+            </exclusions>
+        </dependency>
+
+        <dependency>
+            <groupId>xerces</groupId>
+            <artifactId>xercesImpl</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>commons-io</groupId>
+            <artifactId>commons-io</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>log4j</groupId>
+            <artifactId>log4j</artifactId>
+        </dependency>
+    </dependencies>
 </project>
\ No newline at end of file
index 582703786263bac6eb4cf43f89ab833eed64a807..219ab781af914234b5373c3fce1ee0fbb493759b 100644 (file)
@@ -58,4 +58,8 @@ public class DetachedAnswer implements DetachedCommunication {
                return uuid;
        }
 
+       public void setUuid(String uuid) {
+               this.uuid = uuid;
+       }
+
 }
diff --git a/org.argeo.slc.detached/src/main/java/org/argeo/slc/detached/DetachedXmlConverter.java b/org.argeo.slc.detached/src/main/java/org/argeo/slc/detached/DetachedXmlConverter.java
new file mode 100644 (file)
index 0000000..455597d
--- /dev/null
@@ -0,0 +1,14 @@
+package org.argeo.slc.detached;
+
+import javax.xml.transform.Result;
+import javax.xml.transform.Source;
+
+public interface DetachedXmlConverter {
+       public void marshallRequest(DetachedRequest request, Result result);
+
+       public DetachedRequest unmarshallRequest(Source source);
+
+       public void marshallAnswer(DetachedAnswer answer, Result result);
+
+       public DetachedAnswer unmarshallAnswer(Source source);
+}
diff --git a/org.argeo.slc.detached/src/main/java/org/argeo/slc/detached/DetachedXmlConverterCompat.java b/org.argeo.slc.detached/src/main/java/org/argeo/slc/detached/DetachedXmlConverterCompat.java
new file mode 100644 (file)
index 0000000..1a361df
--- /dev/null
@@ -0,0 +1,109 @@
+package org.argeo.slc.detached;
+
+import java.io.StringReader;
+import java.util.Enumeration;
+import java.util.Properties;
+
+import javax.xml.transform.Result;
+import javax.xml.transform.Source;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMResult;
+import javax.xml.transform.stream.StreamSource;
+
+import org.apache.commons.io.IOUtils;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
+
+/** For compatibility with old JDKs (pre 1.5). Use Spring base one when possible. */
+public class DetachedXmlConverterCompat implements DetachedXmlConverter {
+       public final static String SLC_DETACHED_NAMESPACE_URI = "http://argeo.org/projects/slc/schemas/detached";
+       // private final static Log log = LogFactory
+       // .getLog(DetachedXmlConverterCompat.class);
+
+       private final Transformer copy;
+
+       public DetachedXmlConverterCompat() {
+               try {
+                       copy = TransformerFactory.newInstance().newTransformer();
+               } catch (Exception e) {
+                       throw new DetachedException("Could not create transformer.", e);
+               }
+       }
+
+       public void marshallAnswer(DetachedAnswer answer, Result result) {
+               StringBuffer buf = new StringBuffer("");
+               buf.append("<slc-det:detached-answer xmlns:slc-det=\""
+                               + SLC_DETACHED_NAMESPACE_URI + "\">");
+               if (answer.getUuid() != null)
+                       buf.append("<slc-det:uuid>").append(answer.getUuid()).append(
+                                       "</slc-det:uuid>");
+               buf.append("<slc-det:status>").append(answer.getStatus()).append(
+                               "</slc-det:status>");
+               if (answer.getLog() != null)
+                       buf.append("<slc-det:log>").append(answer.getLog()).append(
+                                       "</slc-det:log>");
+               if (answer.getProperties().size() != 0) {
+                       buf.append("<slc-det:properties>");
+                       Enumeration keys = answer.getProperties().keys();
+                       while (keys.hasMoreElements()) {
+                               String key = (String) keys.nextElement();
+                               buf.append("<slc-det:prop key=\"").append(key).append("\">");
+                               buf.append(answer.getProperties().getProperty(key));
+                               buf.append("</slc-det:prop>");
+                       }
+                       buf.append("</slc-det:properties>");
+               }
+               buf.append("</slc-det:detached-answer>");
+               StringReader reader = new StringReader(buf.toString());
+               Source source = new StreamSource(reader);
+               try {
+                       copy.transform(source, result);
+               } catch (TransformerException e) {
+                       throw new DetachedException("Could not copy xml to result", e);
+               } finally {
+                       IOUtils.closeQuietly(reader);
+               }
+       }
+
+       public void marshallRequest(DetachedRequest request, Result result) {
+               throw new DetachedException("Not implemented.");
+       }
+
+       public DetachedAnswer unmarshallAnswer(Source source) {
+               throw new DetachedException("Not implemented.");
+       }
+
+       public DetachedRequest unmarshallRequest(Source source) {
+               DOMResult result = new DOMResult();
+               try {
+                       copy.transform(source, result);
+               } catch (TransformerException e) {
+                       throw new DetachedException("Could not copy xml source", e);
+               }
+               Element node = (Element) ((Document) result.getNode())
+                               .getDocumentElement();
+               DetachedRequest request = new DetachedRequest();
+               request.setUuid(node.getElementsByTagNameNS(SLC_DETACHED_NAMESPACE_URI,
+                               "uuid").item(0).getTextContent());
+               request.setRef(node.getElementsByTagNameNS(SLC_DETACHED_NAMESPACE_URI,
+                               "ref").item(0).getTextContent());
+               request.setPath(node.getElementsByTagNameNS(SLC_DETACHED_NAMESPACE_URI,
+                               "path").item(0).getTextContent());
+               Element propertiesElement = (Element) node.getElementsByTagNameNS(
+                               SLC_DETACHED_NAMESPACE_URI, "properties").item(0);
+               NodeList propElements = propertiesElement.getElementsByTagNameNS(
+                               SLC_DETACHED_NAMESPACE_URI, "prop");
+               Properties properties = new Properties();
+               for (int i = 0; i < propElements.getLength(); i++) {
+                       Element propElement = (Element) propElements.item(i);
+                       String key = propElement.getAttribute("key");
+                       String value = propElement.getTextContent();
+                       properties.setProperty(key, value);
+               }
+               request.setProperties(properties);
+               return request;
+       }
+}
diff --git a/org.argeo.slc.detached/src/main/resources/org/argeo/slc/detached/slc-detached.xsd b/org.argeo.slc.detached/src/main/resources/org/argeo/slc/detached/slc-detached.xsd
new file mode 100644 (file)
index 0000000..c4fab17
--- /dev/null
@@ -0,0 +1,63 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<xs:schema
+    targetNamespace="http://argeo.org/projects/slc/schemas/detached"
+    xmlns:xs="http://www.w3.org/2001/XMLSchema"
+    elementFormDefault="qualified"
+    xmlns:slc-det="http://argeo.org/projects/slc/schemas/detached">
+
+    <xs:element name="detached-request">
+        <xs:complexType>
+            <xs:all>
+                <xs:element name="uuid" type="xs:string" maxOccurs="1" />
+                <xs:element name="ref" type="xs:string" minOccurs="0"
+                    maxOccurs="1" />
+                <xs:element name="path" type="xs:string" minOccurs="0"
+                    maxOccurs="1" />
+                <xs:element name="properties" minOccurs="0"
+                    maxOccurs="1" type="slc-det:PropertiesType" />
+            </xs:all>
+        </xs:complexType>
+    </xs:element>
+
+    <xs:element name="detached-answer">
+        <xs:complexType>
+            <xs:all>
+                <xs:element name="uuid" type="xs:string" maxOccurs="1" />
+                <xs:element name="status" minOccurs="0" maxOccurs="1">
+                    <xs:simpleType>
+                        <xs:restriction base="xs:integer">
+                            <xs:enumeration value="-1" />
+                            <xs:enumeration value="0" />
+                            <xs:enumeration value="1" />
+                            <xs:enumeration value="2" />
+                            <xs:enumeration value="10" />
+                        </xs:restriction>
+                    </xs:simpleType>
+                </xs:element>
+                <xs:element name="log" type="xs:string" minOccurs="0"
+                    maxOccurs="1" />
+                <xs:element name="properties" minOccurs="0"
+                    maxOccurs="1" type="slc-det:PropertiesType" />
+            </xs:all>
+        </xs:complexType>
+    </xs:element>
+
+    <xs:complexType name="PropertiesType">
+        <xs:sequence>
+            <xs:element name="prop" type="slc-det:PropType"
+                minOccurs="0" maxOccurs="unbounded">
+            </xs:element>
+        </xs:sequence>
+    </xs:complexType>
+
+    <xs:complexType name="PropType">
+        <xs:simpleContent>
+            <xs:extension base="xs:string">
+                <xs:attribute name="key" type="xs:string"
+                    use="required" />
+            </xs:extension>
+        </xs:simpleContent>
+    </xs:complexType>
+
+
+</xs:schema>
\ No newline at end of file