11package com .duy .astyle ;
22
3- import android .util .Log ;
4-
53/**
64 * AStyleInterface jni
75 *
@@ -16,50 +14,82 @@ public class AStyleInterface {
1614 }
1715
1816 /**
19- * This function is called to get the Artistic Style version number.
20- *
21- * @return A Java String containing the Artistic Style version number.
17+ * Error message function for this example.
2218 */
23- public static native String AStyleGetVersion ();
19+ private static void error (String message ) {
20+ System .out .println (message );
21+ System .out .println ("The program has terminated!" );
22+ System .exit (1 );
23+ }
2424
2525 /**
26- * This function is called to format the source code.
26+ * Calls the Java AStyleMain function in Artistic Style.
27+ * The function name is constructed from method names in this program.
2728 *
28- * @param textIn - A string containing the source file to be formatted
29- * @param options A Java String containing the formatting options. They should be in the same
30- * format as in the default options file. The options may be set apart by
31- * new-lines, commas, tabs or spaces. The long options do not need the "--" prefix.
32- * Comments may be used but they must be terminated by a new-line "\n" character.
33- * <p>
34- * If the file is not a C/C++ file, the file mode option "mode=java" or "mode=cs"
35- * must be included.
36- * Otherwise the default mode of C/C++ is used.
37- * @return If the function succeeds, the return value is a Java String containing the formatted
38- * source code.
39- * If the function fails, the return value is an empty Java String. The returned String should
40- * be checked for a length of zero to determine if an error occurred. Before the source is
41- * returned, an error message will be sent to the ErrorHandler method.
42- * <p>
43- * The function will NOT fail for an invalid option in the formatting options. In this case,
44- * an error message is sent to the error handling function and the formatted source code is
45- * returned without using the invalid option.
29+ * @param textIn A string containing the source code to be formatted.
30+ * @param options A string of options to Artistic Style.
31+ * @return A String containing the formatted source from Artistic Style.
4632 */
4733 private native String AStyleMain (String textIn , String options );
4834
49-
50- public String format (String code , String opts ) {
51- return AStyleMain (code , opts );
52- }
35+ /**
36+ * Calls the Java AStyleGetVersion function in Artistic Style.
37+ * The function name is constructed from method names in this program.
38+ *
39+ * @return A String containing the version number of Artistic Style.
40+ */
41+ private native String AStyleGetVersion ();
5342
5443 /**
55- * ErrorHandler is called by the Artistic Style static library when an error occurs. It should
56- * display an error message and then either abort or continue the program depending on the error.
44+ * Error handler for messages from Artistic Style.
45+ * This method is called only if there are errors when AStyleMain is called.
46+ * This is for debugging and there should be no errors when the calling
47+ * parameters are correct.
48+ * Changing the method name requires changing Artistic Style.
49+ * Signature: (ILjava/lang/String;)V.
5750 *
58- * @param errorNumber The error number as defined by Artistic Style.
59- * @param errorMessage A Java String containing the error message as defined by Artistic Style.
51+ * @param errorNumber The error number from Artistic Style.
52+ * @param errorMessage The error message from Artistic Style.
6053 */
6154 @ SuppressWarnings ("unused" )
6255 private void ErrorHandler (int errorNumber , String errorMessage ) {
63- Log .d (TAG , "ErrorHandler() called with: errorNumber = [" + errorNumber + "], errorMessage = [" + errorMessage + "]" );
56+ System .out .println ("AStyle error "
57+ + String .valueOf (errorNumber )
58+ + " - " + errorMessage );
6459 }
60+
61+ /**
62+ * Call the AStyleMain function in Artistic Style.
63+ *
64+ * @param textIn A string containing the source code to be formatted.
65+ * @param options A string of options to Artistic Style.
66+ * @return A String containing the formatted source from Artistic Style,
67+ * or an empty string on error.
68+ */
69+ public String formatSource (String textIn , String options ) { // Return the allocated string
70+ // Memory space is allocated by OnAStyleMemAlloc, a callback function from AStyle
71+ String textOut = "" ;
72+ try {
73+ textOut = AStyleMain (textIn , options );
74+ } catch (UnsatisfiedLinkError e ) { //~ System.out.println(e.getMessage());
75+ error ("Cannot call the Java AStyleMain function" );
76+ }
77+ return textOut ;
78+ }
79+
80+ /**
81+ * Call the AStyleGetVersion function in Artistic Style.
82+ *
83+ * @return A String containing the version number from Artistic Style.
84+ */
85+ public String getVersion () {
86+ String version = new String ();
87+ try {
88+ version = AStyleGetVersion ();
89+ } catch (UnsatisfiedLinkError e ) { //~ System.out.println(e.getMessage());
90+ error ("Cannot call the Java AStyleGetVersion function" );
91+ }
92+ return version ;
93+ }
94+
6595}
0 commit comments