1 /* xmlj_error.c -
2    Copyright (C) 2003, 2004 Free Software Foundation, Inc.
3 
4 This file is part of GNU Classpath.
5 
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10 
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
20 
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library.  Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25 
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module.  An independent module is a module which is not derived from
33 or based on this library.  If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so.  If you do not wish to do so, delete this
36 exception statement from your version. */
37 
38 #include "xmlj_error.h"
39 #include "xmlj_io.h"
40 #include "xmlj_util.h"
41 
42 void
xmljXsltErrorFunc(void * ctx,const char * msg,...)43 xmljXsltErrorFunc (void *ctx, const char *msg, ...)
44 {
45   if (NULL != ctx)
46     {
47       SAXParseContext *sax = ((SAXParseContext *) ctx);
48 
49       if (NULL != sax)
50         {
51           JNIEnv *env = sax->env;
52 
53           if (!(*env)->ExceptionOccurred (env))
54             {
55               jobject target = sax->obj;
56               xmlChar *x_msg;
57               jstring j_msg;
58               va_list args;
59 
60               if (sax->error == NULL)
61                 {
62                   sax->error =
63                     xmljGetMethodID (env,
64                                      target,
65                                      "error",
66                                  "(Ljava/lang/String;IILjava/lang/String;Ljava/lang/String;)V");
67                   if (sax->error == NULL)
68                     {
69                       return;
70                     }
71                 }
72 
73               va_start (args, msg);
74               x_msg = (msg == NULL) ? NULL : xmlCharStrdup (msg);
75               va_end (args);
76               j_msg = xmljNewString (env, x_msg);
77 
78               (*env)->CallVoidMethod (env,
79                                       target,
80                                       sax->error,
81                                       j_msg,
82                                       -1,
83                                       -1,
84                                       NULL,
85                                       NULL);
86             }
87         }
88     }
89   else
90     {
91       va_list va;
92       va_start (va, msg);
93       fprintf (stderr, "libxslt error: ");
94       vfprintf (stderr, msg, va);
95       fflush (stderr);
96       va_end (va);
97     }
98 }
99 
100 void
xmljThrowException(JNIEnv * env,const char * classname,const char * message)101 xmljThrowException (JNIEnv *env,
102                     const char *classname,
103                     const char *message)
104 {
105   jclass cls;
106   jmethodID method;
107   jthrowable ex;
108   jstring jmsg;
109 
110   /*fprintf(stderr, "Throwing exception %s %s\n", classname, message);*/
111   cls = (*env)->FindClass (env, classname);
112   if (cls == NULL)
113     {
114       fprintf (stderr, "Can't find class %s\n", classname);
115       fflush (stderr);
116       return;
117     }
118   method = (*env)->GetMethodID (env, cls, "<init>", "(Ljava/lang/String;)V");
119   if (method == NULL)
120     {
121       fprintf (stderr, "Can't find method %s.<init>\n", classname);
122       fflush (stderr);
123       return;
124     }
125   jmsg = (message == NULL) ? NULL : (*env)->NewStringUTF (env, message);
126   ex = (jthrowable) (*env)->NewObject (env, cls, method, jmsg);
127   if (ex == NULL)
128     {
129       fprintf (stderr, "Can't instantiate new %s\n", classname);
130       fflush (stderr);
131       return;
132     }
133   (*env)->Throw (env, ex);
134 }
135 
136 void
xmljThrowDOMException(JNIEnv * env,int code,const char * message)137 xmljThrowDOMException (JNIEnv *env,
138                        int code,
139                        const char *message)
140 {
141   jclass cls;
142   jmethodID method;
143   jthrowable ex;
144   jstring jmsg;
145 
146   if ((*env)->ExceptionOccurred (env))
147     {
148       return;
149     }
150 
151   cls = (*env)->FindClass (env, "gnu/xml/libxmlj/dom/GnomeDOMException");
152   if (cls == NULL)
153     {
154       fprintf (stderr, "Can't find DOMException class!\n");
155       fflush (stderr);
156       return;
157     }
158   method = (*env)->GetMethodID (env, cls, "<init>", "(SLjava/lang/String;)V");
159   if (method == NULL)
160     {
161       fprintf (stderr, "Can't find DOMException constructor!\n");
162       fflush (stderr);
163       return;
164     }
165   jmsg = (message == NULL) ? NULL : (*env)->NewStringUTF (env, message);
166   ex = (jthrowable) (*env)->NewObject (env, cls, method, code, jmsg);
167   (*env)->Throw (env, ex);
168 }
169 
170