aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/apache/fop/events/model/EventMethodModel.java
blob: 930cda53d62538ab96e5a2f88a336bc58991d062 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* $Id$ */

package org.apache.fop.events.model;

import java.io.Serializable;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;

import org.apache.xmlgraphics.util.XMLizable;

/**
 * Represents an event method. Each method in an event producer interface will result in one
 * instance of <code>EventMethodModel</code>.
 */
public class EventMethodModel implements Serializable, XMLizable {

    private static final long serialVersionUID = -7548882973341444354L;
    
    private String methodName;
    private EventSeverity severity;
    private List params = new java.util.ArrayList();
    private String exceptionClass;
    
    /**
     * Creates an new instance.
     * @param methodName the event method's name
     * @param severity the event severity
     */
    public EventMethodModel(String methodName, EventSeverity severity) {
        this.methodName = methodName;
        this.severity = severity;
    }
    
    /**
     * Adds a method parameter.
     * @param param the method parameter
     */
    public void addParameter(Parameter param) {
        this.params.add(param);
    }
    
    /**
     * Adds a method parameter.
     * @param type the type of the parameter
     * @param name the name of the parameter
     * @return the resulting Parameter instance
     */
    public Parameter addParameter(Class type, String name) {
        Parameter param = new Parameter(type, name); 
        addParameter(param);
        return param;
    }
    
    /**
     * Sets the event method name.
     * @param name the event name
     */
    public void setMethodName(String name) {
        this.methodName = name;
    }
    
    /**
     * Returns the event method name
     * @return the event name
     */
    public String getMethodName() {
        return this.methodName;
    }
    
    /**
     * Sets the event's severity level.
     * @param severity the severity
     */
    public void setSeverity(EventSeverity severity) {
        this.severity = severity;
    }
    
    /**
     * Returns the event's severity level.
     * @return the severity
     */
    public EventSeverity getSeverity() {
        return this.severity;
    }
    
    /**
     * Returns an unmodifiable list of parameters for this event method.
     * @return the list of parameters
     */
    public List getParameters() {
        return Collections.unmodifiableList(this.params);
    }
    
    /**
     * Sets the primary exception class for this event method. Note: Not all event methods throw
     * exceptions!
     * @param exceptionClass the exception class
     */
    public void setExceptionClass(String exceptionClass) {
        this.exceptionClass = exceptionClass;
    }
    
    /**
     * Returns the primary exception class for this event method. This method returns null if
     * the event is only informational or just a warning.
     * @return the primary exception class or null
     */
    public String getExceptionClass() {
        return this.exceptionClass;
    }
    
    /** {@inheritDoc} */
    public void toSAX(ContentHandler handler) throws SAXException {
        AttributesImpl atts = new AttributesImpl();
        atts.addAttribute(null, "name", "name", "CDATA", getMethodName());
        atts.addAttribute(null, "severity", "severity", "CDATA", getSeverity().getName());
        if (getExceptionClass() != null) {
            atts.addAttribute(null, "exception", "exception", "CDATA", getExceptionClass());
        }
        String elName = "method";
        handler.startElement(null, elName, elName, atts);
        Iterator iter = this.params.iterator();
        while (iter.hasNext()) {
            ((XMLizable)iter.next()).toSAX(handler);
        }
        handler.endElement(null, elName, elName);
    }
    
    /**
     * Represents an event parameter.
     */
    public static class Parameter implements Serializable, XMLizable {
        
        private static final long serialVersionUID = 6062500277953887099L;
        
        private Class type;
        private String name;
        
        /**
         * Creates a new event parameter.
         * @param type the parameter type
         * @param name the parameter name
         */
        public Parameter(Class type, String name) {
            this.type = type;
            this.name = name;
        }
        
        /**
         * Returns the parameter type.
         * @return the parameter type
         */
        public Class getType() {
            return this.type;
        }
        
        /**
         * Returns the parameter name.
         * @return the parameter name
         */
        public String getName() {
            return this.name;
        }

        /** {@inheritDoc} */
        public void toSAX(ContentHandler handler) throws SAXException {
            AttributesImpl atts = new AttributesImpl();
            atts.addAttribute(null, "type", "type", "CDATA", getType().getName());
            atts.addAttribute(null, "name", "name", "CDATA", getName());
            String elName = "parameter";
            handler.startElement(null, elName, elName, atts);
            handler.endElement(null, elName, elName);
        }
        
    }
}