KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > schema > SchemaException


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.schema;
24
25 import java.util.*;
26
27 import org.xquark.schema.loader.SchemaComponentRef;
28 import org.xquark.schema.loader.TypeRef;
29
30 /**
31  * <p>Abstract<br>
32  * Encapsulate an schema-validation based error or warning.</p>
33  *
34  * <p>This exception is applied to report schema-validation error. During SAX
35  * based schema validation, there will be two different exceptions :
36  * 1. SchemaException generated by SAX parser
37  * 2. SchemaException generated by schema validation.
38  * But during DOM or DOM-like based schema validation, only SchemaException
39  * will be generated.
40  * </p>
41  */

42 public class SchemaException extends Exception JavaDoc {
43     private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
44     private static final String JavaDoc RCSName = "$Name: $";
45
46     static private ResourceBundle errorStrings =
47         ResourceBundle.getBundle("org.xquark.schema.resources.errors", java.util.Locale.getDefault());
48     /**
49      * @serial The error code.
50      */

51     private String JavaDoc errorCode = null;
52
53     /**
54      * @serial The object responsible for the exception
55      */

56     private Object JavaDoc invalidObject = null;
57
58     /**
59      * @serial The embedded exceptions if tunnelling, or null.
60      */

61     private ArrayList exceptions = null;
62
63     public SchemaException(String JavaDoc errorCode) {
64         this(errorCode, (Object JavaDoc) null);
65     }
66
67     public SchemaException(String JavaDoc errorCode, Object JavaDoc object) {
68         super(errorCode == null ? null : errorStrings.getString(errorCode));
69         this.errorCode = errorCode;
70         this.invalidObject = object;
71     }
72
73     public SchemaException(String JavaDoc errorCode, Object JavaDoc object, Exception JavaDoc exception) {
74         this(errorCode, object);
75         add(exception);
76     }
77
78     public SchemaException(String JavaDoc errorCode, Object JavaDoc object, List exceptions) {
79         this(errorCode, object);
80         add(exceptions);
81     }
82
83     /**
84      * Create a new SchemaException wrapping an existing exception.
85      *
86      * <p>The existing exception will be embedded in the new
87      * one, and its message will become the default message for
88      * the SchemaException.</p>
89      *
90      * @param e The exception to be wrapped in a SchemaException.
91      */

92     public SchemaException(Exception JavaDoc e) {
93         this(null, null, e);
94     }
95
96     public SchemaException(List exceptions) {
97         this(null, null, exceptions);
98     }
99
100     /**
101      * Return the error code for this exception.
102      */

103     public String JavaDoc getErrorCode() {
104         return this.errorCode;
105     }
106
107     public Object JavaDoc getInvalidObject() {
108         return invalidObject;
109     }
110
111     public void setInvalidObject(Object JavaDoc object) {
112         invalidObject = object;
113     }
114
115     /**
116      * Return a detail message for this exception.
117      *
118      * <p>If there is an embedded exception, and if the SchemaException
119      * has no detail message of its own, this method will return
120      * the detail message from the embedded exception.</p>
121      *
122      * @return The error or warning message.
123      */

124     public String JavaDoc getMessage() {
125         return getMessage(new SchemaException.PrintVisitor());
126     }
127
128     private String JavaDoc getMessage(SchemaException.PrintVisitor visitor) {
129         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
130         if (errorCode != null)
131             result.append(errorCode);
132         String JavaDoc message = super.getMessage();
133         if (message != null) {
134             if (result.length() > 0)
135                 result.append(": ");
136             result.append(message);
137         }
138         if (invalidObject != null && visitor != null) {
139             result.append(" (");
140             if (invalidObject instanceof SchemaVisitable) {
141                 result.append(visitor.toString((SchemaVisitable) invalidObject));
142             } else {
143                 result.append(invalidObject.toString());
144             }
145             result.append(")");
146         }
147         if (exceptions != null) {
148             Iterator it = exceptions.iterator();
149             while (it.hasNext()) {
150                 if (result.length() > 0)
151                     result.append('\n');
152                 result.append(((SchemaException) it.next()).getMessage(visitor));
153             }
154         }
155         return result.toString();
156     }
157
158     /**
159      * Return the embedded exception, if any.
160      *
161      * @return The embedded exception, or null if there is none.
162      */

163     public List getExceptions() {
164         return exceptions;
165     }
166
167     public void add(Exception JavaDoc exception) {
168         if (exception == null)
169             return;
170         if (exceptions == null)
171             exceptions = new ArrayList();
172         exceptions.add(exception);
173     }
174
175     public void add(List exceptions) {
176         if (exceptions == null)
177             return;
178         if (this.exceptions == null)
179             this.exceptions = new ArrayList();
180         this.exceptions.addAll(exceptions);
181     }
182
183     public static SchemaException computeException(Particle p) {
184         SchemaException.ComputeExceptionVisitor visitor = new SchemaException.ComputeExceptionVisitor();
185         try {
186             p.accept(visitor);
187         } catch (Exception JavaDoc e) {
188             // Left blank on purpose
189
}
190         return visitor.getResult();
191     }
192
193     public static SchemaException computeMinExtentException(Particle p) {
194         SchemaException.ComputeExceptionVisitor visitor = new SchemaException.ComputeMinExtentExceptionVisitor();
195         try {
196             p.accept(visitor);
197         } catch (Exception JavaDoc e) {
198             // Left blank on purpose
199
}
200         return visitor.getResult();
201     }
202
203     static class ComputeExceptionVisitor extends DefaultSchemaVisitor {
204         private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
205         private static final String JavaDoc RCSName = "$Name: $";
206
207         protected SchemaException result = null;
208
209         public SchemaException getResult() {
210             return result;
211         }
212
213         public void visit(Particle p) throws SchemaException {
214             ((SchemaVisitable) p.getTerm()).accept(this);
215             result.setInvalidObject(p);
216         }
217
218         public void visit(ElementDeclaration decl) throws SchemaException {
219             result = new SchemaException("cvc-particle.2.3");
220         }
221
222         public void visit(Wildcard wildcard) throws SchemaException {
223             result = new SchemaException("cvc-particle.1.3");
224         }
225
226         public void visit(ModelGroup group) throws SchemaException {
227             result = new SchemaException("cvc-particle.3.3");
228         }
229     }
230
231     static class ComputeMinExtentExceptionVisitor extends SchemaException.ComputeExceptionVisitor {
232         private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
233         private static final String JavaDoc RCSName = "$Name: $";
234
235         public void visit(Particle p) throws SchemaException {
236             if (p.getMinExtent() > 0) {
237                 super.visit(p);
238             }
239         }
240
241         public void visit(SequenceModelGroup group) throws SchemaException {
242             Iterator it = group.iterator();
243             while (it.hasNext()) {
244                 Particle p = (Particle) it.next();
245                 p.accept(this);
246                 if (result != null)
247                     break;
248             }
249             result = new SchemaException("cvc-particle.3.3", null, result);
250         }
251
252         public void visit(ChoiceModelGroup group) throws SchemaException {
253             ArrayList exceptions = new ArrayList();
254             Iterator it = group.iterator();
255             while (it.hasNext()) {
256                 Particle p = (Particle) it.next();
257                 result = null;
258                 p.accept(this);
259                 if (result != null)
260                     exceptions.add(result);
261             }
262             result = new SchemaException("cvc-particle.3.3", null, exceptions);
263         }
264
265         public void visit(AllModelGroup group) throws SchemaException {
266             ArrayList exceptions = new ArrayList();
267             Iterator it = group.iterator();
268             while (it.hasNext()) {
269                 Particle p = (Particle) it.next();
270                 result = null;
271                 p.accept(this);
272                 if (result != null)
273                     exceptions.add(result);
274             }
275             result = new SchemaException("cvc-particle.3.3", null, exceptions);
276         }
277     }
278
279     static class PrintVisitor extends DefaultSchemaVisitor {
280         private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
281         private static final String JavaDoc RCSName = "$Name: $";
282         private String JavaDoc result = null;
283         private int minOccurs = -1;
284         private int maxOccurs = -1;
285
286         String JavaDoc toString(SchemaVisitable invalidObject) {
287             result = null;
288             minOccurs = -1;
289             maxOccurs = -1;
290             try {
291                 invalidObject.accept(this);
292             } catch (Exception JavaDoc e) {
293                 // Should not occur
294
}
295             return result;
296         }
297
298         private void addOccurrences() {
299             if (minOccurs != -1) {
300                 if (minOccurs != 1)
301                     result += " minOccurs=\"" + minOccurs + "\"";
302                 if (maxOccurs == Integer.MAX_VALUE)
303                     result += " maxOccurs=\"unbounded\"";
304                 else if (maxOccurs != 1)
305                     result += " maxOccurs=\"" + maxOccurs + "\"";
306             }
307         }
308
309         public void visit(SchemaComponentRef ref) throws SchemaException {
310             result = "<reference name=\"" + ref.getName() + "\">";
311         }
312
313         public void visit(TypeRef ref) throws SchemaException {
314             result = "<reference name=\"" + ref.getName() + "\">";
315         }
316
317         public void visit(ElementDeclaration decl) throws SchemaException {
318             result = "<element name=\"" + decl.getName() + "\"";
319             addOccurrences();
320             result += ">";
321         }
322
323         public void visit(AttributeDeclaration decl) throws SchemaException {
324             result = "<attribute name=\"" + decl.getName() + "\">";
325         }
326
327         public void visit(NotationDeclaration decl) throws SchemaException {
328             result = "<notation name=\"" + decl.getName() + "\">";
329         }
330
331         public void visit(Wildcard w) throws SchemaException {
332             String JavaDoc constraint = null;
333             if (w.isAny())
334                 constraint = null;
335             else if (!w.isInclude())
336                 constraint = "##other";
337             else {
338                 Iterator it = w.getNamespaces().iterator();
339                 while (it.hasNext()) {
340                     String JavaDoc ns = (String JavaDoc) it.next();
341                     if (ns == null)
342                         ns = "##local";
343                     else if (ns.equals(w.getNamespace()))
344                         ns = "##targetNamespace";
345                     if (constraint == null)
346                         constraint = "";
347                     else
348                         constraint += " ";
349                     constraint += ns;
350                 }
351             }
352             if (minOccurs == -1) {
353                 result = "<anyAttribute";
354                 if (constraint != null)
355                     result += " namespace=\"" + constraint + "\"";
356                 result += ">";
357             } else {
358                 result = "<any";
359                 if (constraint != null)
360                     result += " namespace=\"" + constraint + "\"";
361                 addOccurrences();
362                 result += ">";
363             }
364         }
365
366         public void visit(ComplexType type) throws SchemaException {
367             result = "<complexType";
368             if (type.getName() != null)
369                 result += " name=\"" + type.getName() + "\"";
370             result += ">";
371         }
372
373         public void visit(SimpleType type) throws SchemaException {
374             result = "<simpleType";
375             if (type.getName() != null)
376                 result += " name=\"" + type.getName() + "\"";
377             result += ">";
378         }
379
380         public void visit(Particle p) throws SchemaException {
381             minOccurs = p.getMinOccurs();
382             maxOccurs = p.getMaxOccurs();
383             ((SchemaVisitable) p.getTerm()).accept(this);
384             minOccurs = maxOccurs = -1;
385         }
386
387         public void visit(SequenceModelGroup group) throws SchemaException {
388             result = "<sequence";
389             addOccurrences();
390             result += ">";
391         }
392
393         public void visit(ChoiceModelGroup group) throws SchemaException {
394             result = "<choice";
395             addOccurrences();
396             result += ">";
397         }
398
399         public void visit(AllModelGroup group) throws SchemaException {
400             result = "<all";
401             addOccurrences();
402             result += ">";
403         }
404
405         public void visit(IdentityConstraint constraint) throws SchemaException {
406             switch (constraint.getCategory()) {
407                 case IdentityConstraint.CATEGORY_UNIQUE :
408                     result = "<unique name=\"" + constraint.getName() + "\">";
409                     break;
410                 case IdentityConstraint.CATEGORY_KEY :
411                     result = "<key name=\"" + constraint.getName() + "\">";
412                     break;
413                 case IdentityConstraint.CATEGORY_KEYREF :
414                     result =
415                         "<keyref name=\""
416                             + constraint.getName()
417                             + "\" refer=\""
418                             + constraint.getReferencedKey().getName()
419                             + "\">";
420                     break;
421             }
422         }
423     }
424 }
425
Popular Tags