Permalink
Browse files

Nicer API for XmlToWbxml too

  • Loading branch information...
1 parent eb48165 commit 1c6aefe3c773e3451dea84d0993505c8f3660c2a @samstokes samstokes committed May 24, 2012
@@ -1,25 +1,9 @@
package com.rapportive.jawbone;
import com.sun.jna.Native;
-import com.sun.jna.Pointer;
-
-import com.sun.jna.ptr.LongByReference;
-import com.sun.jna.ptr.PointerByReference;
public class JawboneBinding {
static {
Native.register("wbxml2");
}
-
-
- public native int wbxml_conv_xml2wbxml_create(PointerByReference convPtr);
-
- public native void wbxml_conv_xml2wbxml_set_version(Pointer conv, int version);
- public native void wbxml_conv_xml2wbxml_enable_preserve_whitespaces(Pointer conv);
- public native void wbxml_conv_xml2wbxml_disable_string_table(Pointer conv);
- public native void wbxml_conv_xml2wbxml_disable_public_id(Pointer conv);
-
- public native int wbxml_conv_xml2wbxml_run(Pointer conv, byte[] xml, long xmlLength, PointerByReference wbxmlPtr, LongByReference wbxmlLength);
-
- public native void wbxml_conv_xml2wbxml_destroy(Pointer conv);
}
@@ -0,0 +1,57 @@
+package com.rapportive.jawbone;
+
+import static com.rapportive.jawbone.JawboneException.check;
+
+import com.sun.jna.Native;
+import com.sun.jna.Pointer;
+
+import com.sun.jna.ptr.LongByReference;
+import com.sun.jna.ptr.PointerByReference;
+
+public class XmlToWbxml {
+ static {
+ Native.register("wbxml2");
+ }
+
+
+ private final Pointer conv;
+
+
+ public XmlToWbxml() {
+ PointerByReference convPtr = new PointerByReference();
+ check("wbxml_conv_xml2wbxml_create", wbxml_conv_xml2wbxml_create(convPtr));
+ conv = convPtr.getValue();
+ }
+
+ public void enablePreserveWhitespaces() {
+ wbxml_conv_xml2wbxml_enable_preserve_whitespaces(conv);
+ }
+ public void disableStringTable() {
+ wbxml_conv_xml2wbxml_disable_string_table(conv);
+ }
+ public void disablePublicId() {
+ wbxml_conv_xml2wbxml_disable_public_id(conv);
+ }
+
+ public byte[] run(byte[] xml) {
+ PointerByReference wbxmlPtr = new PointerByReference();
+ LongByReference wbxmlLength = new LongByReference();
+
+ check("wbxml_conv_xml2wbxml_run", wbxml_conv_xml2wbxml_run(conv, xml, xml.length, wbxmlPtr, wbxmlLength));
+
+ byte[] wbxmlBytes = wbxmlPtr.getValue().getByteArray(0L, (int) wbxmlLength.getValue());
+ Native.free(Pointer.nativeValue(wbxmlPtr.getValue()));
+ return wbxmlBytes;
+ }
+
+
+ private native int wbxml_conv_xml2wbxml_create(PointerByReference convPtr);
+
+ private native void wbxml_conv_xml2wbxml_enable_preserve_whitespaces(Pointer conv);
+ private native void wbxml_conv_xml2wbxml_disable_string_table(Pointer conv);
+ private native void wbxml_conv_xml2wbxml_disable_public_id(Pointer conv);
+
+ private native int wbxml_conv_xml2wbxml_run(Pointer conv, byte[] xml, long xmlLength, PointerByReference wbxmlPtr, LongByReference wbxmlLength);
+
+ private native void wbxml_conv_xml2wbxml_destroy(Pointer conv);
+}
@@ -49,34 +49,14 @@ public void testWbxmlToXml() throws Exception {
@Test
public void testXmlToWbxml() throws Exception {
- PointerByReference convPtr = new PointerByReference();
- Pointer conv = null;
+ XmlToWbxml conv = new XmlToWbxml();
- int ret = binding.wbxml_conv_xml2wbxml_create(convPtr);
- assertEquals("wbxml_conv_xml2wbxml_create failed", 0, ret);
- conv = convPtr.getValue();
-
- try {
- binding.wbxml_conv_xml2wbxml_enable_preserve_whitespaces(conv);
- binding.wbxml_conv_xml2wbxml_disable_string_table(conv);
- binding.wbxml_conv_xml2wbxml_disable_public_id(conv);
-
- PointerByReference wbxmlPtr = new PointerByReference();
- LongByReference wbxmlLength = new LongByReference();
- ret = binding.wbxml_conv_xml2wbxml_run(conv, xml, xml.length, wbxmlPtr, wbxmlLength);
-
- check("wbxml_conv_xml2wbxml_run", ret);
- assertTrue("returned no WBXML", wbxmlLength.getValue() > 0);
-
- byte[] wbxmlBytes = wbxmlPtr.getValue().getByteArray(0L, (int) wbxmlLength.getValue());
- Native.free(Pointer.nativeValue(wbxmlPtr.getValue()));
+ conv.enablePreserveWhitespaces();
+ conv.disableStringTable();
+ conv.disablePublicId();
- assertEquals("returned b0rked bytes", wbxmlLength.getValue(), wbxmlBytes.length);
- } finally {
- if (conv != null) {
- binding.wbxml_conv_xml2wbxml_destroy(conv);
- }
- }
+ byte[] wbxmlBytes = conv.run(xml);
+ assertTrue("returned no WBXML", wbxmlBytes.length > 0);
}

0 comments on commit 1c6aefe

Please sign in to comment.