~ubuntu-branches/debian/wheezy/jing-trang/wheezy

« back to all changes in this revision

Viewing changes to mod/rng-schema/src/main/com/thaiopensource/relaxng/output/common/ErrorReporter.java

  • Committer: Bazaar Package Importer
  • Author(s): Samuel Thibault
  • Date: 2009-09-01 15:53:03 UTC
  • Revision ID: james.westby@ubuntu.com-20090901155303-2kweef05h5v9j3ni
Tags: upstream-20090818
ImportĀ upstreamĀ versionĀ 20090818

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package com.thaiopensource.relaxng.output.common;
 
2
 
 
3
import org.xml.sax.ErrorHandler;
 
4
import org.xml.sax.SAXParseException;
 
5
import org.xml.sax.SAXException;
 
6
import com.thaiopensource.relaxng.edit.SourceLocation;
 
7
import com.thaiopensource.util.Localizer;
 
8
 
 
9
public class ErrorReporter {
 
10
  private final Localizer localizer;
 
11
  private final ErrorHandler eh;
 
12
  private boolean hadError = false;
 
13
 
 
14
  static public class WrappedSAXException extends RuntimeException {
 
15
    private final SAXException exception;
 
16
 
 
17
    private WrappedSAXException(SAXException exception) {
 
18
      this.exception = exception;
 
19
    }
 
20
 
 
21
    public SAXException getException() {
 
22
      return exception;
 
23
    }
 
24
  }
 
25
 
 
26
  public ErrorReporter(ErrorHandler eh, Class<?> cls) {
 
27
    this.eh = eh;
 
28
    this.localizer = new Localizer(cls);
 
29
  }
 
30
 
 
31
  public void error(String key, SourceLocation loc) {
 
32
    hadError = true;
 
33
    if (eh == null)
 
34
      return;
 
35
    try {
 
36
      eh.error(makeParseException(localizer.message(key), loc));
 
37
    }
 
38
    catch (SAXException e) {
 
39
      throw new WrappedSAXException(e);
 
40
    }
 
41
  }
 
42
 
 
43
  public void error(String key, String arg, SourceLocation loc) {
 
44
    hadError = true;
 
45
    if (eh == null)
 
46
      return;
 
47
    try {
 
48
      eh.error(makeParseException(localizer.message(key, arg), loc));
 
49
    }
 
50
    catch (SAXException e) {
 
51
      throw new WrappedSAXException(e);
 
52
    }
 
53
  }
 
54
 
 
55
  public void error(String key, String arg1, String arg2, SourceLocation loc) {
 
56
    hadError = true;
 
57
    if (eh == null)
 
58
      return;
 
59
    try {
 
60
      eh.error(makeParseException(localizer.message(key, arg1, arg2), loc));
 
61
    }
 
62
    catch (SAXException e) {
 
63
      throw new WrappedSAXException(e);
 
64
    }
 
65
  }
 
66
 
 
67
  public void warning(String key, SourceLocation loc) {
 
68
    if (eh == null)
 
69
      return;
 
70
    try {
 
71
      eh.warning(makeParseException(localizer.message(key), loc));
 
72
    }
 
73
    catch (SAXException e) {
 
74
      throw new WrappedSAXException(e);
 
75
    }
 
76
  }
 
77
 
 
78
  public void warning(String key, String arg, SourceLocation loc) {
 
79
    if (eh == null)
 
80
      return;
 
81
    try {
 
82
      eh.warning(makeParseException(localizer.message(key, arg), loc));
 
83
    }
 
84
    catch (SAXException e) {
 
85
      throw new WrappedSAXException(e);
 
86
    }
 
87
  }
 
88
 
 
89
  public void warning(String key, String arg1, String arg2, SourceLocation loc) {
 
90
    if (eh == null)
 
91
      return;
 
92
    try {
 
93
      eh.warning(makeParseException(localizer.message(key, arg1, arg2), loc));
 
94
    }
 
95
    catch (SAXException e) {
 
96
      throw new WrappedSAXException(e);
 
97
    }
 
98
  }
 
99
 
 
100
  public boolean getHadError() {
 
101
    return hadError;
 
102
  }
 
103
 
 
104
  private static SAXParseException makeParseException(String message, SourceLocation loc) {
 
105
    if (loc == null)
 
106
      return new SAXParseException(message, null);
 
107
    return new SAXParseException(message,
 
108
                                 null,
 
109
                                 loc.getUri(),
 
110
                                 loc.getLineNumber(),
 
111
                                 loc.getColumnNumber());
 
112
  }
 
113
 
 
114
  public Localizer getLocalizer() {
 
115
    return localizer;
 
116
  }
 
117
}