Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Move native gubbinz for wbxml2xml into WbxmlToXml class

  • Loading branch information...
commit 47b949cd3835b4b9211321754132b7e85490b188 1 parent daf1d59
@samstokes samstokes authored
View
18 src/main/java/com/rapportive/jawbone/JawboneBinding.java
@@ -7,29 +7,11 @@
import com.sun.jna.ptr.PointerByReference;
public class JawboneBinding {
- public WbxmlToXml newWbxmlToXml() {
- return new WbxmlToXml(this);
- }
-
-
static {
Native.register("wbxml2");
}
- native int wbxml_conv_wbxml2xml_create(PointerByReference convPtr);
-
- native void wbxml_conv_wbxml2xml_set_gen_type(Pointer conv, int genType);
- native void wbxml_conv_wbxml2xml_set_language(Pointer conv, int language);
- native void wbxml_conv_wbxml2xml_set_charset(Pointer conv, int charset);
- native void wbxml_conv_wbxml2xml_set_indent(Pointer conv, int indent);
- native void wbxml_conv_wbxml2xml_enable_preserve_whitespaces(Pointer conv);
-
- native int wbxml_conv_wbxml2xml_run(Pointer conv, byte[] wbxml, long wbxmlLength, PointerByReference xmlPtr, LongByReference xmlLength);
-
- native void wbxml_conv_wbxml2xml_destroy(Pointer conv);
-
-
public native int wbxml_conv_xml2wbxml_create(PointerByReference convPtr);
public native void wbxml_conv_xml2wbxml_set_version(Pointer conv, int version);
View
55 src/main/java/com/rapportive/jawbone/WbxmlToXml.java
@@ -7,39 +7,41 @@
import com.sun.jna.ptr.PointerByReference;
public class WbxmlToXml {
- private final JawboneBinding jawbone;
- private final Pointer conv;
+ static {
+ Native.register("wbxml2");
+ }
- WbxmlToXml(JawboneBinding jawbone) {
- this.jawbone = jawbone;
+ private final Pointer conv;
+
+ public WbxmlToXml() {
PointerByReference convPtr = new PointerByReference();
- jawbone.check(jawbone.wbxml_conv_wbxml2xml_create(convPtr));
+ check("wbxml_conv_wbxml2xml_create", wbxml_conv_wbxml2xml_create(convPtr));
conv = convPtr.getValue();
}
public void setGenType(int genType) {
- jawbone.wbxml_conv_wbxml2xml_set_gen_type(conv, genType);
+ wbxml_conv_wbxml2xml_set_gen_type(conv, genType);
}
public void setLanguage(int language) {
- jawbone.wbxml_conv_wbxml2xml_set_language(conv, language);
+ wbxml_conv_wbxml2xml_set_language(conv, language);
}
public void setCharset(int charset) {
- jawbone.wbxml_conv_wbxml2xml_set_charset(conv, charset);
+ wbxml_conv_wbxml2xml_set_charset(conv, charset);
}
public void setIndent(int indent) {
- jawbone.wbxml_conv_wbxml2xml_set_indent(conv, indent);
+ wbxml_conv_wbxml2xml_set_indent(conv, indent);
}
public void enablePreserveWhitespaces() {
- jawbone.wbxml_conv_wbxml2xml_enable_preserve_whitespaces(conv);
+ wbxml_conv_wbxml2xml_enable_preserve_whitespaces(conv);
}
public byte[] run(byte[] wbxml) {
PointerByReference xmlPtr = new PointerByReference();
LongByReference xmlLength = new LongByReference();
- jawbone.check(jawbone.wbxml_conv_wbxml2xml_run(conv, wbxml, wbxml.length, xmlPtr, xmlLength));
+ check("wbxml_conv_wbxml2xml_run", wbxml_conv_wbxml2xml_run(conv, wbxml, wbxml.length, xmlPtr, xmlLength));
byte[] xmlBytes = xmlPtr.getValue().getByteArray(0L, (int) xmlLength.getValue());
Native.free(Pointer.nativeValue(xmlPtr.getValue()));
@@ -48,6 +50,35 @@ public void enablePreserveWhitespaces() {
@Override
public void finalize() {
- jawbone.wbxml_conv_wbxml2xml_destroy(conv);
+ wbxml_conv_wbxml2xml_destroy(conv);
}
+
+
+ private void check(String function, int ret) {
+ if (ret != 0) {
+ throw new WbxmlToXmlException(function + ": " + wbxml_errors_string(ret));
+ }
+ }
+
+ @SuppressWarnings("serial")
+ private class WbxmlToXmlException extends RuntimeException {
+ public WbxmlToXmlException(String message) {
+ super(message);
+ }
+ }
+
+
+ private native int wbxml_conv_wbxml2xml_create(PointerByReference convPtr);
+
+ private native void wbxml_conv_wbxml2xml_set_gen_type(Pointer conv, int genType);
+ private native void wbxml_conv_wbxml2xml_set_language(Pointer conv, int language);
+ private native void wbxml_conv_wbxml2xml_set_charset(Pointer conv, int charset);
+ private native void wbxml_conv_wbxml2xml_set_indent(Pointer conv, int indent);
+ private native void wbxml_conv_wbxml2xml_enable_preserve_whitespaces(Pointer conv);
+
+ private native int wbxml_conv_wbxml2xml_run(Pointer conv, byte[] wbxml, long wbxmlLength, PointerByReference xmlPtr, LongByReference xmlLength);
+
+ private native void wbxml_conv_wbxml2xml_destroy(Pointer conv);
+
+ private native String wbxml_errors_string(int ret);
}
View
2  src/test/java/com/rapportive/jawbone/JawboneTest.java
@@ -34,7 +34,7 @@ public static void setUpBeforeClass() throws Exception {
@Test
public void testWbxmlToXml() throws Exception {
- WbxmlToXml conv = binding.newWbxmlToXml();
+ WbxmlToXml conv = new WbxmlToXml();
conv.setGenType(2); /* WBXML_GEN_XML_CANONICAL */
conv.setLanguage(2402); /* WBXML_LANG_ACTIVESYNC */
conv.setCharset(0); /* WBXML_CHARSET_UNKNOWN */
Please sign in to comment.
Something went wrong with that request. Please try again.