On Mon, Jul 25, 2011 at 4:32 PM, David Lutterkort <[email protected]> wrote:

> On Mon, 2011-07-25 at 16:00 -0700, Alex Cruise wrote:
> > I'm using the Java API successfully, but I note that the `span` command
> > isn't supported there.  Would it be extremely difficult to add it?
>
> I know very little about Java bindings, but it shouldn't be any harder
> than say aug_match or aug_get - I assume you could just follow the lead
> of the existing bindings.
>

Hey, wouldja look at that! (attached patch :)

-0xe1a
Index: src/main/java/net/augeas/jna/Aug.java
===================================================================
--- src/main/java/net/augeas/jna/Aug.java	(revision 67c45fba0118dfa45d1c596c75a5c713cd5203e3)
+++ src/main/java/net/augeas/jna/Aug.java	(revision )
@@ -67,4 +67,9 @@
     int aug_set(AugPointer aug, String path, String value);
 
     int aug_setm(AugPointer aug, String base, String sub, String value);
+
+    int aug_span(AugPointer aug, String path, PointerByReference filename,
+                 IntByReference labelStart, IntByReference labelEnd,
+                 IntByReference valueStart, IntByReference valueEnd,
+                 IntByReference spanStart, IntByReference spanEnd);
 }
\ No newline at end of file
Index: src/test/java/net/augeas/AugeasTest.java
===================================================================
--- src/test/java/net/augeas/AugeasTest.java	(revision 67c45fba0118dfa45d1c596c75a5c713cd5203e3)
+++ src/test/java/net/augeas/AugeasTest.java	(revision )
@@ -1,9 +1,9 @@
 package net.augeas;
 
-import java.util.List;
-
 import junit.framework.TestCase;
 
+import java.util.List;
+
 public class AugeasTest extends TestCase {
 
     public void testBasics() {
@@ -71,4 +71,22 @@
             // Good.
         }
     }
+
+    // TODO find some way to make this test more portable!!
+    public void testSpan() {
+        Augeas aug = new Augeas();
+        aug.set("/augeas/span","enable"); // TODO consider an alternate constructor that does this for us
+        aug.rm("/files");
+        aug.load();
+
+        SpanResult got = aug.span("/files/etc/passwd[1]");
+        assertEquals("/etc/passwd", got.getFilename());
+        assertEquals(0, got.getValueStart());
+        assertEquals(0, got.getValueEnd());
+        assertEquals(0, got.getSpanStart());
+        assertTrue(got.getSpanEnd() > 0);
+        assertEquals(0, got.getLabelStart());
+        assertEquals(0, got.getLabelEnd());
-}
+    }
+
+}
Index: src/main/java/net/augeas/Augeas.java
===================================================================
--- src/main/java/net/augeas/Augeas.java	(revision 67c45fba0118dfa45d1c596c75a5c713cd5203e3)
+++ src/main/java/net/augeas/Augeas.java	(revision )
@@ -338,7 +338,9 @@
      */
     protected void processLastCall(String message) {
         if (raiseExceptions && lastReturn == -1) {
-            throw new AugeasException(message);
+            final String err = lastErrorMessage() != null ? (": " + lastErrorMessage()) : "";
+            final String details = lastErrorDetails() != null ? (": " + lastErrorDetails()) : "";
+            throw new AugeasException(message + err + details);
         }
     }
 
@@ -402,6 +404,32 @@
         return lastReturn;
     }
 
+    public SpanResult span(String path) {
+        check();
+
+        final PointerByReference filename = new PointerByReference();
+        final IntByReference labelStart = new IntByReference();
+        final IntByReference labelEnd   = new IntByReference();
+        final IntByReference valueStart = new IntByReference();
+        final IntByReference valueEnd   = new IntByReference();
+        final IntByReference spanStart  = new IntByReference();
+        final IntByReference spanEnd    = new IntByReference();
+
+        lastReturn = AugLib.aug_span(aug, path, filename, labelStart, labelEnd, valueStart, valueEnd, spanStart, spanEnd);
+
+        processLastCall("span failed");
+
+        return new SpanResult(
+                filename.getValue().getString(0),
+                labelStart.getValue(),
+                labelEnd.getValue(),
+                valueStart.getValue(),
+                valueEnd.getValue(),
+                spanStart.getValue(),
+                spanEnd.getValue()
+        );
+    }
+
     /**
      * sets if exceptions should be raised
      */
@@ -411,7 +439,7 @@
 
     /**
      * Add a transform under <tt>/augeas/load</tt>
-     * 
+     *
      * @param lens
      *            the lens to use
      * @param name
Index: src/main/java/net/augeas/SpanResult.java
===================================================================
--- src/main/java/net/augeas/SpanResult.java	(revision )
+++ src/main/java/net/augeas/SpanResult.java	(revision )
@@ -0,0 +1,92 @@
+package net.augeas;
+
+public final class SpanResult {
+    private final String filename;
+    private final int labelStart;
+    private final int labelEnd;
+    private final int valueStart;
+    private final int valueEnd;
+    private final int spanStart;
+    private final int spanEnd;
+
+    public SpanResult(String filename, int labelStart, int labelEnd, int valueStart, int valueEnd, int spanStart, int spanEnd) {
+        this.filename = filename;
+        this.labelStart = labelStart;
+        this.labelEnd = labelEnd;
+        this.valueStart = valueStart;
+        this.valueEnd = valueEnd;
+        this.spanStart = spanStart;
+        this.spanEnd = spanEnd;
+    }
+
+    public String getFilename() {
+        return filename;
+    }
+
+    public int getLabelStart() {
+        return labelStart;
+    }
+
+    public int getLabelEnd() {
+        return labelEnd;
+    }
+
+    public int getValueStart() {
+        return valueStart;
+    }
+
+    public int getValueEnd() {
+        return valueEnd;
+    }
+
+    public int getSpanStart() {
+        return spanStart;
+    }
+
+    public int getSpanEnd() {
+        return spanEnd;
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) return true;
+        if (o == null || getClass() != o.getClass()) return false;
+
+        SpanResult that = (SpanResult) o;
+
+        if (labelEnd != that.labelEnd) return false;
+        if (labelStart != that.labelStart) return false;
+        if (spanEnd != that.spanEnd) return false;
+        if (spanStart != that.spanStart) return false;
+        if (valueEnd != that.valueEnd) return false;
+        if (valueStart != that.valueStart) return false;
+        if (filename != null ? !filename.equals(that.filename) : that.filename != null) return false;
+
+        return true;
+    }
+
+    @Override
+    public int hashCode() {
+        int result = filename != null ? filename.hashCode() : 0;
+        result = 31 * result + labelStart;
+        result = 31 * result + labelEnd;
+        result = 31 * result + valueStart;
+        result = 31 * result + valueEnd;
+        result = 31 * result + spanStart;
+        result = 31 * result + spanEnd;
+        return result;
+    }
+
+    @Override
+    public String toString() {
+        return "SpanResult{" +
+                "filename='" + filename + '\'' +
+                ", labelStart=" + labelStart +
+                ", labelEnd=" + labelEnd +
+                ", valueStart=" + valueStart +
+                ", valueEnd=" + valueEnd +
+                ", spanStart=" + spanStart +
+                ", spanEnd=" + spanEnd +
+                '}';
+    }
+}
_______________________________________________
augeas-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/augeas-devel

Reply via email to